@@ -12,6 +12,12 @@ Use the client library for Azure OpenAI to:
1212
1313Azure 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+
1521Key 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
141147const endpoint = " https://myaccount.openai.azure.com/" ;
142148const 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
155174This example generates text responses to input prompts using an Azure subscription key
156175
@@ -208,7 +227,9 @@ console.log(`Input: ${summarizationPrompt}`);
208227
209228const 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+ });
212233const completion = choices[0 ].text ;
213234console .log (` Summarization: ${ completion} ` );
214235` ` `
@@ -228,8 +249,8 @@ setLogLevel("info");
228249For 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
0 commit comments