Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,10 +8,9 @@
|
|
| 8 |
|
| 9 |
import gradio as gr
|
| 10 |
|
| 11 |
-
import
|
| 12 |
|
| 13 |
from assistants import (
|
| 14 |
-
openai_client,
|
| 15 |
assistant,
|
| 16 |
thread,
|
| 17 |
create_assistant,
|
|
@@ -21,7 +20,6 @@ from assistants import (
|
|
| 21 |
create_run,
|
| 22 |
wait_on_run,
|
| 23 |
get_run_steps,
|
| 24 |
-
execute_tool_calls,
|
| 25 |
get_messages,
|
| 26 |
recurse_execute_tool_calls,
|
| 27 |
extract_content_values,
|
|
@@ -39,80 +37,24 @@ def chat(message, history):
|
|
| 39 |
# see https://platform.openai.com/playground/assistants.
|
| 40 |
# On subsequent runs, load assistant.
|
| 41 |
if assistant == None:
|
| 42 |
-
#assistant = create_assistant(
|
| 43 |
-
assistant = load_assistant(
|
| 44 |
|
| 45 |
# TODO: Use Gradio session to support multiple users
|
| 46 |
if thread == None or len(history) == 0:
|
| 47 |
-
thread = create_thread(
|
| 48 |
|
| 49 |
-
create_message(
|
| 50 |
|
| 51 |
-
run = create_run(
|
| 52 |
|
| 53 |
-
run = wait_on_run(
|
| 54 |
-
|
|
|
|
| 55 |
|
| 56 |
-
### TODO
|
| 57 |
recurse_execute_tool_calls(thread, run, run_steps, 0)
|
| 58 |
-
"""
|
| 59 |
-
tool_call_ids, tool_call_results = execute_tool_calls(run_steps)
|
| 60 |
-
|
| 61 |
-
if len(tool_call_ids) > 0:
|
| 62 |
-
# https://platform.openai.com/docs/api-reference/runs/submitToolOutputs
|
| 63 |
-
tool_output = {}
|
| 64 |
-
|
| 65 |
-
try:
|
| 66 |
-
tool_output = {
|
| 67 |
-
"tool_call_id": tool_call_ids[0],
|
| 68 |
-
"output": tool_call_results[0].to_json()
|
| 69 |
-
}
|
| 70 |
-
except AttributeError:
|
| 71 |
-
tool_output = {
|
| 72 |
-
"tool_call_id": tool_call_ids[0],
|
| 73 |
-
"output": tool_call_results[0]
|
| 74 |
-
}
|
| 75 |
-
|
| 76 |
-
run = openai_client.beta.threads.runs.submit_tool_outputs(
|
| 77 |
-
thread_id=thread.id,
|
| 78 |
-
run_id=run.id,
|
| 79 |
-
tool_outputs=[tool_output]
|
| 80 |
-
)
|
| 81 |
-
|
| 82 |
-
run = wait_on_run(openai_client, thread, run)
|
| 83 |
-
run_steps = get_run_steps(openai_client, thread, run)
|
| 84 |
-
"""
|
| 85 |
-
###
|
| 86 |
-
"""
|
| 87 |
-
tool_call_ids, tool_call_results = execute_tool_calls(run_steps)
|
| 88 |
-
|
| 89 |
-
if len(tool_call_ids) > 1:
|
| 90 |
-
# https://platform.openai.com/docs/api-reference/runs/submitToolOutputs
|
| 91 |
-
tool_output = {}
|
| 92 |
-
|
| 93 |
-
try:
|
| 94 |
-
tool_output = {
|
| 95 |
-
"tool_call_id": tool_call_ids[1],
|
| 96 |
-
"output": tool_call_results[1].to_json()
|
| 97 |
-
}
|
| 98 |
-
except AttributeError:
|
| 99 |
-
tool_output = {
|
| 100 |
-
"tool_call_id": tool_call_ids[1],
|
| 101 |
-
"output": tool_call_results[1]
|
| 102 |
-
}
|
| 103 |
-
|
| 104 |
-
run = openai_client.beta.threads.runs.submit_tool_outputs(
|
| 105 |
-
thread_id=thread.id,
|
| 106 |
-
run_id=run.id,
|
| 107 |
-
tool_outputs=[tool_output]
|
| 108 |
-
)
|
| 109 |
-
|
| 110 |
-
run = wait_on_run(openai_client, thread, run)
|
| 111 |
-
run_steps = get_run_steps(openai_client, thread, run)
|
| 112 |
-
"""
|
| 113 |
-
###
|
| 114 |
|
| 115 |
-
messages = get_messages(
|
| 116 |
|
| 117 |
text_values, image_values = extract_content_values(messages)
|
| 118 |
|
|
|
|
| 8 |
|
| 9 |
import gradio as gr
|
| 10 |
|
| 11 |
+
import os
|
| 12 |
|
| 13 |
from assistants import (
|
|
|
|
| 14 |
assistant,
|
| 15 |
thread,
|
| 16 |
create_assistant,
|
|
|
|
| 20 |
create_run,
|
| 21 |
wait_on_run,
|
| 22 |
get_run_steps,
|
|
|
|
| 23 |
get_messages,
|
| 24 |
recurse_execute_tool_calls,
|
| 25 |
extract_content_values,
|
|
|
|
| 37 |
# see https://platform.openai.com/playground/assistants.
|
| 38 |
# On subsequent runs, load assistant.
|
| 39 |
if assistant == None:
|
| 40 |
+
#assistant = create_assistant()
|
| 41 |
+
assistant = load_assistant()
|
| 42 |
|
| 43 |
# TODO: Use Gradio session to support multiple users
|
| 44 |
if thread == None or len(history) == 0:
|
| 45 |
+
thread = create_thread()
|
| 46 |
|
| 47 |
+
create_message(thread, message)
|
| 48 |
|
| 49 |
+
run = create_run(assistant, thread)
|
| 50 |
|
| 51 |
+
run = wait_on_run(thread, run)
|
| 52 |
+
|
| 53 |
+
run_steps = get_run_steps(thread, run)
|
| 54 |
|
|
|
|
| 55 |
recurse_execute_tool_calls(thread, run, run_steps, 0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
+
messages = get_messages(thread)
|
| 58 |
|
| 59 |
text_values, image_values = extract_content_values(messages)
|
| 60 |
|