@@ -51,147 +51,81 @@ In this example we will demonstrate the possibility to use services with [OpenAP
51
51
1 . First get ` ProjectEndpoint ` and ` ModelDeploymentName ` from config and create a ` PersistentAgentsClient ` . Also, create an ` OpenApiAnonymousAuthDetails ` and ` OpenApiToolDefinition ` from config.
52
52
53
53
``` csharp
54
- var projectEndpoint = configuration [" ProjectEndpoint" ];
55
- var modelDeploymentName = configuration [" ModelDeploymentName" ];
56
- var openApiSpec = configuration [" OpenApiSpec" ];
57
- PersistentAgentsClient client = new (new Uri (projectEndpoint ), new DefaultAzureCredential ());
58
-
59
- var spec = BinaryData .FromBytes (File .ReadAllBytes (openApiSpec ));
60
- OpenApiAnonymousAuthDetails openApiAnonAuth = new ();
61
- OpenApiToolDefinition openApiTool = new (
62
- name : " get_weather" ,
63
- description : " Retrieve weather information for a location" ,
64
- spec : spec ,
65
- auth : openApiAnonAuth ,
66
- defaultParams : [" format" ]
67
- );
54
+ var projectEndpoint = configuration [" ProjectEndpoint" ];
55
+ var modelDeploymentName = configuration [" ModelDeploymentName" ];
56
+ var openApiSpec = configuration [" OpenApiSpec" ];
57
+ PersistentAgentsClient client = new (new Uri (projectEndpoint ), new DefaultAzureCredential ());
58
+
59
+ var spec = BinaryData .FromBytes (File .ReadAllBytes (openApiSpec ));
60
+ OpenApiAnonymousAuthDetails openApiAnonAuth = new ();
61
+ OpenApiToolDefinition openApiTool = new (
62
+ name : " get_weather" ,
63
+ description : " Retrieve weather information for a location" ,
64
+ spec : spec ,
65
+ auth : openApiAnonAuth ,
66
+ defaultParams : [" format" ]
67
+ );
68
68
```
69
69
70
70
2 . Next we will need to create an agent.
71
71
72
- Synchronous sample:
73
-
74
- ``` csharp
75
- PersistentAgent agent = client .CreateAgent (
76
- model : modelDeploymentName ,
77
- name : " Open API Tool Calling Agent" ,
78
- instructions : " You are a helpful agent." ,
79
- tools : [openApiTool ]
80
- );
81
- ```
82
-
83
- Asynchronous sample:
84
-
85
72
``` csharp
86
- PersistentAgent agent = await client .CreateAgentAsync (
87
- model : modelDeploymentName ,
88
- name : " Open API Tool Calling Agent" ,
89
- instructions : " You are a helpful agent." ,
90
- tools : [openApiTool ]
91
- );
73
+ PersistentAgent agent = client .CreateAgent (
74
+ model : modelDeploymentName ,
75
+ name : " Open API Tool Calling Agent" ,
76
+ instructions : " You are a helpful agent." ,
77
+ tools : [openApiTool ]
78
+ );
92
79
```
93
80
94
81
3 . Now we will create a ` ThreadRun ` and wait until it is complete. If the run will not be successful, we will print the last error.
95
82
96
- Synchronous sample:
97
- ``` csharp
98
- PersistentAgentThread thread = client .CreateThread ();
99
- ThreadMessage message = client .CreateMessage (
100
- thread .Id ,
101
- MessageRole .User ,
102
- " What's the weather in Seattle?" );
103
-
104
- ThreadRun run = client .CreateRun (thread , agent );
105
-
106
- do
107
- {
108
- Thread .Sleep (TimeSpan .FromMilliseconds (500 ));
109
- run = client .GetRun (thread .Id , run .Id );
110
- }
111
- while (run .Status == RunStatus .Queued
112
- || run .Status == RunStatus .InProgress
113
- || run .Status == RunStatus .RequiresAction );
114
- ```
115
-
116
- Asynchronous sample:
117
-
118
- ``` csharp
119
- PersistentAgentThread thread = await client .CreateThreadAsync ();
120
- ThreadMessage message = await client .CreateMessageAsync (
121
- thread .Id ,
122
- MessageRole .User ,
123
- " What's the weather in Seattle?" );
124
-
125
- ThreadRun run = await client .CreateRunAsync (thread , agent );
126
-
127
- do
128
- {
129
- await Task .Delay (TimeSpan .FromMilliseconds (500 ));
130
- run = await client .GetRunAsync (thread .Id , run .Id );
131
- }
132
- while (run .Status == RunStatus .Queued
133
- || run .Status == RunStatus .InProgress
134
- || run .Status == RunStatus .RequiresAction );
135
- ```
136
-
137
- 4 . Print the messages to the console in chronological order.
138
-
139
- Synchronous sample:
140
83
141
84
``` csharp
142
- PageableList < ThreadMessage > messages = client .GetMessages (
143
- threadId : thread .Id ,
144
- order : ListSortOrder .Ascending
145
- );
146
-
147
- foreach (ThreadMessage threadMessage in messages )
148
- {
149
- foreach (MessageContent contentItem in threadMessage .ContentItems )
85
+ PersistentAgentThread thread = client .CreateThread ();
86
+ ThreadMessage message = client .CreateMessage (
87
+ thread .Id ,
88
+ MessageRole .User ,
89
+ " What's the weather in Seattle?" );
90
+
91
+ ThreadRun run = client .CreateRun (thread , agent );
92
+
93
+ do
150
94
{
151
- if (contentItem is MessageTextContent textItem )
152
- {
153
- Console .Write ($" {threadMessage .Role }: {textItem .Text }" );
154
- }
155
- Console .WriteLine ();
95
+ Thread .Sleep (TimeSpan .FromMilliseconds (500 ));
96
+ run = client .GetRun (thread .Id , run .Id );
156
97
}
157
- }
98
+ while (run .Status == RunStatus .Queued
99
+ || run .Status == RunStatus .InProgress
100
+ || run .Status == RunStatus .RequiresAction );
158
101
```
159
102
160
- Asynchronous sample:
103
+ 4 . Print the messages to the console in chronological order.
161
104
162
105
``` csharp
163
- PageableList < ThreadMessage > messages = await client .GetMessagesAsync (
164
- threadId : thread .Id ,
165
- order : ListSortOrder .Ascending
166
- );
167
-
168
- foreach (ThreadMessage threadMessage in messages )
169
- {
170
- foreach (MessageContent contentItem in threadMessage .ContentItems )
106
+ Pageable < ThreadMessage > messages = client .Messages .GetMessages (
107
+ threadId : thread .Id ,
108
+ order : ListSortOrder .Ascending );
109
+
110
+ foreach (ThreadMessage threadMessage in messages )
171
111
{
172
- if ( contentItem is MessageTextContent textItem )
112
+ foreach ( MessageContent content in threadMessage . ContentItems )
173
113
{
174
- Console .Write ($" {threadMessage .Role }: {textItem .Text }" );
114
+ switch (content )
115
+ {
116
+ case MessageTextContent textItem :
117
+ Console .WriteLine ($" [{threadMessage .Role }]: {textItem .Text }" );
118
+ break ;
119
+ }
175
120
}
176
- Console .WriteLine ();
177
121
}
178
- }
179
122
```
180
123
181
124
5 . Finally, we delete all the resources, we have created in this sample.
182
125
183
- Synchronous sample:
184
-
185
- ``` csharp
186
- client .DeleteThread (thread .Id );
187
- client .DeleteAgent (agent .Id );
188
- ```
189
-
190
- Asynchronous sample:
191
-
192
126
``` csharp
193
- await client .DeleteThreadAsync (thread .Id );
194
- await client .DeleteAgentAsync (agent .Id );
127
+ client .DeleteThread (thread .Id );
128
+ client .DeleteAgent (agent .Id );
195
129
```
196
130
197
131
::: zone-end
0 commit comments