You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Program.cs 3.1 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright (c) Microsoft Corporation. All rights reserved.
  2. // Program.cs
  3. using Hello;
  4. using Microsoft.AutoGen.Agents;
  5. using Microsoft.AutoGen.Contracts;
  6. using Microsoft.AutoGen.Core;
  7. // send a message to the agent
  8. var builder = WebApplication.CreateBuilder();
  9. // put these in your environment or appsettings.json
  10. builder.Configuration["HelloAIAgents:ModelType"] = "azureopenai";
  11. builder.Configuration["HelloAIAgents:LlmModelName"] = "gpt-3.5-turbo";
  12. Environment.SetEnvironmentVariable("AZURE_OPENAI_CONNECTION_STRING", "Endpoint=https://TODO.openai.azure.com/;Key=TODO;Deployment=TODO");
  13. if (Environment.GetEnvironmentVariable("AZURE_OPENAI_CONNECTION_STRING") == null)
  14. {
  15. throw new InvalidOperationException("AZURE_OPENAI_CONNECTION_STRING not set, try something like AZURE_OPENAI_CONNECTION_STRING = \"Endpoint=https://TODO.openai.azure.com/;Key=TODO;Deployment=TODO\"");
  16. }
  17. builder.Configuration["ConectionStrings:HelloAIAgents"] = Environment.GetEnvironmentVariable("AZURE_OPENAI_CONNECTION_STRING");
  18. builder.AddChatCompletionService("HelloAIAgents");
  19. var agentTypes = new AgentTypes(new Dictionary<string, Type>
  20. {
  21. { "HelloAIAgents", typeof(HelloAIAgent) }
  22. });
  23. var app = await AgentsApp.PublishMessageAsync("HelloAgents", new NewMessageReceived
  24. {
  25. Message = "World"
  26. }, builder, agentTypes, local: true);
  27. await app.WaitForShutdownAsync();
  28. namespace Hello
  29. {
  30. [TopicSubscription("agents")]
  31. public class HelloAgent(
  32. IAgentRuntime context,
  33. [FromKeyedServices("EventTypes")] EventTypes typeRegistry,
  34. IHostApplicationLifetime hostApplicationLifetime) : ConsoleAgent(
  35. context,
  36. typeRegistry),
  37. ISayHello,
  38. IHandle<NewMessageReceived>,
  39. IHandle<ConversationClosed>
  40. {
  41. public async Task Handle(NewMessageReceived item)
  42. {
  43. var response = await SayHello(item.Message).ConfigureAwait(false);
  44. var evt = new Output
  45. {
  46. Message = response
  47. };
  48. await PublishMessageAsync(evt).ConfigureAwait(false);
  49. var goodbye = new ConversationClosed
  50. {
  51. UserId = this.AgentId.Key,
  52. UserMessage = "Goodbye"
  53. };
  54. await PublishMessageAsync(goodbye).ConfigureAwait(false);
  55. }
  56. public async Task Handle(ConversationClosed item)
  57. {
  58. var goodbye = $"********************* {item.UserId} said {item.UserMessage} ************************";
  59. var evt = new Output
  60. {
  61. Message = goodbye
  62. };
  63. await PublishMessageAsync(evt).ConfigureAwait(false);
  64. //sleep30 seconds
  65. await Task.Delay(30000).ConfigureAwait(false);
  66. hostApplicationLifetime.StopApplication();
  67. }
  68. public async Task<string> SayHello(string ask)
  69. {
  70. var response = $"\n\n\n\n***************Hello {ask}**********************\n\n\n\n";
  71. return response;
  72. }
  73. }
  74. public interface ISayHello
  75. {
  76. public Task<string> SayHello(string ask);
  77. }
  78. }