Spaces:
Sleeping
Sleeping
from openai import OpenAI | |
import os | |
class BasicAgent: | |
def __init__(self): | |
api_key = os.getenv("OPENAI_API_KEY") | |
if not api_key: | |
raise ValueError("OPENAI_API_KEY environment variable not set.") | |
self.client = OpenAI(api_key=api_key) | |
print("BasicAgent (GPT-based) initialized.") | |
def __call__(self, question: str) -> str: | |
print(f"Agent received question (first 50 chars): {question[:50]}...") | |
try: | |
response = self.client.chat.completions.create( | |
model="gpt-4", # Bisa diganti dengan "gpt-3.5-turbo" jika quota terbatas | |
messages=[ | |
{"role": "system", "content": "You are a helpful assistant."}, | |
{"role": "user", "content": question} | |
], | |
temperature=0.7, | |
max_tokens=500, | |
) | |
answer = response.choices[0].message.content.strip() | |
print(f"Agent returning answer: {answer[:100]}...") | |
return answer | |
except Exception as e: | |
print(f"Error during OpenAI completion: {e}") | |
return f"[ERROR from agent: {str(e)}]" | |