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

Adding Ollama Aspire integration #625

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
6 changes: 4 additions & 2 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
<PackageVersion Include="Aspire.RabbitMQ.Client" Version="$(AspireVersion)" />
<PackageVersion Include="Aspire.StackExchange.Redis" Version="$(AspireVersion)" />
<PackageVersion Include="Aspire.Azure.AI.OpenAI" Version="$(AspireUnstablePackagesVersion)" />
<PackageVersion Include="CommunityToolkit.Aspire.Hosting.Ollama" Version="9.0.0" />
<PackageVersion Include="CommunityToolkit.Aspire.OllamaSharp" Version="9.0.0" />
<PackageVersion Include="Microsoft.Extensions.ServiceDiscovery" Version="$(AspireVersion)" />
<PackageVersion Include="Microsoft.Extensions.ServiceDiscovery.Yarp" Version="$(AspireVersion)" />
<!-- Version together with Asp.Versioning -->
Expand Down Expand Up @@ -93,6 +95,6 @@
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2" />
<PackageVersion Include="Yarp.ReverseProxy" Version="2.2.0" />
<PackageVersion Include="IdentityModel" Version="7.0.0" />
<PackageVersion Include="Scalar.AspNetCore" Version="1.2.36"/>
<PackageVersion Include="Scalar.AspNetCore" Version="1.2.36" />
</ItemGroup>
</Project>
</Project>
1 change: 1 addition & 0 deletions src/Catalog.API/Catalog.API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<ItemGroup>
<PackageReference Include="Asp.Versioning.Http" />
<PackageReference Include="Aspire.Npgsql.EntityFrameworkCore.PostgreSQL" />
<PackageReference Include="CommunityToolkit.Aspire.OllamaSharp" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
8 changes: 4 additions & 4 deletions src/Catalog.API/Extensions/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ public static void AddApplicationServices(this IHostApplicationBuilder builder)
builder.Services.AddOptions<CatalogOptions>()
.BindConfiguration(nameof(CatalogOptions));

if (builder.Configuration["AI:Ollama:Endpoint"] is string ollamaEndpoint && !string.IsNullOrWhiteSpace(ollamaEndpoint))
if (builder.Configuration["OllamaEnabled"] is string ollamaEnabled && bool.Parse(ollamaEnabled))
{
builder.AddKeyedOllamaSharpEmbeddingGenerator("embedding");
builder.Services.AddEmbeddingGenerator<string, Embedding<float>>(b => b
.UseOpenTelemetry()
.UseLogging()
.Use(new OllamaEmbeddingGenerator(
new Uri(ollamaEndpoint),
builder.Configuration["AI:Ollama:EmbeddingModel"])));
// Use the OllamaSharp embedding generator
.Use(b.Services.GetRequiredKeyedService<IEmbeddingGenerator<string, Embedding<float>>>("embedding")));
}
else if (!string.IsNullOrWhiteSpace(builder.Configuration.GetConnectionString("openai")))
{
Expand Down
9 changes: 4 additions & 5 deletions src/WebApp/Extensions/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,15 @@ public static void AddAuthenticationServices(this IHostApplicationBuilder builde

private static void AddAIServices(this IHostApplicationBuilder builder)
{
string? ollamaEndpoint = builder.Configuration["AI:Ollama:Endpoint"];
if (!string.IsNullOrWhiteSpace(ollamaEndpoint))
if (builder.Configuration["OllamaEnabled"] is string ollamaEnabled && bool.Parse(ollamaEnabled))
{
builder.AddKeyedOllamaSharpChatClient("chat");
builder.Services.AddChatClient(b => b
.UseFunctionInvocation()
.UseOpenTelemetry(configure: t => t.EnableSensitiveData = true)
.UseLogging()
.Use(new OllamaChatClient(
new Uri(ollamaEndpoint),
builder.Configuration["AI:Ollama:ChatModel"] ?? "llama3.1")));
// Use the OllamaSharp chat client
.Use(b.Services.GetRequiredKeyedService<IChatClient>("chat")));
}
else
{
Expand Down
1 change: 1 addition & 0 deletions src/WebApp/WebApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<ItemGroup>
<PackageReference Include="Asp.Versioning.Http.Client" />
<PackageReference Include="Aspire.Azure.AI.OpenAI" />
<PackageReference Include="CommunityToolkit.Aspire.OllamaSharp" />
<PackageReference Include="Microsoft.Extensions.ServiceDiscovery.Yarp" />
<PackageReference Include="Microsoft.Extensions.AI" />
<PackageReference Include="Microsoft.Extensions.AI.OpenAI" />
Expand Down
20 changes: 20 additions & 0 deletions src/eShop.AppHost/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,26 @@
builder.AddOpenAI(catalogApi, webApp);
}

bool useOllama = false;
if (useOllama)
{
var ollama = builder.AddOllama("ollama")
.WithDataVolume()
.WithOpenWebUI();
var embeddings = ollama.AddModel("embedding", "all-minilm");
var chat = ollama.AddModel("chat", "llama3.1");

// WaitFor blocked pending: https://github.com/CommunityToolkit/Aspire/issues/256
catalogApi.WithReference(embeddings)
.WithEnvironment("OllamaEnabled", "true")
//.WaitFor(embeddings)
;
webApp.WithReference(chat)
.WithEnvironment("OllamaEnabled", "true")
//.WaitFor(chat)
;
}

// Wire up the callback urls (self referencing)
webApp.WithEnvironment("CallBackUrl", webApp.GetEndpoint(launchProfileName));
webhooksClient.WithEnvironment("CallBackUrl", webhooksClient.GetEndpoint(launchProfileName));
Expand Down
1 change: 1 addition & 0 deletions src/eShop.AppHost/eShop.AppHost.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<PackageReference Include="Aspire.Hosting.Redis" />
<PackageReference Include="Aspire.Hosting.PostgreSQL" />
<PackageReference Include="Aspire.Hosting.Azure.CognitiveServices" />
<PackageReference Include="CommunityToolkit.Aspire.Hosting.Ollama" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading