Migrating from OpenAI to Azure Open AI (Javascript)

Suresh 0 Reputation points
2024-04-09T16:24:37.1533333+00:00

Hi, 

 

I've been using OpenAI with javascript for an app for a few months now without any issues. My use case involves using the function calling parameters to specify a schema for a specific JSON output. I've now migrated to using an Azure OpenAI Endpoint with the v1.0.0-beta.12 version of the javascript SDK, and I can't seem to figure out the correct way to specify my desired schema for my json output. I've tried using functions/function_calls, also tools/tool_calls. I also noticed that for some versions of the api it expects a parameter named responseFormat. All have not worked. Any help would be appreciated. 

 

Here is a snippet of my code using openAI: 

var raw = JSON.stringify({
model: 'gpt-4',
messages: [
			{
				role: 'system',
				content: advanced_context,
			},
			{
				role: 'user',
				content: user_text,
			},
		],
functions: [
		{
			name: 'food_items',
			parameters: schema,
		},
	],
function_call: {
	name: 'food_items',
	},
temperature: 0,
});

var requestOptions = {
	method: 'POST',
	headers: myHeaders,
	body: raw,
	redirect: 'follow',
};

var response = await fetch(
'https://api.openai.com/v1/chat/completions',
requestOptions,
);
 

And this is with Azure OpenAI: 

const completion = await client.getChatCompletions(deploymentId, [
{
role: "system",
content: advanced_context
},
{
role: "user",
content: input
},
],
response_format={ "type": "json_object" },
temperature=0,
responseFormat= schema,
functions=[
{
"name": "food_items",
"parameters": schema,
}
],
functionCall={
"name": "food_items"
},
tools=[
{
"name": "food_items",
"parameters": schema,
}
],
toolChoice={
"name": "food_items"
},
);

 

JavaScript API
JavaScript API
An Office service that supports add-ins to interact with objects in Office client applications.
871 questions
Azure OpenAI Service
Azure OpenAI Service
An Azure service that provides access to OpenAI’s GPT-3 models with enterprise capabilities.
2,188 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Q&A Assist
    2024-04-09T16:24:37.1733333+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    It seems that you are having trouble specifying the schema for your JSON output when using Azure OpenAI. According to the documentation, the response_format parameter has been deprecated in favor of the tools parameter. You can specify your function and its parameters using the tools parameter, and then call the function using the tool_choice parameter. Here's an example of how you can modify your code to use the tools and tool_choice parameters:

    const completion = await client.getChatCompletions(deploymentId, [
        {
            role: "system",
            content: advanced_context
        },
        {
            role: "user",
            content: input
        },
    ],
    {
        temperature: 0,
        tools: [
            {
                name: "food_items",
                parameters: schema,
            }
        ],
        tool_choice: {
            name: "food_items"
        }
    });
    

    You can define your function and its parameters in the tools array, and then call the function using the tool_choice parameter. Note that the tools and tool_choice parameters are passed as an object, not as separate parameters.

    I hope this helps! Let me know if you have any further questions.


    References:

    0 comments No comments