Skip to content

.Net: Add Ollama ChatCompletion with Vision Sample #11113

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

Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) Microsoft. All rights reserved.

using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Resources;

namespace ChatCompletion;

/// <summary>
/// This sample shows how to use llama3.2-vision model with different content types (text and image).
/// </summary>
public class Ollama_ChatCompletionWithVision(ITestOutputHelper output) : BaseTest(output)
{
/// <summary>
/// This sample uses a local image file and sends it to the model along
/// with a text message the get the description of the image.
/// </summary>
[Fact]
public async Task GetLocalImageDescription()
{
Console.WriteLine($"======== Ollama - {nameof(GetLocalImageDescription)} ========");

var imageBytes = await EmbeddedResource.ReadAllAsync("sample_image.jpg");

var kernel = Kernel.CreateBuilder()
.AddOllamaChatCompletion("llama3.2-vision")
.Build();

var chatCompletionService = kernel.GetRequiredService<IChatCompletionService>();

var chatHistory = new ChatHistory("You are a friendly assistant.");

chatHistory.AddUserMessage(
[
new TextContent("What’s in this image?"),
new ImageContent(imageBytes, "image/jpg")
]);

var reply = await chatCompletionService.GetChatMessageContentAsync(chatHistory);

Console.WriteLine(reply.Content);
}
}
1 change: 1 addition & 0 deletions dotnet/samples/Concepts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ dotnet test -l "console;verbosity=detailed" --filter "FullyQualifiedName=ChatCom
- [MultipleProviders_ChatHistoryReducer](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/Concepts/ChatCompletion/MultipleProviders_ChatHistoryReducer.cs)
- [Ollama_ChatCompletion](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/Concepts/ChatCompletion/Ollama_ChatCompletion.cs)
- [Ollama_ChatCompletionStreaming](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/Concepts/ChatCompletion/Ollama_ChatCompletionStreaming.cs)
- [Ollama_ChatCompletionWithVision](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/Concepts/ChatCompletion/Ollama_ChatCompletionWithVision.cs)
- [Onnx_ChatCompletion](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/Concepts/ChatCompletion/Onnx_ChatCompletion.cs)
- [Onnx_ChatCompletionStreaming](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/Concepts/ChatCompletion/Onnx_ChatCompletionStreaming.cs)
- [OpenAI_ChatCompletion](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/Concepts/ChatCompletion/OpenAI_ChatCompletion.cs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ public OllamaChatCompletionTests()
this._httpClient = new HttpClient(this._multiMessageHandlerStub, false) { BaseAddress = new Uri("http://localhost:11434") };
}

[Fact]
public void AddOllamaChatCompletionShouldWorkOnlyForModelId()
{
// Arrange
var kernel = Kernel.CreateBuilder().AddOllamaChatCompletion("model-id").Build();
var service = kernel.GetRequiredService<IChatCompletionService>();
}

[Fact]
public async Task ShouldSendPromptToServiceAsync()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Embeddings;
using OllamaSharp;
using OllamaSharp.Models;
Expand All @@ -24,6 +25,14 @@ public OllamaTextEmbeddingGenerationTests()
this._httpClient = new HttpClient(this._messageHandlerStub, false) { BaseAddress = new Uri("http://localhost:11434") };
}

[Fact]
public void AddOllamaTextEmbeddingGenerationShouldWorkOnlyForModelId()
{
// Arrange
var kernel = Kernel.CreateBuilder().AddOllamaTextEmbeddingGeneration("model-id").Build();
var service = kernel.GetRequiredService<ITextEmbeddingGenerationService>();
}

[Fact]
public async Task ShouldSendPromptToServiceAsync()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ public static IServiceCollection AddOllamaChatCompletion(

var loggerFactory = serviceProvider.GetService<ILoggerFactory>();

// Set default base address if not already set
httpClient.BaseAddress ??= new Uri("http://localhost:11434");

var builder = ((IChatClient)new OllamaApiClient(httpClient, modelId))
.AsBuilder()
.UseFunctionInvocation(loggerFactory, config => config.MaximumIterationsPerRequest = MaxInflightAutoInvokes);
Expand Down Expand Up @@ -299,6 +302,9 @@ public static IServiceCollection AddOllamaTextEmbeddingGeneration(
{
httpClient ??= HttpClientProvider.GetHttpClient(httpClient, serviceProvider);

// Set default base address if not already set
httpClient.BaseAddress ??= new Uri("http://localhost:11434");

var loggerFactory = serviceProvider.GetService<ILoggerFactory>();

var builder = ((IEmbeddingGenerator<string, Embedding<float>>)new OllamaApiClient(httpClient, modelId))
Expand Down
Loading