Skip to content

Commit

Permalink
edits
Browse files Browse the repository at this point in the history
  • Loading branch information
diberry committed Apr 10, 2024
1 parent f813611 commit c10e1aa
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 188 deletions.
6 changes: 6 additions & 0 deletions .openpublishing.publish.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,12 @@
"branch": "main",
"branch_mapping": {}
},
{
"path_to_root": "azure-typescript-e2e-apps",
"url": "https://github.com/Azure-Samples/azure-typescript-e2e-apps",
"branch": "main",
"branch_mapping": {}
},
{
"path_to_root": "azure-webpubsub",
"url": "https://github.com/Azure/azure-webpubsub",
Expand Down
209 changes: 21 additions & 188 deletions articles/ai-services/openai/includes/assistants-javascript.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,200 +79,29 @@ In our code we are going to specify the following values:

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).

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

Create and run an assistant with the following TypeScript module:

:::code language="typescript" source="~/azure-typescript-e2e-apps/quickstarts/azure-openai-assistants/ts/src/index.ts" :::

#### [TypeScript](#tab/typescript)
#### [JS Passwordless](#tab/javascript-passwordless)

Create and run an assistant with the following TypeScript module:

```typescript
// index.ts
import {
AssistantsClient,
AssistantCreationOptions,
ToolDefinition,
Assistant,
AssistantThread,
ThreadMessage,
ThreadRun,
ListResponseOf,
} from "@azure/openai-assistants";
import { DefaultAzureCredential } from "@azure/identity";

import "dotenv/config";

// Recommended for secure credential management
// const azureOpenAIEndpoint = process.env.AZURE_OPENAI_ENDPOINT as string;
// if (!azureOpenAIEndpoint) {
// throw new Error(
// "Please ensure to set AZURE_OPENAI_ENDPOINT in your environment variables."
// );
// }
// const getClient = (): AssistantsClient => {
// const credential = new DefaultAzureCredential();
// const assistantsClient = new AssistantsClient(azureOpenAIEndpoint, credential);
// return assistantsClient;
// }

// Not recommended - for local demo purposes only
const azureOpenAIKey = process.env.AZURE_OPENAI_API_KEY as string;
const azureOpenAIEndpoint = process.env.AZURE_OPENAI_ENDPOINT as string;
const credential = new AzureKeyCredential(azureOpenAIKey);
const getClient = (): AssistantsClient => {
const assistantsClient = new AssistantsClient(azureOpenAIEndpoint, credential);
return assistantsClient;
}

const assistantsClient: AssistantsClient = getClient();

const options: AssistantCreationOptions = {
model: "gpt-4-1106-preview", // Deployment name seen in Azure AI Studio
name: "Math Tutor",
instructions:
"You are a personal math tutor. Write and run JavaScript code to answer math questions.",
tools: [{ type: "code_interpreter" } as ToolDefinition],
};
const role = "user";
const message = "I need to solve the equation `3x + 11 = 14`. Can you help me?";
const message2 = "What is 3x + 11 = 14?";

// Create an assistant
const assistantResponse: Assistant = await assistantsClient.createAssistant(options);
console.log(`Assistant created: ${JSON.stringify(assistantResponse)}`);

// Create a thread
const assistantThread: AssistantThread = await assistantsClient.createThread({});
console.log(`Thread created: ${JSON.stringify(assistantThread)}`);

// Add a user question to the thread
const threadResponse: ThreadMessage = await assistantsClient.createMessage(
assistantThread.id,
role,
message
);
console.log(`Message created: ${JSON.stringify(threadResponse)}`);

// Run the thread
let runResponse: ThreadRun = await assistantsClient.createRun(assistantThread.id, {
assistantId: assistantResponse.id,
});
console.log(`Run created: ${JSON.stringify(runResponse)}`);

// Wait for the assistant to respond
do {
await new Promise((r) => setTimeout(r, 500));
runResponse = await assistantsClient.getRun(
assistantThread.id,
runResponse.id
);
} while (
runResponse.status === "queued" ||
runResponse.status === "in_progress"
);

// Get the messages
const runMessages: ListResponseOf<ThreadMessage> = await assistantsClient.listMessages(assistantThread.id);
for (const runMessageDatum of runMessages.data) {
for (const item of runMessageDatum.content) {
if (item.type === "text") {
console.log(`Message content: ${JSON.stringify(item.text?.value)}`);
}
}
}
```
:::code language="javascript" source="~/azure-typescript-e2e-apps/quickstarts/azure-openai-assistants/js/src/index.mjs" :::

