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.3 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 = new HostApplicationBuilder();
  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["ConnectionStrings:HelloAIAgents"] = Environment.GetEnvironmentVariable("AZURE_OPENAI_CONNECTION_STRING");
  18. builder.AddChatCompletionService("HelloAIAgents");
  19. var _ = new AgentTypes(new Dictionary<string, Type>
  20. {
  21. { "HelloAIAgents", typeof(HelloAIAgent) }
  22. });
  23. var local = true;
  24. if (Environment.GetEnvironmentVariable("AGENT_HOST") != null) { local = false; }
  25. var app = await Microsoft.AutoGen.Core.Grpc.AgentsApp.PublishMessageAsync("HelloAgents", new NewMessageReceived
  26. {
  27. Message = "World"
  28. }, local: local).ConfigureAwait(false);
  29. await app.WaitForShutdownAsync();
  30. namespace Hello
  31. {
  32. [TopicSubscription("HelloAgents")]
  33. public class HelloAgent(
  34. [FromKeyedServices("AgentsMetadata")] AgentsMetadata typeRegistry,
  35. IHostApplicationLifetime hostApplicationLifetime) : ConsoleAgent(
  36. typeRegistry),
  37. ISayHello,
  38. IHandle<NewMessageReceived>,
  39. IHandle<ConversationClosed>
  40. {
  41. public async Task Handle(NewMessageReceived item, CancellationToken cancellationToken = default)
  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, CancellationToken cancellationToken = default)
  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. }