Skip to content

Commit a39312b

Browse files
committed
Added Azure AI Search tool integration
1 parent c3355ef commit a39312b

File tree

1 file changed

+45
-34
lines changed

1 file changed

+45
-34
lines changed

src/index.ts

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,58 +6,66 @@ import {
66
MessageStreamEvent,
77
RunStreamEvent,
88
ToolUtility
9-
} from "@azure/ai-projects";
9+
} from '@azure/ai-projects';
1010
import type {
1111
MessageDeltaChunk,
1212
MessageDeltaTextContent,
1313
MessageTextContentOutput
14-
} from "@azure/ai-projects";
15-
import { DefaultAzureCredential } from "@azure/identity";
14+
} from '@azure/ai-projects';
15+
import { DefaultAzureCredential } from '@azure/identity';
1616
import dotenv from 'dotenv';
1717

1818
dotenv.config();
1919

2020
// Set the connection string from the environment variable
21-
const connectionString = process.env.PROJECT_CONNECTION_STRING;
22-
const model = "gpt-4o";
21+
const aiFoundryConnectionString = process.env.AI_FOUNDRY_PROJECT_CONNECTION_STRING;
22+
const aiSearchConnectionString = process.env.AI_SEARCH_CONNECTION_STRING;
23+
const model = 'gpt-4o';
2324

2425
// Throw an error if the connection string is not set
25-
if (!connectionString) {
26-
throw new Error("Please set the PROJECT_CONNECTION_STRING environment variable.");
26+
if (!aiFoundryConnectionString && !aiSearchConnectionString) {
27+
throw new Error('Please set the AI_FOUNDRY_PROJECT_CONNECTION_STRING and AI_SEARCH_CONNECTION_STRING environment variable.');
2728
}
2829

2930
export async function main() {
30-
const client = AIProjectsClient.fromConnectionString(
31-
connectionString || "",
32-
new DefaultAzureCredential(),
31+
const client = AIProjectsClient.fromConnectionString(aiFoundryConnectionString || '', new DefaultAzureCredential());
32+
33+
// Step 1 - Add tools
34+
const codeInterpreterTool = ToolUtility.createCodeInterpreterTool([]);;
35+
const aiSearchConnection = await client.connections.getConnection(aiSearchConnectionString || '');
36+
37+
const azureAISearchTool = ToolUtility.createAzureAISearchTool(
38+
aiSearchConnection.id,
39+
aiSearchConnection.name,
3340
);
3441

35-
// Step 1 code interpreter tool
36-
const codeInterpreterTool = ToolUtility.createCodeInterpreterTool([]);
42+
// Step 2 Create an agent
43+
// A few prompts to try
44+
const prompts = [
45+
'I need to solve the equation `3x + 11 = 14`. Can you help me?',
46+
'What are my health insurance plan coverage types?',
47+
];
48+
const prompt = prompts[0];
49+
console.log('Prompt:', prompt);
3750

38-
// Step 2 an agent
3951
const agent = await client.agents.createAgent(model, {
40-
name: "my-agent",
41-
instructions: "You are a helpful agent",
42-
temperature: 0,
43-
tools: [codeInterpreterTool.definition],
44-
toolResources: codeInterpreterTool.resources,
52+
name: 'my-agent',
53+
instructions: 'You are a helpful agent',
54+
temperature: 0.5,
55+
tools: [azureAISearchTool.definition],
56+
toolResources: azureAISearchTool.resources,
4557
});
4658

4759
// Step 3 a thread
4860
const thread = await client.agents.createThread();
4961

50-
// Step 4 a message to thread
51-
await client.agents.createMessage(
52-
thread.id, {
53-
role: "user",
54-
content: "I need to solve the equation `3x + 11 = 14`. Can you help me?",
62+
// Step 4 Add a user message to the thread
63+
await client.agents.createMessage(thread.id, {
64+
role: 'user',
65+
content: prompt,
5566
});
5667

57-
// Intermission is now correlated with thread
58-
// Intermission messages will retrieve the message just added
59-
60-
// Step 5 the agent
68+
// Step 5 Run the agent
6169
const run = client.agents.createRun(thread.id, agent.id);
6270
const streamEventMessages = await run.stream();
6371
let runId = '';
@@ -70,16 +78,17 @@ export async function main() {
7078
{
7179
const messageDelta = eventMessage.data as MessageDeltaChunk;
7280
messageDelta.delta.content.forEach((contentPart) => {
73-
if (contentPart.type === "text") {
81+
if (contentPart.type === 'text') {
7482
const textContent = contentPart as MessageDeltaTextContent;
75-
const textValue = textContent.text?.value || "No text";
83+
const textValue = textContent.text?.value || 'No text';
7684
process.stdout.write(textValue);
7785
}
7886
});
7987
}
8088
break;
8189

8290
case RunStreamEvent.ThreadRunCompleted:
91+
console.log('Thread run completed');
8392
runId = (eventMessage.data as { id: string }).id;
8493
break;
8594
case ErrorEvent.Error:
@@ -92,27 +101,29 @@ export async function main() {
92101

93102
// 6. Print the messages from the agent
94103
const messages = await client.agents.listMessages(thread.id);
95-
console.log("Messages:\n----------------------------------------------");
104+
console.log('\nMessages:\n----------------------------------------------');
96105

97106
// Messages iterate from oldest to newest
98107
// messages[0] is the most recent
99108
const messagesArray = messages.data;
100109
for (let i = messagesArray.length - 1; i >= 0; i--) {
101110
const m = messagesArray[i];
102111
console.log(`Type: ${m.content[0].type}`);
103-
if (isOutputOfType<MessageTextContentOutput>(m.content[0], "text")) {
112+
if (isOutputOfType<MessageTextContentOutput>(m.content[0], 'text')) {
104113
const textContent = m.content[0] as MessageTextContentOutput;
105114
console.log(`Text: ${textContent.text.value}`);
106115
}
107116
}
108117

109118
// 7. Write out stats and delete the agent once done
110-
const completedRun = await client.agents.getRun(thread.id, runId);
111-
console.log("Token usage:", completedRun.usage);
119+
if (runId) {
120+
const completedRun = await client.agents.getRun(thread.id, runId);
121+
console.log('Token usage:', completedRun.usage);
122+
}
112123

113124
await client.agents.deleteAgent(agent.id);
114125
}
115126

116127
main().catch((err) => {
117-
console.error("The sample encountered an error:", err);
128+
console.error('The sample encountered an error:', err);
118129
});

0 commit comments

Comments
 (0)