-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChatBotBrain.py
126 lines (100 loc) · 4.58 KB
/
ChatBotBrain.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
116
117
118
119
120
121
122
123
124
125
import openai
from openai import OpenAI
import json, os
from dotenv import load_dotenv
load_dotenv()
openai.api_key = os.getenv("OPENAI_API_KEY")
from functions_actions import get_weather_forecast, websearch
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
#define main chatcompletion - Chatbot whit functions call
def run_conversation(prompt):
# Step 1: send the conversation and available functions to GPT
messages = [
{"role": "system", "content": "You are a helpfull assistant. Only use the functions provided to you and before answer think about the need to seach on internet using the function"},
{"role": "user", "content": f"{prompt}?"}, ]
functions = [
# {
# "name": "get_weather_forecast",
# "description": "Get the weather forecast up to 5 days with data every 3 hours or the current weather in a given location",
# "parameters": {
# "type": "object",
# "properties": {
# "location": {
# "type": "string",
# "description": "The city and state, e.g. San Francisco, CA",
# },
# "cnt": {"type": "integer",
# "description": "Choice the timestamp 1 up to 40 for forecast each 3 hours to return or timestamp 0 for the current weather. Forecast Default is 1, which returns one forecast timestamp availiable. Current weather Default is 0, wich returns the current wheather"},
# },
# "required": ["location", "cnt"],
# },
# },
{
"name": "websearch",
"description": "Usefull to aswer questions about recent events. Do not use it to general questions",
"parameters": {
"type": "object",
"properties": {
"query":{
"type":"string",
"description": "query to search tools, e.g. who won the us open this year?, recent news about this subject, weather questios, news"
},
},
"required": ["query"],
},
},
]
#response = openai.ChatCompletion.create(
# model="gpt-4o-mini",
# messages=messages,
# functions=functions,
# function_call="auto",
#)
#response_message = response["choices"][0]["message"]
response = client.chat.completions.create(model="gpt-4o-mini",
messages=messages,
functions=functions,
function_call="auto")
response_message = response.choices[0].message
# Step 2: check if GPT wanted to call a function
if response_message.function_call:
# Step 3: call the function
available_functions = {
#"get_weather_forecast": get_weather_forecast,
"websearch":websearch,
} # can have multiple functions
function_name = response_message.function_call.name
fuction_to_call = available_functions[function_name]
function_args = json.loads(response_message.function_call.arguments)
#I got this solution because first I put all args togheter and-> TypeError: websearch() got an unexpected keyword argument 'location'
if function_name == "get_weather_forecast":
function_response = fuction_to_call(
location=function_args.get("location"),
cnt=function_args.get("cnt"),
)
elif function_name == "websearch":
function_response = fuction_to_call(
query=function_args.get("query"),
)
else:
function_response = None
# Step 4: send the info on the function call and function response to GPT
messages.append(response_message) # extend conversation with assistant's reply
messages.append(
{
"role": "function",
"name": function_name,
"content": function_response,
}
) # extend conversation with function response
#second_response = openai.ChatCompletion.create(
# model="gpt-4o-mini",
# messages=messages,
#) # get a new response from GPT where it can see the function response
second_response = client.chat.completions.create(model="gpt-4o-mini",
messages=messages
) # get a new response from GPT where it can see the function response
#print(second_response)
return second_response
else:
return response