File size: 782 Bytes
cc0f7f7
7157c49
 
 
 
97c3d64
7157c49
97c3d64
7157c49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
from openai import OpenAI


class ChatBot:
    def __init__(self):
        self.client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
        self.history = [{"role": "system", "content": "You are a helpful assistant."}]

    def generate_response(self, prompt: str) -> str:
        self.history.append({"role": "user", "content": prompt})
        
        completion = self.client.chat.completions.create(
            model="gpt-3.5-turbo", # NOTE: feel free to change it to gpt-4, or gpt-4o
            messages=self.history
        )
        
        response = completion.choices[0].message.content
        self.history.append({"role": "assistant", "content": response})
        
        return response

    def get_history(self) -> list:
        return self.history