Spaces:
Sleeping
Sleeping
File size: 1,825 Bytes
84bf79a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | 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" |