|
| 1 | +using Azure; |
| 2 | +using Azure.AI.Agents.Persistent; |
| 3 | +using Azure.Identity; |
| 4 | +using Microsoft.Extensions.Configuration; |
| 5 | + |
| 6 | +IConfigurationRoot configuration = new ConfigurationBuilder() |
| 7 | + .SetBasePath(AppContext.BaseDirectory) |
| 8 | + .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) |
| 9 | + .Build(); |
| 10 | + |
| 11 | +var projectEndpoint = configuration["ProjectEndpoint"]; |
| 12 | +var modelDeploymentName = configuration["ModelDeploymentName"]; |
| 13 | +PersistentAgentsClient client = new(projectEndpoint, new DefaultAzureCredential()); |
| 14 | + |
| 15 | +PersistentAgent agent = await client.Administration.CreateAgentAsync( |
| 16 | + model: modelDeploymentName, |
| 17 | + name: "Math Tutor", |
| 18 | + instructions: "You are a personal math tutor. Write and run code to answer math questions." |
| 19 | +); |
| 20 | + |
| 21 | +PersistentAgentThread thread = await client.Threads.CreateThreadAsync(); |
| 22 | + |
| 23 | +client.Messages.CreateMessage( |
| 24 | + thread.Id, |
| 25 | + MessageRole.User, |
| 26 | + "I need to solve the equation `3x + 11 = 14`. Can you help me?"); |
| 27 | + |
| 28 | +ThreadRun run = client.Runs.CreateRun( |
| 29 | + thread.Id, |
| 30 | + agent.Id, |
| 31 | + additionalInstructions: "Please address the user as Jane Doe. The user has a premium account."); |
| 32 | + |
| 33 | +do |
| 34 | +{ |
| 35 | + Thread.Sleep(TimeSpan.FromMilliseconds(500)); |
| 36 | + run = client.Runs.GetRun(thread.Id, run.Id); |
| 37 | +} |
| 38 | +while (run.Status == RunStatus.Queued |
| 39 | + || run.Status == RunStatus.InProgress |
| 40 | + || run.Status == RunStatus.RequiresAction); |
| 41 | + |
| 42 | +Pageable<ThreadMessage> messages = client.Messages.GetMessages( |
| 43 | + threadId: thread.Id, |
| 44 | + order: ListSortOrder.Ascending); |
| 45 | + |
| 46 | +foreach (ThreadMessage threadMessage in messages) |
| 47 | +{ |
| 48 | + foreach (MessageContent contentItem in threadMessage.ContentItems) |
| 49 | + { |
| 50 | + if (contentItem is MessageTextContent textItem) |
| 51 | + { |
| 52 | + Console.WriteLine($"[{threadMessage.Role}]: {textItem.Text}"); |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +client.Threads.DeleteThread(threadId: thread.Id); |
| 58 | +client.Administration.DeleteAgent(agentId: agent.Id); |
0 commit comments