-
Notifications
You must be signed in to change notification settings - Fork 294
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## Motivation and Context (Why the change? What's the scenario?) Although Ollama service protocol seemingly follows OpenAI behavior, Azure Open AI/OpenAI/SK OpenAI C# connectors are not fully functional when pointed to Ollama service, leading to various errors. This PR introduces a new connector dedicated to [Ollama](https://ollama.com), allowing to use it for Text Generation and Text Embedding generation. The Ollama connector can be used for service and serverless mode. The PR includes an example (212), I've also manually tested the service with the new connector, using "phi3:medium-128k" and "nomic-embed-text" on Apple Silicon. ## High level description (Approach, Design) * New Ollama connector based on [OllamaSharp](https://github.com/awaescher/OllamaSharp) * Bump version to 0.72 * Other minor changes: * Switch from gpt4 to gpt4o tokenizer, when no tokenizer is specified. * Add SensitiveDataLogger, usable only on dev environments to log data that potentially includes PII. * Fix warning in service logs when using Anthropic
- Loading branch information
Showing
37 changed files
with
1,136 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\service\Core\Core.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using Microsoft.KernelMemory; | ||
using Microsoft.KernelMemory.AI.Ollama; | ||
using Microsoft.KernelMemory.AI.OpenAI; | ||
using Microsoft.KernelMemory.Diagnostics; | ||
|
||
/* This example shows how to use KM with Ollama | ||
* | ||
* 1. Install and launch Ollama. You should see an icon for the app running the background. | ||
* | ||
* 2. Download your preferred models, e.g. | ||
* - ollama pull nomic-embed-text | ||
* - ollama pull phi3:medium-128k | ||
* | ||
* 3. Run the code below | ||
* | ||
* 4. Other things | ||
* Run "ollama show phi3:medium-128k" to see model's properties | ||
* Run "ollama list" to see the list of models you have on your system | ||
* Run "ollama serve" if you prefer running Ollama from the command line | ||
*/ | ||
public static class Program | ||
{ | ||
public static async Task Main() | ||
{ | ||
var logLevel = LogLevel.Warning; | ||
SensitiveDataLogger.Enabled = false; | ||
|
||
var config = new OllamaConfig | ||
{ | ||
Endpoint = "http://localhost:11434", | ||
TextModel = new OllamaModelConfig("phi3:medium-128k", 131072), | ||
EmbeddingModel = new OllamaModelConfig("nomic-embed-text", 2048) | ||
}; | ||
|
||
var memory = new KernelMemoryBuilder() | ||
.WithOllamaTextGeneration(config, new GPT4oTokenizer()) | ||
.WithOllamaTextEmbeddingGeneration(config, new GPT4oTokenizer()) | ||
.Configure(builder => builder.Services.AddLogging(l => | ||
{ | ||
l.SetMinimumLevel(logLevel); | ||
l.AddSimpleConsole(c => c.SingleLine = true); | ||
})) | ||
.Build(); | ||
|
||
// Import some text | ||
await memory.ImportTextAsync("Today is October 32nd, 2476"); | ||
|
||
// Generate an answer - This uses OpenAI for embeddings and finding relevant data, and LM Studio to generate an answer | ||
var answer = await memory.AskAsync("What's the current date (don't check for validity)?"); | ||
Console.WriteLine(answer.Question); | ||
Console.WriteLine(answer.Result); | ||
|
||
/* | ||
-- Output using phi3:medium-128k: | ||
What's the current date (don't check for validity)? | ||
The given fact states that "Today is October 32nd, 2476." However, it appears to be an incorrect statement as | ||
there are never more than 31 days in any month. If we consider this date without checking its validity and accept | ||
the stated day of October as being 32, then the current date would be "October 32nd, 2476." However, it is important | ||
to note that this date does not align with our calendar system. | ||
*/ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.