Skip to content

Commit

Permalink
.Net: Adding Ollama extension for improved Aspire experience (#10324)
Browse files Browse the repository at this point in the history
### Motivation and Context

- Resolves #10321
  • Loading branch information
RogerBarreto authored Jan 28, 2025
1 parent f005058 commit 989b8d5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
5 changes: 4 additions & 1 deletion docs/decisions/0054-processes.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ In technical terms, a process is something that can be represented as a graph wh

- Customers should be able to leverage their existing investments in all supported languages of Semantic Kernel.
- ```
```

- Customers should be able to leverage their existing investments in infrastructure.
- Customers should be able to collaborate with their business process peers to build up composable processes.
- Customers should be able to use AI to enhance and streamline the steps within their business processes.
Expand Down Expand Up @@ -314,5 +317,5 @@ The following packages will be created for Processes:

In validation of the proposed solution, two runtimes were created, one for the local/server scenario and one for the distributed actor scenario using Orleans. Both of these implementation were based on the [Pregel Algorithm](https://kowshik.github.io/JPregel/pregel_paper.pdf) for large-scale graph processing. This algorithm is well tested and well suited for single machine scenarios as well as distributed systems. More information on how the Pregel algorithm works can be found in the following links.

- [Pregel - The Morning Paper](https://blog.acolyer.org/2015/05/26/pregel-a-system-for-large-scale-graph-processing/)
<!-- [Pregel - The Morning Paper](https://blog.acolyer.org/2015/05/26/pregel-a-system-for-large-scale-graph-processing/) -->
<!-- [Pregel - Distributed Algorithms and Optimization](https://web.stanford.edu/~rezab/classes/cme323/S15/notes/lec8.pdf) -->
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.SemanticKernel.Connectors.Ollama;
using Microsoft.SemanticKernel.Embeddings;
using Microsoft.SemanticKernel.TextGeneration;
using OllamaSharp;
using Xunit;

namespace SemanticKernel.Connectors.Ollama.UnitTests.Extensions;
Expand Down Expand Up @@ -41,6 +42,32 @@ public void AddOllamaChatCompletionToServiceCollection()
Assert.NotNull(service);
}

[Fact]
public void AddOllamaChatCompletionFromServiceCollection()
{
var services = new ServiceCollection();
using var ollamaClient = new OllamaApiClient(new Uri("http://localhost:11434"), "model");

services.AddSingleton(ollamaClient);
services.AddOllamaChatCompletion();
var serviceProvider = services.BuildServiceProvider();
var service = serviceProvider.GetRequiredService<IChatCompletionService>();
Assert.NotNull(service);
}

[Fact]
public void AddOllamaTextEmbeddingGenerationFromServiceCollection()
{
var services = new ServiceCollection();
using var ollamaClient = new OllamaApiClient(new Uri("http://localhost:11434"), "model");

services.AddSingleton(ollamaClient);
services.AddOllamaTextEmbeddingGeneration();
var serviceProvider = services.BuildServiceProvider();
var service = serviceProvider.GetRequiredService<ITextEmbeddingGenerationService>();
Assert.NotNull(service);
}

[Fact]
public void AddOllamaTextEmbeddingsGenerationToServiceCollection()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,15 @@ public static IServiceCollection AddOllamaChatCompletion(
/// <returns>The updated kernel builder.</returns>
public static IServiceCollection AddOllamaChatCompletion(
this IServiceCollection services,
OllamaApiClient ollamaClient,
OllamaApiClient? ollamaClient = null,
string? serviceId = null)
{
Verify.NotNull(services);

return services.AddKeyedSingleton<IChatCompletionService>(serviceId, (serviceProvider, _) =>
{
var loggerFactory = serviceProvider.GetService<ILoggerFactory>();
ollamaClient ??= serviceProvider.GetRequiredService<OllamaApiClient>();

var builder = ((IChatClient)ollamaClient)
.AsBuilder()
Expand Down Expand Up @@ -274,14 +275,15 @@ public static IServiceCollection AddOllamaTextEmbeddingGeneration(
/// <returns>The updated kernel builder.</returns>
public static IServiceCollection AddOllamaTextEmbeddingGeneration(
this IServiceCollection services,
OllamaApiClient ollamaClient,
OllamaApiClient? ollamaClient = null,
string? serviceId = null)
{
Verify.NotNull(services);

return services.AddKeyedSingleton<ITextEmbeddingGenerationService>(serviceId, (serviceProvider, _) =>
{
var loggerFactory = serviceProvider.GetService<ILoggerFactory>();
ollamaClient ??= serviceProvider.GetRequiredService<OllamaApiClient>();

var builder = ((IEmbeddingGenerator<string, Embedding<float>>)ollamaClient)
.AsBuilder();
Expand Down

0 comments on commit 989b8d5

Please sign in to comment.