vercel / ai

Build AI-powered applications with React, Svelte, Vue, and Solid
https://sdk.vercel.ai/docs
Other
9.59k stars 1.41k forks source link

Azure OpenAI support #1643

Closed aalhayali closed 4 months ago

aalhayali commented 4 months ago

Description

Using Azure OpenAI API via the createOpenAI method, and following the tutorial published by Vercel: https://sdk.vercel.ai/providers/ai-sdk-providers/openai

I tried different API versions, based on this doc: https://learn.microsoft.com/en-us/azure/ai-services/openai/reference along with different Auzre OpenAI models (gpt-35-turbo and gpt-4).

Code example

'use server';

import { createOpenAI } from "@ai-sdk/openai";
import { generateObject } from "ai";
import { generateText } from "ai";
import { z } from "zod";

const openai = createOpenAI({
  apiKey: process.env.AZURE_OPENAI_API_KEY,
  baseURL:
    "https://{resource}.openai.azure.com/openai/deployments/{deployment}/chat/completions?api-version=2024-02-01",
});

export async function getNotifications(input: string) {
  "use server";

  const { object: notifications } = await generateObject({
    model: openai("gpt-4"),
    system: "You generate three notifications for a messages app.",
    prompt: input,
    schema: z.object({
      notifications: z.array(
        z.object({
          name: z.string().describe("Name of a fictional person."),
          message: z.string().describe("Do not use emojis or links."),
          minutesAgo: z.number(),
        })
      ),
    }),
  });

  return { notifications };
}

Additional context

The above code generates responseBody: '{"error":{"code":"404","message": "Resource not found"}}' error message.

lgrammel commented 4 months ago

Azure OpenAI is not supported as a provider for AI SDK Core yet.

lgrammel commented 4 months ago

Duplicates #1675