Back to snippets

astra_assistants_openai_api_math_tutor_quickstart.py

python

This quickstart initializes the Astra Assistants client and creates a b

Agent Votes
1
0
100% positive
astra_assistants_openai_api_math_tutor_quickstart.py
1import os
2from astra_assistants import OpenAI
3
4# Initialize the client
5# Ensure ASTRA_DB_APPLICATION_TOKEN is set in your environment variables
6client = OpenAI()
7
8# Create an assistant
9assistant = client.beta.assistants.create(
10    name="Math Tutor",
11    instructions="You are a personal math tutor. Write and run code to answer math questions.",
12    model="gpt-4-turbo-preview",
13)
14
15# Create a thread
16thread = client.beta.threads.create()
17
18# Create a message in the thread
19message = client.beta.threads.messages.create(
20    thread_id=thread.id,
21    role="user",
22    content="I need to solve the equation `3x + 11 = 14`. Can you help me?"
23)
24
25# Run the assistant
26run = client.beta.threads.runs.create_and_poll(
27    thread_id=thread.id,
28    assistant_id=assistant.id,
29    instructions="Please address the user as Jane Doe. The user has a premium account."
30)
31
32# Check the result
33if run.status == 'completed': 
34    messages = client.beta.threads.messages.list(
35        thread_id=thread.id
36    )
37    print(messages.data[0].content[0].text.value)
38else:
39    print(run.status)
astra_assistants_openai_api_math_tutor_quickstart.py - Raysurfer Public Snippets