зеркало из https://github.com/dotnet/extensions.git
Reduce a bit of LINQ in M.E.AI (#5663)
This commit is contained in:
Родитель
655cc50105
Коммит
d70d900dfc
|
@ -111,7 +111,8 @@ public sealed class AdditionalPropertiesDictionary : IDictionary<string, object?
|
|||
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);
|
||||
|
|
|
@ -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 class ChatMessage
|
|||
[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 class ChatMessage
|
|||
public AdditionalPropertiesDictionary? AdditionalProperties { get; set; }
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override string ToString() =>
|
||||
string.Concat(Contents.OfType<TextContent>());
|
||||
public override string ToString() => Contents.ConcatText();
|
||||
}
|
||||
|
|
|
@ -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 class StreamingChatCompletionUpdate
|
|||
[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;
|
||||
}
|
||||
|
@ -116,6 +115,5 @@ public class StreamingChatCompletionUpdate
|
|||
public string? ModelId { get; set; }
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override string ToString() =>
|
||||
string.Concat(Contents.OfType<TextContent>());
|
||||
public override string ToString() => Contents.ConcatText();
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -502,7 +502,7 @@ public sealed partial class OpenTelemetryChatClient : DelegatingChatClient
|
|||
{
|
||||
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;
|
||||
|
|
Загрузка…
Ссылка в новой задаче