Skip to content

Commit f273bd4

Browse files
committed
updates to ollama and caios chat
1 parent 8aeab8b commit f273bd4

3 files changed

Lines changed: 272 additions & 41 deletions

File tree

Project_Andrew/caios_chat.py

Lines changed: 115 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#V05012026
1+
#V05112026
22
#!/usr/bin/env python3
33
"""
44
CAIOS Inference Wrapper
@@ -14,6 +14,14 @@
1414

1515
from 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("\nSelect 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"\nUsing: {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("\nPrompt 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("\nThinking...", 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}")
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#V05112026
2+
# =============================================================================
3+
"""
4+
Ollama Configuration Bridge - CPOL State to Inference Parameters
5+
6+
This module bridges CAIOS's ternary logic (CPOL) state to Ollama's inference
7+
parameters, ensuring the 12D manifold remains stable during local inference.
8+
9+
Created by master_init.py on first boot.
10+
Imported by orchestrator.py and all subsystems.
11+
"""
12+
# =============================================================================
13+
14+
import json
15+
import os
16+
import requests
17+
from typing import Dict, Optional, List
18+
19+
IDENTITY_PATH = "system_identity.json"
20+
CAIOS_PROMPT_PATH = "CAIOS.txt"
21+
OLLAMA_ENDPOINT = "http://localhost:11434/api/tags"
22+
23+
24+
def load_system_config() -> Dict:
25+
if not os.path.exists(IDENTITY_PATH):
26+
return {'node_tier': 1, 'system_id': 'Unconfigured', 'ollama_model': None}
27+
28+
try:
29+
with open(IDENTITY_PATH, 'r', encoding='utf-8') as f:
30+
identity = json.load(f)
31+
return {
32+
'node_tier': identity.get('node_tier', 1),
33+
'system_id': identity.get('system_id', 'Unconfigured'),
34+
'ollama_model': identity.get('ollama_model')
35+
}
36+
except Exception:
37+
print(f"[OLLAMA_CONFIG] Warning: Failed to load system_identity.json: {e}")
38+
return {'node_tier': 1, 'system_id': 'Error', 'ollama_model': None}
39+
40+
41+
def list_available_ollama_models() -> List[str]:
42+
try:
43+
import ollama
44+
models = ollama.list().get('models', [])
45+
return [m['model'] for m in models]
46+
except Exception:
47+
return []
48+
49+
50+
def print_ollama_setup_help():
51+
print("\n[OLLAMA SETUP HELP]")
52+
print("Ollama is not running or no models found.")
53+
print("Please run in another terminal:")
54+
print(" ollama serve")
55+
print(" ollama pull llama3.2")
56+
print("Then restart CAIOS.\n")
57+
58+
59+
def load_caios_system_prompt() -> str:
60+
if not os.path.exists(CAIOS_PROMPT_PATH):
61+
return "You are a helpful, truthful assistant."
62+
try:
63+
with open(CAIOS_PROMPT_PATH, 'r', encoding='utf-8') as f:
64+
return f.read().strip()
65+
except Exception:
66+
return "You are a helpful, truthful assistant."
67+
68+
69+
def get_cpol_ollama_params(
70+
contradiction_density: float = 0.12,
71+
evidence_score: float = 0.5,
72+
preferred_model: str = None
73+
) -> Dict:
74+
"""Main function: returns ready-to-use params for ollama.chat()"""
75+
config = load_system_config()
76+
77+
# Model priority: preferred > saved > first available > safe default
78+
model = preferred_model or config.get('ollama_model')
79+
available = list_available_ollama_models()
80+
81+
if not model and available:
82+
model = available[0]
83+
elif not model:
84+
model = "llama3.2"
85+
86+
# CPOL-aware temperature
87+
base_temp = 0.85 - (contradiction_density * 0.75)
88+
temperature = max(0.1, min(0.9, base_temp))
89+
90+
if config.get('node_tier', 1) == 0:
91+
temperature *= 0.92
92+
93+
return {
94+
"model": model,
95+
"system": load_caios_system_prompt(),
96+
"options": {
97+
"temperature": round(temperature, 2),
98+
"num_predict": 4096,
99+
"top_p": 0.92,
100+
"repeat_penalty": 1.12,
101+
"num_ctx": 8192 if evidence_score > 0.5 else 4096,
102+
"seed": -1
103+
}
104+
}
105+
106+
107+
# Auto-init message
108+
try:
109+
config = load_system_config()
110+
print(f"[OLLAMA_CONFIG] Loaded for Node Tier {config.get('node_tier')} | System ID: {config.get('system_id')}")
111+
if not check_ollama_available():
112+
print_ollama_setup_help()
113+
except Exception as e:
114+
print(f"[OLLAMA_CONFIG] Warning: {e}")

0 commit comments

Comments
 (0)