baconnier commited on
Commit
478b26f
·
verified ·
1 Parent(s): 69e017e

Update art_explorer.py

Browse files
Files changed (1) hide show
  1. art_explorer.py +38 -59
art_explorer.py CHANGED
@@ -1,65 +1,44 @@
1
- from typing import Optional, List, Dict
2
- import os
3
  import instructor
4
- from groq import Groq
5
  from pydantic import BaseModel
6
- from prompts import SYSTEM_PROMPT, format_exploration_prompt, DEFAULT_RESPONSE
7
- from models import ExplorationResponse
8
 
9
- class ExplorationPathGenerator:
10
- def __init__(self, api_key: str):
11
- # Initialize GROQ client
12
- self.client = Groq(
13
- api_key=api_key
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
- def generate_exploration_path(
22
- self,
23
- user_query: str,
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
- try:
37
- formatted_prompt = format_exploration_prompt(
38
- user_query=user_query,
39
- selected_path=selected_path,
40
- exploration_parameters=exploration_parameters
41
- )
 
 
 
42
 
43
- response = self.client.chat.completions.create(
44
- model="mixtral-8x7b-32768",
45
- response_model=ExplorationResponse,
46
- messages=[
47
- {
48
- "role": "system",
49
- "content": SYSTEM_PROMPT
50
- },
51
- {
52
- "role": "user",
53
- "content": formatted_prompt
54
- }
55
- ],
56
- temperature=0.7,
57
- max_tokens=2000
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