Spaces:
Sleeping
Sleeping
Update art_explorer.py
Browse files- art_explorer.py +67 -19
art_explorer.py
CHANGED
@@ -1,9 +1,7 @@
|
|
1 |
-
|
2 |
-
import instructor
|
3 |
from openai import OpenAI
|
4 |
-
from pydantic import BaseModel
|
5 |
-
from typing import List, Optional
|
6 |
import json
|
|
|
7 |
|
8 |
class ExplorationNode(BaseModel):
|
9 |
title: str
|
@@ -13,32 +11,82 @@ class ExplorationNode(BaseModel):
|
|
13 |
|
14 |
class ExplorationPath(BaseModel):
|
15 |
nodes: List[ExplorationNode]
|
16 |
-
|
|
|
17 |
|
18 |
class ExplorationPathGenerator:
|
19 |
def __init__(self, api_key: str):
|
20 |
-
# Initialize OpenAI client with GROQ's base URL
|
21 |
self.client = OpenAI(
|
22 |
api_key=api_key,
|
23 |
-
base_url="https://api.groq.com/
|
24 |
)
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
try:
|
30 |
-
|
31 |
-
|
|
|
32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
response = self.client.chat.completions.create(
|
34 |
model="mixtral-8x7b-32768",
|
35 |
-
response_model=ExplorationPath,
|
36 |
messages=[
|
37 |
{"role": "system", "content": system_prompt},
|
38 |
-
{"role": "user", "content":
|
39 |
-
]
|
|
|
|
|
40 |
)
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
except Exception as e:
|
43 |
-
print(f"Error generating exploration path: {
|
44 |
-
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Dict, Any, List, Optional
|
|
|
2 |
from openai import OpenAI
|
|
|
|
|
3 |
import json
|
4 |
+
from pydantic import BaseModel
|
5 |
|
6 |
class ExplorationNode(BaseModel):
|
7 |
title: str
|
|
|
11 |
|
12 |
class ExplorationPath(BaseModel):
|
13 |
nodes: List[ExplorationNode]
|
14 |
+
query: str
|
15 |
+
domain: Optional[str] = None
|
16 |
|
17 |
class ExplorationPathGenerator:
|
18 |
def __init__(self, api_key: str):
|
|
|
19 |
self.client = OpenAI(
|
20 |
api_key=api_key,
|
21 |
+
base_url="https://api.groq.com/v1"
|
22 |
)
|
23 |
+
|
24 |
+
def generate_exploration_path(
|
25 |
+
self,
|
26 |
+
query: str, # Changed from user_query to query to match app.py
|
27 |
+
selected_path: List[Dict[str, Any]] = None,
|
28 |
+
exploration_parameters: Dict[str, Any] = None
|
29 |
+
) -> Dict[str, Any]:
|
30 |
+
"""
|
31 |
+
Generate an exploration path based on the query and parameters
|
32 |
+
|
33 |
+
Args:
|
34 |
+
query: The user's exploration query
|
35 |
+
selected_path: Previously selected exploration path nodes
|
36 |
+
exploration_parameters: Additional parameters for exploration
|
37 |
+
|
38 |
+
Returns:
|
39 |
+
Dict containing the exploration path and metadata
|
40 |
+
"""
|
41 |
try:
|
42 |
+
# Set defaults
|
43 |
+
selected_path = selected_path or []
|
44 |
+
exploration_parameters = exploration_parameters or {}
|
45 |
|
46 |
+
# Extract parameters
|
47 |
+
depth = exploration_parameters.get("depth", 5)
|
48 |
+
domain = exploration_parameters.get("domain")
|
49 |
+
|
50 |
+
# Prepare system prompt
|
51 |
+
system_prompt = """You are an expert art historian assistant. Generate structured exploration paths
|
52 |
+
that help users discover connections and insights in art history. Format your response as a JSON object
|
53 |
+
with 'nodes' containing exploration points."""
|
54 |
+
|
55 |
+
# Prepare user message
|
56 |
+
user_message = {
|
57 |
+
"query": query,
|
58 |
+
"depth": depth,
|
59 |
+
"domain": domain,
|
60 |
+
"previous_explorations": selected_path
|
61 |
+
}
|
62 |
+
|
63 |
+
# Make API call
|
64 |
response = self.client.chat.completions.create(
|
65 |
model="mixtral-8x7b-32768",
|
|
|
66 |
messages=[
|
67 |
{"role": "system", "content": system_prompt},
|
68 |
+
{"role": "user", "content": json.dumps(user_message)}
|
69 |
+
],
|
70 |
+
temperature=0.7,
|
71 |
+
max_tokens=2000
|
72 |
)
|
73 |
+
|
74 |
+
# Parse and validate response
|
75 |
+
result = json.loads(response.choices[0].message.content)
|
76 |
+
|
77 |
+
# Validate with Pydantic
|
78 |
+
exploration_path = ExplorationPath(
|
79 |
+
nodes=[ExplorationNode(**node) for node in result["nodes"]],
|
80 |
+
query=query,
|
81 |
+
domain=domain
|
82 |
+
)
|
83 |
+
|
84 |
+
return exploration_path.model_dump()
|
85 |
+
|
86 |
except Exception as e:
|
87 |
+
print(f"Error generating exploration path: {e}")
|
88 |
+
return {
|
89 |
+
"error": str(e),
|
90 |
+
"status": "failed",
|
91 |
+
"message": "Failed to generate exploration path"
|
92 |
+
}
|