9
9
using Json . Schema ;
10
10
using Json . Schema . Generation ;
11
11
using OpenAI ;
12
- using OpenAI . Chat ;
13
12
14
13
namespace AutoGen . OpenAI . Sample ;
15
14
16
- internal class Structural_Output
15
+ public class Structural_Output
17
16
{
18
17
public static async Task RunAsync ( )
19
18
{
@@ -23,24 +22,25 @@ public static async Task RunAsync()
23
22
24
23
var schemaBuilder = new JsonSchemaBuilder ( ) . FromType < Person > ( ) ;
25
24
var schema = schemaBuilder . Build ( ) ;
26
-
27
- var personSchemaFormat = ChatResponseFormat . CreateJsonSchemaFormat (
28
- name : "Person" ,
29
- jsonSchema : BinaryData . FromObjectAsJson ( schema ) ,
30
- description : "Person schema" ) ;
31
-
32
25
var openAIClient = new OpenAIClient ( apiKey ) ;
33
26
var openAIClientAgent = new OpenAIChatAgent (
34
27
chatClient : openAIClient . GetChatClient ( model ) ,
35
28
name : "assistant" ,
36
- systemMessage : "You are a helpful assistant" ,
37
- responseFormat : personSchemaFormat ) // structural output by passing schema to response format
29
+ systemMessage : "You are a helpful assistant" )
38
30
. RegisterMessageConnector ( )
39
31
. RegisterPrintMessage ( ) ;
40
32
#endregion create_agent
41
33
42
34
#region chat_with_agent
43
- var reply = await openAIClientAgent . SendAsync ( "My name is John, I am 25 years old, and I live in Seattle. I like to play soccer and read books." ) ;
35
+ var prompt = new TextMessage ( Role . User , """
36
+ My name is John, I am 25 years old, and I live in Seattle. I like to play soccer and read books.
37
+ """ ) ;
38
+ var reply = await openAIClientAgent . GenerateReplyAsync (
39
+ messages : [ prompt ] ,
40
+ options : new GenerateReplyOptions
41
+ {
42
+ OutputSchema = schema ,
43
+ } ) ;
44
44
45
45
var person = JsonSerializer . Deserialize < Person > ( reply . GetContent ( ) ) ;
46
46
Console . WriteLine ( $ "Name: { person . Name } ") ;
@@ -60,31 +60,34 @@ public static async Task RunAsync()
60
60
person . City . Should ( ) . Be ( "Seattle" ) ;
61
61
person . Hobbies . Count . Should ( ) . Be ( 2 ) ;
62
62
}
63
- }
64
63
65
- #region person_class
66
- public class Person
67
- {
68
- [ JsonPropertyName ( "name" ) ]
69
- [ Description ( "Name of the person" ) ]
70
- [ Required ]
71
- public string Name { get ; set ; }
72
-
73
- [ JsonPropertyName ( "age" ) ]
74
- [ Description ( "Age of the person" ) ]
75
- [ Required ]
76
- public int Age { get ; set ; }
77
-
78
- [ JsonPropertyName ( "city" ) ]
79
- [ Description ( "City of the person" ) ]
80
- public string ? City { get ; set ; }
81
-
82
- [ JsonPropertyName ( "address" ) ]
83
- [ Description ( "Address of the person" ) ]
84
- public string ? Address { get ; set ; }
85
-
86
- [ JsonPropertyName ( "hobbies" ) ]
87
- [ Description ( "Hobbies of the person" ) ]
88
- public List < string > ? Hobbies { get ; set ; }
64
+
65
+ #region person_class
66
+ [ Title ( "Person" ) ]
67
+ public class Person
68
+ {
69
+ [ JsonPropertyName ( "name" ) ]
70
+ [ Description ( "Name of the person" ) ]
71
+ [ Required ]
72
+ public string Name { get ; set ; }
73
+
74
+ [ JsonPropertyName ( "age" ) ]
75
+ [ Description ( "Age of the person" ) ]
76
+ [ Required ]
77
+ public int Age { get ; set ; }
78
+
79
+ [ JsonPropertyName ( "city" ) ]
80
+ [ Description ( "City of the person" ) ]
81
+ public string ? City { get ; set ; }
82
+
83
+ [ JsonPropertyName ( "address" ) ]
84
+ [ Description ( "Address of the person" ) ]
85
+ public string ? Address { get ; set ; }
86
+
87
+ [ JsonPropertyName ( "hobbies" ) ]
88
+ [ Description ( "Hobbies of the person" ) ]
89
+ public List < string > ? Hobbies { get ; set ; }
90
+ }
91
+ #endregion person_class
92
+
89
93
}
90
- #endregion person_class
0 commit comments