Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/Joy-less/OllamaSharp into r…
Browse files Browse the repository at this point in the history
…eplace-JsonSchema
  • Loading branch information
Joy-less committed Jan 8, 2025
2 parents a4e831c + a7daa3f commit 1f78ef3
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ jobs:
uses: actions/setup-dotnet@v2
with:
dotnet-version: |
6.0.x
8.0.x
9.0.x
- name: Restore dependencies
run: dotnet restore OllamaSharp.sln
Expand Down
7 changes: 7 additions & 0 deletions Directory.Build.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<Project>
<PropertyGroup>
<LangVersion>preview</LangVersion>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion OllamaSharp.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.002.0
VisualStudioVersion = 17.5.2.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OllamaSharp", "src\OllamaSharp.csproj", "{0194817F-1B9C-4D44-8960-1A0DC92D9D22}"
EndProject
Expand Down
10 changes: 1 addition & 9 deletions demo/OllamaApiConsole.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,10 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<NoWarn>IDE0065;IDE0055;IDE0011</NoWarn>
<TargetFramework>net9.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<!--
SixLabors.ImageSharp added explicitly to fix CVE-2024-41131: https://github.com/advisories/GHSA-63p8-c4ww-9cg7
and can be removed once Spectre.Console.ImageSharp uses a version greater than 3.1.4
-->
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.5" />
<PackageReference Include="Spectre.Console" Version="0.49.1" />
<PackageReference Include="Spectre.Console.ImageSharp" Version="0.49.1" />
</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/MicrosoftAi/AbstractionMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ private static Microsoft.Extensions.AI.ChatRole ToAbstractionRole(OllamaSharp.Mo
"system" => Microsoft.Extensions.AI.ChatRole.System,
"user" => Microsoft.Extensions.AI.ChatRole.User,
"tool" => Microsoft.Extensions.AI.ChatRole.Tool,
_ => new Microsoft.Extensions.AI.ChatRole(role.ToString()),
_ => new Microsoft.Extensions.AI.ChatRole(role.ToString()!),
};
}

