Last active
January 10, 2024 15:26
-
-
Save tgran2028/05d3de7c9bebd4c0395dd362ca502926 to your computer and use it in GitHub Desktop.
Program using OpenAI's assistant API to solve math problems.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
math_tutor_assistant.py | |
Program using OpenAI's assistant API to solve math problems. | |
@sorce: openai/openai-python (examples/assistant.py) | |
""" | |
import time | |
import openai | |
# gets API Key from environment variable OPENAI_API_KEY | |
client = openai.OpenAI() | |
assistant = client.beta.assistants.create( | |
name="Math Tutor", | |
instructions="You are a personal math tutor. Write and run code to answer math questions.", | |
tools=[{"type": "code_interpreter"}], | |
model="gpt-4-1106-preview", | |
) | |
thread = client.beta.threads.create() | |
message = client.beta.threads.messages.create( | |
thread_id=thread.id, | |
role="user", | |
content="I need to solve the equation `3x + 11 = 14`. Can you help me?", | |
) | |
run = client.beta.threads.runs.create( | |
thread_id=thread.id, | |
assistant_id=assistant.id, | |
instructions="Please address the user as Jane Doe. The user has a premium account.", | |
) | |
print("checking assistant status. ") | |
while True: | |
run = client.beta.threads.runs.retrieve(thread_id=thread.id, run_id=run.id) | |
if run.status == "completed": | |
print("done!") | |
messages = client.beta.threads.messages.list(thread_id=thread.id) | |
print("messages: ") | |
for message in messages: | |
assert message.content[0].type == "text" | |
print({"role": message.role, "message": message.content[0].text.value}) | |
client.beta.assistants.delete(assistant.id) | |
break | |
else: | |
print("in progress...") | |
time.sleep(5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Virtual Math Tutor Program
This program utilizes the OpenAI Assistant module to create a virtual math tutor
that can interpret and solve math equations.
The OpenAI client is initialized using the API key that is expected to be set in
the environment variable
OPENAI_API_KEY
.An assistant is created with the name "Math Tutor" and given specific
instructions to act as a personal math tutor that can write and run code to
answer math questions. It is equipped with a tool of type "code_interpreter" and
uses the model "gpt-4-1106-preview".
A thread is created using the OpenAI client. Threads are a way to manage and
keep track of interactions with the assistant.
A message is sent to the thread with the role "user" containing the content
"I need to solve the equation
3x + 11 = 14
. Can you help me?" This simulatesa user asking the math tutor for help with solving an equation.
A run is initiated on the thread with the previously created assistant. The run
includes additional instructions to address the user as "Jane Doe" and
acknowledges that the user has a premium account.
The program enters a loop where it continuously checks the status of the run by
retrieving it using the thread and run IDs.
If the run status is "completed", the program prints "done!" and then retrieves
all messages from the thread. It iterates through these messages, asserting that
each message content type is "text", and prints out the role and message content.
If the run is still in progress, the program prints "in progress..." and waits
for 5 seconds before checking the status again.
Once the run is completed and all messages are printed, the assistant is
deleted using its ID.
The program accomplishes the creation of an interactive session with an AI-powered
assistant designed to solve math problems. It sends a math problem to the
assistant, waits for the solution, and handles the interaction until the problem
is solved, after which it cleans up by deleting the assistant. The use of threads
and runs allows for structured and potentially asynchronous communication with
the AI.