Skip to content

Commit

Permalink
Resolve pull-request remarks
Browse files Browse the repository at this point in the history
  • Loading branch information
awaescher committed Oct 21, 2024
1 parent 087eabe commit 31ffb25
Showing 1 changed file with 23 additions and 20 deletions.
43 changes: 23 additions & 20 deletions src/MicrosoftAi/AbstractionMapper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Nodes;
using Microsoft.Extensions.AI;
using OllamaSharp.Models.Chat;

Expand Down Expand Up @@ -46,13 +47,9 @@ public static class AbstractionMapper
/// <param name="stream">Indicates if the request should be streamed.</param>
public static ChatRequest ToOllamaSharpChatRequest(IOllamaApiClient apiClient, IList<ChatMessage> chatMessages, ChatOptions? options, bool stream)
{
// unused ChatOptions properties
// options.MaxOutputTokens,
// options.ToolMode,

return new ChatRequest
{
Format = options?.ResponseFormat is ChatResponseFormat.Json ? "json" : null,
Format = options?.ResponseFormat == ChatResponseFormat.Json ? "json" : null,
KeepAlive = null,
Messages = ToOllamaSharpMessages(chatMessages),
Model = options?.ModelId ?? apiClient.SelectedModel,
Expand Down Expand Up @@ -110,8 +107,8 @@ public static ChatRequest ToOllamaSharpChatRequest(IOllamaApiClient apiClient, I
Properties = functionMetadata.Parameters.ToDictionary(p => p.Name, p => new Properties
{
Description = p.Description,
Enum = [], // TODO is there such as possible values in AIFunctionParameterMetadata?
Type = ToFunctionTypeString(p.ParameterType)
Enum = GetPossibleValues(p.Schema as JsonObject),
Type = ToFunctionTypeString(p.Schema as JsonObject)
}),
Required = functionMetadata.Parameters.Where(p => p.IsRequired).Select(p => p.Name),
Type = "object"
Expand All @@ -122,10 +119,19 @@ public static ChatRequest ToOllamaSharpChatRequest(IOllamaApiClient apiClient, I
}

/// <summary>
/// Converts a <see cref="Type"/> to a function type string.
/// Converts parameter schema object to a function type string.
/// </summary>
/// <param name="schema">The schema object holding schema type information.</param>
private static IEnumerable<string>? GetPossibleValues(JsonObject? schema)
{
return []; // TODO others supported?
}

/// <summary>
/// Converts parameter schema object to a function type string.
/// </summary>
/// <param name="_">The type to convert.</param>
private static string ToFunctionTypeString(Type? _)
/// <param name="schema">The schema object holding schema type information.</param>
private static string ToFunctionTypeString(JsonObject? schema)
{
return "string"; // TODO others supported?
}
Expand Down Expand Up @@ -157,10 +163,9 @@ private static string ToOllamaImage(DataContent content)
if (content is null || !content.ContainsData)
return string.Empty;

if (content.MediaType?.StartsWith("image", StringComparison.OrdinalIgnoreCase) ?? false)
{
return content.Uri; // If the content is binary data, converts it to a data: URI with base64 encoding
}
var isImage = content is ImageContent || content?.MediaType?.StartsWith("image", StringComparison.OrdinalIgnoreCase) == true;
if (isImage)
return content?.Uri ?? ""; // If the content is binary data, converts it to a data: URI with base64 encoding

return string.Empty;
}
Expand Down Expand Up @@ -222,13 +227,11 @@ private static Microsoft.Extensions.AI.ChatRole ToAbstractionRole(OllamaSharp.Mo
/// <param name="response">The response stream to convert.</param>
public static StreamingChatCompletionUpdate ToStreamingChatCompletionUpdate(ChatResponseStream? response)
{
return new StreamingChatCompletionUpdate // TODO
return new StreamingChatCompletionUpdate
{
//AdditionalProperties
//AuthorName
//ChoiceIndex
//CompletionId
//Contents
// no need to set "Contents" as we set the text
CompletionId = response?.CreatedAtString,
ChoiceIndex = 0, // should be left at 0 as Ollama does not support this
CreatedAt = response?.CreatedAt,
FinishReason = response?.Done == true ? ChatFinishReason.Stop : null,
RawRepresentation = response,
Expand Down

0 comments on commit 31ffb25

Please sign in to comment.