| import requests | |
| import time | |
| class GroqOpenAIAPIModel: | |
| def __init__(self, api_key, url="https://api.groq.com/openai/v1/chat/completions", model="llama3-8b-8192"): | |
| self.url = url | |
| self.model = model | |
| self.API_KEY = api_key | |
| print(f"GroqOpenAIAPIModel initialized with model: {self.model}") | |
| def generate(self, text: str, temperature=0.7, system="You are a helpful assistant.", top_p=1): | |
| time.sleep(5) | |
| headers = { | |
| "Authorization": f"Bearer {self.API_KEY}", | |
| "Content-Type": "application/json" | |
| } | |
| query = { | |
| "model": self.model, | |
| "temperature": temperature, | |
| "top_p": top_p, | |
| "messages": [ | |
| {"role": "system", "content": system}, | |
| {"role": "user", "content": text} | |
| ], | |
| "stream": False | |
| } | |
| try: | |
| response = requests.post(self.url, headers=headers, json=query) | |
| response.raise_for_status() | |
| response_json = response.json() | |
| if 'choices' not in response_json: | |
| print("β οΈ 'choices' missing in response") | |
| print("π Input text:", text) | |
| print("π¦ Full response:", response_json) | |
| return "[ERROR] Response missing 'choices'" | |
| return response_json['choices'][0]['message']['content'] | |
| except requests.exceptions.RequestException as e: | |
| print("β HTTP error during API call:", e) | |
| print("π Input text:", text) | |
| return "[ERROR] API call failed" | |
| except Exception as e: | |
| print("β Unexpected error:", e) | |
| print("π Input text:", text) | |
| return "[ERROR] Unexpected failure" |