Update core_creator/intent_parser.py
Browse files- core_creator/intent_parser.py +29 -14
core_creator/intent_parser.py
CHANGED
@@ -1,17 +1,11 @@
|
|
1 |
-
# intent_parser.py -
|
2 |
|
3 |
import os
|
4 |
-
import
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
if not GEMINI_API_KEY:
|
9 |
-
raise EnvironmentError("GEMINI_API_KEY not set in environment variables.")
|
10 |
|
11 |
-
genai.configure(api_key=GEMINI_API_KEY)
|
12 |
-
model = genai.GenerativeModel("gemini-pro")
|
13 |
-
|
14 |
-
# Categories the system understands
|
15 |
INTENT_CATEGORIES = [
|
16 |
"educational",
|
17 |
"assistive",
|
@@ -23,7 +17,16 @@ INTENT_CATEGORIES = [
|
|
23 |
"creative"
|
24 |
]
|
25 |
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
def classify_robot_idea(user_input: str) -> str:
|
29 |
prompt = f"""
|
@@ -35,10 +38,22 @@ def classify_robot_idea(user_input: str) -> str:
|
|
35 |
Category:
|
36 |
"""
|
37 |
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
# Example
|
42 |
if __name__ == "__main__":
|
43 |
-
idea = "Build a robot that
|
44 |
print("Predicted Intent:", classify_robot_idea(idea))
|
|
|
1 |
+
# intent_parser.py - Smart fallback to Gemini or OpenAI for intent classification
|
2 |
|
3 |
import os
|
4 |
+
import json
|
5 |
|
6 |
+
USE_GEMINI = bool(os.getenv("GEMINI_API_KEY"))
|
7 |
+
USE_OPENAI = bool(os.getenv("OPENAI_API_KEY"))
|
|
|
|
|
8 |
|
|
|
|
|
|
|
|
|
9 |
INTENT_CATEGORIES = [
|
10 |
"educational",
|
11 |
"assistive",
|
|
|
17 |
"creative"
|
18 |
]
|
19 |
|
20 |
+
if USE_GEMINI:
|
21 |
+
import google.generativeai as genai
|
22 |
+
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
|
23 |
+
gemini_model = genai.GenerativeModel(model_name="models/gemini-1.5-pro-latest")
|
24 |
+
|
25 |
+
elif USE_OPENAI:
|
26 |
+
from openai import OpenAI
|
27 |
+
openai_client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
28 |
+
else:
|
29 |
+
raise EnvironmentError("No valid API key set. Please set GEMINI_API_KEY or OPENAI_API_KEY.")
|
30 |
|
31 |
def classify_robot_idea(user_input: str) -> str:
|
32 |
prompt = f"""
|
|
|
38 |
Category:
|
39 |
"""
|
40 |
|
41 |
+
if USE_GEMINI:
|
42 |
+
response = gemini_model.generate_content(prompt)
|
43 |
+
return response.text.strip().lower()
|
44 |
+
|
45 |
+
elif USE_OPENAI:
|
46 |
+
response = openai_client.chat.completions.create(
|
47 |
+
model="gpt-4o",
|
48 |
+
messages=[
|
49 |
+
{"role": "system", "content": "You are a classification AI for robotics ideas."},
|
50 |
+
{"role": "user", "content": prompt}
|
51 |
+
],
|
52 |
+
temperature=0
|
53 |
+
)
|
54 |
+
return response.choices[0].message.content.strip().lower()
|
55 |
|
56 |
# Example
|
57 |
if __name__ == "__main__":
|
58 |
+
idea = "Build a robot that helps kids focus while doing homework."
|
59 |
print("Predicted Intent:", classify_robot_idea(idea))
|