Skip to content

Commit 2b9f93c

Browse files
committed
update JS to OpenAI without types
1 parent 9719b62 commit 2b9f93c

File tree

1 file changed

+39
-19
lines changed

1 file changed

+39
-19
lines changed

articles/ai-services/openai/includes/use-your-data-javascript.md

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -148,40 +148,64 @@ const { AzureOpenAI } = require("openai");
148148

149149
// Set the Azure and AI Search values from environment variables
150150
const endpoint = process.env["AZURE_OPENAI_ENDPOINT"];
151-
const azureApiKey = process.env["AZURE_OPENAI_API_KEY"];
152-
const deploymentId = process.env["AZURE_OPENAI_DEPLOYMENT_ID"];
151+
const apiKey = process.env["AZURE_OPENAI_API_KEY"];
153152
const searchEndpoint = process.env["AZURE_AI_SEARCH_ENDPOINT"];
154153
const searchKey = process.env["AZURE_AI_SEARCH_API_KEY"];
155154
const searchIndex = process.env["AZURE_AI_SEARCH_INDEX"];
156155

156+
// Required Azure OpenAI deployment name and API version
157+
const deploymentName = "gpt-4";
158+
const apiVersion = "2024-07-01-preview";
157159

158-
async function main(){
159-
const client = new OpenAIClient(endpoint, new AzureKeyCredential(azureApiKey));
160+
function getClient() {
161+
return new AzureOpenAI({
162+
endpoint,
163+
apiKey: apiKey,
164+
deployment: deploymentName,
165+
apiVersion,
166+
});
167+
}
168+
169+
async function main() {
170+
const client = getClient();
160171

161172
const messages = [
162173
{ role: "user", content: "What are my available health plans?" },
163174
];
164175

165176
console.log(`Message: ${messages.map((m) => m.content).join("\n")}`);
166177

167-
const events = await client.streamChatCompletions(deploymentId, messages, {
168-
maxTokens: 128,
169-
azureExtensionOptions: {
170-
extensions: [
171-
{
172-
type: "AzureCognitiveSearch",
178+
const events = await client.chat.completions.create({
179+
stream: true,
180+
messages: [
181+
{
182+
role: "user",
183+
content:
184+
"What's the most common feedback we received from our customers about the product?",
185+
},
186+
],
187+
max_tokens: 128,
188+
model: "",
189+
data_sources: [
190+
{
191+
type: "azure_search",
192+
parameters: {
173193
endpoint: searchEndpoint,
174-
key: searchKey,
175-
indexName: searchIndex,
194+
index_name: searchIndex,
195+
authentication: {
196+
type: "api_key",
197+
key: searchKey,
198+
},
176199
},
177-
],
178-
},
200+
},
201+
],
179202
});
203+
180204
let response = "";
181205
for await (const event of events) {
182206
for (const choice of event.choices) {
183207
const newText = choice.delta?.content;
184-
if (!!newText) {
208+
if (newText) {
185209
response += newText;
186210
// To see streaming results as they arrive, uncomment line below
187211
// console.log(newText);
@@ -194,10 +218,6 @@ async function main(){
194218
main().catch((err) => {
195219
console.error("The sample encountered an error:", err);
196220
});
197-
198-
199-
200-
module.exports = { main };
201221
```
202222

203223
1. Run the application with the following command:

0 commit comments

Comments
 (0)