@@ -11,6 +11,14 @@ namespace Betalgo.Ranul.OpenAI.Managers;
11
11
12
12
public partial class OpenAIService : IChatClient
13
13
{
14
+ private static readonly AIJsonSchemaTransformCache s_schemaTransformCache = new ( new ( )
15
+ {
16
+ // https://platform.openai.com/docs/guides/structured-outputs?api-mode=responses#supported-schemas
17
+ DisallowAdditionalProperties = true ,
18
+ RequireAllProperties = true ,
19
+ MoveDefaultKeywordToDescription = true ,
20
+ } ) ;
21
+
14
22
private ChatClientMetadata ? _chatMetadata ;
15
23
16
24
/// <inheritdoc />
@@ -43,7 +51,8 @@ async Task<ChatResponse> IChatClient.GetResponseAsync(IEnumerable<ChatMessage> m
43
51
{
44
52
Role = new ( choice . Message . Role ) ,
45
53
AuthorName = choice . Message . Name ,
46
- RawRepresentation = choice
54
+ RawRepresentation = choice ,
55
+ MessageId = response . Id
47
56
} ;
48
57
49
58
PopulateContents ( choice . Message , m . Contents ) ;
@@ -91,6 +100,7 @@ async IAsyncEnumerable<ChatResponseUpdate> IChatClient.GetStreamingResponseAsync
91
100
ModelId = response . Model ,
92
101
RawRepresentation = response ,
93
102
ResponseId = response . Id ,
103
+ MessageId = response . Id ,
94
104
Role = choice . Delta . Role is not null ? new ( choice . Delta . Role ) : null
95
105
} ;
96
106
@@ -118,6 +128,7 @@ async IAsyncEnumerable<ChatResponseUpdate> IChatClient.GetStreamingResponseAsync
118
128
FinishReason = choice . FinishReason is not null ? new ( choice . FinishReason ) : null ,
119
129
ModelId = response . Model ,
120
130
ResponseId = response . Id ,
131
+ MessageId = response . Id ,
121
132
Role = choice . Delta . Role is not null ? new ( choice . Delta . Role ) : null
122
133
} ;
123
134
}
@@ -150,12 +161,12 @@ private ChatCompletionCreateRequest CreateRequest(IEnumerable<ChatMessage> chatM
150
161
request . PresencePenalty = options . PresencePenalty ;
151
162
request . Seed = ( int ? ) options . Seed ;
152
163
request . StopAsList = options . StopSequences ;
164
+ request . ParallelToolCalls = options . AllowMultipleToolCalls ;
153
165
154
166
// Non-strongly-typed properties from additional properties
155
167
request . LogitBias = options . AdditionalProperties ? . TryGetValue ( nameof ( request . LogitBias ) , out var logitBias ) is true ? logitBias : null ;
156
168
request . LogProbs = options . AdditionalProperties ? . TryGetValue ( nameof ( request . LogProbs ) , out bool logProbs ) is true ? logProbs : null ;
157
169
request . N = options . AdditionalProperties ? . TryGetValue ( nameof ( request . N ) , out int n ) is true ? n : null ;
158
- request . ParallelToolCalls = options . AdditionalProperties ? . TryGetValue ( nameof ( request . ParallelToolCalls ) , out bool parallelToolCalls ) is true ? parallelToolCalls : null ;
159
170
request . ServiceTier = options . AdditionalProperties ? . TryGetValue ( nameof ( request . ServiceTier ) , out string ? serviceTier ) is true ? serviceTier : null ! ;
160
171
request . User = options . AdditionalProperties ? . TryGetValue ( nameof ( request . User ) , out string ? user ) is true ? user : null ! ;
161
172
request . TopLogprobs = options . AdditionalProperties ? . TryGetValue ( nameof ( request . TopLogprobs ) , out int topLogprobs ) is true ? topLogprobs : null ;
@@ -309,7 +320,8 @@ private ChatCompletionCreateRequest CreateRequest(IEnumerable<ChatMessage> chatM
309
320
310
321
private static PropertyDefinition CreateParameters ( AIFunction f )
311
322
{
312
- return JsonSerializer . Deserialize < PropertyDefinition > ( f . JsonSchema ) ?? new ( ) ;
323
+ JsonElement openAISchema = s_schemaTransformCache . GetOrCreateTransformedSchema ( f ) ;
324
+ return JsonSerializer . Deserialize < PropertyDefinition > ( openAISchema ) ?? new ( ) ;
313
325
}
314
326
315
327
private static void PopulateContents ( ObjectModels . RequestModels . ChatMessage source , IList < AIContent > destination )
0 commit comments