Skip to content

Commit

Permalink
use explicit/aliases namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
jochenkirstaetter committed Nov 26, 2024
1 parent 0b05496 commit fa0a203
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 57 deletions.
24 changes: 12 additions & 12 deletions src/Mscc.GenerativeAI.Microsoft/GeminiChatClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@
using System.Threading;
using System.Threading.Tasks;
#endif
using Microsoft.Extensions.AI;
using mea = Microsoft.Extensions.AI;
using System.Runtime.CompilerServices;

namespace Mscc.GenerativeAI.Microsoft;

public sealed class GeminiChatClient : IChatClient
public sealed class GeminiChatClient : mea.IChatClient
{
private const string providerName = "gemini";
private const string ProviderName = "gemini";

/// <summary>
/// Gets the Gemini model that is used to communicate with.
/// </summary>
private readonly GenerativeModel _client;

/// <inheritdoc/>
public ChatClientMetadata Metadata { get; }
public mea.ChatClientMetadata Metadata { get; }

/// <summary>
/// Creates an instance of the Gemini API client.
Expand All @@ -30,27 +30,27 @@ public GeminiChatClient(string apiKey, string model = "")
{
var genAi = new GoogleAI(apiKey);
_client = genAi.GenerativeModel(model);
Metadata = new(providerName, null, model);
Metadata = new(ProviderName, null, model);
}

/// <inheritdoc/>
public async Task<ChatCompletion> CompleteAsync(
IList<ChatMessage> chatMessages,
ChatOptions? options = null,
public async Task<mea.ChatCompletion> CompleteAsync(
IList<mea.ChatMessage> chatMessages,
mea.ChatOptions? options = null,
CancellationToken cancellationToken = default)
{
if (chatMessages == null) throw new ArgumentNullException(nameof(chatMessages));

var request = MicrosoftAi.AbstractionMapper.ToGeminiGenerateContentRequest(chatMessages, options);
var requestOptions = MicrosoftAi.AbstractionMapper.ToGeminiGenerateContentRequestOptions(options);
var response = await _client.GenerateContent(request, requestOptions);
return MicrosoftAi.AbstractionMapper.ToChatCompletion(response) ?? new ChatCompletion([]);
return MicrosoftAi.AbstractionMapper.ToChatCompletion(response) ?? new mea.ChatCompletion([]);
}

/// <inheritdoc/>
public async IAsyncEnumerable<StreamingChatCompletionUpdate> CompleteStreamingAsync(
IList<ChatMessage> chatMessages,
ChatOptions? options = null,
public async IAsyncEnumerable<mea.StreamingChatCompletionUpdate> CompleteStreamingAsync(
IList<mea.ChatMessage> chatMessages,
mea.ChatOptions? options = null,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
{
if (chatMessages == null) throw new ArgumentNullException(nameof(chatMessages));
Expand Down
88 changes: 44 additions & 44 deletions src/Mscc.GenerativeAI.Microsoft/MicrosoftAi/AbstractionMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Collections.Generic;
using System.Linq;
#endif
using Microsoft.Extensions.AI;
using mea = Microsoft.Extensions.AI;

namespace Mscc.GenerativeAI.Microsoft.MicrosoftAi
{
Expand All @@ -18,9 +18,9 @@ public static class AbstractionMapper
/// <param name="chatMessages">A list of chat messages.</param>
/// <param name="options">Optional. Chat options to configure the request.</param>
/// <returns></returns>
public static GenerateContentRequest? ToGeminiGenerateContentRequest(IList<ChatMessage> chatMessages, ChatOptions? options)
public static GenerateContentRequest? ToGeminiGenerateContentRequest(IList<mea.ChatMessage> chatMessages, mea.ChatOptions? options)
{
var prompt = string.Join<ChatMessage>(" ", chatMessages.ToArray()) ?? "";
var prompt = string.Join<mea.ChatMessage>(" ", chatMessages.ToArray()) ?? "";

GenerationConfig? generationConfig = null;
if (options?.AdditionalProperties?.Any() ?? false)
Expand All @@ -47,11 +47,11 @@ public static class AbstractionMapper
}

/// <summary>
/// Converts a <see cref="ChatOptions"/> to a <see cref="RequestOptions"/>.
/// Converts a <see cref="mea.ChatOptions"/> to a <see cref="RequestOptions"/>.
/// </summary>
/// <param name="options">Chat options to configure the request.</param>
/// <returns></returns>
public static RequestOptions? ToGeminiGenerateContentRequestOptions(ChatOptions? options)
public static RequestOptions? ToGeminiGenerateContentRequestOptions(mea.ChatOptions? options)
{
if (options is null) return null;

Expand Down Expand Up @@ -79,7 +79,7 @@ public static class AbstractionMapper
/// </summary>
/// <param name="values">The values to get embeddings for</param>
/// <param name="options">The options for the embeddings</param>
public static EmbedContentRequest ToGeminiEmbedContentRequest(IEnumerable<string> values, EmbeddingGenerationOptions? options)
public static EmbedContentRequest ToGeminiEmbedContentRequest(IEnumerable<string> values, mea.EmbeddingGenerationOptions? options)
{
return new EmbedContentRequest(string.Join(" ", values))
{
Expand All @@ -88,17 +88,17 @@ public static EmbedContentRequest ToGeminiEmbedContentRequest(IEnumerable<string
}

/// <summary>
/// Converts a <see cref="GenerateContentResponse"/> to a <see cref="ChatCompletion"/>.
/// Converts a <see cref="GenerateContentResponse"/> to a <see cref="mea.ChatCompletion"/>.
/// </summary>
/// <param name="response">The response with completion data.</param>
/// <returns></returns>
public static ChatCompletion? ToChatCompletion(GenerateContentResponse? response)
public static mea.ChatCompletion? ToChatCompletion(GenerateContentResponse? response)
{
if (response is null) return null;

var chatMessage = ToChatMessage(response);

return new ChatCompletion(chatMessage)
return new mea.ChatCompletion(chatMessage)
{
FinishReason = ToFinishReason(response.Candidates?.FirstOrDefault()?.FinishReason),
AdditionalProperties = null,
Expand All @@ -112,35 +112,35 @@ public static EmbedContentRequest ToGeminiEmbedContentRequest(IEnumerable<string
}

/// <summary>
/// Converts a <see cref="GenerateContentResponse"/> to a <see cref="StreamingChatCompletionUpdate"/>.
/// Converts a <see cref="GenerateContentResponse"/> to a <see cref="mea.StreamingChatCompletionUpdate"/>.
/// </summary>
/// <param name="response">The response stream to convert.</param>
public static StreamingChatCompletionUpdate ToStreamingChatCompletionUpdate(GenerateContentResponse? response)
public static mea.StreamingChatCompletionUpdate ToStreamingChatCompletionUpdate(GenerateContentResponse? response)
{
return new StreamingChatCompletionUpdate
return new mea.StreamingChatCompletionUpdate
{
// no need to set "Contents" as we set the text
CompletionId = null,
ChoiceIndex = 0, // should be left at 0 as Mscc.GenerativeAI does not support this
CreatedAt = null,
AdditionalProperties = null,
FinishReason = response?.Candidates?.FirstOrDefault()?.FinishReason == FinishReason.Other ? ChatFinishReason.Stop : null,
FinishReason = response?.Candidates?.FirstOrDefault()?.FinishReason == FinishReason.Other ? mea.ChatFinishReason.Stop : null,
RawRepresentation = response,
Text = response?.Text,
Role = ToAbstractionRole(response?.Candidates?.FirstOrDefault()?.Content?.Role)
};
}

public static GeneratedEmbeddings<Embedding<float>> ToGeneratedEmbeddings(EmbedContentRequest request, EmbedContentResponse response)
public static mea.GeneratedEmbeddings<mea.Embedding<float>> ToGeneratedEmbeddings(EmbedContentRequest request, EmbedContentResponse response)
{
if (request == null) throw new ArgumentNullException(nameof(request));
if (response == null) throw new ArgumentNullException(nameof(response));

AdditionalPropertiesDictionary? responseProps = null;
UsageDetails? usage = null;
mea.AdditionalPropertiesDictionary? responseProps = null;
mea.UsageDetails? usage = null;

return new GeneratedEmbeddings<Embedding<float>>([
new Embedding<float>(response.Embedding?.Values.ToArray() ?? [])
return new mea.GeneratedEmbeddings<mea.Embedding<float>>([
new mea.Embedding<float>(response.Embedding?.Values.ToArray() ?? [])
{
CreatedAt = DateTimeOffset.Now,
ModelId = request.Model
Expand All @@ -155,63 +155,63 @@ public static GeneratedEmbeddings<Embedding<float>> ToGeneratedEmbeddings(EmbedC
/// Maps a <see cref="GenerateContentResponse"/> to a <see cref="ChatMessage"/>.
/// </summary>
/// <param name="response">The response to map.</param>
private static ChatMessage ToChatMessage(GenerateContentResponse response)
private static mea.ChatMessage ToChatMessage(GenerateContentResponse response)
{
var contents = new List<AIContent>();
var contents = new List<mea.AIContent>();
if (response.Text?.Length > 0)
contents.Insert(0, new TextContent(response.Text));
contents.Insert(0, new mea.TextContent(response.Text));

return new ChatMessage(ToAbstractionRole(response.Candidates?.FirstOrDefault()?.Content?.Role), contents)
return new mea.ChatMessage(ToAbstractionRole(response.Candidates?.FirstOrDefault()?.Content?.Role), contents)
{
RawRepresentation = response
};
}

/// <summary>
/// Maps a <see cref="Mscc.GenerativeAI.Role"/> to a <see cref="ChatRole"/>.
/// Maps a <see cref="Mscc.GenerativeAI.Role"/> to a <see cref="mea.ChatRole"/>.
/// </summary>
/// <param name="role">The role to map.</param>
private static ChatRole ToAbstractionRole(string? role)
private static mea.ChatRole ToAbstractionRole(string? role)
{
if (string.IsNullOrEmpty(role)) return new ChatRole("unknown");
if (string.IsNullOrEmpty(role)) return new mea.ChatRole("unknown");

return role switch
{
Role.User => ChatRole.User,
Role.Model => ChatRole.Assistant,
Role.System => ChatRole.System,
Role.Function => ChatRole.Tool,
_ => new ChatRole(role)
Role.User => mea.ChatRole.User,
Role.Model => mea.ChatRole.Assistant,
Role.System => mea.ChatRole.System,
Role.Function => mea.ChatRole.Tool,
_ => new mea.ChatRole(role)
};
}

/// <summary>
/// Maps a <see cref="FinishReason"/> to a <see cref="ChatFinishReason"/>.
/// Maps a <see cref="FinishReason"/> to a <see cref="mea.ChatFinishReason"/>.
/// </summary>
/// <param name="finishReason">The finish reason to map.</param>
private static ChatFinishReason? ToFinishReason(FinishReason? finishReason)
private static mea.ChatFinishReason? ToFinishReason(FinishReason? finishReason)
{
return finishReason switch
{
null => null,
FinishReason.MaxTokens => ChatFinishReason.Length,
FinishReason.Stop => ChatFinishReason.Stop,
FinishReason.Safety => ChatFinishReason.ContentFilter,
FinishReason.ProhibitedContent => ChatFinishReason.ContentFilter,
FinishReason.Recitation => ChatFinishReason.ContentFilter,
FinishReason.Spii => ChatFinishReason.ContentFilter,
FinishReason.Blocklist => ChatFinishReason.ContentFilter,
FinishReason.MalformedFunctionCall => ChatFinishReason.ToolCalls,
_ => new ChatFinishReason(Enum.GetName(typeof(FinishReason), finishReason)!)
FinishReason.MaxTokens => mea.ChatFinishReason.Length,
FinishReason.Stop => mea.ChatFinishReason.Stop,
FinishReason.Safety => mea.ChatFinishReason.ContentFilter,
FinishReason.ProhibitedContent => mea.ChatFinishReason.ContentFilter,
FinishReason.Recitation => mea.ChatFinishReason.ContentFilter,
FinishReason.Spii => mea.ChatFinishReason.ContentFilter,
FinishReason.Blocklist => mea.ChatFinishReason.ContentFilter,
FinishReason.MalformedFunctionCall => mea.ChatFinishReason.ToolCalls,
_ => new mea.ChatFinishReason(Enum.GetName(typeof(FinishReason), finishReason)!)
};
}

/// <summary>
/// Parses usage details from a <see cref="GenerateContentResponse"/>
/// </summary>
/// <param name="response">The response to parse.</param>
/// <returns>A <see cref="UsageDetails"/> instance containing the parsed usage details.</returns>
private static UsageDetails? ParseContentResponseUsage(GenerateContentResponse response)
/// <returns>A <see cref="mea.UsageDetails"/> instance containing the parsed usage details.</returns>
private static mea.UsageDetails? ParseContentResponseUsage(GenerateContentResponse response)
{
if (response.UsageMetadata is null) return null;

Expand All @@ -230,7 +230,7 @@ private static ChatRole ToAbstractionRole(string? role)
/// <param name="option">The Gemini setting to add</param>
/// <param name="optionSetter">The <see cref="Action"/> to set the Gemini option, if available in the chat options</param>
/// <typeparam name="T">The type of the option</typeparam>
private static void TryAddOption<T>(ChatOptions chatOptions, string option, Action<T> optionSetter)
private static void TryAddOption<T>(mea.ChatOptions chatOptions, string option, Action<T> optionSetter)
{
if (chatOptions.AdditionalProperties?.TryGetValue(option, out var value) ?? false)
optionSetter((T)value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.AI" Version="9.0.1-preview.1.24570.5" />
<PackageReference Include="Mscc.GenerativeAI" Version="1.9.2" />
<PackageReference Include="Mscc.GenerativeAI" Version="1.9.5" />
</ItemGroup>

<Target Name="InjectPackageReleaseNotesFromFile" BeforeTargets="GenerateNuspec" Condition="Exists('CHANGELOG.md')">
Expand Down

0 comments on commit fa0a203

Please sign in to comment.