How to use Azure AI Foundry OpenAI Deployments with the OpenAI SDK
Azure OpenAI Deployments are a way to deploy and manage your OpenAI models in Azure. However examples in the dashboard and documentation use the
AzureOpenAI
sdk. This is fine if you plan to stay within Azure but can be a pain if you plan to switch between other OpenAI compatible models.
Most of the information you need is available in the code example from the playground. To access it select your model from the Chat Playground
and select View Code
. Switching to curl mode should give you an example that looks like this.
payload="{\"messages\":[],\"temperature\":0.7,\"top_p\":0.95,\"max_tokens\":800}"
curl "https://<resource_id>.openai.azure.com/openai/deployments/<deployment_name>/chat/completions?api-version=<api_version>" \
-H "Content-Type: application/json" \
-H "api-key: YOUR_API_KEY" \
-d "$payload"
Porting this to the OpenAI SDK is pretty straight forward. I'm using the javascript SDK in this example but the parameters are similar in other languages.
const open_ai = new OpenAI({
apiKey: "",
baseURL: "https://<resource_id>.openai.azure.com/openai/deployments/<deployment_name>/",
defaultQuery: { 'api-version': "<api_version>" },
defaultHeaders: { 'api-key': YOUR_API_KEY },
});
It's worth noting that the apiKey
parameter passes the API key in requests using the authorization: Bearer <api_key>
header. This doesn't work with Azure which wants the key in a separate api-key
header. However the SDK requires a value to be set so we can set it to an empty string.
Making requests can be done as usual.
const res = await open_ai.chat.completions.create({
messages: [{ role: 'user', content: 'Hello, world!' }],
});
Since deployments typically contain a single model, the model name is not required.