-
Notifications
You must be signed in to change notification settings - Fork 24
/
chatbot.py
executable file
·115 lines (95 loc) · 3.16 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env python3
from openai_decorator.openai_decorator import openaifunc, get_openai_funcs
import openai
import os
import sys
import json
openai.api_key = os.getenv("OPENAI_API_KEY")
@openaifunc
def get_current_weather(location: str, country: str) -> str:
"""
Gets the current weather information
@param location: The location for which to get the weather
@param country: The ISO 3166-1 alpha-2 country code
"""
if country == "FR":
return "The weather is terrible, as always"
elif location == "California":
return "The weather is nice and sunny"
else:
return "It's rainy and windy"
@openaifunc
def recommend_youtube_channel() -> str:
"""
Gets a really good recommendation for a YouTube channel to watch
"""
return "Unconventional Coding"
@openaifunc
def calculate_str_length(string: str) -> str:
"""
Calculates the length of a string
"""
return str(len(string))
# ChatGPT API Function
def send_message(message, messages):
# add user message to message list
messages.append(message)
try:
# send prompt to chatgpt
response = openai.ChatCompletion.create(
# model="gpt-4-0613",
model="gpt-3.5-turbo-0613",
messages=messages,
functions=get_openai_funcs(),
function_call="auto",
)
except openai.error.AuthenticationError:
print("AuthenticationError: Check your API-key")
sys.exit(1)
# add response to message list
messages.append(response["choices"][0]["message"])
return messages
# MAIN FUNCTION
def run_conversation(prompt, messages=[]):
# add user prompt to chatgpt messages
messages = send_message({"role": "user", "content": prompt}, messages)
# get chatgpt response
message = messages[-1]
# loop until project is finished
while True:
if message.get("function_call"):
# get function name and arguments
function_name = message["function_call"]["name"]
arguments = json.loads(message["function_call"]["arguments"])
# call function dangerously
function_response = globals()[function_name](**arguments)
# send function result to chatgpt
messages = send_message(
{
"role": "function",
"name": function_name,
"content": function_response,
},
messages,
)
else:
# if chatgpt doesn't respond with a function call, ask user for input
print("ChatGPT: " + message["content"])
user_message = input("You: ")
# send user message to chatgpt
messages = send_message(
{
"role": "user",
"content": user_message,
},
messages,
)
# save last response for the while loop
message = messages[-1]
# ASK FOR PROMPT
print(
"Go ahead, ask for the weather, a YouTube channel recommendation or to calculate the length of a string!"
)
prompt = input("You: ")
# RUN CONVERSATION
run_conversation(prompt)