22// The .NET Foundation licenses this file to you under the MIT license.
33
44using System ;
5+ using System . ClientModel . Primitives ;
56using System . Collections . Generic ;
67using System . ComponentModel ;
78using System . Linq ;
89using System . Text . Json ;
10+ using System . Text . RegularExpressions ;
911using System . Threading . Tasks ;
1012using OpenAI . Assistants ;
1113using OpenAI . Chat ;
@@ -22,6 +24,75 @@ public class OpenAIConversionTests
2224 "test_function" ,
2325 "A test function for conversion" ) ;
2426
27+ [ Fact ]
28+ public void AsOpenAIChatResponseFormat_HandlesVariousFormats ( )
29+ {
30+ Assert . Null ( MicrosoftExtensionsAIChatExtensions . AsOpenAIChatResponseFormat ( null ) ) ;
31+
32+ var text = MicrosoftExtensionsAIChatExtensions . AsOpenAIChatResponseFormat ( ChatResponseFormat . Text ) ;
33+ Assert . NotNull ( text ) ;
34+ Assert . Equal ( """{"type":"text"}""" , ( ( IJsonModel < OpenAI . Chat . ChatResponseFormat > ) text ) . Write ( ModelReaderWriterOptions . Json ) . ToString ( ) ) ;
35+
36+ var json = MicrosoftExtensionsAIChatExtensions . AsOpenAIChatResponseFormat ( ChatResponseFormat . Json ) ;
37+ Assert . NotNull ( json ) ;
38+ Assert . Equal ( """{"type":"json_object"}""" , ( ( IJsonModel < OpenAI . Chat . ChatResponseFormat > ) json ) . Write ( ModelReaderWriterOptions . Json ) . ToString ( ) ) ;
39+
40+ var jsonSchema = ChatResponseFormat . ForJsonSchema ( typeof ( int ) , schemaName : "my_schema" , schemaDescription : "A test schema" ) . AsOpenAIChatResponseFormat ( ) ;
41+ Assert . NotNull ( jsonSchema ) ;
42+ Assert . Equal ( RemoveWhitespace ( """
43+ {"type":"json_schema","json_schema":{"description":"A test schema","name":"my_schema","schema":{
44+ "$schema": "https://json-schema.org/draft/2020-12/schema",
45+ "type": "integer"
46+ }}}
47+ """ ) , RemoveWhitespace ( ( ( IJsonModel < OpenAI . Chat . ChatResponseFormat > ) jsonSchema ) . Write ( ModelReaderWriterOptions . Json ) . ToString ( ) ) ) ;
48+
49+ jsonSchema = ChatResponseFormat . ForJsonSchema ( typeof ( int ) , schemaName : "my_schema" , schemaDescription : "A test schema" ) . AsOpenAIChatResponseFormat (
50+ new ( ) { AdditionalProperties = new AdditionalPropertiesDictionary { [ "strictJsonSchema" ] = true } } ) ;
51+ Assert . NotNull ( jsonSchema ) ;
52+ Assert . Equal ( RemoveWhitespace ( """
53+ {
54+ "type":"json_schema","json_schema":{"description":"A test schema","name":"my_schema","schema":{
55+ "$schema": "https://json-schema.org/draft/2020-12/schema",
56+ "type": "integer"
57+ },"strict":true}}
58+ """ ) , RemoveWhitespace ( ( ( IJsonModel < OpenAI . Chat . ChatResponseFormat > ) jsonSchema ) . Write ( ModelReaderWriterOptions . Json ) . ToString ( ) ) ) ;
59+ }
60+
61+ [ Fact ]
62+ public void AsOpenAIResponseTextFormat_HandlesVariousFormats ( )
63+ {
64+ Assert . Null ( MicrosoftExtensionsAIResponsesExtensions . AsOpenAIResponseTextFormat ( null ) ) ;
65+
66+ var text = MicrosoftExtensionsAIResponsesExtensions . AsOpenAIResponseTextFormat ( ChatResponseFormat . Text ) ;
67+ Assert . NotNull ( text ) ;
68+ Assert . Equal ( ResponseTextFormatKind . Text , text . Kind ) ;
69+
70+ var json = MicrosoftExtensionsAIResponsesExtensions . AsOpenAIResponseTextFormat ( ChatResponseFormat . Json ) ;
71+ Assert . NotNull ( json ) ;
72+ Assert . Equal ( ResponseTextFormatKind . JsonObject , json . Kind ) ;
73+
74+ var jsonSchema = ChatResponseFormat . ForJsonSchema ( typeof ( int ) , schemaName : "my_schema" , schemaDescription : "A test schema" ) . AsOpenAIResponseTextFormat ( ) ;
75+ Assert . NotNull ( jsonSchema ) ;
76+ Assert . Equal ( ResponseTextFormatKind . JsonSchema , jsonSchema . Kind ) ;
77+ Assert . Equal ( RemoveWhitespace ( """
78+ {"type":"json_schema","description":"A test schema","name":"my_schema","schema":{
79+ "$schema": "https://json-schema.org/draft/2020-12/schema",
80+ "type": "integer"
81+ }}
82+ """ ) , RemoveWhitespace ( ( ( IJsonModel < ResponseTextFormat > ) jsonSchema ) . Write ( ModelReaderWriterOptions . Json ) . ToString ( ) ) ) ;
83+
84+ jsonSchema = ChatResponseFormat . ForJsonSchema ( typeof ( int ) , schemaName : "my_schema" , schemaDescription : "A test schema" ) . AsOpenAIResponseTextFormat (
85+ new ( ) { AdditionalProperties = new AdditionalPropertiesDictionary { [ "strictJsonSchema" ] = true } } ) ;
86+ Assert . NotNull ( jsonSchema ) ;
87+ Assert . Equal ( ResponseTextFormatKind . JsonSchema , jsonSchema . Kind ) ;
88+ Assert . Equal ( RemoveWhitespace ( """
89+ {"type":"json_schema","description":"A test schema","name":"my_schema","schema":{
90+ "$schema": "https://json-schema.org/draft/2020-12/schema",
91+ "type": "integer"
92+ },"strict":true}
93+ """ ) , RemoveWhitespace ( ( ( IJsonModel < ResponseTextFormat > ) jsonSchema ) . Write ( ModelReaderWriterOptions . Json ) . ToString ( ) ) ) ;
94+ }
95+
2596 [ Fact ]
2697 public void AsOpenAIChatTool_ProducesValidInstance ( )
2798 {
@@ -1113,4 +1184,6 @@ private static async IAsyncEnumerable<T> CreateAsyncEnumerable<T>(IEnumerable<T>
11131184 yield return item ;
11141185 }
11151186 }
1187+
1188+ private static string RemoveWhitespace ( string input ) => Regex . Replace ( input , @"\s+" , "" ) ;
11161189}
0 commit comments