63 lines
2.2 KiB
Python
63 lines
2.2 KiB
Python
# services/llm/grok_service.py
|
|
"""
|
|
Grok service implementation
|
|
"""
|
|
from typing import Dict, List, Optional
|
|
import json
|
|
from .base import LLMService
|
|
from config.api_keys import APIKeyManager
|
|
|
|
class GrokService(LLMService):
|
|
def __init__(self, model: str = "grok-1", temperature: float = 0.3):
|
|
api_key = APIKeyManager.get_grok_key()
|
|
if not api_key:
|
|
raise ValueError("Grok API key not found. Please set up your API keys.")
|
|
|
|
self.api_key = api_key
|
|
self.model = model
|
|
self.temperature = temperature
|
|
|
|
def generate_text(self, prompt: str) -> str:
|
|
"""
|
|
Generate text using the Grok API
|
|
TODO: Update this method when Grok API is available
|
|
"""
|
|
try:
|
|
# Placeholder for Grok API implementation
|
|
# Update this when the API is released
|
|
raise NotImplementedError("Grok API is not implemented yet")
|
|
|
|
except Exception as e:
|
|
print(f"Error in Grok API call: {e}")
|
|
return None
|
|
|
|
def get_similarity_scores(self, texts_pairs: Dict[str, List[str]]) -> List[float]:
|
|
"""
|
|
Calculate similarity scores using the Grok API
|
|
TODO: Update this method when Grok API is available
|
|
"""
|
|
try:
|
|
system_prompt = (
|
|
"Evaluate the semantic similarity between the following table of pairs of texts "
|
|
"in json format on a scale from 0 to 1. Return the similarity scores for every "
|
|
"row in JSON format as a list of numbers, without any additional text or formatting."
|
|
)
|
|
|
|
request_payload = json.dumps(texts_pairs)
|
|
|
|
# Placeholder for Grok API implementation
|
|
# Update this when the API is released
|
|
raise NotImplementedError("Grok API is not implemented yet")
|
|
|
|
except Exception as e:
|
|
print(f"Error in Grok similarity calculation: {e}")
|
|
return None
|
|
|
|
# Update config/api_keys.py to include Grok
|
|
@classmethod
|
|
def get_grok_key(cls) -> Optional[str]:
|
|
"""Get Grok API key from environment or stored configuration"""
|
|
return (
|
|
os.getenv('GROK_API_KEY') or
|
|
cls._get_stored_key('grok')
|
|
) |