Skip to content

Commit c10e1aa

Browse files
committed
edits
1 parent f813611 commit c10e1aa

File tree

2 files changed

+27
-188
lines changed

2 files changed

+27
-188
lines changed

.openpublishing.publish.config.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -974,6 +974,12 @@
974974
"branch": "main",
975975
"branch_mapping": {}
976976
},
977+
{
978+
"path_to_root": "azure-typescript-e2e-apps",
979+
"url": "https://github.com/Azure-Samples/azure-typescript-e2e-apps",
980+
"branch": "main",
981+
"branch_mapping": {}
982+
},
977983
{
978984
"path_to_root": "azure-webpubsub",
979985
"url": "https://github.com/Azure/azure-webpubsub",

articles/ai-services/openai/includes/assistants-javascript.md

Lines changed: 21 additions & 188 deletions
Original file line numberDiff line numberDiff line change
@@ -79,200 +79,29 @@ In our code we are going to specify the following values:
7979

8080
An individual assistant can access up to 128 tools including `code interpreter`, as well as any custom tools you create via [functions](../how-to/assistant-functions.md).
8181

82+
#### [Recommended: TS Passwordless](#tab/typescript-passwordless)
8283

84+
Create and run an assistant with the following TypeScript module:
85+
86+
:::code language="typescript" source="~/azure-typescript-e2e-apps/quickstarts/azure-openai-assistants/ts/src/index.ts" :::
8387

84-
#### [TypeScript](#tab/typescript)
88+
#### [JS Passwordless](#tab/javascript-passwordless)
8589

8690
Create and run an assistant with the following TypeScript module:
8791

88-
```typescript
89-
// index.ts
90-
import {
91-
AssistantsClient,
92-
AssistantCreationOptions,
93-
ToolDefinition,
94-
Assistant,
95-
AssistantThread,
96-
ThreadMessage,
97-
ThreadRun,
98-
ListResponseOf,
99-
} from "@azure/openai-assistants";
100-
import { DefaultAzureCredential } from "@azure/identity";
101-
102-
import "dotenv/config";
103-
104-
// Recommended for secure credential management
105-
// const azureOpenAIEndpoint = process.env.AZURE_OPENAI_ENDPOINT as string;
106-
// if (!azureOpenAIEndpoint) {
107-
// throw new Error(
108-
// "Please ensure to set AZURE_OPENAI_ENDPOINT in your environment variables."
109-
// );
110-
// }
111-
// const getClient = (): AssistantsClient => {
112-
// const credential = new DefaultAzureCredential();
113-
// const assistantsClient = new AssistantsClient(azureOpenAIEndpoint, credential);
114-
// return assistantsClient;
115-
// }
116-
117-
// Not recommended - for local demo purposes only
118-
const azureOpenAIKey = process.env.AZURE_OPENAI_API_KEY as string;
119-
const azureOpenAIEndpoint = process.env.AZURE_OPENAI_ENDPOINT as string;
120-
const credential = new AzureKeyCredential(azureOpenAIKey);
121-
const getClient = (): AssistantsClient => {
122-
const assistantsClient = new AssistantsClient(azureOpenAIEndpoint, credential);
123-
return assistantsClient;
124-
}
125-
126-
const assistantsClient: AssistantsClient = getClient();
127-
128-
const options: AssistantCreationOptions = {
129-
model: "gpt-4-1106-preview", // Deployment name seen in Azure AI Studio
130-
name: "Math Tutor",
131-
instructions:
132-
"You are a personal math tutor. Write and run JavaScript code to answer math questions.",
133-
tools: [{ type: "code_interpreter" } as ToolDefinition],
134-
};
135-
const role = "user";
136-
const message = "I need to solve the equation `3x + 11 = 14`. Can you help me?";
137-
const message2 = "What is 3x + 11 = 14?";
138-
139-
// Create an assistant
140-
const assistantResponse: Assistant = await assistantsClient.createAssistant(options);
141-
console.log(`Assistant created: ${JSON.stringify(assistantResponse)}`);
142-
143-
// Create a thread
144-
const assistantThread: AssistantThread = await assistantsClient.createThread({});
145-
console.log(`Thread created: ${JSON.stringify(assistantThread)}`);
146-
147-
// Add a user question to the thread
148-
const threadResponse: ThreadMessage = await assistantsClient.createMessage(
149-
assistantThread.id,
150-
role,
151-
message
152-
);
153-
console.log(`Message created: ${JSON.stringify(threadResponse)}`);
154-
155-
// Run the thread
156-
let runResponse: ThreadRun = await assistantsClient.createRun(assistantThread.id, {
157-
assistantId: assistantResponse.id,
158-
});
159-
console.log(`Run created: ${JSON.stringify(runResponse)}`);
160-
161-
// Wait for the assistant to respond
162-
do {
163-
await new Promise((r) => setTimeout(r, 500));
164-
runResponse = await assistantsClient.getRun(
165-
assistantThread.id,
166-
runResponse.id
167-
);
168-
} while (
169-
runResponse.status === "queued" ||
170-
runResponse.status === "in_progress"
171-
);
172-
173-
// Get the messages
174-
const runMessages: ListResponseOf<ThreadMessage> = await assistantsClient.listMessages(assistantThread.id);
175-
for (const runMessageDatum of runMessages.data) {
176-
for (const item of runMessageDatum.content) {
177-
if (item.type === "text") {
178-
console.log(`Message content: ${JSON.stringify(item.text?.value)}`);
179-
}
180-
}
181-
}
182-
```
92+
:::code language="javascript" source="~/azure-typescript-e2e-apps/quickstarts/azure-openai-assistants/js/src/index.mjs" :::
18393

184-
#### [JavaScript](#tab/javascript)
185-
186-
Create and run an assistant with the following ECMAScript module:
187-
188-
```javascript
189-
// index.mjs
190-
import {
191-
AssistantsClient
192-
} from "@azure/openai-assistants";
193-
import { DefaultAzureCredential } from "@azure/identity";
194-
195-
import "dotenv/config";
196-
197-
// Recommended for secure credential management
198-
// const azureOpenAIEndpoint = process.env.AZURE_OPENAI_ENDPOINT;
199-
// if (!azureOpenAIEndpoint) {
200-
// throw new Error(
201-
// "Please ensure to set AZURE_OPENAI_ENDPOINT in your environment variables."
202-
// );
203-
// }
204-
// const getClient = () => {
205-
// const credential = new DefaultAzureCredential();
206-
// const assistantsClient = new AssistantsClient(azureOpenAIEndpoint, credential);
207-
// return assistantsClient;
208-
// }
209-
210-
// Not recommended - for local demo purposes only
211-
const azureOpenAIKey = process.env.AZURE_OPENAI_API_KEY;
212-
const azureOpenAIEndpoint = process.env.AZURE_OPENAI_ENDPOINT;
213-
const credential = new AzureKeyCredential(azureOpenAIKey);
214-
const getClient = () => {
215-
const assistantsClient = new AssistantsClient(azureOpenAIEndpoint, credential);
216-
return assistantsClient;
217-
}
218-
219-
const assistantsClient = getClient();
220-
221-
const options = {
222-
model: "gpt-4-1106-preview", // Deployment name seen in Azure AI Studio
223-
name: "Math Tutor",
224-
instructions:
225-
"You are a personal math tutor. Write and run JavaScript code to answer math questions.",
226-
tools: [{ type: "code_interpreter" }],
227-
};
228-
const role = "user";
229-
const message = "I need to solve the equation `3x + 11 = 14`. Can you help me?";
230-
const message2 = "What is 3x + 11 = 14?";
231-
232-
// Create an assistant
233-
const assistantResponse = await assistantsClient.createAssistant(options);
234-
console.log(`Assistant created: ${JSON.stringify(assistantResponse)}`);
235-
236-
// Create a thread
237-
const assistantThread = await assistantsClient.createThread({});
238-
console.log(`Thread created: ${JSON.stringify(assistantThread)}`);
239-
240-
// Add a user question to the thread
241-
const threadResponse = await assistantsClient.createMessage(
242-
assistantThread.id,
243-
role,
244-
message
245-
);
246-
console.log(`Message created: ${JSON.stringify(threadResponse)}`);
247-
248-
// Run the thread
249-
let runResponse = await assistantsClient.createRun(assistantThread.id, {
250-
assistantId: assistantResponse.id,
251-
});
252-
console.log(`Run created: ${JSON.stringify(runResponse)}`);
253-
254-
// Wait for the assistant to respond
255-
do {
256-
await new Promise((r) => setTimeout(r, 500));
257-
runResponse = await assistantsClient.getRun(
258-
assistantThread.id,
259-
runResponse.id
260-
);
261-
} while (
262-
runResponse.status === "queued" ||
263-
runResponse.status === "in_progress"
264-
);
265-
266-
// Get the messages
267-
const runMessages = await assistantsClient.listMessages(assistantThread.id);
268-
for (const runMessageDatum of runMessages.data) {
269-
for (const item of runMessageDatum.content) {
270-
if (item.type === "text") {
271-
console.log(`Message content: ${JSON.stringify(item.text?.value)}`);
272-
}
273-
}
274-
}
275-
```
94+
#### [TS Password](#tab/typescript-password)
95+
96+
Create and run an assistant with the following TypeScript module:
97+
98+
:::code language="typescript" source="~/azure-typescript-e2e-apps/quickstarts/azure-openai-assistants/ts/src/index-using-password.ts" :::
99+
100+
#### [JS Password](#tab/javascript-password)
101+
102+
Create and run an assistant with the following TypeScript module:
103+
104+
:::code language="javascript" source="~/azure-typescript-e2e-apps/quickstarts/azure-openai-assistants/js/src/index-using-password.mjs" :::
276105

277106
---
278107

@@ -298,6 +127,10 @@ If you want to clean up and remove an Azure OpenAI resource, you can delete the
298127
- [Portal](../../multi-service-resource.md?pivots=azportal#clean-up-resources)
299128
- [Azure CLI](../../multi-service-resource.md?pivots=azcli#clean-up-resources)
300129

130+
## Sample code
131+
132+
* [Quickstart sample code](https://github.com/Azure-Samples/azure-typescript-e2e-apps/tree/main/quickstarts/azure-openai-assistants)
133+
301134
## See also
302135

303136
* Learn more about how to use Assistants with our [How-to guide on Assistants](../how-to/assistant.md).

0 commit comments

Comments
 (0)