Skip to content

Commit 27b3733

Browse files
committed
fb
1 parent 22cbd15 commit 27b3733

16 files changed

+1062
-639
lines changed

api/OpenAI.net8.0.cs

Lines changed: 89 additions & 130 deletions
Large diffs are not rendered by default.

api/OpenAI.netstandard2.0.cs

Lines changed: 83 additions & 122 deletions
Large diffs are not rendered by default.

src/Custom/Chat/ChatClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ public virtual ClientResult<ChatCompletionResult> CompleteChat(CompleteChatOptio
226226
{
227227
Argument.AssertNotNull(options, nameof(options));
228228

229-
ClientResult result = this.CompleteChat(options.Body, cancellationToken.CanBeCanceled ? new RequestOptions { CancellationToken = cancellationToken } : null);
229+
ClientResult result = this.CompleteChat(options, cancellationToken.CanBeCanceled ? new RequestOptions { CancellationToken = cancellationToken } : null);
230230
return ClientResult.FromValue((ChatCompletionResult)result.GetRawResponse().Content, result.GetRawResponse());
231231
}
232232

src/Custom/Chat/CompleteChatOptions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ public static CompleteChatOptions Create(IEnumerable<ChatMessage> messages, Chat
152152
#pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
153153
request.ResponseFormat = options.ResponseFormat switch
154154
{
155-
InternalDotNetChatResponseFormatText => new ResponseFormatText(),
156-
InternalDotNetChatResponseFormatJsonObject => new ResponseFormatJsonObject(),
155+
InternalDotNetChatResponseFormatText => new ResponseFormat(ResponseFormatType.Text),
156+
InternalDotNetChatResponseFormatJsonObject => new ResponseFormat(ResponseFormatType.JsonObject),
157157
InternalDotNetChatResponseFormatJsonSchema js => new ResponseFormatJsonSchema(new ResponseFormatJsonSchemaJsonSchema(js.JsonSchema.Name)),
158158
_ => null,
159159
};

src/Custom/Chat/Messages/AssistantChatMessage.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,50 @@ public AssistantChatMessage(ChatCompletion chatCompletion)
178178
}
179179
}
180180

181+
/// <summary>
182+
/// Creates a new instance of <see cref="AssistantChatMessage"/> from a <see cref="ChatCompletion"/> with
183+
/// an <c>assistant</c> role response.
184+
/// </summary>
185+
/// <remarks>
186+
/// This constructor will copy the <c>content</c>, <c>tool_calls</c>, and <c>function_call</c> from a chat
187+
/// completion response into a new <c>assistant</c> role request message.
188+
/// </remarks>
189+
/// <param name="chatCompletionResult">
190+
/// The <see cref="ChatCompletion"/> from which the conversation history request message should be created.
191+
/// </param>
192+
/// <exception cref="ArgumentException">
193+
/// The <c>role</c> of the provided chat completion response was not <see cref="ChatMessageRole.Assistant"/>.
194+
/// </exception>
195+
public AssistantChatMessage(ChatCompletionResult chatCompletionResult)
196+
: this(
197+
content: chatCompletionResult.Choices[0].Message.Content is null
198+
? new()
199+
: new(chatCompletionResult.Choices[0].Message.Content),
200+
role: ChatMessageRole.Assistant,
201+
patch: default,
202+
refusal: chatCompletionResult.Choices[0].Message.Refusal,
203+
participantName: null,
204+
toolCalls: null,
205+
functionCall: chatCompletionResult.Choices[0].Message.FunctionCall is null
206+
? null
207+
: new(chatCompletionResult.Choices[0].Message.FunctionCall.Name, new(chatCompletionResult.Choices[0].Message.FunctionCall.Arguments)),
208+
outputAudioReference: chatCompletionResult.Choices[0].Message.Audio is not null
209+
? new(chatCompletionResult.Choices[0].Message.Audio.Id)
210+
: null)
211+
{
212+
Argument.AssertNotNull(chatCompletionResult, nameof(chatCompletionResult));
213+
214+
if (chatCompletionResult.Choices[0].Message.Role != ChatMessageRole.Assistant)
215+
{
216+
throw new NotSupportedException($"Cannot instantiate an {nameof(AssistantChatMessage)} from a {nameof(ChatCompletion)} with role: {chatCompletionResult.Choices[0].Message.Role}.");
217+
}
218+
219+
foreach (ChatToolCall toolCall in chatCompletionResult.Choices[0].Message.ToolCalls ?? [])
220+
{
221+
ToolCalls.Add(toolCall);
222+
}
223+
}
224+
181225
// CUSTOM: Renamed.
182226
/// <summary>
183227
/// An optional <c>name</c> associated with the assistant message. This is typically defined with a <c>system</c>

src/Custom/Chat/Messages/ChatMessage.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ internal ChatMessage(ChatMessageRole role)
128128

129129
/// <inheritdoc cref="AssistantChatMessage(ChatCompletion)"/>
130130
public static AssistantChatMessage CreateAssistantMessage(ChatCompletion chatCompletion) => new(chatCompletion);
131+
public static AssistantChatMessage CreateAssistantMessage(ChatCompletionResult chatCompletionResult) => new(chatCompletionResult);
131132

132133
/// <inheritdoc cref="AssistantChatMessage(ChatOutputAudioReference)"/>
133134
[Experimental("OPENAI001")]

src/Custom/Chat/ResponseFormat.Serialization.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace OpenAI.Chat
88
{
9-
[PersistableModelProxy(typeof(UnknownResponseFormat))]
9+
[PersistableModelProxy(typeof(ResponseFormat))]
1010
public partial class ResponseFormat : IJsonModel<ResponseFormat>
1111
{
1212
internal ResponseFormat()
@@ -52,17 +52,16 @@ internal static ResponseFormat DeserializeResponseFormat(JsonElement element, Bi
5252
}
5353
if (element.TryGetProperty("type"u8, out JsonElement discriminator))
5454
{
55-
switch (discriminator.GetString())
55+
var kind = discriminator.GetString();
56+
switch (kind)
5657
{
57-
case "text":
58-
return ResponseFormatText.DeserializeResponseFormatText(element, data, options);
59-
case "json_object":
60-
return ResponseFormatJsonObject.DeserializeResponseFormatJsonObject(element, data, options);
6158
case "json_schema":
6259
return ResponseFormatJsonSchema.DeserializeResponseFormatJsonSchema(element, data, options);
60+
default:
61+
return new ResponseFormat((ResponseFormatType)Enum.Parse(typeof(ResponseFormatType), kind));
6362
}
6463
}
65-
return UnknownResponseFormat.DeserializeUnknownResponseFormat(element, data, options);
64+
return new ResponseFormat((ResponseFormatType)Enum.Parse(typeof(ResponseFormatType), "unknown"));
6665
}
6766

6867
BinaryData IPersistableModel<ResponseFormat>.Write(ModelReaderWriterOptions options) => PersistableModelWriteCore(options);

src/Custom/Chat/ResponseFormat.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
namespace OpenAI.Chat
66
{
77
[Experimental("OPENAI001")]
8-
public abstract partial class ResponseFormat
8+
public partial class ResponseFormat
99
{
1010
[Experimental("SCME0001")]
1111
private JsonPatch _patch;
1212

13-
private protected ResponseFormat(ResponseFormatType kind)
13+
internal protected ResponseFormat(ResponseFormatType kind)
1414
{
1515
Kind = kind;
1616
}

src/Custom/Chat/UnknownResponseFormat.Serialization.cs

Lines changed: 0 additions & 112 deletions
This file was deleted.

src/Custom/Chat/UnknownResponseFormat.cs

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)