Skip to content

Commit 6b7f870

Browse files
committed
feat(use-examples): optimize code
1 parent 55a6d80 commit 6b7f870

File tree

2 files changed

+97
-49
lines changed

2 files changed

+97
-49
lines changed

function.py

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
function_list = [
2+
{
3+
"name": "query_city_weather", # Function Name
4+
"description": "query weather temperature", # Meta information of function
5+
"parameters": { # parameters
6+
"type": "object",
7+
"properties": {
8+
"city": {
9+
"type": "string",
10+
"description": "The city",
11+
},
12+
},
13+
"required": ["city"],
14+
},
15+
},
16+
{
17+
"name": "send_email",
18+
"description": "Send email information",
19+
"parameters": {
20+
"type": "object",
21+
"properties": {
22+
"to_email": {
23+
"type": "string",
24+
"description": "Recipient's email address"
25+
},
26+
"title": {
27+
"type": "string",
28+
"description": "The title of the email"
29+
},
30+
"body": {
31+
"type": "string",
32+
"description": "The Body of the email"
33+
}
34+
},
35+
"required": ["to_email", "title", "body"],
36+
}
37+
},
38+
{
39+
"name": "addition_function",
40+
"description": "Calculate the left and right values ​​of the + method operator symbol",
41+
"parameters": {
42+
"type": "object",
43+
"properties": {
44+
"left": {
45+
"type": "number",
46+
"description": "+The value on the left side of the operator symbol"
47+
},
48+
"right": {
49+
"type": "number",
50+
"description": "+The value on the right side of the operator symbol"
51+
}
52+
},
53+
"required": ["left", "right"]
54+
}
55+
},
56+
{
57+
"name": "substruction_function",
58+
"description": "substruction the left and right values ​​of the - method operator symbol",
59+
"parameters": {
60+
"type": "object",
61+
"properties": {
62+
"left": {
63+
"type": "number",
64+
"description": "-The value on the left side of the operator symbol"
65+
},
66+
"right": {
67+
"type": "number",
68+
"description": "-The value on the right side of the operator symbol"
69+
}
70+
},
71+
"required": ["left", "right"]
72+
}
73+
}
74+
]

main.py

+23-49
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import streamlit as st
33
import openai
44
import json
5+
import function as f
56
import requests as rq
67
from streamlit_chat import message
78
from dotenv import dotenv_values
@@ -62,6 +63,14 @@ def send_email(to_email, title, body):
6263

6364
return "Mail Sent,Recipient:{to_email}, Email Title: {title}, Email body: {body}"
6465

66+
@staticmethod
67+
def addition_function(left, right):
68+
return left + right
69+
70+
@staticmethod
71+
def substruction_function(left, right):
72+
return left - right
73+
6574

6675
def call_gpt(user_input):
6776
"""
@@ -78,63 +87,24 @@ def call_gpt(user_input):
7887
st.session_state['messages'].append(
7988
{"role": "user", "content": user_input})
8089

81-
function = [
82-
{
83-
"name": "query_city_weather", # Function Name
84-
"description": "query weather temperature", # Meta information of function
85-
"parameters": { # parameters
86-
"type": "object",
87-
"properties": {
88-
"city": {
89-
"type": "string",
90-
"description": "The city",
91-
},
92-
},
93-
"required": ["city"],
94-
},
95-
},
96-
{
97-
"name": "send_email",
98-
"description": "Send email information",
99-
"parameters": {
100-
"type": "object",
101-
"properties": {
102-
"to_email": {
103-
"type": "string",
104-
"description": "Recipient's email address"
105-
},
106-
"title": {
107-
"type": "string",
108-
"description": "The title of the email"
109-
},
110-
"body": {
111-
"type": "string",
112-
"description": "The Body of the email"
113-
}
114-
},
115-
"required": ["to_email", "title", "body"],
116-
}
117-
}
118-
]
119-
12090
completion = openai.ChatCompletion.create(
12191
model="gpt-3.5-turbo-0613", # gpt-4-0613
12292
messages=st.session_state['messages'],
12393
# messages=messages,
124-
functions=function, # Receive a list of functions
94+
functions=f.function_list, # Receive a list of functions
12595
# Indicates whether the OpenAI model should use the functions in the function list, set to auto, which means that the AI model should guess by itself.
12696
function_call="auto",
12797
)
12898

99+
print(completion)
100+
129101
return completion.choices[0].message
130102

131103

132104
if __name__ == "__main__":
133-
134105
st.title("Small assistant")
135106

136107
env = dotenv_values()
137-
138108
openai.api_key = env['OPENAI_API_KEY']
139109

140110
intents_list_obj = IntentsList()
@@ -154,18 +124,20 @@ def call_gpt(user_input):
154124
'name'), function_call.get('arguments')
155125

156126
method_args_dict = json.loads(method_args)
157-
158127
method = getattr(intents_list_obj, method_name)
128+
method_result = method(**method_args_dict)
159129

160-
method_response = method(**method_args_dict)
161-
130+
# Append output to messages
162131
st.session_state['messages'].append(assistant_output)
163132

133+
# Int to string
134+
if type(method_result) == int:
135+
method_result = str(method_result)
136+
137+
# Append method result to messages
164138
st.session_state['messages'].append(
165139
{"role": "function", "name": method_name,
166-
"content": method_response, })
167-
168-
time.sleep(21)
140+
"content": method_result, })
169141

170142
second_response = openai.ChatCompletion.create(
171143
model="gpt-3.5-turbo-0613",
@@ -181,12 +153,14 @@ def call_gpt(user_input):
181153
st.session_state['generated'].append(
182154
assistant_output.get('content'))
183155

156+
# Append content to messages
184157
st.session_state['messages'].append(
185158
{"role": "assistant", "content": content})
186159

187-
# History Chat Container
160+
# History chat container
188161
response_container = st.container()
189162

163+
# Render session
190164
if st.session_state['generated']:
191165
with response_container:
192166
for i in range(len(st.session_state['generated'])):

0 commit comments

Comments
 (0)