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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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 @@ -26,6 +26,8 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageVersion>
<PackageVersion Include="CommunityToolkit.Aspire.Hosting.Ollama" Version="9.1.0" />
<PackageVersion Include="CommunityToolkit.Aspire.OllamaSharp" Version="9.1.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 @@ -97,6 +99,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.44"/>
<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 @@ -17,6 +17,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 @@ -37,12 +37,12 @@ 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.Services.AddEmbeddingGenerator(new OllamaEmbeddingGenerator(ollamaEndpoint, builder.Configuration["AI:Ollama:EmbeddingModel"]))
builder.AddOllamaSharpEmbeddingGenerator("embedding");
builder.Services.AddEmbeddingGenerator(b => b.GetRequiredService<IEmbeddingGenerator<string, Embedding<float>>>())
.UseOpenTelemetry()
.UseLogging()
.Build();
.UseLogging();
}
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,14 +98,13 @@ 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.Services.AddChatClient(new OllamaChatClient(ollamaEndpoint, builder.Configuration["AI:Ollama:ChatModel"] ?? "llama3.1"))
builder.AddOllamaSharpChatClient("chat");
builder.Services.AddChatClient(b => b.GetRequiredService<IChatClient>())
.UseFunctionInvocation()
.UseOpenTelemetry(configure: t => t.EnableSensitiveData = true)
.UseLogging()
.Build();
.UseLogging();
}
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
24 changes: 24 additions & 0 deletions src/eShop.AppHost/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,28 @@ public static IDistributedApplicationBuilder AddOpenAI(this IDistributedApplicat

return builder;
}

/// <summary>
/// Configures eShop projects to use Ollama for text embedding and chat.
/// </summary>
public static IDistributedApplicationBuilder AddOllama(this IDistributedApplicationBuilder builder,
IResourceBuilder<ProjectResource> catalogApi,
IResourceBuilder<ProjectResource> webApp)
{
var ollama = builder.AddOllama("ollama")
.WithDataVolume()
.WithGPUSupport()
.WithOpenWebUI();
var embeddings = ollama.AddModel("embedding", "all-minilm");
var chat = ollama.AddModel("chat", "llama3.1");

catalogApi.WithReference(embeddings)
.WithEnvironment("OllamaEnabled", "true")
.WaitFor(embeddings);
webApp.WithReference(chat)
.WithEnvironment("OllamaEnabled", "true")
.WaitFor(chat);

return builder;
}
}
6 changes: 6 additions & 0 deletions src/eShop.AppHost/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@
builder.AddOpenAI(catalogApi, webApp);
}

bool useOllama = false;
if (useOllama)
{
builder.AddOllama(catalogApi, webApp);
}

// 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