Expand Down
18 changes: 11 additions & 7 deletions src/OllamaApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ namespace OllamaSharp;
/// </summary>
public class OllamaApiClient : IOllamaApiClient, IChatClient, IEmbeddingGenerator<string, Embedding<float>>
{
private readonly bool _disposeHttpClient;

/// <summary>
/// Gets the default request headers that are sent to the Ollama API.
/// </summary>
Expand All @@ -46,15 +44,19 @@ public class OllamaApiClient : IOllamaApiClient, IChatClient, IEmbeddingGenerato
public Configuration Config { get; }

/// <inheritdoc />
public Uri Uri => _client.BaseAddress;
public Uri Uri => _client.BaseAddress!;

/// <inheritdoc />
public string SelectedModel { get; set; }

/// <summary>
/// Gets the <see cref="HttpClient" /> used to communicate with the Ollama API.
/// Gets the <see cref="HttpClient"/> used to communicate with the Ollama API.
/// </summary>
private readonly HttpClient _client;
/// <summary>
/// If true, the <see cref="HttpClient"/> will be disposed when the <see cref="OllamaApiClient"/> is disposed.
/// </summary>
private readonly bool _disposeHttpClient;

/// <summary>
/// Creates a new instance of the Ollama API client.
Expand Down Expand Up @@ -297,7 +299,7 @@ private async Task<TResponse> PostAsync<TRequest, TResponse>(string endpoint, TR

while (!reader.EndOfStream && !cancellationToken.IsCancellationRequested)
{
var line = await reader.ReadLineAsync().ConfigureAwait(false);
var line = await reader.ReadLineAsync().ConfigureAwait(false) ?? "";
yield return JsonSerializer.Deserialize<TLine?>(line, IncomingJsonSerializerOptions);
}
}
Expand All @@ -309,7 +311,7 @@ private async Task<TResponse> PostAsync<TRequest, TResponse>(string endpoint, TR

while (!reader.EndOfStream && !cancellationToken.IsCancellationRequested)
{
var line = await reader.ReadLineAsync().ConfigureAwait(false);
var line = await reader.ReadLineAsync().ConfigureAwait(false) ?? "";
var streamedResponse = JsonSerializer.Deserialize<GenerateResponseStream>(line, IncomingJsonSerializerOptions);

yield return streamedResponse?.Done ?? false
Expand All @@ -325,7 +327,7 @@ private async Task<TResponse> PostAsync<TRequest, TResponse>(string endpoint, TR

while (!reader.EndOfStream && !cancellationToken.IsCancellationRequested)
{
var line = await reader.ReadLineAsync().ConfigureAwait(false);
var line = await reader.ReadLineAsync().ConfigureAwait(false) ?? "";
var streamedResponse = JsonSerializer.Deserialize<ChatResponseStream>(line, IncomingJsonSerializerOptions);

yield return streamedResponse?.Done ?? false
Expand Down Expand Up @@ -428,6 +430,8 @@ async Task<GeneratedEmbeddings<Embedding<float>>> IEmbeddingGenerator<string, Em
/// </summary>
public void Dispose()
{
GC.SuppressFinalize(this);

if (_disposeHttpClient)
_client?.Dispose();
}
Expand Down
6 changes: 2 additions & 4 deletions src/OllamaSharp.csproj
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Nullable>enable</Nullable>
<LangVersion>12</LangVersion>
<TargetFrameworks>netstandard2.0;netstandard2.1;net8.0;net9.0</TargetFrameworks>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<Title>OllamaSharp</Title>
Expand Down Expand Up @@ -44,7 +42,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="8.0.0" />
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.AI.Abstractions" Version="9.0.1-preview.1.24570.5" />
</ItemGroup>
</Project>
1 change: 0 additions & 1 deletion test/FunctionalTests/ChatTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using FluentAssertions;
using Microsoft.Extensions.AI;
using NUnit.Framework;
using OllamaSharp;

Expand Down
3 changes: 0 additions & 3 deletions test/FunctionalTests/OllamaApiClientTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using FluentAssertions;
using Microsoft.Extensions.AI;
using NUnit.Framework;
using OllamaSharp;
using OllamaSharp.Models;
Expand All @@ -15,9 +14,7 @@ public class OllamaApiClientTests
private readonly string _localModel = "OllamaSharpTest";
private readonly string _embeddingModel = "all-minilm:22m";

#pragma warning disable NUnit1032
private OllamaApiClient _client = null!;
#pragma warning restore NUnit1032

[SetUp]
public async Task Setup()
Expand Down
6 changes: 2 additions & 4 deletions test/TestOllamaApiClient.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#pragma warning disable CS8424 // The EnumeratorCancellationAttribute will have no effect. The attribute is only effective on a parameter of type CancellationToken in an async-iterator method returning IAsyncEnumerable

using System.Runtime.CompilerServices;
using OllamaSharp;
using OllamaSharp.Models;
using OllamaSharp.Models.Chat;

namespace Tests;

#pragma warning disable CS8424 // The EnumeratorCancellationAttribute will have no effect. The attribute is only effective on a parameter of type CancellationToken in an async-iterator method returning IAsyncEnumerable

public class TestOllamaApiClient : IOllamaApiClient
{
private ChatResponseStream[] _expectedChatResponses = [];
Expand Down Expand Up @@ -98,6 +98,4 @@ public Task<ShowModelResponse> ShowModelAsync(ShowModelRequest request, Cancella
{
throw new NotImplementedException();
}

#pragma warning restore CS8424 // The EnumeratorCancellationAttribute will have no effect. The attribute is only effective on a parameter of type CancellationToken in an async-iterator method returning IAsyncEnumerable
}
14 changes: 6 additions & 8 deletions test/Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,24 @@

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<NoWarn>CS8604;CS8602</NoWarn>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
<NoWarn>IDE0065;IDE0055;IDE0011;CS8602;CS8604;S6608</NoWarn>
<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>..\OllamaSharp.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.12.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="FluentAssertions" Version="7.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="NUnit" Version="4.2.2" />
<PackageReference Include="NUnit" Version="4.3.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
<PackageReference Include="NUnit.Analyzers" Version="4.3.0">
<PackageReference Include="NUnit.Analyzers" Version="4.5.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.2">
<PackageReference Include="coverlet.collector" Version="6.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down

0 comments on commit 1f78ef3

Please sign in to comment.