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

Reduce a bit of LINQ in M.E.AI #5663

Merged
merged 1 commit into from
Nov 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -111,7 +111,8 @@ public bool TryAdd(string key, object? value)
public void Clear() => _dictionary.Clear();

/// <inheritdoc />
bool ICollection<KeyValuePair<string, object?>>.Contains(KeyValuePair<string, object?> item) => _dictionary.Contains(item);
bool ICollection<KeyValuePair<string, object?>>.Contains(KeyValuePair<string, object?> item) =>
((ICollection<KeyValuePair<string, object?>>)_dictionary).Contains(item);

/// <inheritdoc />
public bool ContainsKey(string key) => _dictionary.ContainsKey(key);
Original file line number Diff line number Diff line change
@@ -3,7 +3,6 @@

using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text.Json.Serialization;
using Microsoft.Shared.Diagnostics;

@@ -60,10 +59,10 @@ public string? AuthorName
[JsonIgnore]
public string? Text
{
get => Contents.OfType<TextContent>().FirstOrDefault()?.Text;
get => Contents.FindFirst<TextContent>()?.Text;
set
{
if (Contents.OfType<TextContent>().FirstOrDefault() is { } textContent)
if (Contents.FindFirst<TextContent>() is { } textContent)
{
textContent.Text = value;
}
@@ -95,6 +94,5 @@ public IList<AIContent> Contents
public AdditionalPropertiesDictionary? AdditionalProperties { get; set; }

/// <inheritdoc/>
public override string ToString() =>
string.Concat(Contents.OfType<TextContent>());
public override string ToString() => Contents.ConcatText();
}
Original file line number Diff line number Diff line change
@@ -4,7 +4,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text.Json.Serialization;

namespace Microsoft.Extensions.AI;
@@ -66,10 +65,10 @@ public string? AuthorName
[JsonIgnore]
public string? Text
{
get => Contents.OfType<TextContent>().FirstOrDefault()?.Text;
stephentoub marked this conversation as resolved.
Show resolved Hide resolved
get => Contents.FindFirst<TextContent>()?.Text;
set
{
if (Contents.OfType<TextContent>().FirstOrDefault() is { } textContent)
if (Contents.FindFirst<TextContent>() is { } textContent)
{
textContent.Text = value;
}
@@ -116,6 +115,5 @@ public IList<AIContent> Contents
public string? ModelId { get; set; }

/// <inheritdoc/>
public override string ToString() =>
string.Concat(Contents.OfType<TextContent>());
public override string ToString() => Contents.ConcatText();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
#if !NET
using System.Linq;
#else
using System.Runtime.CompilerServices;
#endif

namespace Microsoft.Extensions.AI;

/// <summary>Internal extensions for working with <see cref="AIContent"/>.</summary>
internal static class AIContentExtensions
{
/// <summary>Finds the first occurrence of a <typeparamref name="T"/> in the list.</summary>
public static T? FindFirst<T>(this IList<AIContent> contents)
where T : AIContent
{
int count = contents.Count;
for (int i = 0; i < count; i++)
{
if (contents[i] is T t)
{
return t;
}
}

return null;
}

/// <summary>Concatenates the text of all <see cref="TextContent"/> instances in the list.</summary>
public static string ConcatText(this IList<AIContent> contents)
{
int count = contents.Count;
switch (count)
{
case 0:
break;

case 1:
return contents[0] is TextContent tc ? tc.Text : string.Empty;

default:
#if NET
DefaultInterpolatedStringHandler builder = new(0, 0, null, stackalloc char[512]);
for (int i = 0; i < count; i++)
{
if (contents[i] is TextContent text)
{
builder.AppendLiteral(text.Text);
}
}

return builder.ToStringAndClear();
#else
return string.Concat(contents.OfType<TextContent>());
#endif
}

return string.Empty;
}
}
Original file line number Diff line number Diff line change
@@ -502,7 +502,7 @@ private AssistantEvent CreateAssistantEvent(ChatMessage message)
{
if (EnableSensitiveData)
{
string content = string.Concat(message.Contents.OfType<TextContent>().Select(c => c.Text));
string content = string.Concat(message.Contents.OfType<TextContent>());
if (content.Length > 0)
{
return content;
Loading
Oops, something went wrong.