Spaces:
Sleeping
Sleeping
Update art_explorer.py
Browse files- art_explorer.py +38 -59
art_explorer.py
CHANGED
@@ -1,65 +1,44 @@
|
|
1 |
-
|
2 |
-
import os
|
3 |
import instructor
|
4 |
-
from
|
5 |
from pydantic import BaseModel
|
6 |
-
from
|
7 |
-
|
8 |
|
9 |
-
class
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
)
|
15 |
-
# Patch with instructor using TOOLS mode
|
16 |
-
self.client = instructor.from_groq(
|
17 |
-
self.client,
|
18 |
-
mode=instructor.Mode.TOOLS
|
19 |
-
)
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
selected_path: Optional[List[Dict[str, str]]] = None,
|
25 |
-
exploration_parameters: Optional[Dict] = None
|
26 |
-
) -> Dict:
|
27 |
-
if selected_path is None:
|
28 |
-
selected_path = []
|
29 |
-
if exploration_parameters is None:
|
30 |
-
exploration_parameters = {
|
31 |
-
"depth": 5,
|
32 |
-
"domain": None,
|
33 |
-
"previous_explorations": []
|
34 |
-
}
|
35 |
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
return response.model_dump()
|
61 |
-
|
62 |
-
except Exception as e:
|
63 |
-
print(f"Error in API call: {e}")
|
64 |
-
print(f"Error details: {str(e)}")
|
65 |
-
return DEFAULT_RESPONSE
|
|
|
1 |
+
# art_explorer.py
|
|
|
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
|
10 |
+
description: str
|
11 |
+
connections: List[str]
|
12 |
+
depth: int
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
+
class ExplorationPath(BaseModel):
|
15 |
+
nodes: List[ExplorationNode]
|
16 |
+
context: str
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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/openai/v1"
|
24 |
+
)
|
25 |
+
# Patch the client with instructor
|
26 |
+
self.client = instructor.patch(self.client)
|
27 |
|
28 |
+
def generate_path(self, query: str, max_depth: int = 3) -> ExplorationPath:
|
29 |
+
try:
|
30 |
+
system_prompt = """You are an art history expert creating exploration paths.
|
31 |
+
Generate a structured exploration path based on the query, with connected concepts."""
|
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": f"Generate an exploration path for: {query}. Maximum depth: {max_depth}"}
|
39 |
+
]
|
40 |
+
)
|
41 |
+
return response
|
42 |
+
except Exception as e:
|
43 |
+
print(f"Error generating exploration path: {str(e)}")
|
44 |
+
raise
|
|
|
|
|
|
|
|
|
|
|
|