Spaces:
Sleeping
Sleeping
Update art_explorer.py
Browse files- art_explorer.py +17 -17
art_explorer.py
CHANGED
@@ -1,7 +1,12 @@
|
|
1 |
from typing import Optional, List, Dict
|
2 |
import json
|
|
|
3 |
from openai import OpenAI
|
4 |
from prompts import SYSTEM_PROMPT, format_exploration_prompt, DEFAULT_RESPONSE
|
|
|
|
|
|
|
|
|
5 |
|
6 |
class ExplorationPathGenerator:
|
7 |
def __init__(self, api_key: str):
|
@@ -25,13 +30,14 @@ class ExplorationPathGenerator:
|
|
25 |
"previous_explorations": []
|
26 |
}
|
27 |
|
28 |
-
formatted_prompt = format_exploration_prompt(
|
29 |
-
user_query=user_query,
|
30 |
-
selected_path=selected_path,
|
31 |
-
exploration_parameters=exploration_parameters
|
32 |
-
)
|
33 |
-
|
34 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
response = self.client.chat.completions.create(
|
36 |
model="mixtral-8x7b-32768",
|
37 |
messages=[
|
@@ -45,19 +51,13 @@ class ExplorationPathGenerator:
|
|
45 |
}
|
46 |
],
|
47 |
temperature=0.7,
|
48 |
-
max_tokens=2000
|
|
|
49 |
)
|
50 |
|
51 |
-
|
52 |
-
|
53 |
-
parsed_result = json.loads(result)
|
54 |
-
print("Generated response:", json.dumps(parsed_result, indent=2)) # Debug print
|
55 |
-
return parsed_result
|
56 |
-
except json.JSONDecodeError as e:
|
57 |
-
print(f"Error parsing JSON response: {e}")
|
58 |
-
print("Raw response:", result) # Debug print
|
59 |
-
return DEFAULT_RESPONSE
|
60 |
|
61 |
except Exception as e:
|
62 |
-
print(f"Error in API call: {e}")
|
63 |
return DEFAULT_RESPONSE
|
|
|
1 |
from typing import Optional, List, Dict
|
2 |
import json
|
3 |
+
import instructor
|
4 |
from openai import OpenAI
|
5 |
from prompts import SYSTEM_PROMPT, format_exploration_prompt, DEFAULT_RESPONSE
|
6 |
+
from models import ExplorationResponse
|
7 |
+
|
8 |
+
# Enable instructor
|
9 |
+
instructor.patch()
|
10 |
|
11 |
class ExplorationPathGenerator:
|
12 |
def __init__(self, api_key: str):
|
|
|
30 |
"previous_explorations": []
|
31 |
}
|
32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
try:
|
34 |
+
formatted_prompt = format_exploration_prompt(
|
35 |
+
user_query=user_query,
|
36 |
+
selected_path=selected_path,
|
37 |
+
exploration_parameters=exploration_parameters
|
38 |
+
)
|
39 |
+
|
40 |
+
# Use instructor to generate and validate the response
|
41 |
response = self.client.chat.completions.create(
|
42 |
model="mixtral-8x7b-32768",
|
43 |
messages=[
|
|
|
51 |
}
|
52 |
],
|
53 |
temperature=0.7,
|
54 |
+
max_tokens=2000,
|
55 |
+
response_model=ExplorationResponse
|
56 |
)
|
57 |
|
58 |
+
# Convert to dict for JSON serialization
|
59 |
+
return response.model_dump()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
except Exception as e:
|
62 |
+
print(f"Error in API call: {e}")
|
63 |
return DEFAULT_RESPONSE
|