Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit c02326e

Browse files
committed
Update views
1 parent 6395c17 commit c02326e

File tree

1 file changed

+63
-45
lines changed

1 file changed

+63
-45
lines changed

docs/docs/examples/openai-node.md

Lines changed: 63 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -85,54 +85,60 @@ chatCompletion()
8585

8686
```typescript
8787
import OpenAI from 'openai';
88-
// The name of your Azure OpenAI Resource.
89-
// https://learn.microsoft.com/en-us/azure/cognitive-services/openai/how-to/create-resource?pivots=web-portal#create-a-resource
90-
const resource = '<your resource name>';
9188

92-
// Corresponds to your Model deployment within your OpenAI resource, e.g. my-gpt35-16k-deployment
93-
// Navigate to the Azure OpenAI Studio to deploy a model.
89+
const resource = '<your resource name>';
9490
const model = '<your model>';
95-
96-
// https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#rest-api-versioning
9791
const apiVersion = '2023-06-01-preview';
98-
9992
const apiKey = process.env['AZURE_OPENAI_API_KEY'];
93+
10094
if (!apiKey) {
101-
throw new Error('The AZURE_OPENAI_API_KEY environment variable is missing or empty.');
95+
throw new Error('The AZURE_OPENAI_API_KEY variable is missing.');
10296
}
10397

98+
const baseURL = `https://${resource}.openai.azure.com/openai/` +
99+
`deployments/${model}`;
100+
104101
const openai = new OpenAI({
105102
apiKey,
106-
baseURL: `https://${resource}.openai.azure.com/openai/deployments/${model}`,
103+
baseURL,
107104
defaultQuery: { 'api-version': apiVersion },
108105
defaultHeaders: { 'api-key': apiKey },
109106
});
110107

111108
async function chatCompletion() {
112-
const stream = await openai.beta.chat.completions.stream({
113-
model: 'gpt-3.5-turbo',
114-
messages: [{ role: 'user', content: 'Say this is a test' }],
115-
stream: true,
116-
});
117-
118-
stream.on('content', (delta, snapshot) => {
119-
process.stdout.write(delta);
120-
});
121-
122-
for await (const chunk of stream) {
123-
process.stdout.write(chunk.choices[0]?.delta?.content || '');
109+
try {
110+
const stream = await openai.beta.chat.completions.stream({
111+
model: 'gpt-3.5-turbo',
112+
messages: [{ role: 'user', content: 'Say this is a test' }],
113+
stream: true,
114+
});
115+
116+
stream.on('content', (delta, snapshot) => {
117+
process.stdout.write(delta);
118+
});
119+
120+
for await (const chunk of stream) {
121+
process.stdout.write(chunk.choices[0]?.delta?.content || '');
122+
}
123+
124+
const chatCompletion = await stream.finalChatCompletion();
125+
console.log(chatCompletion); // Log the final completion
126+
} catch (error) {
127+
console.error('Error in chat completion:', error);
124128
}
125-
126-
const chatCompletion = await stream.finalChatCompletion();
127-
console.log(chatCompletion); // {id: "…", choices: […], …}
128129
}
129-
chatCompletion()
130+
131+
chatCompletion();
130132
```
131133

132134
</td>
133135
</tr>
134136
</table>
135137

138+
> Resource:
139+
> - [Azure Create a resource](https://learn.microsoft.com/en-us/azure/cognitive-services/openai/how-to/create-resource?pivots=web-portal#create-a-resource)
140+
> - [Azure-OAI Rest API versoning](https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#rest-api-versioning)
141+
136142
## Embedding
137143
<table>
138144
<tr>
@@ -146,16 +152,24 @@ chatCompletion()
146152
import OpenAI from 'openai';
147153

148154
const openai = new OpenAI({
149-
apiKey: '', // defaults to process.env["OPENAI_API_KEY"]
150-
baseURL: "http://localhost:3928/v1/" // https://api.openai.com/v1
155+
apiKey: '', // Defaults to process.env["OPENAI_API_KEY"]
156+
baseURL: 'http://localhost:3928/v1/'
157+
// 'https://api.openai.com/v1'
151158
});
152159

153160
async function embedding() {
154-
const embedding = await openai.embeddings.create({input: 'Hello How are you?', model: 'text-embedding-ada-002'});
155-
console.log(embedding); // {object: "list", data: […], …}
161+
try {
162+
const response = await openai.embeddings.create({
163+
input: 'Hello How are you?',
164+
model: 'text-embedding-ada-002'
165+
});
166+
console.log(response); // Log the response
167+
} catch (error) {
168+
console.error('Error in fetching embedding:', error);
169+
}
156170
}
157171

158-
chatCompletion();
172+
embedding();
159173
```
160174
</td>
161175
</tr>
@@ -171,11 +185,14 @@ const openai = new OpenAI({
171185
});
172186

173187
async function embedding() {
174-
const embedding = await openai.embeddings.create({input: 'Hello How are you?', model: 'text-embedding-ada-002'});
188+
const embedding = await openai.embeddings.create({
189+
input: 'Hello How are you?',
190+
model: 'text-embedding-ada-002'
191+
});
175192
console.log(embedding); // {object: "list", data: […], …}
176193
}
177194

178-
chatCompletion();
195+
embedding();
179196
```
180197

181198
</td>
@@ -186,35 +203,36 @@ chatCompletion();
186203

187204
```typescript
188205
import OpenAI from 'openai';
189-
// The name of your Azure OpenAI Resource.
190-
// https://learn.microsoft.com/en-us/azure/cognitive-services/openai/how-to/create-resource?pivots=web-portal#create-a-resource
191-
const resource = '<your resource name>';
192206

193-
// Corresponds to your Model deployment within your OpenAI resource, e.g. my-gpt35-16k-deployment
194-
// Navigate to the Azure OpenAI Studio to deploy a model.
207+
const resource = '<your resource name>';
195208
const model = '<your model>';
196-
197-
// https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#rest-api-versioning
198209
const apiVersion = '2023-06-01-preview';
199-
200210
const apiKey = process.env['AZURE_OPENAI_API_KEY'];
211+
201212
if (!apiKey) {
202-
throw new Error('The AZURE_OPENAI_API_KEY environment variable is missing or empty.');
213+
throw new Error('The AZURE_OPENAI_API_KEY variable is missing.');
203214
}
204215

216+
// Splitting the baseURL into concatenated parts for readability
217+
const baseURL = `https://${resource}.openai.azure.com/openai/` +
218+
`deployments/${model}`;
219+
205220
const openai = new OpenAI({
206221
apiKey,
207-
baseURL: `https://${resource}.openai.azure.com/openai/deployments/${model}`,
222+
baseURL,
208223
defaultQuery: { 'api-version': apiVersion },
209224
defaultHeaders: { 'api-key': apiKey },
210225
});
211226

212227
async function embedding() {
213-
const embedding = await openai.embeddings.create({input: 'Hello How are you?', model: 'text-embedding-ada-002'});
228+
const embedding = await openai.embeddings.create({
229+
input: 'Hello How are you?',
230+
model: 'text-embedding-ada-002'
231+
});
214232
console.log(embedding); // {object: "list", data: […], …}
215233
}
216234

217-
chatCompletion();
235+
embedding();
218236
```
219237

220238
</td>

0 commit comments

Comments
 (0)