AIencoder commited on
Commit
90195b4
Β·
verified Β·
1 Parent(s): 3abac1d

Update src/chimera_core.py

Browse files
Files changed (1) hide show
  1. 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
- # On Hugging Face, we use os.getenv to grab the secret key safely
8
- self.api_key = os.getenv("GEMINI_API_KEY")
9
-
10
- # Fallback for local testing if env var is missing
11
- if not self.api_key:
12
  try:
13
  import config
14
- self.api_key = config.API_KEY
15
  except ImportError:
16
  pass
 
 
 
 
 
 
 
17
 
18
- if not self.api_key:
19
- raise ValueError("❌ CRITICAL: API Key missing! Set GEMINI_API_KEY in Secrets.")
20
-
21
- self.client = genai.Client(api_key=self.api_key)
22
- self.model_name = "gemini-2.5-flash"
23
- print(f"🦁 Chimera Core: ONLINE [{self.model_name}]")
 
 
24
 
25
  def _route_task(self, prompt):
26
- """ The Router: Classifies the task automatically. """
 
27
  routing_prompt = f"""
28
- Classify this user task into one of these roles:
29
- [ASM] - Abstract Symbology Module (Code, Math)
30
- [SFE] - Sensory Fusion Engine (Data, Science)
31
- [CSM] - Creative Synthesis Module (Story, Art)
32
- [CHAT] - General conversation.
33
 
34
  User Task: "{prompt}"
35
- Reply ONLY with the tag (e.g., [ASM]).
36
  """
37
  try:
38
- response = self.client.models.generate_content(
39
- model=self.model_name,
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 # User forced this role
54
- print(f"πŸ‘‰ Manual Override: [{role}]")
55
  else:
56
  role = self._route_task(user_message)
57
- print(f"πŸ‘‰ Auto-Routing to: [{role}]")
 
58
 
59
- # 2. Assign the Persona
60
  system_instruction = ""
 
 
61
  if role == "ASM":
62
- system_instruction = "You are the ASM (Abstract Symbology Module). You are an expert Software Architect and Mathematician. Your responses must be code-centric, precise, and optimized."
 
 
 
 
63
  elif role == "SFE":
64
- system_instruction = "You are the SFE (Sensory Fusion Engine). You are a Lead Data Scientist. Analyze this request using logic, empirical data, and patterns."
 
65
  elif role == "CSM":
66
- system_instruction = "You are the CSM (Creative Synthesis Module). You are a Visionary Author. Respond with creativity, rich imagery, and narrative flair."
 
67
  else:
68
- system_instruction = "You are Project Chimera, an advanced multi-persona AI system."
69
 
70
- # 3. Generate
71
- full_prompt = f"System Instruction: {system_instruction}\n\nUser Message: {user_message}"
72
-
73
  try:
74
- response = self.client.models.generate_content(
75
- model=self.model_name,
76
- contents=full_prompt
77
- )
78
- return response.text, role
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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"