PizzAI/frontend/chatgpt_wrap.py

101 lines
3.1 KiB
Python
Raw Normal View History

import random
2023-12-04 14:12:07 +01:00
from dupa import dupa
import openai
class ChatGPTWrap:
def __init__(self, use_chatgpt_placeholder = False):
self.use_chatgpt_placeholder = use_chatgpt_placeholder
# true chatgpt
if not use_chatgpt_placeholder:
print("Initializing ChatGPT... ", end="")
with open("system_instructions.txt", "r", encoding="utf-8") as f:
self.system_inst_template = f.read()
2023-12-04 14:12:07 +01:00
#### true openai chat gpt initialization stuff below (everything that needs to be done only once) ####
2023-12-04 14:12:07 +01:00
#raise NotImplementedError("True ChatGPT is not implemented yet!")
self.client = openai.OpenAI(api_key=dupa)
print("Done!")
# placeholder chatgpt
else:
print("Using ChatGPT placeholder!")
self.message_idx = 0
def init_order(self, phone_number, order_items, delivery_address, payment_method):
self.phone_number = phone_number
self.order_items = order_items
self.delivery_address = delivery_address
self.payment_method = payment_method
# true chatgpt
if not self.use_chatgpt_placeholder:
# generate system instructions from template
self.system_inst = self.system_inst_template.format(
phone_number = self.phone_number,
order_items = self.order_items,
delivery_location = self.delivery_address,
payment_method = self.payment_method
)
print("System:")
print(self.system_inst)
#### true openai chat gpt system instructions initialization stuff below ####
##### (everything that needs to be done each assistant session like some chat gpt conversation cleanup) ####
2023-12-04 14:12:07 +01:00
self.chat_history = [{
"role": "system",
"content": self.system_inst
}]
def get_response(self, input_message):
# true chatgpt
if not self.use_chatgpt_placeholder:
#### true openai chat gpt response stuff below ####
2023-12-04 14:12:07 +01:00
self.chat_history.append(
{"role": "user", "content": input_message},
)
chat = self.client.chat.completions.create(
model="ft:gpt-3.5-turbo-1106:personal::8QBgBttE",
messages=self.chat_history,
temperature=0.57,
max_tokens=256,
top_p=1,
frequency_penalty=0,
presence_penalty=0
2023-12-04 14:12:07 +01:00
)
2023-12-04 14:12:07 +01:00
reply = chat.choices[0].message.content
2023-12-04 14:12:07 +01:00
self.chat_history.append(
{"role": "assistant", "content": reply}
)
return reply
# placeholder chatgpt
else:
choices = (
self.phone_number,
self.order_items,
self.delivery_address,
self.payment_method
)
self.message_idx += 1
return f"czat dżi pi ti plejsholder {random.choice(choices)}{' CALLEND' if self.message_idx == 3 else ''}"