|
| 1 | +# Sample using agents with OpenAPI tool in Azure.AI.Agents |
| 2 | + |
| 3 | +In this example we will demonstrate the possibility to use services with [OpenAPI Specification](https://en.wikipedia.org/wiki/OpenAPI_Specification) with the agent. We will use [wttr.in](https://wttr.in) service to get weather and its specification file [weather_openapi.json](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/ai/Azure.AI.Projects/tests/Samples/Agent/weather_openapi.json). |
| 4 | + |
| 5 | +1. First get `ProjectEndpoint` and `ModelDeploymentName` from config and create a `PersistentAgentsClient`. Also, create an `OpenApiAnonymousAuthDetails` and `OpenApiToolDefinition` from config. |
| 6 | +```C# Snippet:AgentsOpenAPICallingExample_CreateClient |
| 7 | +var projectEndpoint = configuration["ProjectEndpoint"]; |
| 8 | +var modelDeploymentName = configuration["ModelDeploymentName"]; |
| 9 | +var openApiSpec = configuration["OpenApiSpec"]; |
| 10 | +PersistentAgentsClient client = new(new Uri(projectEndpoint), new DefaultAzureCredential()); |
| 11 | + |
| 12 | +var spec = BinaryData.FromBytes(File.ReadAllBytes(openApiSpec)); |
| 13 | +OpenApiAnonymousAuthDetails openApiAnonAuth = new(); |
| 14 | +OpenApiToolDefinition openApiTool = new( |
| 15 | + name: "get_weather", |
| 16 | + description: "Retrieve weather information for a location", |
| 17 | + spec: spec, |
| 18 | + auth: openApiAnonAuth, |
| 19 | + defaultParams: ["format"] |
| 20 | +); |
| 21 | +``` |
| 22 | + |
| 23 | +2. Next we will need to create an agent. |
| 24 | + |
| 25 | +Synchronous sample: |
| 26 | +```C# Snippet:AgentsOverviewCreateAgentSync |
| 27 | +PersistentAgent agent = client.CreateAgent( |
| 28 | + model: modelDeploymentName, |
| 29 | + name: "Open API Tool Calling Agent", |
| 30 | + instructions: "You are a helpful agent.", |
| 31 | + tools: [openApiTool] |
| 32 | +); |
| 33 | +``` |
| 34 | + |
| 35 | +Asynchronous sample: |
| 36 | +```C# Snippet:AgentsOverviewCreateAgent |
| 37 | +PersistentAgent agent = await client.CreateAgentAsync( |
| 38 | + model: modelDeploymentName, |
| 39 | + name: "Open API Tool Calling Agent", |
| 40 | + instructions: "You are a helpful agent.", |
| 41 | + tools: [openApiTool] |
| 42 | +); |
| 43 | +``` |
| 44 | + |
| 45 | +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. |
| 46 | + |
| 47 | +Synchronous sample: |
| 48 | +```C# Snippet:AgentsOpenAPISyncHandlePollingWithRequiredAction |
| 49 | +PersistentAgentThread thread = client.CreateThread(); |
| 50 | +ThreadMessage message = client.CreateMessage( |
| 51 | + thread.Id, |
| 52 | + MessageRole.User, |
| 53 | + "What's the weather in Seattle?"); |
| 54 | + |
| 55 | +ThreadRun run = client.CreateRun(thread, agent); |
| 56 | + |
| 57 | +do |
| 58 | +{ |
| 59 | + Thread.Sleep(TimeSpan.FromMilliseconds(500)); |
| 60 | + run = client.GetRun(thread.Id, run.Id); |
| 61 | +} |
| 62 | +while (run.Status == RunStatus.Queued |
| 63 | + || run.Status == RunStatus.InProgress |
| 64 | + || run.Status == RunStatus.RequiresAction); |
| 65 | +``` |
| 66 | + |
| 67 | +Asynchronous sample: |
| 68 | +```C# Snippet:AgentsOpenAPIHandlePollingWithRequiredAction |
| 69 | +PersistentAgentThread thread = await client.CreateThreadAsync(); |
| 70 | +ThreadMessage message = await client.CreateMessageAsync( |
| 71 | + thread.Id, |
| 72 | + MessageRole.User, |
| 73 | + "What's the weather in Seattle?"); |
| 74 | + |
| 75 | +ThreadRun run = await client.CreateRunAsync(thread, agent); |
| 76 | + |
| 77 | +do |
| 78 | +{ |
| 79 | + await Task.Delay(TimeSpan.FromMilliseconds(500)); |
| 80 | + run = await client.GetRunAsync(thread.Id, run.Id); |
| 81 | +} |
| 82 | +while (run.Status == RunStatus.Queued |
| 83 | + || run.Status == RunStatus.InProgress |
| 84 | + || run.Status == RunStatus.RequiresAction); |
| 85 | +``` |
| 86 | + |
| 87 | +4. Print the messages to the console in chronological order. |
| 88 | + |
| 89 | +Synchronous sample: |
| 90 | +```C# Snippet:AgentsOpenAPISync_Print |
| 91 | +PageableList<ThreadMessage> messages = client.GetMessages( |
| 92 | + threadId: thread.Id, |
| 93 | + order: ListSortOrder.Ascending |
| 94 | +); |
| 95 | + |
| 96 | +foreach (ThreadMessage threadMessage in messages) |
| 97 | +{ |
| 98 | + foreach (MessageContent contentItem in threadMessage.ContentItems) |
| 99 | + { |
| 100 | + if (contentItem is MessageTextContent textItem) |
| 101 | + { |
| 102 | + Console.Write($"{threadMessage.Role}: {textItem.Text}"); |
| 103 | + } |
| 104 | + Console.WriteLine(); |
| 105 | + } |
| 106 | +} |
| 107 | +``` |
| 108 | + |
| 109 | +Asynchronous sample: |
| 110 | +```C# Snippet:AgentsOpenAPI_Print |
| 111 | +PageableList<ThreadMessage> messages = await client.GetMessagesAsync( |
| 112 | + threadId: thread.Id, |
| 113 | + order: ListSortOrder.Ascending |
| 114 | +); |
| 115 | + |
| 116 | +foreach (ThreadMessage threadMessage in messages) |
| 117 | +{ |
| 118 | + foreach (MessageContent contentItem in threadMessage.ContentItems) |
| 119 | + { |
| 120 | + if (contentItem is MessageTextContent textItem) |
| 121 | + { |
| 122 | + Console.Write($"{threadMessage.Role}: {textItem.Text}"); |
| 123 | + } |
| 124 | + Console.WriteLine(); |
| 125 | + } |
| 126 | +} |
| 127 | +``` |
| 128 | + |
| 129 | +5. Finally, we delete all the resources, we have created in this sample. |
| 130 | + |
| 131 | +Synchronous sample: |
| 132 | +```C# Snippet:AgentsOpenAPISync_Cleanup |
| 133 | +client.DeleteThread(thread.Id); |
| 134 | +client.DeleteAgent(agent.Id); |
| 135 | +``` |
| 136 | + |
| 137 | +Asynchronous sample: |
| 138 | +```C# Snippet:AgentsOpenAPI_Cleanup |
| 139 | +await client.DeleteThreadAsync(thread.Id); |
| 140 | +await client.DeleteAgentAsync(agent.Id); |
| 141 | +``` |
0 commit comments