RGB26Demo / OpenAIAPIModel.py
Ajaykanth Maddi
Code Changes - output path fix.
2ea2192
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"