Azure Open AI Assistant API connection error

Hi, I am attempting to create an assistant via Azure Open AI Assistant API and am getting a connection error.

      try:
          assistant = client.beta.assistants.create(
              name="InfraBot",
              instructions=instructions,
              tools=[{"type": "retrieval"}, {"type": "code_interpreter"}],
              model="gpt-4-1106-preview",
          )
          assistant_id = assistant.id

          # ai_assistants = client.beta.assistants.list(
          #     order="desc",
          #     limit="20",
          # )
      except Exception as e:
          logging.error(str(e))

The Azure Endpoint is this: AZURE_OPENAI_ENDPOINT=https://[myresource].openai.azure.com/.

I also tried AZURE_OPENAI_ENDPOINT=https://[myresource].openai.azure.com//openai/assistants?api-version=2024-02-15-preview with the same error. Screenshot attached.

The client.beta.assistants.list() function produces the same error.

1 Like

Here’s how you can consume Azure OpenAI service with the OpenAI python package.

1 Like

Particularly, you’ll need to use your model deployment name besides the different library methods.

Hmm, so I updated the code to include the azure_deployment parameter in the model and I still get the connection error. Do you know if there is something specific to Azure that needs to happen? The deployed model is exactly what’s in the code, gpt-4 1106-Preview.

client = AzureOpenAI(
            api_key=config.AZURE_OPENAI_API_KEY,
            api_version="2024-02-15-preview",
            azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT"),
            azure_deployment="gpt4test",
        )
1 Like

It is the model name that you must replace with the deployment name. It often can be the same, but you are also given an opportunity to rename it.

Then more modifications in your use of the OpenAI python library, as outlined in my link above.

from openai import AzureOpenAI
    
client = AzureOpenAI(
    api_key=os.getenv("AZURE_OPENAI_API_KEY"),  
    api_version="2024-02-15-preview",
    azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT")
    )

If this is the first of any use of OpenAI Azure services, I’d start with chat completions and see that you get a simple response there.

More, if using entra ID:

from azure.identity import DefaultAzureCredential, get_bearer_token_provider
from openai import AzureOpenAI

token_provider = get_bearer_token_provider(
    DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
)

api_version = "2023-12-01-preview"
endpoint = "https://my-resource.openai.azure.com"

client = AzureOpenAI(
    api_version=api_version,
    azure_endpoint=endpoint,
    azure_ad_token_provider=token_provider,
)
1 Like

Primero tienes que verificar la Clave de la API, que sea verificada y tenga validez, comprobar el punto inicial de conexión de la API, asegurar los parámetros, y como siempre digo utilicen la Documentación, si esta desactualizada, pregunten al area de ayuda… inspeccionen la conectividad con Internet, sea local, global, etc que no existan firewalls, cortadores que son de los mas antiguos, o tambien VPNs o bloqueadores, por el desarrollo, disponibilidad de direcciones regionales que confirme el modelo general. Tambien reúna información con descripciones… por ultimo para resolución de problemas, actualización de Software, si no tenemos eso, cambiar dispositivos físicos, bibliotecas de Clientes, aprovechar registro Azure; y verificar estado de la API ¡Saludos!
:computer::books::smiley:

1 Like

When I am trying to run Python code for Azure OpenAI API Chat Completions locally, providing all required parameters, it is giving me below error intermittently. Need your help, if you could suggest how this could be resolved.

httpcore.ConnectError: [Errno 11001] getaddrinfo failed
The above exception was the direct cause of the following exception:
httpx.ConnectError: [Errno 11001] getaddrinfo failed
The above exception was the direct cause of the following exception:
raise APIConnectionError(request=request) from err
openai.APIConnectionError: Connection error.

Hey, not sure if this will help you, but here is what worked for me for Azure Open AI Chat completions:

Screenshot from 2024-03-27 08-48-18
Screenshot from 2024-03-27 08-47-57

Thanks for sharing. We have the same code & it’s working in AWS EC2 environment, but problem lies (connection error) when we run it from local internet connection.
By the way, when you say worked , you run it in your local internet connection or any other organization internet etc.

Hey, for us it’s working on the corporate network.

Ok, not in your local network…right? we have the same problem. but not sure why?

Sorry, I wasn’t clear - the code works both locally and on the corporate network when deployed to production.