-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from CommunityToolkit/aaronpowell/issue31
OllamaSharp client integration
- Loading branch information
Showing
11 changed files
with
337 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Aspire.CommunityTools.OllamaSharp | ||
|
||
[](https://nuget.org/packages/Aspire.CommunityToolkit.OllamaSharp/) | [>)](https://nuget.org/packages/Aspire.CommunityToolkit.OllamaSharp/absoluteLatest) | ||
|
||
## Overview | ||
|
||
A .NET Aspire client integration that uses the [OllamaSharp](https://www.nuget.org/packages/OllamaSharp) client to interact with the Ollama container. | ||
|
||
## Usage | ||
|
||
Use the static `AddOllamaClientApi` method to add this client integration to the application builder of your client application. A callback can be provided to the `AddOllamaClientApi` method to configure the settings of the Ollama client. | ||
|
||
```csharp | ||
builder.AddOllamaClientApi("ollama"); | ||
``` | ||
|
||
Then you can inject the `IOllamaClientApi`` into your client application and use it to interact with the Ollama container. | ||
|
||
```csharp | ||
public class MyService(IOllamaClientApi ollamaClientApi) | ||
{ | ||
public async Task DoSomething() | ||
{ | ||
var chat = new Chat(ollamaClientApi); | ||
while (true) | ||
{ | ||
var message = Console.ReadLine(); | ||
await foreach (var answerToken in chat.Send(message)) | ||
Console.Write(answerToken); | ||
} | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/Aspire.CommunityToolkit.OllamaSharp/Aspire.CommunityToolkit.OllamaSharp.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<PackageReadmeFile>ollamasharp.md</PackageReadmeFile> | ||
<Description>A .NET Aspire client integration for the OllamaSharp library.</Description> | ||
<AdditionalPackageTags>ollama ai ollamasharp</AdditionalPackageTags> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="OllamaSharp" /> | ||
</ItemGroup> | ||
|
||
</Project> |
73 changes: 73 additions & 0 deletions
73
src/Aspire.CommunityToolkit.OllamaSharp/AspireOllamaSharpExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
using Aspire.CommunityToolkit.OllamaSharp; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using OllamaSharp; | ||
|
||
namespace Microsoft.Extensions.Hosting; | ||
|
||
public static class AspireOllamaSharpExtensions | ||
{ | ||
private const string DefaultConfigSectionName = "Aspire:OllamaSharp"; | ||
|
||
/// <summary> | ||
/// Adds <see cref="OllamaApiClient"/> services to the container. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="IHostApplicationBuilder" /> to read config from and add services to.</param> | ||
/// <param name="connectionName">A name used to retrieve the connection string from the ConnectionStrings configuration section.</param> | ||
/// <param name="configureSettings">An optional delegate that can be used for customizing options. It's invoked after the settings are read from the configuration.</param> | ||
/// <exception cref="UriFormatException">Thrown when no Ollama endpoint is provided.</exception> | ||
public static void AddOllamaApiClient(this IHostApplicationBuilder builder, string connectionName, Action<OllamaSharpSettings>? configureSettings = null) | ||
{ | ||
ArgumentException.ThrowIfNullOrWhiteSpace(connectionName, nameof(connectionName)); | ||
ArgumentNullException.ThrowIfNull(builder, nameof(builder)); | ||
AddOllamaClientInternal(builder, connectionName, configureSettings: configureSettings); | ||
} | ||
|
||
/// <summary> | ||
/// Adds <see cref="OllamaApiClient"/> services to the container using the <paramref name="connectionName"/> as the service key. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="IHostApplicationBuilder" /> to read config from and add services to.</param> | ||
/// <param name="connectionName">A name used to retrieve the connection string from the ConnectionStrings configuration section.</param> | ||
/// <param name="configureSettings">An optional delegate that can be used for customizing options. It's invoked after the settings are read from the configuration.</param> | ||
/// <exception cref="UriFormatException">Thrown when no Ollama endpoint is provided.</exception> | ||
public static void AddKeyedOllamaApiClient(this IHostApplicationBuilder builder, string connectionName, Action<OllamaSharpSettings>? configureSettings = null) | ||
{ | ||
ArgumentException.ThrowIfNullOrWhiteSpace(connectionName, nameof(connectionName)); | ||
ArgumentNullException.ThrowIfNull(builder, nameof(builder)); | ||
AddOllamaClientInternal(builder, connectionName, serviceKey: connectionName, configureSettings: configureSettings); | ||
} | ||
|
||
private static void AddOllamaClientInternal(IHostApplicationBuilder builder, string connectionName, string? serviceKey = null, Action<OllamaSharpSettings>? configureSettings = null) | ||
{ | ||
OllamaSharpSettings settings = new(); | ||
builder.Configuration.GetSection($"{DefaultConfigSectionName}:{connectionName}").Bind(settings); | ||
|
||
if (builder.Configuration.GetConnectionString(connectionName) is string connectionString) | ||
{ | ||
settings.ConnectionString = connectionString; | ||
} | ||
|
||
configureSettings?.Invoke(settings); | ||
|
||
if (string.IsNullOrWhiteSpace(settings.ConnectionString)) | ||
{ | ||
throw new UriFormatException("No endpoint for Ollama defined."); | ||
} | ||
|
||
OllamaApiClient client = new(new HttpClient { BaseAddress = new Uri(settings.ConnectionString) }); | ||
|
||
if (!string.IsNullOrWhiteSpace(settings.SelectedModel)) | ||
{ | ||
client.SelectedModel = settings.SelectedModel; | ||
} | ||
|
||
if (!string.IsNullOrEmpty(serviceKey)) | ||
{ | ||
builder.Services.AddKeyedSingleton<IOllamaApiClient>(serviceKey, client); | ||
} | ||
else | ||
{ | ||
builder.Services.AddSingleton<IOllamaApiClient>(client); | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/Aspire.CommunityToolkit.OllamaSharp/OllamaSharpSettings.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
namespace Aspire.CommunityToolkit.OllamaSharp; | ||
|
||
/// <summary> | ||
/// Represents the settings for OllamaSharp. | ||
/// </summary> | ||
public sealed class OllamaSharpSettings | ||
{ | ||
/// <summary> | ||
/// Gets or sets the connection string. | ||
/// </summary> | ||
public string? ConnectionString { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the selected model. | ||
/// </summary> | ||
public string? SelectedModel { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the list of models to available. | ||
/// </summary> | ||
public IReadOnlyList<string> Models { get; set; } = []; | ||
} |
12 changes: 12 additions & 0 deletions
12
...spire.CommunityToolkit.OllamaSharp.Tests/Aspire.CommunityToolkit.OllamaSharp.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Aspire.CommunityToolkit.OllamaSharp\Aspire.CommunityToolkit.OllamaSharp.csproj" /> | ||
</ItemGroup> | ||
|
||
<PropertyGroup> | ||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
</Project> |
Oops, something went wrong.