Skip to content

Commit

Permalink
Adding Ollama Aspire integration (#625)
Browse files Browse the repository at this point in the history
* Adding Ollama Aspire integration

Using the Ollama hosting integration from the Community Toolkit in the app host. There is a conditional in place that will not enable ollama support by default.

Using the OllamaSharp client integration to provide the IChatClient and IEmbeddingGenerator implementations

* Using the updated MEAI structure

* Following the pattern for OpenAI with Ollama registration

* Removing usage of Keyed services for non-keyed registrations

* Update Directory.Packages.props

Co-authored-by: Eric Erhardt <[email protected]>

* Removing appsettings from old ollama design

---------

Co-authored-by: Eric Erhardt <[email protected]>
  • Loading branch information
aaronpowell and eerhardt authored Jan 3, 2025
1 parent cc4c3bf commit cd3f752
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 18 deletions.
4 changes: 3 additions & 1 deletion 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 @@ -99,4 +101,4 @@
<PackageVersion Include="IdentityModel" Version="7.0.0" />
<PackageVersion Include="Scalar.AspNetCore" Version="1.2.72"/>
</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
4 changes: 0 additions & 4 deletions src/Catalog.API/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@
"AI": {
"OpenAI": {
"EmbeddingModel": "text-embedding-3-small"
},
"Ollama": {
"Endpoint": "",
"EmbeddingModel": "all-minilm"
}
}
}
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
4 changes: 0 additions & 4 deletions src/WebApp/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@
"AI": {
"OpenAI": {
//"ChatModel": ""
},
"Ollama": {
"Endpoint": "",
"ChatModel": "llama3.1"
}
}
}
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

0 comments on commit cd3f752

Please sign in to comment.