Spaces:
Sleeping
Sleeping
| from typing import Optional, List, Dict | |
| import json | |
| import instructor | |
| from openai import OpenAI | |
| from prompts import SYSTEM_PROMPT, format_exploration_prompt, DEFAULT_RESPONSE | |
| from models import ExplorationResponse | |
| # Configure instructor for GROQ | |
| instructor.patch(backend="groq") | |
| class ExplorationPathGenerator: | |
| def __init__(self, api_key: str): | |
| self.client = OpenAI( | |
| base_url="https://api.groq.com/openai/v1", | |
| api_key=api_key | |
| ) | |
| def generate_exploration_path( | |
| self, | |
| user_query: str, | |
| selected_path: Optional[List[Dict[str, str]]] = None, | |
| exploration_parameters: Optional[Dict] = None | |
| ) -> Dict: | |
| if selected_path is None: | |
| selected_path = [] | |
| if exploration_parameters is None: | |
| exploration_parameters = { | |
| "depth": 5, | |
| "domain": None, | |
| "previous_explorations": [] | |
| } | |
| try: | |
| formatted_prompt = format_exploration_prompt( | |
| user_query=user_query, | |
| selected_path=selected_path, | |
| exploration_parameters=exploration_parameters | |
| ) | |
| # Use instructor with GROQ backend | |
| response = instructor.llm_mode(mode="groq")(ExplorationResponse).from_response( | |
| client=self.client, | |
| messages=[ | |
| { | |
| "role": "system", | |
| "content": SYSTEM_PROMPT | |
| }, | |
| { | |
| "role": "user", | |
| "content": formatted_prompt | |
| } | |
| ], | |
| model="mixtral-8x7b-32768", | |
| temperature=0.7, | |
| max_tokens=2000 | |
| ) | |
| # Convert to dict for JSON serialization | |
| return response.model_dump() | |
| except Exception as e: | |
| print(f"Error in API call: {e}") | |
| print(f"Error details: {str(e)}") | |
| return DEFAULT_RESPONSE |