File size: 933 Bytes
cc0f7f7
7157c49
 
 
 
0db9e34
7157c49
3cf3de4
0db9e34
 
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
26
27
import os
from openai import OpenAI


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

    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