@@ -6,58 +6,66 @@ import {
6
6
MessageStreamEvent ,
7
7
RunStreamEvent ,
8
8
ToolUtility
9
- } from " @azure/ai-projects" ;
9
+ } from ' @azure/ai-projects' ;
10
10
import type {
11
11
MessageDeltaChunk ,
12
12
MessageDeltaTextContent ,
13
13
MessageTextContentOutput
14
- } from " @azure/ai-projects" ;
15
- import { DefaultAzureCredential } from " @azure/identity" ;
14
+ } from ' @azure/ai-projects' ;
15
+ import { DefaultAzureCredential } from ' @azure/identity' ;
16
16
import dotenv from 'dotenv' ;
17
17
18
18
dotenv . config ( ) ;
19
19
20
20
// 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' ;
23
24
24
25
// 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.' ) ;
27
28
}
28
29
29
30
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 ,
33
40
) ;
34
41
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 ) ;
37
50
38
- // Step 2 an agent
39
51
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 ,
45
57
} ) ;
46
58
47
59
// Step 3 a thread
48
60
const thread = await client . agents . createThread ( ) ;
49
61
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 ,
55
66
} ) ;
56
67
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
61
69
const run = client . agents . createRun ( thread . id , agent . id ) ;
62
70
const streamEventMessages = await run . stream ( ) ;
63
71
let runId = '' ;
@@ -70,16 +78,17 @@ export async function main() {
70
78
{
71
79
const messageDelta = eventMessage . data as MessageDeltaChunk ;
72
80
messageDelta . delta . content . forEach ( ( contentPart ) => {
73
- if ( contentPart . type === " text" ) {
81
+ if ( contentPart . type === ' text' ) {
74
82
const textContent = contentPart as MessageDeltaTextContent ;
75
- const textValue = textContent . text ?. value || " No text" ;
83
+ const textValue = textContent . text ?. value || ' No text' ;
76
84
process . stdout . write ( textValue ) ;
77
85
}
78
86
} ) ;
79
87
}
80
88
break ;
81
89
82
90
case RunStreamEvent . ThreadRunCompleted :
91
+ console . log ( 'Thread run completed' ) ;
83
92
runId = ( eventMessage . data as { id : string } ) . id ;
84
93
break ;
85
94
case ErrorEvent . Error :
@@ -92,27 +101,29 @@ export async function main() {
92
101
93
102
// 6. Print the messages from the agent
94
103
const messages = await client . agents . listMessages ( thread . id ) ;
95
- console . log ( "Messages :\n----------------------------------------------" ) ;
104
+ console . log ( '\nMessages :\n----------------------------------------------' ) ;
96
105
97
106
// Messages iterate from oldest to newest
98
107
// messages[0] is the most recent
99
108
const messagesArray = messages . data ;
100
109
for ( let i = messagesArray . length - 1 ; i >= 0 ; i -- ) {
101
110
const m = messagesArray [ i ] ;
102
111
console . log ( `Type: ${ m . content [ 0 ] . type } ` ) ;
103
- if ( isOutputOfType < MessageTextContentOutput > ( m . content [ 0 ] , " text" ) ) {
112
+ if ( isOutputOfType < MessageTextContentOutput > ( m . content [ 0 ] , ' text' ) ) {
104
113
const textContent = m . content [ 0 ] as MessageTextContentOutput ;
105
114
console . log ( `Text: ${ textContent . text . value } ` ) ;
106
115
}
107
116
}
108
117
109
118
// 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
+ }
112
123
113
124
await client . agents . deleteAgent ( agent . id ) ;
114
125
}
115
126
116
127
main ( ) . catch ( ( err ) => {
117
- console . error ( " The sample encountered an error:" , err ) ;
128
+ console . error ( ' The sample encountered an error:' , err ) ;
118
129
} ) ;
0 commit comments