Skip to content

Commit 87af5ea

Browse files
authored
[OpenAI] Update README (#25947)
Per feedback from @achandmsft
1 parent 9c2c9cf commit 87af5ea

File tree

5 files changed

+63
-23
lines changed

5 files changed

+63
-23
lines changed

common/tools/dev-tool/src/commands/run/testNodeJSInput.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT license
33

44
import { leafCommand, makeCommandInfo } from "../../framework/command";
5+
import { resolveProject } from "../../util/resolveProject";
56
import { runTestsWithProxyTool } from "../../util/testUtils";
67

78
export const commandInfo = makeCommandInfo(
@@ -10,8 +11,11 @@ export const commandInfo = makeCommandInfo(
1011
);
1112

1213
export default leafCommand(commandInfo, async (options) => {
14+
const projectInfo = await resolveProject(process.cwd());
1315
const defaultMochaArgs =
14-
"-r esm --require source-map-support/register --reporter ../../../common/tools/mocha-multi-reporter.js --full-trace";
16+
`${
17+
projectInfo.packageJson.type === "module" ? "" : "-r esm "
18+
} --require source-map-support/register --reporter ../../../common/tools/mocha-multi-reporter.js --full-trace`;
1519
const updatedArgs = options["--"]?.map((opt) =>
1620
opt.includes("**") && !opt.startsWith("'") && !opt.startsWith('"') ? `"${opt}"` : opt
1721
);

sdk/openai/openai/README.md

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ Use the client library for Azure OpenAI to:
1212

1313
Azure OpenAI is a managed service that allows developers to deploy, tune, and generate content from OpenAI models on Azure resources.
1414

15+
Checkout the following examples:
16+
17+
- [Multiple Completions](#generate-multiple-completions-with-subscription-key)
18+
- [Chatbot](#generate-chatbot-response)
19+
- [Summarize Text](#summarize-text-with-completion)
20+
1521
Key links:
1622

1723
- [Source code](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/openai/openai)
@@ -135,22 +141,35 @@ You can familiarize yourself with different APIs using [Samples](https://github.
135141

136142
### Generate Chatbot Response
137143

138-
This example authenticates using a DefaultAzureCredential, then generates text responses to input prompts.
144+
This example authenticates using a DefaultAzureCredential, then generates chat responses to input chat question and messages.
139145

140146
```javascript
141147
const endpoint = "https://myaccount.openai.azure.com/";
142148
const client = new OpenAIClient(endpoint, new DefaultAzureCredential());
143149

144-
const deploymentName = "text-davinci-003";
145-
const prompt = ["What is Azure OpenAI?"];
146-
console.log(`Input: ${prompt}`);
150+
const deploymentId = "gpt-35-turbo";
147151

148-
const { choices } = await client.getCompletions(deploymentName, prompt);
149-
const completion = choices[0].text;
150-
console.log(`Chatbot: ${completion}`);
152+
const messages = [
153+
{ role: "system", content: "You are a helpful assistant. You will talk like a pirate." },
154+
{ role: "user", content: "Can you help me?" },
155+
{ role: "assistant", content: "Arrrr! Of course, me hearty! What can I do for ye?" },
156+
{ role: "user", content: "What's the best way to train a parrot?" },
157+
];
158+
159+
console.log(`Messages: ${messages.map((m) => m.content).join("\n")}`);
160+
161+
const events = await client.listChatCompletions(deploymentId, messages, { maxTokens: 128 });
162+
for await (const event of events) {
163+
for (const choice of event.choices) {
164+
const delta = choice.delta?.content;
165+
if (delta !== undefined) {
166+
console.log(`Chatbot: ${delta}`);
167+
}
168+
}
169+
}
151170
```
152171
153-
### Generate Multiple Chatbot Responses With Subscription Key
172+
### Generate Multiple Completions With Subscription Key
154173
155174
This example generates text responses to input prompts using an Azure subscription key
156175
@@ -208,7 +227,9 @@ console.log(`Input: ${summarizationPrompt}`);
208227

209228
const deploymentName = "text-davinci-003";
210229

211-
const { choices } = await client.getCompletions(deploymentName, summarizationPrompt);
230+
const { choices } = await client.getCompletions(deploymentName, examplePrompts, {
231+
maxTokens: 64
232+
});
212233
const completion = choices[0].text;
213234
console.log(`Summarization: ${completion}`);
214235
```
@@ -228,8 +249,8 @@ setLogLevel("info");
228249
For more detailed instructions on how to enable logs, you can look at the [@azure/logger package docs](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/core/logger).
229250
230251
<!-- LINKS -->
231-
[msdocs_openai_completion]: https://learn.microsoft.com/azure/cognitive-services/openai/how-to/completions
232-
[msdocs_openai_chat_completion]: https://learn.microsoft.com/azure/cognitive-services/openai/how-to/chatgpt
252+
[msdocs_openai_completion]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/openai/openai/samples/v1-beta/javascript/completions.js
253+
[msdocs_openai_chat_completion]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/openai/openai/samples/v1-beta/javascript/listChatCompletions.js
233254
[msdocs_openai_embedding]: https://learn.microsoft.com/azure/cognitive-services/openai/concepts/understand-embeddings
234255
[azure_openai_completions_docs]: https://learn.microsoft.com/azure/cognitive-services/openai/how-to/completions
235256
[defaultazurecredential]: https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/identity/identity#defaultazurecredential

sdk/openai/openai/assets.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
"AssetsRepo": "Azure/azure-sdk-assets",
33
"AssetsRepoPrefixPath": "js",
44
"TagPrefix": "js/openai/openai",
5-
"Tag": "js/openai/openai_99dc8874ba"
5+
"Tag": "js/openai/openai_2df52bb465"
66
}

sdk/openai/openai/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@
4444
"execute:samples": "dev-tool samples run samples-dev",
4545
"extract-api": "tsc -p . && api-extractor run --local",
4646
"format": "prettier --write --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"sources/customizations/**/*.ts\" \"src/**/*.ts\" \"test/**/*.ts\" \"samples-dev/**/*.ts\" \"*.{js,json}\"",
47-
"integration-test:browser": "echo skipped",
48-
"integration-test:node": "nyc mocha --timeout 600000 \"test/internal/**/*.spec.ts\" \"test/public/{,!(browser)/**/}*.spec.ts\"",
47+
"integration-test:browser": "npm run unit-test:browser",
48+
"integration-test:node": "dev-tool run test:node-js-input -- --timeout 5000000 \"dist-esm/test/**/*.spec.js\"",
4949
"integration-test": "npm run integration-test:node && npm run integration-test:browser",
5050
"lint:fix": "eslint README.md package.json api-extractor.json src test --ext .ts,.javascript,.js --fix --fix-type [problem,suggestion]",
5151
"lint": "eslint README.md package.json api-extractor.json src test --ext .ts,.javascript,.js",

sdk/openai/openai/test/public/samples.spec.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,29 @@ describe("README samples", () => {
2525
});
2626

2727
it("Generate Chatbot Response", async function () {
28-
const deploymentName = "text-davinci-003";
29-
const prompt = ["What is Azure OpenAI?"];
30-
console.log(`Input: ${prompt}`);
28+
const deploymentId = "gpt-35-turbo";
3129

32-
const { choices } = await client.getCompletions(deploymentName, prompt);
33-
const completion = choices[0].text;
34-
console.log(`Chatbot: ${completion}`);
30+
const messages = [
31+
{ role: "system", content: "You are a helpful assistant. You will talk like a pirate." },
32+
{ role: "user", content: "Can you help me?" },
33+
{ role: "assistant", content: "Arrrr! Of course, me hearty! What can I do for ye?" },
34+
{ role: "user", content: "What's the best way to train a parrot?" },
35+
];
36+
37+
console.log(`Messages: ${messages.map((m) => m.content).join("\n")}`);
38+
39+
const events = await client.listChatCompletions(deploymentId, messages, { maxTokens: 128 });
40+
for await (const event of events) {
41+
for (const choice of event.choices) {
42+
const delta = choice.delta?.content;
43+
if (delta !== undefined) {
44+
console.log(`Chatbot: ${delta}`);
45+
}
46+
}
47+
}
3548
});
3649

37-
it("Generate Multiple Chatbot Responses", async function () {
50+
it("Generate Multiple Completions", async function () {
3851
const examplePrompts = [
3952
"How are you today?",
4053
"What is Azure OpenAI?",
@@ -46,7 +59,9 @@ describe("README samples", () => {
4659
const deploymentName = "text-davinci-003";
4760

4861
let promptIndex = 0;
49-
const { choices } = await client.getCompletions(deploymentName, examplePrompts);
62+
const { choices } = await client.getCompletions(deploymentName, examplePrompts, {
63+
maxTokens: 64,
64+
});
5065
for (const choice of choices) {
5166
const completion = choice.text;
5267
console.log(`Input: ${examplePrompts[promptIndex++]}`);

0 commit comments

Comments
 (0)