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.

Structural_Output.cs 2.9 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright (c) Microsoft Corporation. All rights reserved.
  2. // Structural_Output.cs
  3. using System.Text.Json;
  4. using System.Text.Json.Serialization;
  5. using AutoGen.Core;
  6. using AutoGen.OpenAI.Extension;
  7. using FluentAssertions;
  8. using Json.Schema;
  9. using Json.Schema.Generation;
  10. using OpenAI;
  11. namespace AutoGen.OpenAI.Sample;
  12. public class Structural_Output
  13. {
  14. public static async Task RunAsync()
  15. {
  16. #region create_agent
  17. var apiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY") ?? throw new Exception("Please set OPENAI_API_KEY environment variable.");
  18. var model = "gpt-4o-mini";
  19. var schemaBuilder = new JsonSchemaBuilder().FromType<Person>();
  20. var schema = schemaBuilder.Build();
  21. var openAIClient = new OpenAIClient(apiKey);
  22. var openAIClientAgent = new OpenAIChatAgent(
  23. chatClient: openAIClient.GetChatClient(model),
  24. name: "assistant",
  25. systemMessage: "You are a helpful assistant")
  26. .RegisterMessageConnector()
  27. .RegisterPrintMessage();
  28. #endregion create_agent
  29. #region chat_with_agent
  30. var prompt = new TextMessage(Role.User, """
  31. My name is John, I am 25 years old, and I live in Seattle. I like to play soccer and read books.
  32. """);
  33. var reply = await openAIClientAgent.GenerateReplyAsync(
  34. messages: [prompt],
  35. options: new GenerateReplyOptions
  36. {
  37. OutputSchema = schema,
  38. });
  39. var person = JsonSerializer.Deserialize<Person>(reply.GetContent());
  40. Console.WriteLine($"Name: {person.Name}");
  41. Console.WriteLine($"Age: {person.Age}");
  42. if (!string.IsNullOrEmpty(person.Address))
  43. {
  44. Console.WriteLine($"Address: {person.Address}");
  45. }
  46. Console.WriteLine("Done.");
  47. #endregion chat_with_agent
  48. person.Name.Should().Be("John");
  49. person.Age.Should().Be(25);
  50. person.Address.Should().BeNullOrEmpty();
  51. person.City.Should().Be("Seattle");
  52. person.Hobbies.Count.Should().Be(2);
  53. }
  54. #region person_class
  55. [Title("Person")]
  56. public class Person
  57. {
  58. [JsonPropertyName("name")]
  59. [Description("Name of the person")]
  60. [Required]
  61. public string Name { get; set; }
  62. [JsonPropertyName("age")]
  63. [Description("Age of the person")]
  64. [Required]
  65. public int Age { get; set; }
  66. [JsonPropertyName("city")]
  67. [Description("City of the person")]
  68. public string? City { get; set; }
  69. [JsonPropertyName("address")]
  70. [Description("Address of the person")]
  71. public string? Address { get; set; }
  72. [JsonPropertyName("hobbies")]
  73. [Description("Hobbies of the person")]
  74. public List<string>? Hobbies { get; set; }
  75. }
  76. #endregion person_class
  77. }