OpenAI Azure: Chat Completion API works fine, Assistant API does not

In the example below, the first part, which uses the completion API succeeds. The second part, which attempts to use the assistant API, with the same endpoint, API key and deployment name, throws a “resource not found” exception. What am I doing wrong here? How do I use the Assistant API with OpenAI Azure?

import os

import dotenv

from openai import AzureOpenAI

dotenv.load_dotenv()

client = AzureOpenAI(

api_key=os.getenv("AZURE_OPENAI_API_KEY"),  

api_version="2024-02-01",#also tried with 2024-02-15-preview and 2024.05.01-preview

azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT"),

)

deployment_name=os.getenv(“AZURE_DEPLOYMENT_NAME”)

#completion API - succeeds

response = client.chat.completions.create(

        model=deployment_name,

        messages = [{"role":"system", "content":'Write a tagline for an ice cream shop. '}])

print(response.choices[0].message.content)

assistant API - errors out with “resource not found”

assistant = client.beta.assistants.create(

            name = "Test OpenAI Azure Assistant",

            instructions="You are a helpful assistant.",

            model=deployment_name

        ) 

creating a thread also fails with ‘resource not found’

thread = client.beta.threads.create()

Assistants are their own deployments in azure - you need to create an assistant deployment in AI studio (or maybe the CLI) before you can use it…

Are you saying that create assistant API is not supported? Why would they provide an example then? How to create Assistants with Azure OpenAI Service - Azure OpenAI | Microsoft Learn

Also that would not explain the error when attempting to create a thread:
thread = client.beta.threads.create()

No dude, it’s kinda tricky in terms of terminology I guess:

you need to first create an assistants deployment on azure - that is like the runtime in which your assistants will run

once you created that assistants deployment, you should be able to deploy/ create assistants within that deployment. (not instances of assistants! assistants.)

then within that assistants deployment, you have created an assistant, whithin which you can then create a thread, to which you can then add a message…

I think you’re skipping a couple of initial steps, so it’s gonna give you the not found errors.

Assistants on azure have always been a bit iffy… I’d recommend trying to get it to work on OAI first if it’s your first time, and then move to azure…

Dude,

Here is a code snippet from the URL you posted. Please study it carefully before responding.

import os
import json
from openai import AzureOpenAI

client = AzureOpenAI(
api_key=os.getenv(“AZURE_OPENAI_API_KEY”),
api_version=“2024-05-01-preview”,
azure_endpoint = os.getenv(“AZURE_OPENAI_ENDPOINT”)
)

Create an assistant

assistant = client.beta.assistants.create(
name=“Data Visualization”,
instructions=f"You are a helpful AI assistant who makes interesting visualizations based on data."
f"You have access to a sandboxed environment for writing and testing code."
f"When you are asked to create a visualization you should follow these steps:"
f"1. Write the code."
f"2. Anytime you write new code display a preview of the code to show your work."
f"3. Run the code to confirm that it runs."
f"4. If the code is successful display the visualization."
f"5. If the code is unsuccessful display the error message and try to revise the code and rerun going through the steps from above again.",
tools=[{“type”: “code_interpreter”}],
model=“gpt-4-1106-preview” #You must replace this value with the deployment name for your model.
)

you’re right, I was in the wrong resource. :person_shrugging: none of what I said is relevant.

I guess the following could be the reason I’m getting this error. When I go to Azure OpenAI Studio - Microsoft Azure and select “Assistants” I get:

And I’m South-Central US. If so, then the error message “resource not found” or “invalid URL” is very unhelpful.

1 Like