vercel / ai

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

Azure OpenAI function calling does not work #1156

Closed aalhayali closed 5 months ago

aalhayali commented 7 months ago

Description

Hi everyone,

I've been trying to use function calling and tooling and neither seem to work. I'm using the following code snippets as reference:

function calling - https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/openai/openai/samples/v1-beta/typescript/src/functions.ts

tools - https://www.npmjs.com/package/@azure/openai?activeTab=readme#use-chat-tools

My current code looks like the following:

import { OpenAIClient, AzureKeyCredential } from "@azure/openai";
import { OpenAIStream, StreamingTextResponse } from "ai";

// Create an OpenAI API client (that's edge friendly!)
const client = new OpenAIClient(
  "https://[OMITTED].openai.azure.com",
  new AzureKeyCredential(process.env.AZURE_OPENAI_API_KEY!)
);

// IMPORTANT! Set the runtime to edge
export const runtime = "edge";

const getCurrentWeather = {
  name: "get_current_weather",
  description: "Get the current weather in a given location",
  parameters: {
    type: "object",
    properties: {
      location: {
        type: "string",
        description: "The city and state, e.g. San Francisco, CA",
      },
      unit: {
        type: "string",
        enum: ["celsius", "fahrenheit"],
      },
    },
    required: ["location"],
  },
};

export async function POST(req: Request) {
  const { messages } = await req.json();
  // Ask Azure OpenAI for a streaming chat completion given the prompt
  const response = await client.getChatCompletions(
    "[OMITTED]",
    messages,
    {
      functions: [getCurrentWeather],
    }
  );

  console.log(response)

  // Convert the response into a friendly text-stream
  const stream = OpenAIStream(response);
//   console.log(stream)
  // Respond with the stream
  return new StreamingTextResponse(stream);
}

One thing to note is that I used both await client.getChatCompletions and await client.streamChatCompletions, and neither worked. The behaviour I see is an infinite loop of something, as the AI response is blank and constantly refreshing when using client.streamChatCompletions, and the client completely shutting off when using await client.getChatCompletions. May I know how I can go about fixing that? There doesn't seem to be a lot of documentation around.

Thanks!

Code example

No response

Additional context

No response

lgrammel commented 5 months ago

Superseeded by #1675