Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion dotnet/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<PackageVersion Include="System.Text.Json" Version="8.0.4" />
<PackageVersion Include="System.Threading.Tasks.Extensions" Version="4.5.4" />
<PackageVersion Include="System.ValueTuple" Version="4.5.0" />
<PackageVersion Include="OllamaSharp" Version="2.0.6" />
<PackageVersion Include="OllamaSharp" Version="2.0.10" />
<!-- Tokenizers -->
<PackageVersion Include="Microsoft.ML.Tokenizers" Version="0.22.0-preview.24271.1" />
<PackageVersion Include="Microsoft.DeepDev.TokenizerLib" Version="1.3.3" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) Microsoft. All rights reserved.

using System.Linq;
using System.Text.Json;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Connectors.Ollama;
Expand Down Expand Up @@ -46,7 +47,7 @@ public void FromExecutionSettingsWhenSerializedHasPropertiesShouldPopulateSpecia
{
string jsonSettings = """
{
"stop": "stop me",
"stop": ["stop me"],
"temperature": 0.5,
"top_p": 0.9,
"top_k": 100
Expand All @@ -56,7 +57,7 @@ public void FromExecutionSettingsWhenSerializedHasPropertiesShouldPopulateSpecia
var executionSettings = JsonSerializer.Deserialize<PromptExecutionSettings>(jsonSettings);
var ollamaExecutionSettings = OllamaPromptExecutionSettings.FromExecutionSettings(executionSettings);

Assert.Equal("stop me", ollamaExecutionSettings.Stop);
Assert.Equal("stop me", ollamaExecutionSettings.Stop?.FirstOrDefault());
Assert.Equal(0.5f, ollamaExecutionSettings.Temperature);
Assert.Equal(0.9f, ollamaExecutionSettings.TopP!.Value, 0.1f);
Assert.Equal(100, ollamaExecutionSettings.TopK);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,11 @@ public async Task ShouldHandleServiceResponseAsync()
}

[Fact]
public async Task GetChatMessageContentsShouldHaveModelIdDefinedAsync()
public async Task GetChatMessageContentsShouldHaveModelAndMetadataAsync()
{
//Arrange
var sut = new OllamaChatCompletionService(
"fake-model",
"phi3",
new Uri("http://localhost:11434"),
httpClient: this._httpClient);

Expand All @@ -135,11 +135,11 @@ public async Task GetChatMessageContentsShouldHaveModelIdDefinedAsync()

// Assert
Assert.NotNull(message.ModelId);
Assert.Equal("fake-model", message.ModelId);
Assert.Equal("phi3", message.ModelId);
}

[Fact]
public async Task GetStreamingChatMessageContentsShouldHaveModelIdDefinedAsync()
public async Task GetStreamingChatMessageContentsShouldHaveModelAndMetadataAsync()
{
//Arrange
var expectedModel = "phi3";
Expand All @@ -161,11 +161,18 @@ public async Task GetStreamingChatMessageContentsShouldHaveModelIdDefinedAsync()
await foreach (var message in sut.GetStreamingChatMessageContentsAsync(chat))
{
lastMessage = message;
Assert.NotNull(message.Metadata);
}

// Assert
Assert.NotNull(lastMessage!.ModelId);
Assert.Equal(expectedModel, lastMessage.ModelId);

Assert.IsType<OllamaMetadata>(lastMessage.Metadata);
var metadata = lastMessage.Metadata as OllamaMetadata;
Assert.NotNull(metadata);
Assert.NotEmpty(metadata);
Assert.True(metadata.Done);
}

public void Dispose()
Expand Down
11 changes: 11 additions & 0 deletions dotnet/src/Connectors/Connectors.Ollama/OllamaMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ internal OllamaMetadata(ChatResponseStream? message) : base(new Dictionary<strin
}
}

internal OllamaMetadata(ChatResponse response) : base(new Dictionary<string, object?>())
{
this.TotalDuration = response.TotalDuration;
this.EvalCount = response.EvalCount;
this.EvalDuration = response.EvalDuration;
this.CreatedAt = response.CreatedAt;
this.LoadDuration = response.LoadDuration;
this.PromptEvalDuration = response.PromptEvalDuration;
this.CreatedAt = response.CreatedAt;
}

/// <summary>
/// Time spent in nanoseconds evaluating the prompt
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft. All rights reserved.

using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.SemanticKernel.Text;
Expand Down Expand Up @@ -46,7 +47,7 @@ public static OllamaPromptExecutionSettings FromExecutionSettings(PromptExecutio
/// </summary>
[JsonPropertyName("stop")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? Stop
public List<string>? Stop
{
get => this._stop;

Expand Down Expand Up @@ -112,7 +113,7 @@ public float? Temperature

#region private ================================================================================

private string? _stop;
private List<string>? _stop;
private float? _temperature;
private float? _topP;
private int? _topK;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,14 @@ public async Task<IReadOnlyList<ChatMessageContent>> GetChatMessageContentsAsync
var settings = OllamaPromptExecutionSettings.FromExecutionSettings(executionSettings);
var request = CreateChatRequest(chatHistory, settings, this._client.SelectedModel);

var answer = await this._client.SendChat(request, _ => { }, cancellationToken).ConfigureAwait(false);

// Ollama Client gives back the same requested history with added message at the end
// To be compatible with this API behavior, we only return the added message (last).
var message = answer.Last();
var response = await this._client.Chat(request, cancellationToken).ConfigureAwait(false);

return [new ChatMessageContent(
role: GetAuthorRole(message.Role) ?? AuthorRole.Assistant,
content: message.Content,
modelId: this._client.SelectedModel,
innerContent: message)]; // Currently the Ollama Message does not provide any metadata
role: GetAuthorRole(response.Message.Role) ?? AuthorRole.Assistant,
content: response.Message.Content,
modelId: response.Model,
innerContent: response,
metadata: new OllamaMetadata(response))];
}

/// <inheritdoc />
Expand All @@ -89,9 +86,9 @@ public async IAsyncEnumerable<StreamingChatMessageContent> GetStreamingChatMessa
await foreach (var message in this._client.StreamChat(request, cancellationToken).ConfigureAwait(false))
{
yield return new StreamingChatMessageContent(
GetAuthorRole(message?.Message.Role),
message?.Message.Content,
modelId: message?.Model,
role: GetAuthorRole(message!.Message.Role),
content: message.Message.Content,
modelId: message.Model,
innerContent: message,
metadata: new OllamaMetadata(message));
}
Expand Down Expand Up @@ -130,7 +127,7 @@ private static ChatRequest CreateChatRequest(ChatHistory chatHistory, OllamaProm
Temperature = settings.Temperature,
TopP = settings.TopP,
TopK = settings.TopK,
Stop = settings.Stop
Stop = settings.Stop?.ToArray()
},
Messages = messages.ToList(),
Model = selectedModel,
Expand Down