Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[OpenAI] Structured output test & samples #31476

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
11 changes: 6 additions & 5 deletions common/config/rush/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion sdk/openai/openai/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@
"openai": "^4.47.2",
"playwright": "^1.45.3",
"typescript": "~5.6.2",
"vitest": "^2.0.4"
"vitest": "^2.0.4",
"zod": "^3.23.8"
},
"dependencies": {
"@azure-rest/core-client": "^2.2.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

/**
* Demonstrates how to get chat completions with structured output.
*
* @summary get chat completions with structured output.
* @azsdk-weight 100
*/

import { AzureOpenAI } from "openai";
import { DefaultAzureCredential, getBearerTokenProvider } from "@azure/identity";
import { z } from "zod";
import { zodResponseFormat } from "openai/helpers/zod";

// Set AZURE_OPENAI_ENDPOINT to the endpoint of your
// OpenAI resource. You can find this in the Azure portal.
// Load the .env file if it exists
import "dotenv/config";

export async function main() {
console.log("== Chat Completions With Structured Output Sample ==");

const scope = "https://cognitiveservices.azure.com/.default";
const azureADTokenProvider = getBearerTokenProvider(new DefaultAzureCredential(), scope);
const deployment = "gpt-4o-2024-08-06";
const apiVersion = "2024-09-01-preview";
const client = new AzureOpenAI({ azureADTokenProvider, deployment, apiVersion });

const Step = z.object({
explanation: z.string(),
output: z.string(),
});
const MathResponse = z.object({
steps: z.array(Step),
final_answer: z.string(),
});
const result = await client.beta.chat.completions.parse({
model: deployment,
messages: [
{
role: "system",
content: "You are a helpful math tutor. Only use the schema for math responses.",
},
{ role: "user", content: "solve 8x + 3 = 21" },
],
response_format: zodResponseFormat(MathResponse, "mathResponse"),
});
const message = result.choices[0]?.message;
if (message?.parsed) {
console.log(message.parsed.steps);
console.log(message.parsed.final_answer);
} else {
console.log(message.refusal);
}
}

main().catch((err) => {
console.error("The sample encountered an error:", err);
});
2 changes: 2 additions & 0 deletions sdk/openai/openai/samples/v2-beta/javascript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ These sample programs show how to use the JavaScript client libraries for Azure
| [audioTranslation.js][audiotranslation] | audio translation. |
| [batch.js][batch] | create and retrieve batch content. |
| [chatCompletions.js][chatcompletions] | get chat completions. |
| [chatCompletionsWithStructuredOutput.js][chatcompletionswithstructuredoutput] | get chat completions with structured output. |
| [codeInterpreter.js][codeinterpreter] | interpreting code. |
| [completions.js][completions] | get completions. |
| [embeddings.js][embeddings] | generates embedding vectors from a prompt using Azure OpenAI Get Embeddings. |
Expand Down Expand Up @@ -74,6 +75,7 @@ Take a look at our [API Documentation][apiref] for more information about the AP
[audiotranslation]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/openai/openai/samples/v2-beta/javascript/audioTranslation.js
[batch]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/openai/openai/samples/v2-beta/javascript/batch.js
[chatcompletions]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/openai/openai/samples/v2-beta/javascript/chatCompletions.js
[chatcompletionswithstructuredoutput]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/openai/openai/samples/v2-beta/javascript/chatCompletionsWithStructuredOutput.js
[codeinterpreter]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/openai/openai/samples/v2-beta/javascript/codeInterpreter.js
[completions]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/openai/openai/samples/v2-beta/javascript/completions.js
[embeddings]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/openai/openai/samples/v2-beta/javascript/embeddings.js
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

/**
* Demonstrates how to get chat completions with structured output.
*
* @summary get chat completions with structured output.
*/

const { AzureOpenAI } = require("openai");
const { DefaultAzureCredential, getBearerTokenProvider } = require("@azure/identity");
const { z } = require("zod");
const { zodResponseFormat } = require("openai/helpers/zod");

// Set AZURE_OPENAI_ENDPOINT to the endpoint of your
// OpenAI resource. You can find this in the Azure portal.
// Load the .env file if it exists
require("dotenv/config");

async function main() {
console.log("== Chat Completions With Structured Output Sample ==");

const scope = "https://cognitiveservices.azure.com/.default";
const azureADTokenProvider = getBearerTokenProvider(new DefaultAzureCredential(), scope);
const deployment = "gpt-4o-2024-08-06";
const apiVersion = "2024-09-01-preview";
const client = new AzureOpenAI({ azureADTokenProvider, deployment, apiVersion });

const Step = z.object({
explanation: z.string(),
output: z.string(),
});
const MathResponse = z.object({
steps: z.array(Step),
final_answer: z.string(),
});
const result = await client.beta.chat.completions.parse({
model: deployment,
messages: [
{
role: "system",
content: "You are a helpful math tutor. Only use the schema for math responses.",
},
{ role: "user", content: "solve 8x + 3 = 21" },
],
response_format: zodResponseFormat(MathResponse, "mathResponse"),
});
const message = result.choices[0]?.message;
if (message?.parsed) {
console.log(message.parsed.steps);
console.log(message.parsed.final_answer);
} else {
console.log(message.refusal);
}
}

main().catch((err) => {
console.error("The sample encountered an error:", err);
});

module.exports = { main };
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

const { AzureOpenAI } = require("openai");
const { DefaultAzureCredential, getBearerTokenProvider } = require("@azure/identity");
require("@azure/openai/types");

// Set AZURE_OPENAI_ENDPOINT to the endpoint of your
// OpenAI resource. You can find this in the Azure portal.
Expand Down
3 changes: 2 additions & 1 deletion sdk/openai/openai/samples/v2-beta/javascript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@azure/openai": "next",
"dotenv": "latest",
"openai": "^4.47.2",
"@azure/identity": "^4.2.0"
"@azure/identity": "^4.2.0",
"zod": "^3.23.8"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

const { AzureOpenAI } = require("openai");
const { DefaultAzureCredential, getBearerTokenProvider } = require("@azure/identity");
require("@azure/openai/types");

// Set AZURE_OPENAI_ENDPOINT to the endpoint of your
// OpenAI resource. You can find this in the Azure portal.
Expand Down
2 changes: 2 additions & 0 deletions sdk/openai/openai/samples/v2-beta/typescript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ These sample programs show how to use the TypeScript client libraries for Azure
| [audioTranslation.ts][audiotranslation] | audio translation. |
| [batch.ts][batch] | create and retrieve batch content. |
| [chatCompletions.ts][chatcompletions] | get chat completions. |
| [chatCompletionsWithStructuredOutput.ts][chatcompletionswithstructuredoutput] | get chat completions with structured output. |
| [codeInterpreter.ts][codeinterpreter] | interpreting code. |
| [completions.ts][completions] | get completions. |
| [embeddings.ts][embeddings] | generates embedding vectors from a prompt using Azure OpenAI Get Embeddings. |
Expand Down Expand Up @@ -86,6 +87,7 @@ Take a look at our [API Documentation][apiref] for more information about the AP
[audiotranslation]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/openai/openai/samples/v2-beta/typescript/src/audioTranslation.ts
[batch]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/openai/openai/samples/v2-beta/typescript/src/batch.ts
[chatcompletions]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/openai/openai/samples/v2-beta/typescript/src/chatCompletions.ts
[chatcompletionswithstructuredoutput]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/openai/openai/samples/v2-beta/typescript/src/chatCompletionsWithStructuredOutput.ts
[codeinterpreter]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/openai/openai/samples/v2-beta/typescript/src/codeInterpreter.ts
[completions]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/openai/openai/samples/v2-beta/typescript/src/completions.ts
[embeddings]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/openai/openai/samples/v2-beta/typescript/src/embeddings.ts
Expand Down
3 changes: 2 additions & 1 deletion sdk/openai/openai/samples/v2-beta/typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"@azure/openai": "next",
"dotenv": "latest",
"openai": "^4.47.2",
"@azure/identity": "^4.2.0"
"@azure/identity": "^4.2.0",
"zod": "^3.23.8"
},
"devDependencies": {
"@types/node": "^18.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

/**
* Demonstrates how to get chat completions with structured output.
*
* @summary get chat completions with structured output.
*/

import { AzureOpenAI } from "openai";
import { DefaultAzureCredential, getBearerTokenProvider } from "@azure/identity";
import { z } from "zod";
import { zodResponseFormat } from "openai/helpers/zod";

// Set AZURE_OPENAI_ENDPOINT to the endpoint of your
// OpenAI resource. You can find this in the Azure portal.
// Load the .env file if it exists
import "dotenv/config";

export async function main() {
console.log("== Chat Completions With Structured Output Sample ==");

const scope = "https://cognitiveservices.azure.com/.default";
const azureADTokenProvider = getBearerTokenProvider(new DefaultAzureCredential(), scope);
const deployment = "gpt-4o-2024-08-06";
const apiVersion = "2024-09-01-preview";
const client = new AzureOpenAI({ azureADTokenProvider, deployment, apiVersion });

const Step = z.object({
explanation: z.string(),
output: z.string(),
});
const MathResponse = z.object({
steps: z.array(Step),
final_answer: z.string(),
});
const result = await client.beta.chat.completions.parse({
model: deployment,
messages: [
{
role: "system",
content: "You are a helpful math tutor. Only use the schema for math responses.",
},
{ role: "user", content: "solve 8x + 3 = 21" },
],
response_format: zodResponseFormat(MathResponse, "mathResponse"),
});
const message = result.choices[0]?.message;
if (message?.parsed) {
console.log(message.parsed.steps);
console.log(message.parsed.final_answer);
} else {
console.log(message.refusal);
}
}

main().catch((err) => {
console.error("The sample encountered an error:", err);
});
Loading