Skip to content

Commit

Permalink
Remove some defunct lazy init from chat clients (#5561)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub authored Oct 24, 2024
1 parent 2616bb8 commit a5e5e8c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -480,15 +480,15 @@ private static List<ChatMessageContentItem> GetContentParts(IList<AIContent> con
switch (content)
{
case TextContent textContent:
(parts ??= []).Add(new ChatMessageTextContentItem(textContent.Text));
parts.Add(new ChatMessageTextContentItem(textContent.Text));
break;

case ImageContent imageContent when imageContent.Data is { IsEmpty: false } data:
(parts ??= []).Add(new ChatMessageImageContentItem(BinaryData.FromBytes(data), imageContent.MediaType));
parts.Add(new ChatMessageImageContentItem(BinaryData.FromBytes(data), imageContent.MediaType));
break;

case ImageContent imageContent when imageContent.Uri is string uri:
(parts ??= []).Add(new ChatMessageImageContentItem(new Uri(uri)));
parts.Add(new ChatMessageImageContentItem(new Uri(uri)));
break;
}
}
Expand Down
13 changes: 5 additions & 8 deletions src/Libraries/Microsoft.Extensions.AI.OpenAI/OpenAIChatClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -530,8 +530,6 @@ private sealed class OpenAIChatToolJson
{
AIContent? aiContent = null;

AdditionalPropertiesDictionary? additionalProperties = null;

if (contentPart.Kind == ChatMessageContentPartKind.Text)
{
aiContent = new TextContent(contentPart.Text);
Expand All @@ -546,18 +544,17 @@ private sealed class OpenAIChatToolJson

if (imageContent is not null && contentPart.ImageDetailLevel?.ToString() is string detail)
{
(additionalProperties ??= [])[nameof(contentPart.ImageDetailLevel)] = detail;
(imageContent.AdditionalProperties ??= [])[nameof(contentPart.ImageDetailLevel)] = detail;
}
}

if (aiContent is not null)
{
if (contentPart.Refusal is string refusal)
{
(additionalProperties ??= [])[nameof(contentPart.Refusal)] = refusal;
(aiContent.AdditionalProperties ??= [])[nameof(contentPart.Refusal)] = refusal;
}

aiContent.AdditionalProperties = additionalProperties;
aiContent.RawRepresentation = contentPart;
}

Expand Down Expand Up @@ -641,15 +638,15 @@ private static List<ChatMessageContentPart> GetContentParts(IList<AIContent> con
switch (content)
{
case TextContent textContent:
(parts ??= []).Add(ChatMessageContentPart.CreateTextPart(textContent.Text));
parts.Add(ChatMessageContentPart.CreateTextPart(textContent.Text));
break;

case ImageContent imageContent when imageContent.Data is { IsEmpty: false } data:
(parts ??= []).Add(ChatMessageContentPart.CreateImagePart(BinaryData.FromBytes(data), imageContent.MediaType));
parts.Add(ChatMessageContentPart.CreateImagePart(BinaryData.FromBytes(data), imageContent.MediaType));
break;

case ImageContent imageContent when imageContent.Uri is string uri:
(parts ??= []).Add(ChatMessageContentPart.CreateImagePart(new Uri(uri)));
parts.Add(ChatMessageContentPart.CreateImagePart(new Uri(uri)));
break;
}
}
Expand Down

0 comments on commit a5e5e8c

Please sign in to comment.