Spaces:
Sleeping
Sleeping
Update src/chimera_core.py
Browse files- src/chimera_core.py +70 -42
src/chimera_core.py
CHANGED
|
@@ -1,42 +1,52 @@
|
|
| 1 |
-
from google import genai
|
| 2 |
import os
|
| 3 |
import sys
|
|
|
|
|
|
|
| 4 |
|
| 5 |
class Chimera:
|
| 6 |
def __init__(self):
|
| 7 |
-
#
|
| 8 |
-
self.
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
if not self.api_key:
|
| 12 |
try:
|
| 13 |
import config
|
| 14 |
-
self.
|
| 15 |
except ImportError:
|
| 16 |
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
| 24 |
|
| 25 |
def _route_task(self, prompt):
|
| 26 |
-
""" The Router:
|
|
|
|
| 27 |
routing_prompt = f"""
|
| 28 |
-
Classify this
|
| 29 |
-
[ASM] -
|
| 30 |
-
[SFE] -
|
| 31 |
-
[CSM] - Creative
|
| 32 |
-
[CHAT] -
|
| 33 |
|
| 34 |
User Task: "{prompt}"
|
| 35 |
-
Reply ONLY with the tag
|
| 36 |
"""
|
| 37 |
try:
|
| 38 |
-
response = self.
|
| 39 |
-
model=self.
|
| 40 |
contents=routing_prompt
|
| 41 |
)
|
| 42 |
tag = response.text.strip().replace("[", "").replace("]", "")
|
|
@@ -45,36 +55,54 @@ class Chimera:
|
|
| 45 |
return "CHAT"
|
| 46 |
|
| 47 |
def process_request(self, user_message, history, manual_role="Auto"):
|
| 48 |
-
|
| 49 |
-
Main Processor with Manual Override.
|
| 50 |
-
"""
|
| 51 |
-
# 1. Determine Role (Auto or Manual)
|
| 52 |
if manual_role and manual_role != "Auto":
|
| 53 |
-
role = manual_role
|
| 54 |
-
print(f"π Manual Override: [{role}]")
|
| 55 |
else:
|
| 56 |
role = self._route_task(user_message)
|
| 57 |
-
|
|
|
|
| 58 |
|
| 59 |
-
# 2. Assign
|
| 60 |
system_instruction = ""
|
|
|
|
|
|
|
| 61 |
if role == "ASM":
|
| 62 |
-
system_instruction = "You are the ASM (Abstract Symbology Module). You are an expert
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
elif role == "SFE":
|
| 64 |
-
system_instruction = "You are the SFE (Sensory Fusion Engine). You are a
|
|
|
|
| 65 |
elif role == "CSM":
|
| 66 |
-
system_instruction = "You are the CSM (Creative Synthesis Module). You are a
|
|
|
|
| 67 |
else:
|
| 68 |
-
system_instruction = "You are Project Chimera
|
| 69 |
|
| 70 |
-
# 3.
|
| 71 |
-
full_prompt = f"System Instruction: {system_instruction}\n\nUser Message: {user_message}"
|
| 72 |
-
|
| 73 |
try:
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
except Exception as e:
|
| 80 |
return f"β System Error: {str(e)}", "ERR"
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import sys
|
| 3 |
+
from google import genai
|
| 4 |
+
from openai import OpenAI
|
| 5 |
|
| 6 |
class Chimera:
|
| 7 |
def __init__(self):
|
| 8 |
+
# 1. SETUP GEMINI (Google)
|
| 9 |
+
self.gemini_key = os.getenv("GEMINI_API_KEY")
|
| 10 |
+
if not self.gemini_key:
|
| 11 |
+
# Fallback for local testing if env var is missing
|
|
|
|
| 12 |
try:
|
| 13 |
import config
|
| 14 |
+
self.gemini_key = config.API_KEY
|
| 15 |
except ImportError:
|
| 16 |
pass
|
| 17 |
+
|
| 18 |
+
if self.gemini_key:
|
| 19 |
+
self.gemini_client = genai.Client(api_key=self.gemini_key)
|
| 20 |
+
self.gemini_model = "gemini-2.5-flash"
|
| 21 |
+
print(f"π¦ Gemini Core: ONLINE [{self.gemini_model}]")
|
| 22 |
+
else:
|
| 23 |
+
raise ValueError("β CRITICAL: GEMINI_API_KEY missing in Secrets!")
|
| 24 |
|
| 25 |
+
# 2. SETUP GPT-4o (OpenAI)
|
| 26 |
+
self.openai_key = os.getenv("OPENAI_API_KEY")
|
| 27 |
+
self.openai_client = None
|
| 28 |
+
if self.openai_key:
|
| 29 |
+
self.openai_client = OpenAI(api_key=self.openai_key)
|
| 30 |
+
print("π’ OpenAI Core: ONLINE [gpt-4o]")
|
| 31 |
+
else:
|
| 32 |
+
print("β οΈ OpenAI Key not found. GPT-4o disabled.")
|
| 33 |
|
| 34 |
def _route_task(self, prompt):
|
| 35 |
+
""" The Router: Decides between Gemini (Fast) and GPT-4o (Smart) """
|
| 36 |
+
# We use Gemini for routing because it's cheaper/faster
|
| 37 |
routing_prompt = f"""
|
| 38 |
+
Classify this task.
|
| 39 |
+
[ASM] - Coding/Math (Best for GPT-4o)
|
| 40 |
+
[SFE] - Data/Science (Best for Gemini)
|
| 41 |
+
[CSM] - Creative/Story (Best for Gemini)
|
| 42 |
+
[CHAT] - Casual (Best for Gemini)
|
| 43 |
|
| 44 |
User Task: "{prompt}"
|
| 45 |
+
Reply ONLY with the tag.
|
| 46 |
"""
|
| 47 |
try:
|
| 48 |
+
response = self.gemini_client.models.generate_content(
|
| 49 |
+
model=self.gemini_model,
|
| 50 |
contents=routing_prompt
|
| 51 |
)
|
| 52 |
tag = response.text.strip().replace("[", "").replace("]", "")
|
|
|
|
| 55 |
return "CHAT"
|
| 56 |
|
| 57 |
def process_request(self, user_message, history, manual_role="Auto"):
|
| 58 |
+
# 1. Determine Role
|
|
|
|
|
|
|
|
|
|
| 59 |
if manual_role and manual_role != "Auto":
|
| 60 |
+
role = manual_role
|
|
|
|
| 61 |
else:
|
| 62 |
role = self._route_task(user_message)
|
| 63 |
+
|
| 64 |
+
print(f"π Routing to: [{role}]")
|
| 65 |
|
| 66 |
+
# 2. Assign Persona & Model
|
| 67 |
system_instruction = ""
|
| 68 |
+
model_to_use = "Gemini"
|
| 69 |
+
|
| 70 |
if role == "ASM":
|
| 71 |
+
system_instruction = "You are the ASM (Abstract Symbology Module). You are an expert Python Developer. Write efficient code."
|
| 72 |
+
# ASM (Coding) is better with GPT-4o if available
|
| 73 |
+
if self.openai_client:
|
| 74 |
+
model_to_use = "OpenAI"
|
| 75 |
+
|
| 76 |
elif role == "SFE":
|
| 77 |
+
system_instruction = "You are the SFE (Sensory Fusion Engine). You are a Data Scientist. Analyze facts objectively."
|
| 78 |
+
|
| 79 |
elif role == "CSM":
|
| 80 |
+
system_instruction = "You are the CSM (Creative Synthesis Module). You are a Novelist. Write with creativity."
|
| 81 |
+
|
| 82 |
else:
|
| 83 |
+
system_instruction = "You are Project Chimera."
|
| 84 |
|
| 85 |
+
# 3. Execute
|
|
|
|
|
|
|
| 86 |
try:
|
| 87 |
+
if model_to_use == "OpenAI" and self.openai_client:
|
| 88 |
+
# Call GPT-4o
|
| 89 |
+
response = self.openai_client.chat.completions.create(
|
| 90 |
+
model="gpt-4o",
|
| 91 |
+
messages=[
|
| 92 |
+
{"role": "system", "content": system_instruction},
|
| 93 |
+
{"role": "user", "content": user_message}
|
| 94 |
+
]
|
| 95 |
+
)
|
| 96 |
+
return response.choices[0].message.content, f"{role} (GPT-4o)"
|
| 97 |
+
|
| 98 |
+
else:
|
| 99 |
+
# Call Gemini
|
| 100 |
+
full_prompt = f"System Instruction: {system_instruction}\n\nUser Message: {user_message}"
|
| 101 |
+
response = self.gemini_client.models.generate_content(
|
| 102 |
+
model=self.gemini_model,
|
| 103 |
+
contents=full_prompt
|
| 104 |
+
)
|
| 105 |
+
return response.text, f"{role} (Gemini)"
|
| 106 |
+
|
| 107 |
except Exception as e:
|
| 108 |
return f"β System Error: {str(e)}", "ERR"
|