1- #V05012026
1+ #V05112026
22#!/usr/bin/env python3
33"""
44CAIOS Inference Wrapper
1414
1515from typing import Dict , Any , List
1616
17+ try :
18+ import orchestrator as orch
19+ print ("[CAIOS_CHAT] Full Orchestrator loaded successfully." )
20+ ORCHESTRATOR_AVAILABLE = True
21+ except ImportError as e :
22+ print (f"[CAIOS_CHAT] Orchestrator not loaded: { e } " )
23+ ORCHESTRATOR_AVAILABLE = False
24+
1725# =============================================================================
1826# Load shared memory (created by master_init.py)
1927# =============================================================================
@@ -114,12 +122,15 @@ def select_client(clients: Dict[str, Any]) -> tuple:
114122 for i , provider in enumerate (options , 1 ):
115123 if provider == 'ollama_local' :
116124 try :
117- import ollama_config
118- print (f" { i } . OLLAMA Local "
119- f"(Tier { ollama_config .NODE_TIER } | "
120- f"{ ollama_config .SYSTEM_CONFIG .get ('ollama_model' , 'local' )} )" )
125+ import ollama
126+ models = ollama .list ().get ('models' , [])
127+ model_names = [m ['model' ] for m in models ]
128+ print (f" { i } . OLLAMA Local — { len (model_names )} models available" )
129+ if model_names :
130+ for j , m in enumerate (model_names , 1 ):
131+ print (f" { i } .{ j } { m } " )
121132 except Exception :
122- print (f" { i } . OLLAMA Local" )
133+ print (f" { i } . OLLAMA Local (could not list models) " )
123134 else :
124135 print (f" { i } . { provider .upper ()} " )
125136
@@ -128,39 +139,83 @@ def select_client(clients: Dict[str, Any]) -> tuple:
128139 choice = int (input ("\n Select model (number): " ))
129140 if 1 <= choice <= len (options ):
130141 provider = options [choice - 1 ]
131- return provider , clients [provider ]
142+ client = clients [provider ]
143+
144+ if provider == 'ollama_local' :
145+ try :
146+ import ollama
147+ models = ollama .list ().get ('models' , [])
148+ model_names = [m ['model' ] for m in models ]
149+
150+ if not model_names :
151+ selected_model = input ("No models found. Enter model name (e.g. llama3.2): " ) or "llama3.2"
152+ elif len (model_names ) == 1 :
153+ selected_model = model_names [0 ]
154+ print (f"Using: { selected_model } " )
155+ else :
156+ for k , m in enumerate (model_names , 1 ):
157+ print (f" { k } . { m } " )
158+ m_choice = int (input ("Select Ollama model (number): " ))
159+ selected_model = model_names [m_choice - 1 ]
160+ except Exception as e :
161+ print (f"Warning: Could not list Ollama models: { e } " )
162+ selected_model = input ("Enter Ollama model name: " ) or "llama3.2"
163+ return provider , client , selected_model
164+
165+ return provider , client , None
132166 print (f"Please enter a number between 1 and { len (options )} ." )
133167 except ValueError :
134168 print ("Please enter a valid number." )
135169
170+ def chat_with_model (provider : str , client : Any ,
171+ messages : List [Dict [str , str ]],
172+ ollama_model : str = None ) -> str :
173+ """Main entry point: CAIOS Prompt → Orchestrator → Model"""
174+ user_input = messages [- 1 ]["content" ] if messages else ""
136175
137- def chat_with_model (provider : str , client : Any , messages : List [Dict [str , str ]]) -> str :
138- """Direct Ollama call with forced full CAIOS system prompt."""
139- if provider == "ollama_local" :
140- try :
141- import ollama
142-
143- # Force fresh personalized CAIOS prompt every time
144- full_system_prompt = get_personalized_prompt ()
145-
146- # Build messages with full system prompt at the start
147- full_messages = [
148- {"role" : "system" , "content" : full_system_prompt }
149- ] + messages [1 :] # Keep conversation history but always reset system prompt
150-
151- response = ollama .chat (
152- model = "llama3.2:3b" , # Change to bigger model later
153- messages = full_messages ,
154- options = {
155- "temperature" : 0.7 ,
156- "num_ctx" : 8192
157- }
158- )
176+ # === 1. Try Full Orchestrator (Preferred Path) ===
177+ try :
178+ import orchestrator as orch
179+
180+ # Pass through the full orchestration pipeline
181+ result = orch .system_step (
182+ user_input = user_input ,
183+ prompt_complexity = "medium" , # Can be made dynamic later
184+ api_clients = shared_memory .get ('api_clients' ),
185+ user_id = shared_memory .get ('active_user' )
186+ )
187+
188+ # Extract final output
189+ if isinstance (result , dict ):
190+ output = result .get ('output' ) or result .get ('llm_response' ) or str (result )
191+ return output
192+ else :
193+ return str (result )
159194
160- return response ['message' ]['content' ].strip ()
195+ except Exception as e :
196+ print (f"[ORCHESTRATOR] Failed: { e } — falling back to direct model" )
161197
162- except Exception as e :
163- return f"[OLLAMA ERROR] { str (e )} "
198+ # === 2. Fallback: Direct Ollama with full prompt ===
199+ try :
200+ import ollama
201+ from ollama_config import get_cpol_ollama_params
202+
203+ params = get_cpol_ollama_params (preferred_model = ollama_model )
204+ full_system_prompt = get_personalized_prompt ()
205+
206+ full_messages = [
207+ {"role" : "system" , "content" : full_system_prompt }
208+ ] + [msg for msg in messages if msg .get ("role" ) != "system" ]
209+
210+ response = ollama .chat (
211+ model = params ['model' ],
212+ messages = full_messages ,
213+ options = params ['options' ]
214+ )
215+ return response ['message' ]['content' ].strip ()
216+
217+ except Exception as e :
218+ return f"[ERROR] Both orchestrator and direct model failed: { e } "
164219
165220 try :
166221 if provider == "openai" :
@@ -250,7 +305,7 @@ def main():
250305 print ("Run 'python master_init.py' first to initialize clients." )
251306 return
252307
253- provider , client = select_client (clients )
308+ provider , client , ollama_model = select_client (clients )
254309 print (f"\n Using: { provider .upper ()} " )
255310
256311 # Generate the prompt dynamically at boot
@@ -285,7 +340,32 @@ def main():
285340 print ("-" * 30 )
286341 continue # Don't send this to the AI, it's for you.
287342
288- # 3. LOCAL COMMAND: /mesh
343+ # 3. LOCAL COMMAND: /debug
344+ if user_input .lower () == "/debug" :
345+ print ("\n " + "=" * 70 )
346+ print ("CAIOS DEBUG INFORMATION" )
347+ print ("=" * 70 )
348+
349+ identity = shared_memory .get ('system_identity' , {})
350+ print (f"System ID : { identity .get ('system_id' , 'Not set' )} " )
351+ print (f"Primary User : { identity .get ('primary_user' , 'Not set' )} " )
352+ print (f"Auth Method : { identity .get ('auth_method' , 'TEXT_USERNAME' )} " )
353+ print (f"Current Model : { ollama_model or 'Unknown' } " )
354+
355+ prompt_len = len (full_system_prompt ) if 'full_system_prompt' in locals () else len (get_personalized_prompt ())
356+ print (f"System Prompt Size : { prompt_len } characters" )
357+
358+ print ("\n Prompt Preview (first 500 characters):" )
359+ preview = full_system_prompt [:500 ] if 'full_system_prompt' in locals () else get_personalized_prompt ()[:500 ]
360+ print (preview + "..." if len (preview ) == 500 else preview )
361+
362+ cpol = shared_memory .get ('cpol_state' , {})
363+ print (f"CPOL Status : { cpol .get ('status' , 'Unknown' )} " )
364+
365+ print ("=" * 70 )
366+ continue
367+
368+ # 4. LOCAL COMMAND: /mesh
289369 if user_input .lower () == "/mesh" :
290370 # Assuming mesh_peers is a dict of {node_id: last_seen_timestamp}
291371 peers = shared_memory .get ('mesh_peers' , {})
@@ -313,7 +393,7 @@ def main():
313393 print ("\n Thinking..." , end = "" , flush = True )
314394
315395 # Get response from model
316- response_text = chat_with_model (provider , client , conversation )
396+ response_text = chat_with_model (provider , client , conversation , ollama_model )
317397
318398 print ("\r " + " " * 20 + "\r " , end = "" ) # clear "Thinking..."
319399 print (f"CAIOS: { response_text } " )
0 commit comments