I created and configured an AI assistant (including uploading a number of custom files) via the playground and have the unique id number corresponding to the assistant.
Is it possible with the OpenAI Python API library to “connect” to this particular assistant so I can send prompts and receive responses?
I would look at this API reference for help. Just take that ID number you got from the playground and place it in the model parameter as specified in these docs, and you should be good to go.
Took me several hours to get it just right. Here’s the correct solution:
import openai
# Initialize OpenAI client
client = openai.OpenAI()
# Vector store and assistant IDs
vector_store_id = "vs_..."
assistant_id = "asst_..."
# Step 1: Create a thread and attach the vector store
thread = client.beta.threads.create(
tool_resources={
"file_search": {
"vector_store_ids": [vector_store_id], # Attach your vector store ID
},
}
)
# Step 2: Add the prompt from user input
user_prompt = input("Enter your prompt: ") # Get the prompt from the user
message = client.beta.threads.messages.create(
thread_id=thread.id,
role="user",
content=user_prompt # Use the user input as the content
)
# Step 3: Run the assistant with file_search
run = client.beta.threads.runs.create_and_poll(
thread_id=thread.id,
assistant_id=assistant_id,
instructions="You are a warm, knowledgeable assistant with in-depth expertise about the University. You offer clear advice, guidance, and step-by-step instructions for anyone seeking information about University’s operations, procedures, and unique campus culture. Use the file_search tool to find information from the attached vector store.",
tools=[
{"type": "file_search"} # Specify the file_search tool
]
)
# Step 4: Retrieve and print messages if the run completes
if run.status == "completed":
print("Run completed successfully.")
messages = client.beta.threads.messages.list(thread_id=thread.id)
print("Messages: ")
for message in messages:
if message.content[0].type == "text":
print({"role": message.role, "message": message.content[0].text.value})