-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode_interpreter.py
132 lines (90 loc) · 3.05 KB
/
code_interpreter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# Imports
from openai import OpenAI
import dotenv
import os
from time import sleep
import json
from types import SimpleNamespace
dotenv.load_dotenv()
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
# Ask if previous run should be used.
use_run = input("Use previous run? (y/n): ")
# Yes? Load the run from the json file.
if use_run == "y":
with open("run.json") as json_file:
data = json.load(json_file)
assistant = SimpleNamespace(id=data["assistant_id"])
thread = SimpleNamespace(id=data["thread_id"])
run = SimpleNamespace(id=data["run_id"])
# No? Create a new assistant, thread and run.
if use_run == "n":
# create the assistant
assistant = client.beta.assistants.create(
name="Math tutor",
instructions="You are a helpfull math instructor. Write and run code to answer math questions.",
tools=[{
"type": "code_interpreter"
}],
model="gpt-3.5-turbo-1106"
)
# Create the thread
thread = client.beta.threads.create()
while True:
user_choice = input("Do you want to ask a question? (y/n): ")
if user_choice == "n":
break
# Get user question input
user_question = input("What is your math question? ")
# Add message to thread
message = client.beta.threads.messages.create(
thread_id = thread.id,
role= "user",
# content="Solve this problem: 3x + 11 = 14"
content=user_question
)
# Create the run
run = client.beta.threads.runs.create(
thread_id = thread.id,
assistant_id = assistant.id,
)
# Save the assistant, thread and run to a json file for later use.
data = {
"assistant_id": assistant.id,
"thread_id": thread.id,
"run_id": run.id
}
with open("run.json", "w") as json_file:
json.dump(data, json_file)
# Retrieve information about the run and wait for it to complete.
run = client.beta.threads.runs.retrieve(
thread_id=thread.id,
run_id=run.id
)
while run.status != "completed":
sleep(2)
run = client.beta.threads.runs.retrieve(
thread_id=thread.id,
run_id=run.id
)
if run.status == "failed":
print("Failed")
break
# Find the code interpreter step and print the output.
# obs.: Should have a better way to do this, verifying if the step is a code interpreter step. And if it has an output.
run_steps = client.beta.threads.runs.steps.list(
thread_id=thread.id,
run_id=run.id
)
interpreter_code = run_steps.data[1].step_details.tool_calls[0].code_interpreter.input
# Get the list of messages between assistant and user.
messages = client.beta.threads.messages.list(
thread_id=thread.id
)
print("\n")
# Print the messages
for message in reversed(messages.data):
print(message.role + ": "+message.content[0].text.value)
print("\n")
# Print with code from code interpreter
print("\n\n")
print("Interpreter code: " + interpreter_code)