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

.Net Ollama Connector with Ollama Sharp Client Update #7059

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
1 change: 1 addition & 0 deletions dotnet/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<PackageVersion Include="System.Text.Json" Version="8.0.2" />
<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" />
<!-- Tokenizers -->
<PackageVersion Include="Microsoft.ML.Tokenizers" Version="0.22.0-preview.24271.1" />
<PackageVersion Include="Microsoft.DeepDev.TokenizerLib" Version="1.3.3" />
Expand Down
18 changes: 18 additions & 0 deletions dotnet/SK-dotnet.sln
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Connectors.Redis.UnitTests"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Connectors.Qdrant.UnitTests", "src\Connectors\Connectors.Qdrant.UnitTests\Connectors.Qdrant.UnitTests.csproj", "{E92AE954-8F3A-4A6F-A4F9-DC12017E5AAF}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Connectors.Ollama", "src\Connectors\Connectors.Ollama\Connectors.Ollama.csproj", "{E7E60E1D-1A44-4DE9-A44D-D5052E809DDD}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Connectors.Ollama.UnitTests", "src\Connectors\Connectors.Ollama.UnitTests\Connectors.Ollama.UnitTests.csproj", "{924DB138-1223-4C99-B6E6-0938A3FA14EF}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -787,6 +791,18 @@ Global
{E92AE954-8F3A-4A6F-A4F9-DC12017E5AAF}.Publish|Any CPU.Build.0 = Debug|Any CPU
{E92AE954-8F3A-4A6F-A4F9-DC12017E5AAF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E92AE954-8F3A-4A6F-A4F9-DC12017E5AAF}.Release|Any CPU.Build.0 = Release|Any CPU
{E7E60E1D-1A44-4DE9-A44D-D5052E809DDD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E7E60E1D-1A44-4DE9-A44D-D5052E809DDD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E7E60E1D-1A44-4DE9-A44D-D5052E809DDD}.Publish|Any CPU.ActiveCfg = Publish|Any CPU
{E7E60E1D-1A44-4DE9-A44D-D5052E809DDD}.Publish|Any CPU.Build.0 = Publish|Any CPU
{E7E60E1D-1A44-4DE9-A44D-D5052E809DDD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E7E60E1D-1A44-4DE9-A44D-D5052E809DDD}.Release|Any CPU.Build.0 = Release|Any CPU
{924DB138-1223-4C99-B6E6-0938A3FA14EF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{924DB138-1223-4C99-B6E6-0938A3FA14EF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{924DB138-1223-4C99-B6E6-0938A3FA14EF}.Publish|Any CPU.ActiveCfg = Debug|Any CPU
{924DB138-1223-4C99-B6E6-0938A3FA14EF}.Publish|Any CPU.Build.0 = Debug|Any CPU
{924DB138-1223-4C99-B6E6-0938A3FA14EF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{924DB138-1223-4C99-B6E6-0938A3FA14EF}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -895,6 +911,8 @@ Global
{B0B3901E-AF56-432B-8FAA-858468E5D0DF} = {24503383-A8C4-4255-9998-28D70FE8E99A}
{1D4667B9-9381-4E32-895F-123B94253EE8} = {0247C2C9-86C3-45BA-8873-28B0948EDC0C}
{E92AE954-8F3A-4A6F-A4F9-DC12017E5AAF} = {0247C2C9-86C3-45BA-8873-28B0948EDC0C}
{E7E60E1D-1A44-4DE9-A44D-D5052E809DDD} = {1B4CBDE0-10C2-4E7D-9CD0-FE7586C96ED1}
{924DB138-1223-4C99-B6E6-0938A3FA14EF} = {1B4CBDE0-10C2-4E7D-9CD0-FE7586C96ED1}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {FBDC56A3-86AD-4323-AA0F-201E59123B83}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<AssemblyName>SemanticKernel.Connectors.Ollama.UnitTests</AssemblyName>
<RootNamespace>SemanticKernel.Connectors.Ollama.UnitTests</RootNamespace>
<TargetFramework>net8.0</TargetFramework>
<LangVersion>12</LangVersion>
<RollForward>LatestMajor</RollForward>
<IsTestProject>true</IsTestProject>
<Nullable>enable</Nullable>
<ImplicitUsings>disable</ImplicitUsings>
<IsPackable>false</IsPackable>
<NoWarn>CA2007,CA1861,VSTHRD111,CS1591,SKEXP0001,SKEXP0070</NoWarn>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="Moq" />
<PackageReference Include="OllamaSharp" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="System.Numerics.Tensors" />
<PackageReference Include="System.Text.Json" VersionOverride="8.0.3" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" />
</ItemGroup>

<ItemGroup>
<Compile Include="$(RepoRoot)/dotnet/src/InternalUtilities/test/AssertExtensions.cs" Link="%(RecursiveDir)%(Filename)%(Extension)" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\SemanticKernel.Core\SemanticKernel.Core.csproj" />
<ProjectReference Include="..\Connectors.Ollama\Connectors.Ollama.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="TestData\*">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) Microsoft. All rights reserved.

using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace SemanticKernel.Connectors.Ollama.UnitTests;

internal sealed class HttpMessageHandlerStub : DelegatingHandler
{
public HttpRequestHeaders? RequestHeaders { get; private set; }

public HttpContentHeaders? ContentHeaders { get; private set; }

public byte[]? RequestContent { get; private set; }

public Uri? RequestUri { get; private set; }

public HttpMethod? Method { get; private set; }

public HttpResponseMessage ResponseToReturn { get; set; }

public HttpMessageHandlerStub()
{
this.ResponseToReturn = new HttpResponseMessage(System.Net.HttpStatusCode.OK);
this.ResponseToReturn.Content = new StringContent("{}", Encoding.UTF8, "application/json");
}

protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
this.Method = request.Method;
this.RequestUri = request.RequestUri;
this.RequestHeaders = request.Headers;
if (request.Content is not null)
{
#pragma warning disable CA2016 // Forward the 'CancellationToken' parameter to methods; overload doesn't exist on .NET Framework
this.RequestContent = await request.Content.ReadAsByteArrayAsync();
#pragma warning restore CA2016
}

this.ContentHeaders = request.Content?.Headers;

return await Task.FromResult(this.ResponseToReturn);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright (c) Microsoft. All rights reserved.

using System;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.Ollama;
using Microsoft.SemanticKernel.Embeddings;
using Microsoft.SemanticKernel.TextGeneration;
using Xunit;

namespace SemanticKernel.Connectors.Ollama.UnitTests;

/// <summary>
/// Unit tests of <see cref="OllamaKernelBuilderExtensions"/>.
/// </summary>
public class OllamaKernelBuilderExtensionsTests
{
[Fact]
public void AddOllamaTextGenerationCreatesService()
{
var builder = Kernel.CreateBuilder();
builder.AddOllamaTextGeneration("model", new Uri("http://localhost:11434"));

var kernel = builder.Build();
var service = kernel.GetRequiredService<ITextGenerationService>();

Assert.NotNull(kernel);
Assert.NotNull(service);
Assert.IsType<OllamaTextGenerationService>(service);
}

[Fact]
public void AddOllamaChatCompletionCreatesService()
{
var builder = Kernel.CreateBuilder();
builder.AddOllamaChatCompletion("model", new Uri("http://localhost:11434"));

var kernel = builder.Build();
var service = kernel.GetRequiredService<IChatCompletionService>();

Assert.NotNull(kernel);
Assert.NotNull(service);
Assert.IsType<OllamaChatCompletionService>(service);
}

[Fact]
public void AddOllamaTextEmbeddingGenerationCreatesService()
{
var builder = Kernel.CreateBuilder();
builder.AddOllamaTextEmbeddingGeneration("model", new Uri("http://localhost:11434"));

var kernel = builder.Build();
var service = kernel.GetRequiredService<ITextEmbeddingGenerationService>();

Assert.NotNull(kernel);
Assert.NotNull(service);
Assert.IsType<OllamaTextEmbeddingGenerationService>(service);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright (c) Microsoft. All rights reserved.

using System.Text.Json;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Connectors.Ollama;
using Xunit;

namespace SemanticKernel.Connectors.Ollama.UnitTests;

/// <summary>
/// Unit tests of <see cref="OllamaPromptExecutionSettings"/>.
/// </summary>
public class OllamaPromptExecutionSettingsTests
{
[Fact]
public void FromExecutionSettingsWhenAlreadyOllamaShouldReturnSameAsync()
{
// Arrange
var executionSettings = new OllamaPromptExecutionSettings();

// Act
var ollamaExecutionSettings = OllamaPromptExecutionSettings.FromExecutionSettings(executionSettings);

// Assert
Assert.Same(executionSettings, ollamaExecutionSettings);
}

[Fact]
public void FromExecutionSettingsWhenNullShouldReturnDefaultAsync()
{
// Arrange
OllamaPromptExecutionSettings? executionSettings = null;

// Act
var ollamaExecutionSettings = OllamaPromptExecutionSettings.FromExecutionSettings(executionSettings);

// Assert
Assert.Null(ollamaExecutionSettings.Stop);
Assert.Null(ollamaExecutionSettings.Temperature);
Assert.Null(ollamaExecutionSettings.TopP);
Assert.Null(ollamaExecutionSettings.TopK);
}

[Fact]
public void FromExecutionSettingsWhenSerializedHasPropertiesShouldPopulateSpecialized()
{
string jsonSettings = """
{
"stop": "stop me",
"temperature": 0.5,
"top_p": 0.9,
"top_k": 100
}
""";

var executionSettings = JsonSerializer.Deserialize<PromptExecutionSettings>(jsonSettings);
var ollamaExecutionSettings = OllamaPromptExecutionSettings.FromExecutionSettings(executionSettings);

Assert.Equal("stop me", ollamaExecutionSettings.Stop);
Assert.Equal(0.5f, ollamaExecutionSettings.Temperature);
Assert.Equal(0.9f, ollamaExecutionSettings.TopP!.Value, 0.1f);
Assert.Equal(100, ollamaExecutionSettings.TopK);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright (c) Microsoft. All rights reserved.

using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.Ollama;
using Microsoft.SemanticKernel.Embeddings;
using Microsoft.SemanticKernel.TextGeneration;
using Xunit;

namespace SemanticKernel.Connectors.Ollama.UnitTests;

/// <summary>
/// Unit tests of <see cref="OllamaServiceCollectionExtensions"/>.
/// </summary>
public class OllamaServiceCollectionExtensionsTests
{
[Fact]
public void AddOllamaTextGenerationToServiceCollection()
{
var services = new ServiceCollection();
services.AddOllamaTextGeneration("model", new Uri("http://localhost:11434"));

var serviceProvider = services.BuildServiceProvider();
var service = serviceProvider.GetRequiredService<ITextGenerationService>();

Assert.NotNull(service);
Assert.IsType<OllamaTextGenerationService>(service);
}

[Fact]
public void AddOllamaChatCompletionToServiceCollection()
{
var services = new ServiceCollection();
services.AddOllamaChatCompletion("model", new Uri("http://localhost:11434"));

var serviceProvider = services.BuildServiceProvider();
var service = serviceProvider.GetRequiredService<IChatCompletionService>();

Assert.NotNull(service);
Assert.IsType<OllamaChatCompletionService>(service);
}

[Fact]
public void AddOllamaTextEmbeddingsGenerationToServiceCollection()
{
var services = new ServiceCollection();
services.AddOllamaTextEmbeddingGeneration("model", new Uri("http://localhost:11434"));

var serviceProvider = services.BuildServiceProvider();
var service = serviceProvider.GetRequiredService<ITextEmbeddingGenerationService>();

Assert.NotNull(service);
Assert.IsType<OllamaTextEmbeddingGenerationService>(service);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) Microsoft. All rights reserved.

using System;
using System.IO;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Moq;
using Moq.Protected;

namespace SemanticKernel.Connectors.Ollama.UnitTests;

/// <summary>
/// Helper for HuggingFace test purposes.
/// </summary>
internal static class OllamaTestHelper
{
/// <summary>
/// Reads test response from file for mocking purposes.
/// </summary>
/// <param name="fileName">Name of the file with test response.</param>
internal static string GetTestResponse(string fileName)
{
return File.ReadAllText($"./TestData/{fileName}");
}

internal static ReadOnlyMemory<byte> GetTestResponseBytes(string fileName)
{
return File.ReadAllBytes($"./TestData/{fileName}");
}

/// <summary>
/// Returns mocked instance of <see cref="HttpClientHandler"/>.
/// </summary>
/// <param name="httpResponseMessage">Message to return for mocked <see cref="HttpClientHandler"/>.</param>
internal static HttpClientHandler GetHttpClientHandlerMock(HttpResponseMessage httpResponseMessage)
{
var httpClientHandler = new Mock<HttpClientHandler>();

httpClientHandler
.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.IsAny<HttpRequestMessage>(),
ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(httpResponseMessage);

return httpClientHandler.Object;
}
}
Loading
Loading