Unable to use Azure OpenAI Assistant API

Romanowski, Tomasz 50 Reputation points
2024-06-10T21:52:41.6733333+00:00

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",

    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

            ) 
```Also tried with different Azure API versions, 2024-02-15-preview or 2024-05-01-preview. With those versions, the Chat Completion API still run just fine, while the Assistant API produces a a different error: 

invalid URL (POST /v1/assistants)'

Azure OpenAI Service
Azure OpenAI Service
An Azure service that provides access to OpenAI’s GPT-3 models with enterprise capabilities.
2,658 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Vlad Costa 935 Reputation points
    2024-06-10T23:28:38.2366667+00:00

    HJi @Romanowski, Tomasz

    In your code, the client.beta.assistants.create() method seems to be trying to create a new assistant with each API call, which might not be the correct API usage. Instead, you should create the assistant on the OpenAI platform and then use the API to interact with the already-created assistant.

    References:
    https://learn.microsoft.com/en-us/azure/ai-services/openai/concepts/assistants
    https://learn.microsoft.com/en-us/azure/ai-services/openai/assistants-reference

    1. Interacting with the Assistant: Once the assistant is created, you can use the API to send messages to the assistant and receive responses. This is typically done using the client.chat.completions.create() method in your code.
    2. Assistant Tools: Assistants can access multiple tools in parallel. These tools can be Azure OpenAI-hosted tools like code interpreter and file search or tools you build, host, and access through function calls.
    3. Persistent Threads: The Assistants API supports persistent, automatically managed threads. As a developer, you no longer need to develop conversation state management systems and work around a model’s context window constraints.

    If you find this response helpful and it resolves your issue, please consider marking it as “Accepted” or giving it an upvote. This will help others in the community find the solution more easily.


  2. Romanowski, Tomasz 50 Reputation points
    2024-06-11T21:39:06.0966667+00:00

    This is probably the reason for those otherwise cryptic and unhelpful error messages:https://learn.microsoft.com/en-us/answers/questions/1693862/unable-to-use-azure-openai-assistant-api