Επεξεργασία

Κοινοποίηση μέσω


Import an Azure OpenAI API

APPLIES TO: All API Management tiers

You can import AI model endpoints deployed in Azure OpenAI in Foundry Models to your API Management instance as a REST API. Use AI gateway policies and other capabilities in API Management to simplify integration, improve observability, and enhance control over the model endpoints.

This article shows two options to import an Azure OpenAI API into an Azure API Management instance as a REST API:

Learn more about managing LLM APIs in API Management:

Prerequisites

  • An existing API Management instance. Create one if you haven't already.

  • (For import from Microsoft Foundry) A Microsoft Foundry project with an Azure OpenAI model deployed. For more information about model deployment in Azure OpenAI, see the resource deployment guide.

    Make a note of the ID (name) of the deployment. You need it when you test the imported API in API Management.

  • Permissions to grant access to the Azure OpenAI resource from the API Management instance.

Option 1. Import OpenAI API from Microsoft Foundry

You can import an Azure OpenAI model deployment directly from Microsoft Foundry to API Management. For details, see Import a Microsoft Foundry API.

When you import the API:

  • Specify the Microsoft Foundry service that hosts the Azure OpenAI model deployment.
  • Specify the Azure OpenAI client compatibility option. This option configures the API Management API with a /openai endpoint.

Option 2. Add an OpenAPI specification to API Management

Alternatively, manually download the OpenAPI specification for the Azure OpenAI REST API and add it to API Management as an OpenAPI API.

Download the OpenAPI specification

Download the OpenAPI specification for the Azure OpenAI REST API, such as the 2024-10-21 GA version.

  1. In a text editor, open the specification file that you downloaded.

  2. In the servers element in the specification, substitute the name of your Azure OpenAI endpoint in the placeholder values of url and default endpoint. For example, if your Azure OpenAI endpoint is contoso.openai.azure.com, update the servers element with the following values:

    • url: https://contoso.openai.azure.com/openai
    • default endpoint: contoso.openai.azure.com
    [...]
    "servers": [
        {
          "url": "https://contoso.openai.azure.com/openai",
          "variables": {
            "endpoint": {
              "default": "contoso.openai.azure.com"
            }
          }
        }
      ],
    [...]
    
  3. Make a note of the value of the API version in the specification. You need it to test the API. Example: 2024-10-21.

Add OpenAPI specification to API Management

  1. In the Azure portal, go to your API Management instance.
  2. In the left menu, select APIs > + Add API.
  3. Under Define a new API, select OpenAPI. Enter a Display name and Name for the API.
  4. Enter an API URL suffix ending in /openai to access the Azure OpenAI API endpoints in your API Management instance. For example: my-openai-api/openai.
  5. Select Create.

API Management imports the API and displays operations from the OpenAPI specification.

Configure authentication to Azure OpenAI API

To authenticate to the Azure OpenAI API, provide an API key or use a managed identity. If you imported the Azure OpenAI API directly from Microsoft Foundry, authentication by using the API Management instance's managed identity is automatically configured.

If you added the Azure OpenAI API from its OpenAPI specification, you need to configure authentication. For more information about configuring authentication by using API Management policies, see Authenticate and authorize to LLM APIs.

Test the Azure OpenAI API

To make sure your Azure OpenAI API works as expected, test it in the API Management test console. You need to provide a model deployment ID (name) that you configured in the Microsoft Foundry project resource and the API version to test the API.

  1. Select the API you created in the previous step.

  2. Select the Test tab.

  3. Select an operation that's compatible with the model you deployed in the Azure OpenAI resource. The page displays fields for parameters and headers.

  4. In Template parameters, enter the following values:

    • deployment-id - the ID of an Azure OpenAI model deployment in Microsoft Foundry
    • api-version - a valid Azure OpenAI API version, such as the API version you selected when you imported the API. Screenshot of testing an Azure OpenAI API in the portal.
  5. Enter other parameters and headers as needed. Depending on the operation and model, you might need to configure or update a Request body. For example, here's a basic request body for a chat completions operation:

    {
      "model": "any",
      "messages": [
        {
          "role": "user",
          "content": "Help me plan a vacation trip to Paris."
        }
      ],
      "max_tokens": 100
    }
    

    Note

    In the test console, API Management automatically adds an Ocp-Apim-Subscription-Key header and sets the subscription key for the built-in all-access subscription. This key provides access to every API in the API Management instance. To optionally display the Ocp-Apim-Subscription-Key header, select the "eye" icon next to the HTTP Request.

  6. Select Send.

    When the test succeeds, the backend responds with a successful HTTP response code and some data. The response includes token usage data to help you monitor and manage your Azure OpenAI API token consumption.

    Screenshot of token usage data in API response in the portal.