Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -13,6 +13,8 @@
|
|
13 |
# https://platform.openai.com/docs/assistants/tools
|
14 |
|
15 |
import gradio as gr
|
|
|
|
|
16 |
import json, openai, os, time
|
17 |
|
18 |
from datetime import date
|
@@ -24,13 +26,22 @@ client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
|
|
24 |
assistant, thread = None, None
|
25 |
|
26 |
def today_tool(text: str) -> str:
|
27 |
-
"""Returns today's date.
|
28 |
-
|
29 |
-
|
|
|
30 |
return str(date.today())
|
31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
tools = {
|
33 |
"today_tool": today_tool,
|
|
|
34 |
}
|
35 |
|
36 |
def create_assistant(client):
|
@@ -39,12 +50,13 @@ def create_assistant(client):
|
|
39 |
instructions=(
|
40 |
"You are a Python programming language expert that "
|
41 |
"generates Pylint-compliant code and explains it. "
|
42 |
-
"
|
43 |
),
|
44 |
model="gpt-4o",
|
45 |
tools=[
|
46 |
{"type": "code_interpreter"},
|
47 |
{"type": "function", "function": function_to_schema(today_tool)},
|
|
|
48 |
],
|
49 |
)
|
50 |
|
@@ -97,7 +109,7 @@ def wait_on_run(client, thread, run):
|
|
97 |
run_id=run.id,
|
98 |
)
|
99 |
|
100 |
-
time.sleep(0.
|
101 |
|
102 |
#show_json("run", run)
|
103 |
|
@@ -206,6 +218,26 @@ def chat(message, history):
|
|
206 |
run_steps = get_run_steps(client, thread, run)
|
207 |
tool_call_id, tool_call_result = execute_tool_calls(run_steps)
|
208 |
###
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
209 |
|
210 |
messages = get_messages(client, thread)
|
211 |
|
|
|
13 |
# https://platform.openai.com/docs/assistants/tools
|
14 |
|
15 |
import gradio as gr
|
16 |
+
import yfinance as yf
|
17 |
+
|
18 |
import json, openai, os, time
|
19 |
|
20 |
from datetime import date
|
|
|
26 |
assistant, thread = None, None
|
27 |
|
28 |
def today_tool(text: str) -> str:
|
29 |
+
"""Returns today's date.
|
30 |
+
Use this function for any questions related to knowing today's date.
|
31 |
+
There is no input.
|
32 |
+
This function always returns today's date."""
|
33 |
return str(date.today())
|
34 |
|
35 |
+
def yfinance_download_tool(tickers: str[], start: date, end: date) -> str:
|
36 |
+
"""Returns stock data for given tickers from a start date to an end date.
|
37 |
+
Use this function for any questions related to getting stock data.
|
38 |
+
The input should be a string array of tickers, a start date, and an end date.
|
39 |
+
This function always returns yf.download."""
|
40 |
+
return yf.download(tickers, start=start_date, end=end_date)
|
41 |
+
|
42 |
tools = {
|
43 |
"today_tool": today_tool,
|
44 |
+
"yf_download_tool": yf_download_tool,
|
45 |
}
|
46 |
|
47 |
def create_assistant(client):
|
|
|
50 |
instructions=(
|
51 |
"You are a Python programming language expert that "
|
52 |
"generates Pylint-compliant code and explains it. "
|
53 |
+
"Execute code when explicitly asked to."
|
54 |
),
|
55 |
model="gpt-4o",
|
56 |
tools=[
|
57 |
{"type": "code_interpreter"},
|
58 |
{"type": "function", "function": function_to_schema(today_tool)},
|
59 |
+
{"type": "function", "function": function_to_schema(yf_download_tool)},
|
60 |
],
|
61 |
)
|
62 |
|
|
|
109 |
run_id=run.id,
|
110 |
)
|
111 |
|
112 |
+
time.sleep(0.5)
|
113 |
|
114 |
#show_json("run", run)
|
115 |
|
|
|
218 |
run_steps = get_run_steps(client, thread, run)
|
219 |
tool_call_id, tool_call_result = execute_tool_calls(run_steps)
|
220 |
###
|
221 |
+
if tool_call_result:
|
222 |
+
print("### tool_call_id=" + tool_call_id)
|
223 |
+
print("### tool_call_result=" + tool_call_result)
|
224 |
+
|
225 |
+
# https://platform.openai.com/docs/api-reference/runs/submitToolOutputs
|
226 |
+
run = client.beta.threads.runs.submit_tool_outputs(
|
227 |
+
thread_id=thread.id,
|
228 |
+
run_id=run.id,
|
229 |
+
tool_outputs=[
|
230 |
+
{
|
231 |
+
"tool_call_id": tool_call_id,
|
232 |
+
"output": tool_call_result
|
233 |
+
}
|
234 |
+
]
|
235 |
+
)
|
236 |
+
|
237 |
+
run = wait_on_run(client, thread, run)
|
238 |
+
run_steps = get_run_steps(client, thread, run)
|
239 |
+
tool_call_id, tool_call_result = execute_tool_calls(run_steps)
|
240 |
+
###
|
241 |
|
242 |
messages = get_messages(client, thread)
|
243 |
|