From 703cd1f904f2713ab7d5b5df6451f1e5daa0693d Mon Sep 17 00:00:00 2001 From: Deyaaeldeen Almahallawi Date: Fri, 25 Aug 2023 11:12:38 -0700 Subject: [PATCH] [OpenAI] Prepare beta.5 release (#26941) --- sdk/openai/openai/CHANGELOG.md | 8 +-- sdk/openai/openai/assets.json | 2 +- .../samples/v1-beta/javascript/README.md | 28 +++++--- .../v1-beta/javascript/bringYourOwnData.js | 70 +++++++++++++++++++ .../v1-beta/javascript/completionsRest.js | 45 ++++++++++++ .../samples/v1-beta/javascript/functions.js | 58 +++++++++++++++ .../samples/v1-beta/javascript/getImages.js | 41 +++++++++++ .../v1-beta/javascript/listChatCompletions.js | 2 +- .../v1-beta/javascript/listCompletions.js | 2 +- .../samples/v1-beta/javascript/package.json | 3 +- .../samples/v1-beta/typescript/README.md | 28 +++++--- .../samples/v1-beta/typescript/package.json | 3 +- .../typescript/src/bringYourOwnData.ts | 69 ++++++++++++++++++ .../v1-beta/typescript/src/completionsRest.ts | 43 ++++++++++++ .../v1-beta/typescript/src/functions.ts | 57 +++++++++++++++ .../v1-beta/typescript/src/getImages.ts | 40 +++++++++++ .../typescript/src/listChatCompletions.ts | 2 +- .../v1-beta/typescript/src/listCompletions.ts | 2 +- sdk/openai/openai/test/public/openai.spec.ts | 2 +- 19 files changed, 471 insertions(+), 34 deletions(-) create mode 100644 sdk/openai/openai/samples/v1-beta/javascript/bringYourOwnData.js create mode 100644 sdk/openai/openai/samples/v1-beta/javascript/completionsRest.js create mode 100644 sdk/openai/openai/samples/v1-beta/javascript/functions.js create mode 100644 sdk/openai/openai/samples/v1-beta/javascript/getImages.js create mode 100644 sdk/openai/openai/samples/v1-beta/typescript/src/bringYourOwnData.ts create mode 100644 sdk/openai/openai/samples/v1-beta/typescript/src/completionsRest.ts create mode 100644 sdk/openai/openai/samples/v1-beta/typescript/src/functions.ts create mode 100644 sdk/openai/openai/samples/v1-beta/typescript/src/getImages.ts diff --git a/sdk/openai/openai/CHANGELOG.md b/sdk/openai/openai/CHANGELOG.md index 016bcc36f1a5..c7b35ba964da 100644 --- a/sdk/openai/openai/CHANGELOG.md +++ b/sdk/openai/openai/CHANGELOG.md @@ -1,6 +1,6 @@ # Release History -## 1.0.0-beta.5 (Unreleased) +## 1.0.0-beta.5 (2023-08-25) ### Features Added @@ -14,11 +14,7 @@ ### Breaking Changes - Remove `beginAzureBatchImageGeneration` and `getAzureBatchImageGenerationOperationStatus` methods. -- `getImages` has been updated to return the image URLs/paylods directly, rather than requiring the user to call `getAzureBatchImageGenerationOperationStatus` to retrieve them. - -### Bugs Fixed - -### Other Changes +- `getImages` has been updated to return the image URLs/payloads directly, rather than requiring the user to call `getAzureBatchImageGenerationOperationStatus` to retrieve them. ## 1.0.0-beta.4 (2023-08-09) diff --git a/sdk/openai/openai/assets.json b/sdk/openai/openai/assets.json index 3326db2713d7..194115fbcfbb 100644 --- a/sdk/openai/openai/assets.json +++ b/sdk/openai/openai/assets.json @@ -2,5 +2,5 @@ "AssetsRepo": "Azure/azure-sdk-assets", "AssetsRepoPrefixPath": "js", "TagPrefix": "js/openai/openai", - "Tag": "js/openai/openai_ffe29131dc" + "Tag": "js/openai/openai_d5f951ca47" } diff --git a/sdk/openai/openai/samples/v1-beta/javascript/README.md b/sdk/openai/openai/samples/v1-beta/javascript/README.md index 06b4c038649c..0387387323de 100644 --- a/sdk/openai/openai/samples/v1-beta/javascript/README.md +++ b/sdk/openai/openai/samples/v1-beta/javascript/README.md @@ -13,13 +13,17 @@ urlFragment: openai-javascript-beta These sample programs show how to use the JavaScript client libraries for Azure OpenAI in some common scenarios. -| **File Name** | **Description** | -| --------------------------------------------- | ------------------------------------- | -| [chatCompletions.js][chatcompletions] | get chat completions. | -| [completions.js][completions] | get completions. | -| [listChatCompletions.js][listchatcompletions] | list chat completions. | -| [listCompletions.js][listcompletions] | list completions. | -| [openAi.js][openai] | get completions using the OpenAI API. | +| **File Name** | **Description** | +| --------------------------------------------- | ------------------------------------------------------------------------ | +| [bringYourOwnData.js][bringyourowndata] | chat completions with your own data. | +| [chatCompletions.js][chatcompletions] | get chat completions. | +| [completions.js][completions] | get completions. | +| [completionsRest.js][completionsrest] | get completions. | +| [functions.js][functions] | get chat completions with functions. | +| [getImages.js][getimages] | generates images from prompts using Azure OpenAI Batch Image Generation. | +| [listChatCompletions.js][listchatcompletions] | list chat completions. | +| [listCompletions.js][listcompletions] | list completions. | +| [openAi.js][openai] | get completions using the OpenAI API. | ## Prerequisites @@ -48,25 +52,29 @@ npm install 3. Run whichever samples you like (note that some samples may require additional setup, see the table above): ```bash -node chatCompletions.js +node bringYourOwnData.js ``` Alternatively, run a single sample with the correct environment variables set (setting up the `.env` file is not required if you do this), for example (cross-platform): ```bash -npx cross-env ENDPOINT="" AZURE_API_KEY="" node chatCompletions.js +npx cross-env ENDPOINT="" AZURE_API_KEY="" AZURE_SEARCH_ENDPOINT="" AZURE_SEARCH_KEY="" AZURE_SEARCH_INDEX="" node bringYourOwnData.js ``` ## Next Steps Take a look at our [API Documentation][apiref] for more information about the APIs that are available in the clients. +[bringyourowndata]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/openai/openai/samples/v1-beta/javascript/bringYourOwnData.js [chatcompletions]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/openai/openai/samples/v1-beta/javascript/chatCompletions.js [completions]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/openai/openai/samples/v1-beta/javascript/completions.js +[completionsrest]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/openai/openai/samples/v1-beta/javascript/completionsRest.js +[functions]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/openai/openai/samples/v1-beta/javascript/functions.js +[getimages]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/openai/openai/samples/v1-beta/javascript/getImages.js [listchatcompletions]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/openai/openai/samples/v1-beta/javascript/listChatCompletions.js [listcompletions]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/openai/openai/samples/v1-beta/javascript/listCompletions.js [openai]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/openai/openai/samples/v1-beta/javascript/openAi.js -[apiref]: https://learn.microsoft.com/azure/cognitive-services/openai/ +[apiref]: https://docs.microsoft.com/javascript/api/@azure/openai [freesub]: https://azure.microsoft.com/free/ [createinstance_azurecognitiveservicesinstance]: https://learn.microsoft.com/azure/cognitive-services/openai/how-to/create-resource [package]: https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/openai/openai/README.md diff --git a/sdk/openai/openai/samples/v1-beta/javascript/bringYourOwnData.js b/sdk/openai/openai/samples/v1-beta/javascript/bringYourOwnData.js new file mode 100644 index 000000000000..489ffad568dd --- /dev/null +++ b/sdk/openai/openai/samples/v1-beta/javascript/bringYourOwnData.js @@ -0,0 +1,70 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +/** + * Demonstrates how to use Azure's Bring Your Own Data with Azure OpenAI Chat Completions. + * + * @summary chat completions with your own data. + */ + +const { OpenAIClient, AzureKeyCredential } = require("@azure/openai"); + +// Load the .env file if it exists +require("dotenv").config(); + +// You will need to set these environment variables or edit the following values +// The endpoint you will use to access your Azure OpenAI instance +const endpoint = process.env["ENDPOINT"] || ""; +// Your Azure OpenAI API key +const azureApiKey = process.env["AZURE_API_KEY"] || ""; +// Your Azure Cognitive Search endpoint, admin key, and index name +const azureSearchEndpoint = process.env["AZURE_SEARCH_ENDPOINT"] || ""; +const azureSearchAdminKey = process.env["AZURE_SEARCH_KEY"] || ""; +const azureSearchIndexName = process.env["AZURE_SEARCH_INDEX"] || ""; + +const messages = [ + { + role: "user", + content: "What's the most common feedback we received from our customers about the product?", + }, +]; + +async function main() { + console.log("== Bring Your Own Data Sample =="); + + const client = new OpenAIClient(endpoint, new AzureKeyCredential(azureApiKey)); + const deploymentId = "gpt-35-turbo"; + const events = client.listChatCompletions(deploymentId, messages, { + maxTokens: 128, + /** + * The `azureExtensionOptions` property is used to configure the + * Azure-specific extensions. In this case, we are using the + * Azure Cognitive Search extension with a vector index to provide + * the model with additional context. + */ + azureExtensionOptions: { + extensions: [ + { + type: "AzureCognitiveSearch", + parameters: { + endpoint: azureSearchEndpoint, + key: azureSearchAdminKey, + indexName: azureSearchIndexName, + }, + }, + ], + }, + }); + + for await (const event of events) { + for (const choice of event.choices) { + console.log(choice.delta?.content); + } + } +} + +main().catch((err) => { + console.error("The sample encountered an error:", err); +}); + +module.exports = { main }; diff --git a/sdk/openai/openai/samples/v1-beta/javascript/completionsRest.js b/sdk/openai/openai/samples/v1-beta/javascript/completionsRest.js new file mode 100644 index 000000000000..cbaed9ba7901 --- /dev/null +++ b/sdk/openai/openai/samples/v1-beta/javascript/completionsRest.js @@ -0,0 +1,45 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +/** + * Demonstrates how to get completions for the provided prompt. + * + * @summary get completions. + */ + +const { AzureKeyCredential } = require("@azure/core-auth"); +const OpenAIClient = require("@azure/openai/rest").default, + { isUnexpected } = require("@azure/openai/rest"); + +// Load the .env file if it exists +require("dotenv").config(); + +// You will need to set these environment variables or edit the following values +const endpoint = process.env["ENDPOINT"] || ""; +const azureApiKey = process.env["AZURE_API_KEY"] || ""; + +const prompt = ["What is Azure OpenAI?"]; + +async function main() { + console.log("== Get completions Sample =="); + + const client = OpenAIClient(endpoint, new AzureKeyCredential(azureApiKey)); + const deploymentId = "text-davinci-003"; + const result = await client.path("/deployments/{deploymentId}/completions", deploymentId).post({ + body: { prompt, max_tokens: 128 }, + }); + + if (isUnexpected(result)) { + throw result; + } + + for (const choice of result.body.choices) { + console.log(choice.text); + } +} + +main().catch((err) => { + console.error("The sample encountered an error:", err); +}); + +module.exports = { main }; diff --git a/sdk/openai/openai/samples/v1-beta/javascript/functions.js b/sdk/openai/openai/samples/v1-beta/javascript/functions.js new file mode 100644 index 000000000000..f0357968aed4 --- /dev/null +++ b/sdk/openai/openai/samples/v1-beta/javascript/functions.js @@ -0,0 +1,58 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +/** + * Demonstrates how to define and call functions with chat completions. + * + * @summary get chat completions with functions. + */ + +const { OpenAIClient, AzureKeyCredential } = require("@azure/openai"); + +// Load the .env file if it exists +require("dotenv").config(); + +// You will need to set these environment variables or edit the following values +const endpoint = process.env["ENDPOINT"] || ""; +const azureApiKey = process.env["AZURE_API_KEY"] || ""; + +const messages = [{ role: "user", content: "What's the weather like in Boston?" }]; + +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"], + }, +}; + +async function main() { + console.log("== Chat Completions Sample With Functions =="); + + const client = new OpenAIClient(endpoint, new AzureKeyCredential(azureApiKey)); + const deploymentId = "gpt-4"; + const result = await client.getChatCompletions(deploymentId, messages, { + functions: [getCurrentWeather], + }); + + for (const choice of result.choices) { + console.log(choice.message?.functionCall); + } +} + +main().catch((err) => { + console.error("The sample encountered an error:", err); +}); + +module.exports = { main }; diff --git a/sdk/openai/openai/samples/v1-beta/javascript/getImages.js b/sdk/openai/openai/samples/v1-beta/javascript/getImages.js new file mode 100644 index 000000000000..93ce128d340e --- /dev/null +++ b/sdk/openai/openai/samples/v1-beta/javascript/getImages.js @@ -0,0 +1,41 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +/** + * Demonstrates how to generate images from prompts using Azure OpenAI Batch Image Generation. + * + * @summary generates images from prompts using Azure OpenAI Batch Image Generation. + */ + +const { OpenAIClient, AzureKeyCredential } = require("@azure/openai"); + +// Load the .env file if it exists +require("dotenv").config(); + +// You will need to set these environment variables or edit the following values +const endpoint = process.env["ENDPOINT"] || ""; +const azureApiKey = process.env["AZURE_API_KEY"] || ""; + +// The prompt to generate images from +const prompt = "a monkey eating a banana"; +const size = "256x256"; + +// The number of images to generate +const n = 3; + +async function main() { + console.log("== Batch Image Generation =="); + + const client = new OpenAIClient(endpoint, new AzureKeyCredential(azureApiKey)); + const results = await client.getImages(prompt, { n, size }); + + for (const image of results.data) { + console.log(`Image generation result URL: ${image.url}`); + } +} + +main().catch((err) => { + console.error("The sample encountered an error:", err); +}); + +module.exports = { main }; diff --git a/sdk/openai/openai/samples/v1-beta/javascript/listChatCompletions.js b/sdk/openai/openai/samples/v1-beta/javascript/listChatCompletions.js index dedd8c477fe7..3626cf104031 100644 --- a/sdk/openai/openai/samples/v1-beta/javascript/listChatCompletions.js +++ b/sdk/openai/openai/samples/v1-beta/javascript/listChatCompletions.js @@ -28,7 +28,7 @@ async function main() { const client = new OpenAIClient(endpoint, new AzureKeyCredential(azureApiKey)); const deploymentId = "gpt-35-turbo"; - const events = await client.listChatCompletions(deploymentId, messages, { maxTokens: 128 }); + const events = client.listChatCompletions(deploymentId, messages, { maxTokens: 128 }); for await (const event of events) { for (const choice of event.choices) { diff --git a/sdk/openai/openai/samples/v1-beta/javascript/listCompletions.js b/sdk/openai/openai/samples/v1-beta/javascript/listCompletions.js index 5addb35bea3d..f7a2d49d687c 100644 --- a/sdk/openai/openai/samples/v1-beta/javascript/listCompletions.js +++ b/sdk/openai/openai/samples/v1-beta/javascript/listCompletions.js @@ -23,7 +23,7 @@ async function main() { const client = new OpenAIClient(endpoint, new AzureKeyCredential(azureApiKey)); const deploymentId = "text-davinci-003"; - const events = await client.listCompletions(deploymentId, prompt, { maxTokens: 128 }); + const events = client.listCompletions(deploymentId, prompt, { maxTokens: 128 }); for await (const event of events) { for (const choice of event.choices) { diff --git a/sdk/openai/openai/samples/v1-beta/javascript/package.json b/sdk/openai/openai/samples/v1-beta/javascript/package.json index 0a0d2e32df33..7ba3439d0921 100644 --- a/sdk/openai/openai/samples/v1-beta/javascript/package.json +++ b/sdk/openai/openai/samples/v1-beta/javascript/package.json @@ -29,6 +29,7 @@ "homepage": "https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/openai/openai", "dependencies": { "@azure/openai": "next", - "dotenv": "latest" + "dotenv": "latest", + "@azure/core-auth": "^1.4.0" } } diff --git a/sdk/openai/openai/samples/v1-beta/typescript/README.md b/sdk/openai/openai/samples/v1-beta/typescript/README.md index 783fda7f08f0..a3a76d50e815 100644 --- a/sdk/openai/openai/samples/v1-beta/typescript/README.md +++ b/sdk/openai/openai/samples/v1-beta/typescript/README.md @@ -13,13 +13,17 @@ urlFragment: openai-typescript-beta These sample programs show how to use the TypeScript client libraries for Azure OpenAI in some common scenarios. -| **File Name** | **Description** | -| --------------------------------------------- | ------------------------------------- | -| [chatCompletions.ts][chatcompletions] | get chat completions. | -| [completions.ts][completions] | get completions. | -| [listChatCompletions.ts][listchatcompletions] | list chat completions. | -| [listCompletions.ts][listcompletions] | list completions. | -| [openAi.ts][openai] | get completions using the OpenAI API. | +| **File Name** | **Description** | +| --------------------------------------------- | ------------------------------------------------------------------------ | +| [bringYourOwnData.ts][bringyourowndata] | chat completions with your own data. | +| [chatCompletions.ts][chatcompletions] | get chat completions. | +| [completions.ts][completions] | get completions. | +| [completionsRest.ts][completionsrest] | get completions. | +| [functions.ts][functions] | get chat completions with functions. | +| [getImages.ts][getimages] | generates images from prompts using Azure OpenAI Batch Image Generation. | +| [listChatCompletions.ts][listchatcompletions] | list chat completions. | +| [listCompletions.ts][listcompletions] | list completions. | +| [openAi.ts][openai] | get completions using the OpenAI API. | ## Prerequisites @@ -60,25 +64,29 @@ npm run build 4. Run whichever samples you like (note that some samples may require additional setup, see the table above): ```bash -node dist/chatCompletions.js +node dist/bringYourOwnData.js ``` Alternatively, run a single sample with the correct environment variables set (setting up the `.env` file is not required if you do this), for example (cross-platform): ```bash -npx cross-env ENDPOINT="" AZURE_API_KEY="" node dist/chatCompletions.js +npx cross-env ENDPOINT="" AZURE_API_KEY="" AZURE_SEARCH_ENDPOINT="" AZURE_SEARCH_KEY="" AZURE_SEARCH_INDEX="" node dist/bringYourOwnData.js ``` ## Next Steps Take a look at our [API Documentation][apiref] for more information about the APIs that are available in the clients. +[bringyourowndata]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/openai/openai/samples/v1-beta/typescript/src/bringYourOwnData.ts [chatcompletions]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/openai/openai/samples/v1-beta/typescript/src/chatCompletions.ts [completions]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/openai/openai/samples/v1-beta/typescript/src/completions.ts +[completionsrest]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/openai/openai/samples/v1-beta/typescript/src/completionsRest.ts +[functions]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/openai/openai/samples/v1-beta/typescript/src/functions.ts +[getimages]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/openai/openai/samples/v1-beta/typescript/src/getImages.ts [listchatcompletions]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/openai/openai/samples/v1-beta/typescript/src/listChatCompletions.ts [listcompletions]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/openai/openai/samples/v1-beta/typescript/src/listCompletions.ts [openai]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/openai/openai/samples/v1-beta/typescript/src/openAi.ts -[apiref]: https://learn.microsoft.com/azure/cognitive-services/openai/ +[apiref]: https://docs.microsoft.com/javascript/api/@azure/openai [freesub]: https://azure.microsoft.com/free/ [createinstance_azurecognitiveservicesinstance]: https://learn.microsoft.com/azure/cognitive-services/openai/how-to/create-resource [package]: https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/openai/openai/README.md diff --git a/sdk/openai/openai/samples/v1-beta/typescript/package.json b/sdk/openai/openai/samples/v1-beta/typescript/package.json index 0e4eda38e693..8f228407178e 100644 --- a/sdk/openai/openai/samples/v1-beta/typescript/package.json +++ b/sdk/openai/openai/samples/v1-beta/typescript/package.json @@ -33,7 +33,8 @@ "homepage": "https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/openai/openai", "dependencies": { "@azure/openai": "next", - "dotenv": "latest" + "dotenv": "latest", + "@azure/core-auth": "^1.4.0" }, "devDependencies": { "@types/node": "^14.0.0", diff --git a/sdk/openai/openai/samples/v1-beta/typescript/src/bringYourOwnData.ts b/sdk/openai/openai/samples/v1-beta/typescript/src/bringYourOwnData.ts new file mode 100644 index 000000000000..43cf55de374c --- /dev/null +++ b/sdk/openai/openai/samples/v1-beta/typescript/src/bringYourOwnData.ts @@ -0,0 +1,69 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +/** + * Demonstrates how to use Azure's Bring Your Own Data with Azure OpenAI Chat Completions. + * + * @summary chat completions with your own data. + */ + +import { OpenAIClient, AzureKeyCredential } from "@azure/openai"; + +// Load the .env file if it exists +import * as dotenv from "dotenv"; +dotenv.config(); + +// You will need to set these environment variables or edit the following values +// The endpoint you will use to access your Azure OpenAI instance +const endpoint = process.env["ENDPOINT"] || ""; +// Your Azure OpenAI API key +const azureApiKey = process.env["AZURE_API_KEY"] || ""; +// Your Azure Cognitive Search endpoint, admin key, and index name +const azureSearchEndpoint = process.env["AZURE_SEARCH_ENDPOINT"] || ""; +const azureSearchAdminKey = process.env["AZURE_SEARCH_KEY"] || ""; +const azureSearchIndexName = process.env["AZURE_SEARCH_INDEX"] || ""; + +const messages = [ + { + role: "user", + content: "What's the most common feedback we received from our customers about the product?", + }, +]; + +export async function main() { + console.log("== Bring Your Own Data Sample =="); + + const client = new OpenAIClient(endpoint, new AzureKeyCredential(azureApiKey)); + const deploymentId = "gpt-35-turbo"; + const events = client.listChatCompletions(deploymentId, messages, { + maxTokens: 128, + /** + * The `azureExtensionOptions` property is used to configure the + * Azure-specific extensions. In this case, we are using the + * Azure Cognitive Search extension with a vector index to provide + * the model with additional context. + */ + azureExtensionOptions: { + extensions: [ + { + type: "AzureCognitiveSearch", + parameters: { + endpoint: azureSearchEndpoint, + key: azureSearchAdminKey, + indexName: azureSearchIndexName, + }, + }, + ], + }, + }); + + for await (const event of events) { + for (const choice of event.choices) { + console.log(choice.delta?.content); + } + } +} + +main().catch((err) => { + console.error("The sample encountered an error:", err); +}); diff --git a/sdk/openai/openai/samples/v1-beta/typescript/src/completionsRest.ts b/sdk/openai/openai/samples/v1-beta/typescript/src/completionsRest.ts new file mode 100644 index 000000000000..c10750b6aaff --- /dev/null +++ b/sdk/openai/openai/samples/v1-beta/typescript/src/completionsRest.ts @@ -0,0 +1,43 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +/** + * Demonstrates how to get completions for the provided prompt. + * + * @summary get completions. + */ + +import { AzureKeyCredential } from "@azure/core-auth"; +import OpenAIClient, { isUnexpected } from "@azure/openai/rest"; + +// Load the .env file if it exists +import * as dotenv from "dotenv"; +dotenv.config(); + +// You will need to set these environment variables or edit the following values +const endpoint = process.env["ENDPOINT"] || ""; +const azureApiKey = process.env["AZURE_API_KEY"] || ""; + +const prompt = ["What is Azure OpenAI?"]; + +export async function main() { + console.log("== Get completions Sample =="); + + const client = OpenAIClient(endpoint, new AzureKeyCredential(azureApiKey)); + const deploymentId = "text-davinci-003"; + const result = await client.path("/deployments/{deploymentId}/completions", deploymentId).post({ + body: { prompt, max_tokens: 128 }, + }); + + if (isUnexpected(result)) { + throw result; + } + + for (const choice of result.body.choices) { + console.log(choice.text); + } +} + +main().catch((err) => { + console.error("The sample encountered an error:", err); +}); diff --git a/sdk/openai/openai/samples/v1-beta/typescript/src/functions.ts b/sdk/openai/openai/samples/v1-beta/typescript/src/functions.ts new file mode 100644 index 000000000000..3077c53084d1 --- /dev/null +++ b/sdk/openai/openai/samples/v1-beta/typescript/src/functions.ts @@ -0,0 +1,57 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +/** + * Demonstrates how to define and call functions with chat completions. + * + * @summary get chat completions with functions. + */ + +import { OpenAIClient, AzureKeyCredential } from "@azure/openai"; + +// Load the .env file if it exists +import * as dotenv from "dotenv"; +dotenv.config(); + +// You will need to set these environment variables or edit the following values +const endpoint = process.env["ENDPOINT"] || ""; +const azureApiKey = process.env["AZURE_API_KEY"] || ""; + +const messages = [{ role: "user", content: "What's the weather like in Boston?" }]; + +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 main() { + console.log("== Chat Completions Sample With Functions =="); + + const client = new OpenAIClient(endpoint, new AzureKeyCredential(azureApiKey)); + const deploymentId = "gpt-4"; + const result = await client.getChatCompletions(deploymentId, messages, { + functions: [getCurrentWeather], + }); + + for (const choice of result.choices) { + console.log(choice.message?.functionCall); + } +} + +main().catch((err) => { + console.error("The sample encountered an error:", err); +}); diff --git a/sdk/openai/openai/samples/v1-beta/typescript/src/getImages.ts b/sdk/openai/openai/samples/v1-beta/typescript/src/getImages.ts new file mode 100644 index 000000000000..2cfcc4dcf0a1 --- /dev/null +++ b/sdk/openai/openai/samples/v1-beta/typescript/src/getImages.ts @@ -0,0 +1,40 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +/** + * Demonstrates how to generate images from prompts using Azure OpenAI Batch Image Generation. + * + * @summary generates images from prompts using Azure OpenAI Batch Image Generation. + */ + +import { OpenAIClient, AzureKeyCredential, ImageLocation } from "@azure/openai"; + +// Load the .env file if it exists +import * as dotenv from "dotenv"; +dotenv.config(); + +// You will need to set these environment variables or edit the following values +const endpoint = process.env["ENDPOINT"] || ""; +const azureApiKey = process.env["AZURE_API_KEY"] || ""; + +// The prompt to generate images from +const prompt = "a monkey eating a banana"; +const size = "256x256"; + +// The number of images to generate +const n = 3; + +export async function main() { + console.log("== Batch Image Generation =="); + + const client = new OpenAIClient(endpoint, new AzureKeyCredential(azureApiKey)); + const results = await client.getImages(prompt, { n, size }); + + for (const image of results.data as ImageLocation[]) { + console.log(`Image generation result URL: ${image.url}`); + } +} + +main().catch((err) => { + console.error("The sample encountered an error:", err); +}); diff --git a/sdk/openai/openai/samples/v1-beta/typescript/src/listChatCompletions.ts b/sdk/openai/openai/samples/v1-beta/typescript/src/listChatCompletions.ts index 177c2d03b5fd..560644cb4149 100644 --- a/sdk/openai/openai/samples/v1-beta/typescript/src/listChatCompletions.ts +++ b/sdk/openai/openai/samples/v1-beta/typescript/src/listChatCompletions.ts @@ -29,7 +29,7 @@ export async function main() { const client = new OpenAIClient(endpoint, new AzureKeyCredential(azureApiKey)); const deploymentId = "gpt-35-turbo"; - const events = await client.listChatCompletions(deploymentId, messages, { maxTokens: 128 }); + const events = client.listChatCompletions(deploymentId, messages, { maxTokens: 128 }); for await (const event of events) { for (const choice of event.choices) { diff --git a/sdk/openai/openai/samples/v1-beta/typescript/src/listCompletions.ts b/sdk/openai/openai/samples/v1-beta/typescript/src/listCompletions.ts index 46cbed9404e7..96dd9812f8f7 100644 --- a/sdk/openai/openai/samples/v1-beta/typescript/src/listCompletions.ts +++ b/sdk/openai/openai/samples/v1-beta/typescript/src/listCompletions.ts @@ -24,7 +24,7 @@ export async function main() { const client = new OpenAIClient(endpoint, new AzureKeyCredential(azureApiKey)); const deploymentId = "text-davinci-003"; - const events = await client.listCompletions(deploymentId, prompt, { maxTokens: 128 }); + const events = client.listCompletions(deploymentId, prompt, { maxTokens: 128 }); for await (const event of events) { for (const choice of event.choices) { diff --git a/sdk/openai/openai/test/public/openai.spec.ts b/sdk/openai/openai/test/public/openai.spec.ts index df5897382090..e14252cb75fc 100644 --- a/sdk/openai/openai/test/public/openai.spec.ts +++ b/sdk/openai/openai/test/public/openai.spec.ts @@ -387,7 +387,7 @@ describe("OpenAI", function () { }); describe("getImages", function () { - it("get images test", async function () { + it("gets images with the expected dimensions", async function () { const prompt = "monkey eating banana"; const numberOfImages = 2; const height = 256;