Skip to content

Commit

Permalink
[OpenAI] Prepare beta.5 release (#26941)
Browse files Browse the repository at this point in the history
  • Loading branch information
deyaaeldeen authored Aug 25, 2023
1 parent d8b0c68 commit 703cd1f
Show file tree
Hide file tree
Showing 19 changed files with 471 additions and 34 deletions.
8 changes: 2 additions & 6 deletions sdk/openai/openai/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Release History

## 1.0.0-beta.5 (Unreleased)
## 1.0.0-beta.5 (2023-08-25)

### Features Added

Expand All @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion sdk/openai/openai/assets.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
28 changes: 18 additions & 10 deletions sdk/openai/openai/samples/v1-beta/javascript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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="<endpoint>" AZURE_API_KEY="<azure api key>" node chatCompletions.js
npx cross-env ENDPOINT="<endpoint>" AZURE_API_KEY="<azure api key>" AZURE_SEARCH_ENDPOINT="<azure search endpoint>" AZURE_SEARCH_KEY="<azure search key>" AZURE_SEARCH_INDEX="<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
70 changes: 70 additions & 0 deletions sdk/openai/openai/samples/v1-beta/javascript/bringYourOwnData.js
Original file line number Diff line number Diff line change
@@ -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"] || "<endpoint>";
// Your Azure OpenAI API key
const azureApiKey = process.env["AZURE_API_KEY"] || "<api key>";
// Your Azure Cognitive Search endpoint, admin key, and index name
const azureSearchEndpoint = process.env["AZURE_SEARCH_ENDPOINT"] || "<search endpoint>";
const azureSearchAdminKey = process.env["AZURE_SEARCH_KEY"] || "<search key>";
const azureSearchIndexName = process.env["AZURE_SEARCH_INDEX"] || "<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 };
45 changes: 45 additions & 0 deletions sdk/openai/openai/samples/v1-beta/javascript/completionsRest.js
Original file line number Diff line number Diff line change
@@ -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"] || "<endpoint>";
const azureApiKey = process.env["AZURE_API_KEY"] || "<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 };
58 changes: 58 additions & 0 deletions sdk/openai/openai/samples/v1-beta/javascript/functions.js
Original file line number Diff line number Diff line change
@@ -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"] || "<endpoint>";
const azureApiKey = process.env["AZURE_API_KEY"] || "<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 };
41 changes: 41 additions & 0 deletions sdk/openai/openai/samples/v1-beta/javascript/getImages.js
Original file line number Diff line number Diff line change
@@ -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"] || "<endpoint>";
const azureApiKey = process.env["AZURE_API_KEY"] || "<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 };
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion sdk/openai/openai/samples/v1-beta/javascript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
28 changes: 18 additions & 10 deletions sdk/openai/openai/samples/v1-beta/typescript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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="<endpoint>" AZURE_API_KEY="<azure api key>" node dist/chatCompletions.js
npx cross-env ENDPOINT="<endpoint>" AZURE_API_KEY="<azure api key>" AZURE_SEARCH_ENDPOINT="<azure search endpoint>" AZURE_SEARCH_KEY="<azure search key>" AZURE_SEARCH_INDEX="<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
Expand Down
3 changes: 2 additions & 1 deletion sdk/openai/openai/samples/v1-beta/typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading

0 comments on commit 703cd1f

Please sign in to comment.