PizzAI/frontend/chatgpt_wrap.py

100 lines
4.1 KiB
Python
Raw Normal View History

import random
2023-12-04 14:12:07 +01:00
from dupa import dupa
import openai
#
#
#data = [{
# "role": "system",
# "content": "You are a bot that will act as a guy that orders a pizza you will be connected on the phone with a pizza place. you will be asked questions about example: Where should be the pizza delivered etc. and you will respond with the data in the data section also you HAVE to respond in a full sentence because it will be transformed into audio using a tts software so you cant use a list just make a sentence like: i would like a margharitta and a cocacola please. basically just write it like you would say it dont put numbers but put words that are numbers dont add shortcuts add the full word\n\nDATA:\nphone: 103 870 844\ndelivery location: ul. Zielona 98/3, Kurwiszynów\npaymentMethod: gotówka\n2x Pizza Prościutto\n3x Pizza Pesto\n3x Lemoniada\n3x Sos Łagodny\n\nREMEMBER DONT USE NUMBERS, USE WORDS for example dont say 1x, say one time also REMEMBER to use replacement words to a word so it is appropriate to the whole sentence example:\nWRONG:\nChciałbym zamówić jedną Margherittę, dwie Colę, pięć Fant i jedną Sprite.\nRIGHT:\nChciałbym zamówić jedną Margarittę, dwie Kole, pięć Fant i jednego Sprajta.\n\nIf the call ends, say at the end of your final response \"CALLEND\"",
#}]
#while True:
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_address = self.delivery_address,
payment_method = self.payment_method
)
#### 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="gpt-3.5-turbo", messages=self.chat_history,
stop=["CALLEND"]
)
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 ''}"