2
2
import streamlit as st
3
3
import openai
4
4
import json
5
+ import function as f
5
6
import requests as rq
6
7
from streamlit_chat import message
7
8
from dotenv import dotenv_values
@@ -62,6 +63,14 @@ def send_email(to_email, title, body):
62
63
63
64
return "Mail Sent,Recipient:{to_email}, Email Title: {title}, Email body: {body}"
64
65
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
+
65
74
66
75
def call_gpt (user_input ):
67
76
"""
@@ -78,63 +87,24 @@ def call_gpt(user_input):
78
87
st .session_state ['messages' ].append (
79
88
{"role" : "user" , "content" : user_input })
80
89
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
-
120
90
completion = openai .ChatCompletion .create (
121
91
model = "gpt-3.5-turbo-0613" , # gpt-4-0613
122
92
messages = st .session_state ['messages' ],
123
93
# messages=messages,
124
- functions = function , # Receive a list of functions
94
+ functions = f . function_list , # Receive a list of functions
125
95
# 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.
126
96
function_call = "auto" ,
127
97
)
128
98
99
+ print (completion )
100
+
129
101
return completion .choices [0 ].message
130
102
131
103
132
104
if __name__ == "__main__" :
133
-
134
105
st .title ("Small assistant" )
135
106
136
107
env = dotenv_values ()
137
-
138
108
openai .api_key = env ['OPENAI_API_KEY' ]
139
109
140
110
intents_list_obj = IntentsList ()
@@ -154,18 +124,20 @@ def call_gpt(user_input):
154
124
'name' ), function_call .get ('arguments' )
155
125
156
126
method_args_dict = json .loads (method_args )
157
-
158
127
method = getattr (intents_list_obj , method_name )
128
+ method_result = method (** method_args_dict )
159
129
160
- method_response = method (** method_args_dict )
161
-
130
+ # Append output to messages
162
131
st .session_state ['messages' ].append (assistant_output )
163
132
133
+ # Int to string
134
+ if type (method_result ) == int :
135
+ method_result = str (method_result )
136
+
137
+ # Append method result to messages
164
138
st .session_state ['messages' ].append (
165
139
{"role" : "function" , "name" : method_name ,
166
- "content" : method_response , })
167
-
168
- time .sleep (21 )
140
+ "content" : method_result , })
169
141
170
142
second_response = openai .ChatCompletion .create (
171
143
model = "gpt-3.5-turbo-0613" ,
@@ -181,12 +153,14 @@ def call_gpt(user_input):
181
153
st .session_state ['generated' ].append (
182
154
assistant_output .get ('content' ))
183
155
156
+ # Append content to messages
184
157
st .session_state ['messages' ].append (
185
158
{"role" : "assistant" , "content" : content })
186
159
187
- # History Chat Container
160
+ # History chat container
188
161
response_container = st .container ()
189
162
163
+ # Render session
190
164
if st .session_state ['generated' ]:
191
165
with response_container :
192
166
for i in range (len (st .session_state ['generated' ])):
0 commit comments