#### [JavaScript](#tab/javascript)

Create and run an assistant with the following ECMAScript module:

```javascript
// index.mjs
import {
AssistantsClient
} from "@azure/openai-assistants";
import { DefaultAzureCredential } from "@azure/identity";

import "dotenv/config";

// Recommended for secure credential management
// const azureOpenAIEndpoint = process.env.AZURE_OPENAI_ENDPOINT;
// if (!azureOpenAIEndpoint) {
// throw new Error(
// "Please ensure to set AZURE_OPENAI_ENDPOINT in your environment variables."
// );
// }
// const getClient = () => {
// const credential = new DefaultAzureCredential();
// const assistantsClient = new AssistantsClient(azureOpenAIEndpoint, credential);
// return assistantsClient;
// }

// Not recommended - for local demo purposes only
const azureOpenAIKey = process.env.AZURE_OPENAI_API_KEY;
const azureOpenAIEndpoint = process.env.AZURE_OPENAI_ENDPOINT;
const credential = new AzureKeyCredential(azureOpenAIKey);
const getClient = () => {
const assistantsClient = new AssistantsClient(azureOpenAIEndpoint, credential);
return assistantsClient;
}

const assistantsClient = getClient();

const options = {
model: "gpt-4-1106-preview", // Deployment name seen in Azure AI Studio
name: "Math Tutor",
instructions:
"You are a personal math tutor. Write and run JavaScript code to answer math questions.",
tools: [{ type: "code_interpreter" }],
};
const role = "user";
const message = "I need to solve the equation `3x + 11 = 14`. Can you help me?";
const message2 = "What is 3x + 11 = 14?";

// Create an assistant
const assistantResponse = await assistantsClient.createAssistant(options);
console.log(`Assistant created: ${JSON.stringify(assistantResponse)}`);

// Create a thread
const assistantThread = await assistantsClient.createThread({});
console.log(`Thread created: ${JSON.stringify(assistantThread)}`);

// Add a user question to the thread
const threadResponse = await assistantsClient.createMessage(
assistantThread.id,
role,
message
);
console.log(`Message created: ${JSON.stringify(threadResponse)}`);

// Run the thread
let runResponse = await assistantsClient.createRun(assistantThread.id, {
assistantId: assistantResponse.id,
});
console.log(`Run created: ${JSON.stringify(runResponse)}`);

// Wait for the assistant to respond
do {
await new Promise((r) => setTimeout(r, 500));
runResponse = await assistantsClient.getRun(
assistantThread.id,
runResponse.id
);
} while (
runResponse.status === "queued" ||
runResponse.status === "in_progress"
);

// Get the messages
const runMessages = await assistantsClient.listMessages(assistantThread.id);
for (const runMessageDatum of runMessages.data) {
for (const item of runMessageDatum.content) {
if (item.type === "text") {
console.log(`Message content: ${JSON.stringify(item.text?.value)}`);
}
}
}
```
#### [TS Password](#tab/typescript-password)

Create and run an assistant with the following TypeScript module:

:::code language="typescript" source="~/azure-typescript-e2e-apps/quickstarts/azure-openai-assistants/ts/src/index-using-password.ts" :::

#### [JS Password](#tab/javascript-password)

Create and run an assistant with the following TypeScript module:

:::code language="javascript" source="~/azure-typescript-e2e-apps/quickstarts/azure-openai-assistants/js/src/index-using-password.mjs" :::

---

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

## Sample code

* [Quickstart sample code](https://github.com/Azure-Samples/azure-typescript-e2e-apps/tree/main/quickstarts/azure-openai-assistants)

## See also

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

0 comments on commit c10e1aa

Please sign in to comment.