-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatbot.py
76 lines (61 loc) · 2.36 KB
/
chatbot.py
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from pydantic import BaseModel
from openai import OpenAI
import json
import speech_recognition as sr
import pyttsx3
config_data = json.load(open('config.json'))
OPENAI_KEY = config_data['OPENAI_KEY']
client = OpenAI(
api_key=OPENAI_KEY
)
class Prompt(BaseModel):
output: str
class Review(BaseModel):
score: int
feedback: str
recognizer = sr.Recognizer()
engine = pyttsx3.init()
def input_audio():
while True:
try:
with sr.Microphone() as mic:
recognizer.adjust_for_ambient_noise(mic, duration=0.2)
print("Please speak now...")
audio = recognizer.listen(mic)
text = recognizer.recognize_sphinx(audio)
print("You said:", text)
return text
except sr.UnknownValueError:
print("Could not understand audio, try again...")
continue
def output_audio(text):
engine.setProperty('rate', 150)
engine.setProperty('volume', 1)
engine.say(text)
engine.runAndWait()
def make_prompt(setting:str, level:int):
prompt = f"Ask me a conversational question appropriate for a {setting} setting. This should be a level {level} question, where level 1 indicates a straightforward, typical question, and higher levels indicate more complex questions."
completion = client.beta.chat.completions.parse(
model="gpt-4o-mini",
store=True,
messages=[
{"role": "user", "content": prompt}
],
response_format=Prompt,
)
return completion.choices[0].message.content
def evaluate_response(question:str, response:str):
prompt = f"Based on this question: {question}, how would you rate this response: {response}? Give it a score out of 10, where 1 is a very poor response and 10 is an excellent response. Then, give specific feedback on what you liked or didn't like about the response, as well as ways in which it could be improved."
completion = client.beta.chat.completions.parse(
model="gpt-4o-mini",
store=True,
messages=[
{"role": "user", "content": prompt}
],
response_format=Review,
)
return completion.choices[0].message.content
def init_response():
prompt_1 = "Choose the conversation type: Professional, Romantic, or Casual."
output_audio(prompt_1)
return jsonify({"response": prompt_1}), 200