File size: 1,175 Bytes
1803c5e
10e9b7d
904e0bd
1803c5e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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)}]"