Spaces:
Runtime error
Runtime error
Sohyun Sim
commited on
Commit
β’
8617bcb
1
Parent(s):
e6b75fb
app.py revision
Browse files- app.py +40 -9
- src/functions.py +4 -17
app.py
CHANGED
@@ -5,7 +5,7 @@ import pandas as pd
|
|
5 |
import gradio as gr
|
6 |
import openai
|
7 |
|
8 |
-
from src.semantle import get_guess, get_secret
|
9 |
from src.functions import get_functions
|
10 |
from src.utils import add_guess
|
11 |
|
@@ -15,7 +15,37 @@ TITLE = "γγγ¨γSemantle"
|
|
15 |
system_content = task_background+task_description
|
16 |
system_message = [{"role": "system", "content": system_content}]
|
17 |
|
18 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
openai.api_key = api_key
|
20 |
chat_messages = [{"role": "user", "content": user_input}]
|
21 |
response = openai.ChatCompletion.create(
|
@@ -30,8 +60,9 @@ def create_chat(user_input, chat_history, api_key):
|
|
30 |
# Step 3: call the function
|
31 |
# Note: the JSON response may not always be valid; be sure to handle errors
|
32 |
available_functions = {
|
33 |
-
"
|
34 |
-
"
|
|
|
35 |
}
|
36 |
function_name = response_message["function_call"]["name"]
|
37 |
function_to_call = available_functions[function_name]
|
@@ -54,11 +85,11 @@ def create_chat(user_input, chat_history, api_key):
|
|
54 |
messages=system_message+chat_history+chat_messages,
|
55 |
) # get a new response from GPT where it can se the function response
|
56 |
chat_messages.append(second_response["choices"][0]["message"].to_dict())
|
57 |
-
chat_history = chat_history
|
58 |
return chat_messages[-1]
|
59 |
|
60 |
chat_messages.append(response_message.to_dict())
|
61 |
-
chat_history = chat_history
|
62 |
return chat_messages[-1]
|
63 |
|
64 |
with gr.Blocks() as demo:
|
@@ -105,11 +136,11 @@ with gr.Blocks() as demo:
|
|
105 |
def greet():
|
106 |
return "", [("[START]", "γ²γΌγ γε§γΎγγΎγοΌε₯½γγͺθ¨θγγ²γ¨γ€γ γγγ£γ¦γΏγ¦γγ γγγ")]
|
107 |
|
108 |
-
def respond(key, user_input):
|
109 |
reply = create_chat(key, user_input)
|
110 |
-
|
111 |
time.sleep(2)
|
112 |
-
return "",
|
113 |
|
114 |
def update_guesses(cur, i, guessed_words, guesses_df):
|
115 |
if cur[0] not in guessed_words:
|
|
|
5 |
import gradio as gr
|
6 |
import openai
|
7 |
|
8 |
+
from src.semantle import get_guess, get_secret, get_puzzle_num
|
9 |
from src.functions import get_functions
|
10 |
from src.utils import add_guess
|
11 |
|
|
|
15 |
system_content = task_background+task_description
|
16 |
system_message = [{"role": "system", "content": system_content}]
|
17 |
|
18 |
+
def _execute_function(function_call, chat_messages):
|
19 |
+
# Step 3: call the function
|
20 |
+
# Note: the JSON response may not always be valid; be sure to handle errors
|
21 |
+
available_functions = {
|
22 |
+
"guess_word": get_guess,
|
23 |
+
"lookup_answer": get_secret,
|
24 |
+
"retrive_puzzle": get_puzzle_num
|
25 |
+
}
|
26 |
+
function_name = function_call["name"]
|
27 |
+
function_to_call = available_functions[function_name]
|
28 |
+
function_args = json.loads(function_call["arguments"])
|
29 |
+
function_response = function_to_call(
|
30 |
+
**function_args
|
31 |
+
)
|
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 |
+
second_response = openai.ChatCompletion.create(
|
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(second_response["choices"][0]["message"].to_dict())
|
46 |
+
return chat_messages
|
47 |
+
|
48 |
+
def create_chat(api_key, user_input):
|
49 |
openai.api_key = api_key
|
50 |
chat_messages = [{"role": "user", "content": user_input}]
|
51 |
response = openai.ChatCompletion.create(
|
|
|
60 |
# Step 3: call the function
|
61 |
# Note: the JSON response may not always be valid; be sure to handle errors
|
62 |
available_functions = {
|
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]
|
|
|
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:
|
|
|
136 |
def greet():
|
137 |
return "", [("[START]", "γ²γΌγ γε§γΎγγΎγοΌε₯½γγͺθ¨θγγ²γ¨γ€γ γγγ£γ¦γΏγ¦γγ γγγ")]
|
138 |
|
139 |
+
def respond(key, user_input, chat_messages):
|
140 |
reply = create_chat(key, user_input)
|
141 |
+
chat_messages.append((user_input, reply["content"]))
|
142 |
time.sleep(2)
|
143 |
+
return "", chat_messages
|
144 |
|
145 |
def update_guesses(cur, i, guessed_words, guesses_df):
|
146 |
if cur[0] not in guessed_words:
|
src/functions.py
CHANGED
@@ -15,20 +15,7 @@ guess_word = {"name": "guess_word",
|
|
15 |
"required": ["word", "puzzle_num"]
|
16 |
}}
|
17 |
|
18 |
-
|
19 |
-
"description": "Use this function to retrieve information for hint.",
|
20 |
-
"parameters": {
|
21 |
-
"type": "object",
|
22 |
-
"properties": {
|
23 |
-
"puzzle_num": {
|
24 |
-
"type": "interger",
|
25 |
-
"description": "An index for today's puzzle set."
|
26 |
-
}
|
27 |
-
},
|
28 |
-
"required": ["puzzle_num"]
|
29 |
-
}}
|
30 |
-
|
31 |
-
get_secret = {"name": "get_secret",
|
32 |
"description": "You can check what the correct answer of today's puzzle is by this function.",
|
33 |
"parameters": {
|
34 |
"type": "object",
|
@@ -41,8 +28,8 @@ get_secret = {"name": "get_secret",
|
|
41 |
"required": ["puzzle_num"]
|
42 |
}}
|
43 |
|
44 |
-
|
45 |
-
"description": "Use this function to
|
46 |
"parameters": {
|
47 |
"type": "object",
|
48 |
"properties": {}
|
@@ -68,5 +55,5 @@ update_history = {"name": "update_history",
|
|
68 |
|
69 |
|
70 |
def get_functions():
|
71 |
-
functions = [guess_word]
|
72 |
return functions
|
|
|
15 |
"required": ["word", "puzzle_num"]
|
16 |
}}
|
17 |
|
18 |
+
lookup_answer = {"name": "lookup_answer",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
"description": "You can check what the correct answer of today's puzzle is by this function.",
|
20 |
"parameters": {
|
21 |
"type": "object",
|
|
|
28 |
"required": ["puzzle_num"]
|
29 |
}}
|
30 |
|
31 |
+
retrieve_puzzle_num = {"name": "retrieve_puzzle_num",
|
32 |
+
"description": "Use this function to retrieve today's puzzle number.",
|
33 |
"parameters": {
|
34 |
"type": "object",
|
35 |
"properties": {}
|
|
|
55 |
|
56 |
|
57 |
def get_functions():
|
58 |
+
functions = [guess_word, lookup_answer]
|
59 |
return functions
|