Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix chat completion create response #372

Open
wants to merge 8 commits into
base: dev
Choose a base branch
from
14 changes: 10 additions & 4 deletions OpenAI.SDK/Managers/OpenAIChatCompletions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,22 @@ public void Process(ChatCompletionCreateResponse block)
// We're going to steal the partial message and squirrel it away for the time being.
if (!IsFnAssemblyActive && isStreamingFnCall)
{
FnCall = firstChoice.Message.FunctionCall;
firstChoice.Message.FunctionCall = null;
justStarted = true;
if (firstChoice.Message != null)
{
FnCall = firstChoice.Message.FunctionCall;
firstChoice.Message.FunctionCall = null;
justStarted = true;
}
}

// As long as we're assembling, keep on appending those args
// (Skip the first one, because it was already processed in the block above)
if (IsFnAssemblyActive && !justStarted)
{
FnCall.Arguments += ExtractArgsSoFar();
if (FnCall != null)
{
FnCall.Arguments += ExtractArgsSoFar();
}
}

// If we were assembling and it just finished, fill this block with the info we've assembled, and we're done.
Expand Down
8 changes: 4 additions & 4 deletions OpenAI.SDK/ObjectModels/RequestModels/ChatMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ public class ChatMessage
/// length of 64 characters.
/// </param>
/// <param name="functionCall">The name and arguments of a function that should be called, as generated by the model.</param>
public ChatMessage(string role, string content, string? name = null, FunctionCall? functionCall = null)
public ChatMessage(string? role = null, string? content = null, string? name = null, FunctionCall? functionCall = null)
{
Role = role;
Content = content;
Role = role ?? string.Empty;
Content = content ?? string.Empty;
Name = name;
FunctionCall = functionCall;
}
Expand All @@ -30,7 +30,7 @@ public ChatMessage(string role, string content, string? name = null, FunctionCal
/// The role of the author of this message. One of system, user, or assistant.
/// </summary>
[JsonPropertyName("role")]
public string Role { get; set; }
public string? Role { get; set; }

/// <summary>
/// The contents of the message.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ public record ChatCompletionCreateResponse : BaseResponse, IOpenAiModels.IId, IO
{
[JsonPropertyName("model")] public string Model { get; set; }

[JsonPropertyName("choices")] public List<ChatChoiceResponse> Choices { get; set; }
[JsonPropertyName("choices")] public List<ChatChoiceResponse>? Choices { get; set; }

[JsonPropertyName("usage")] public UsageResponse Usage { get; set; }
[JsonPropertyName("usage")] public UsageResponse? Usage { get; set; }

[JsonPropertyName("created")] public int CreatedAt { get; set; }

Expand Down
6 changes: 3 additions & 3 deletions OpenAI.SDK/ObjectModels/SharedModels/ChatChoiceResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ namespace OpenAI.ObjectModels.SharedModels;
public record ChatChoiceResponse
{
[JsonPropertyName("delta")]
public ChatMessage Delta
public ChatMessage? Delta
{
get => Message;
set => Message = value;
}

[JsonPropertyName("message")] public ChatMessage Message { get; set; }
[JsonPropertyName("message")] public ChatMessage? Message { get; set; }

[JsonPropertyName("index")] public int? Index { get; set; }

[JsonPropertyName("finish_reason")] public string FinishReason { get; set; }
[JsonPropertyName("finish_reason")] public string? FinishReason { get; set; }
}