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.
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:
One thing to note is that I used both
await client.getChatCompletions
and awaitclient.streamChatCompletions
, and neither worked. The behaviour I see is an infinite loop of something, as the AI response is blank and constantly refreshing when usingclient.streamChatCompletions
, and the client completely shutting off when usingawait 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