Skip to content

Instantly share code, notes, and snippets.

@tgran2028
Last active January 10, 2024 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tgran2028/05d3de7c9bebd4c0395dd362ca502926 to your computer and use it in GitHub Desktop.
Save tgran2028/05d3de7c9bebd4c0395dd362ca502926 to your computer and use it in GitHub Desktop.
Program using OpenAI's assistant API to solve math problems.
"""
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)
@tgran2028
Copy link
Author

Virtual Math Tutor Program

This program utilizes the OpenAI Assistant module to create a virtual math tutor
that can interpret and solve math equations.

  1. The OpenAI client is initialized using the API key that is expected to be set in
    the environment variable OPENAI_API_KEY.

  2. 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".

  3. A thread is created using the OpenAI client. Threads are a way to manage and
    keep track of interactions with the assistant.

  4. 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 simulates
    a user asking the math tutor for help with solving an equation.

  5. 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.

  6. The program enters a loop where it continuously checks the status of the run by
    retrieving it using the thread and run IDs.

  7. 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.

  8. If the run is still in progress, the program prints "in progress..." and waits
    for 5 seconds before checking the status again.

  9. 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment