-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStructuredOutputsExtensions.cs
44 lines (38 loc) · 1.54 KB
/
StructuredOutputsExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using OpenAI.Chat;
namespace StructuredOutputs;
public static class StructuredOutputsExtensions
{
public static ChatResponseFormat CreateJsonSchemaFormat<T>(
string jsonSchemaFormatName,
string? jsonSchemaFormatDescription = null,
bool? jsonSchemaIsStrict = null)
{
var formatObjectType = typeof(T);
var type = formatObjectType.IsGenericType && formatObjectType.GetGenericTypeDefinition() == typeof(Nullable<>)
? Nullable.GetUnderlyingType(formatObjectType)!
: formatObjectType;
var jsonSchema = OpenAIJsonSchema.For(type);
return ChatResponseFormat.CreateJsonSchemaFormat(
jsonSchemaFormatName,
jsonSchema: BinaryData.FromString(jsonSchema),
jsonSchemaFormatDescription: jsonSchemaFormatDescription,
jsonSchemaIsStrict: jsonSchemaIsStrict
);
}
public static ParsedChatCompletion<T?> CompleteChat<T>(
this ChatClient chatClient,
List<ChatMessage> messages,
ChatCompletionOptions? options = null,
CancellationToken cancellationToken = default)
{
return chatClient.CompleteChat(messages, options, cancellationToken);
}
public static async Task<ParsedChatCompletion<T?>> CompleteChatAsync<T>(
this ChatClient chatClient,
List<ChatMessage> messages,
ChatCompletionOptions? options = null,
CancellationToken cancellationToken = default)
{
return await chatClient.CompleteChatAsync(messages, options, cancellationToken);
}
}