Spaces:
Runtime error
Runtime error
Sohyun Sim commited on
Commit ·
e483d1b
1
Parent(s): 8617bcb
edit app.py
Browse files
app.py
CHANGED
|
@@ -32,64 +32,35 @@ def _execute_function(function_call, chat_messages):
|
|
| 32 |
if function_call["name"] == "guess_word":
|
| 33 |
print("update guess")
|
| 34 |
# Step 4: send the info on the function call and function response to GPT
|
| 35 |
-
chat_messages.append(response_message.to_dict()) # extend conversation with assistant's reply
|
| 36 |
chat_messages.append(
|
| 37 |
{"role": "function",
|
| 38 |
"name": function_name,
|
| 39 |
"content": function_response["choices"][0]}
|
| 40 |
) # extend conversation with function response
|
| 41 |
-
|
| 42 |
model=GPT_MODEL,
|
| 43 |
messages=chat_messages,
|
| 44 |
) # get a new response from GPT where it can se the function response
|
| 45 |
-
chat_messages.append(
|
| 46 |
-
return chat_messages
|
| 47 |
|
| 48 |
-
def create_chat(
|
| 49 |
-
openai.api_key =
|
| 50 |
chat_messages = [{"role": "user", "content": user_input}]
|
| 51 |
response = openai.ChatCompletion.create(
|
| 52 |
model=GPT_MODEL,
|
| 53 |
messages=system_message+chat_messages,
|
| 54 |
functions=get_functions()
|
| 55 |
)
|
| 56 |
-
response_message = response.choices[0].message
|
|
|
|
| 57 |
|
| 58 |
# Step 2: check if CPT wanted to call a function
|
| 59 |
-
|
| 60 |
# Step 3: call the function
|
| 61 |
# Note: the JSON response may not always be valid; be sure to handle errors
|
| 62 |
-
|
| 63 |
-
"guess_word": get_guess,
|
| 64 |
-
"lookup_answer": get_secret,
|
| 65 |
-
"retrive_puzzle": get_puzzle_num
|
| 66 |
-
}
|
| 67 |
-
function_name = response_message["function_call"]["name"]
|
| 68 |
-
function_to_call = available_functions[function_name]
|
| 69 |
-
function_args = json.loads(response_message["function_call"]["arguments"])
|
| 70 |
-
function_response = function_to_call(
|
| 71 |
-
word=function_args.get("word"),
|
| 72 |
-
puzzle_num=puzzle_num
|
| 73 |
-
)
|
| 74 |
-
guess_result = update_guess(function_response, guessed, guesses)
|
| 75 |
-
print(guess_result)
|
| 76 |
-
# Step 4: send the info on the function call and function response to GPT
|
| 77 |
-
chat_messages.append(response_message.to_dict()) # extend conversation with assistant's reply
|
| 78 |
-
chat_messages.append(
|
| 79 |
-
{"role": "function",
|
| 80 |
-
"name": function_name,
|
| 81 |
-
"content": guess_result}
|
| 82 |
-
) # extend conversation with function response
|
| 83 |
-
second_response = openai.ChatCompletion.create(
|
| 84 |
-
model=GPT_MODEL,
|
| 85 |
-
messages=system_message+chat_history+chat_messages,
|
| 86 |
-
) # get a new response from GPT where it can se the function response
|
| 87 |
-
chat_messages.append(second_response["choices"][0]["message"].to_dict())
|
| 88 |
-
chat_history = chat_history + chat_messages
|
| 89 |
-
return chat_messages[-1]
|
| 90 |
|
| 91 |
-
chat_messages.append(response_message.to_dict())
|
| 92 |
-
chat_history = chat_history + chat_messages
|
| 93 |
return chat_messages[-1]
|
| 94 |
|
| 95 |
with gr.Blocks() as demo:
|
|
|
|
| 32 |
if function_call["name"] == "guess_word":
|
| 33 |
print("update guess")
|
| 34 |
# Step 4: send the info on the function call and function response to GPT
|
|
|
|
| 35 |
chat_messages.append(
|
| 36 |
{"role": "function",
|
| 37 |
"name": function_name,
|
| 38 |
"content": function_response["choices"][0]}
|
| 39 |
) # extend conversation with function response
|
| 40 |
+
next_response = openai.ChatCompletion.create(
|
| 41 |
model=GPT_MODEL,
|
| 42 |
messages=chat_messages,
|
| 43 |
) # get a new response from GPT where it can se the function response
|
| 44 |
+
chat_messages.append(next_response.choices[0].message.to_dict())
|
| 45 |
+
return next_response, chat_messages
|
| 46 |
|
| 47 |
+
def create_chat(key, user_input):
|
| 48 |
+
openai.api_key = key
|
| 49 |
chat_messages = [{"role": "user", "content": user_input}]
|
| 50 |
response = openai.ChatCompletion.create(
|
| 51 |
model=GPT_MODEL,
|
| 52 |
messages=system_message+chat_messages,
|
| 53 |
functions=get_functions()
|
| 54 |
)
|
| 55 |
+
response_message = response.choices[0].message.to_dict()
|
| 56 |
+
chat_messages.append(response_message) # extend conversation with assistant's reply
|
| 57 |
|
| 58 |
# Step 2: check if CPT wanted to call a function
|
| 59 |
+
while response_message.get("function_call"):
|
| 60 |
# Step 3: call the function
|
| 61 |
# Note: the JSON response may not always be valid; be sure to handle errors
|
| 62 |
+
response_message, chat_messages = _execute_function(response_message["function_call"], chat_messages)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
|
|
|
|
|
|
| 64 |
return chat_messages[-1]
|
| 65 |
|
| 66 |
with gr.Blocks() as demo:
|