-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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 This PR brings support for Ollama Connector, this Connector uses the `OllamaSharp` library client to allow usage of native Ollama Endpoints. --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com> Co-authored-by: Dmytro Struk <13853051+dmytrostruk@users.noreply.github.com> Co-authored-by: Ikko Eltociear Ashimine <eltociear@gmail.com> Co-authored-by: Chris <66376200+crickman@users.noreply.github.com> Co-authored-by: ShuaiHua Du <shuaihua.du@outlook.com> Co-authored-by: Krzysztof Kasprowicz <60486987+Krzysztof318@users.noreply.github.com> Co-authored-by: Mark Wallace <127216156+markwallace-microsoft@users.noreply.github.com> Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com> Co-authored-by: Nico Möller <nkm-moeller@mail.de> Co-authored-by: Nico Möller <nicomoller@microsoft.com> Co-authored-by: westey <164392973+westey-m@users.noreply.github.com> Co-authored-by: Tao Chen <taochen@microsoft.com> Co-authored-by: Eduard van Valkenburg <eavanvalkenburg@users.noreply.github.com> Co-authored-by: NEWTON MALLICK <38786893+N-E-W-T-O-N@users.noreply.github.com> Co-authored-by: qowlsdn8007 <33804074+qowlsdn8007@users.noreply.github.com> Co-authored-by: Gil LaHaye <gillahaye@microsoft.com>
- Loading branch information
1 parent
504d60c
commit f149f95
Showing
40 changed files
with
2,767 additions
and
37 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
73 changes: 73 additions & 0 deletions
73
dotnet/samples/Concepts/ChatCompletion/Ollama_ChatCompletion.cs
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,73 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System.Text; | ||
using Microsoft.SemanticKernel; | ||
using Microsoft.SemanticKernel.ChatCompletion; | ||
using Microsoft.SemanticKernel.Connectors.Ollama; | ||
|
||
namespace ChatCompletion; | ||
|
||
// The following example shows how to use Semantic Kernel with Ollama Chat Completion API | ||
public class Ollama_ChatCompletion(ITestOutputHelper output) : BaseTest(output) | ||
{ | ||
[Fact] | ||
public async Task ServicePromptAsync() | ||
{ | ||
Assert.NotNull(TestConfiguration.Ollama.ModelId); | ||
|
||
Console.WriteLine("======== Ollama - Chat Completion ========"); | ||
|
||
var chatService = new OllamaChatCompletionService( | ||
endpoint: new Uri(TestConfiguration.Ollama.Endpoint), | ||
modelId: TestConfiguration.Ollama.ModelId); | ||
|
||
Console.WriteLine("Chat content:"); | ||
Console.WriteLine("------------------------"); | ||
|
||
var chatHistory = new ChatHistory("You are a librarian, expert about books"); | ||
|
||
// First user message | ||
chatHistory.AddUserMessage("Hi, I'm looking for book suggestions"); | ||
this.OutputLastMessage(chatHistory); | ||
|
||
// First assistant message | ||
var reply = await chatService.GetChatMessageContentAsync(chatHistory); | ||
chatHistory.Add(reply); | ||
this.OutputLastMessage(chatHistory); | ||
|
||
// Second user message | ||
chatHistory.AddUserMessage("I love history and philosophy, I'd like to learn something new about Greece, any suggestion"); | ||
this.OutputLastMessage(chatHistory); | ||
|
||
// Second assistant message | ||
reply = await chatService.GetChatMessageContentAsync(chatHistory); | ||
chatHistory.Add(reply); | ||
this.OutputLastMessage(chatHistory); | ||
} | ||
|
||
[Fact] | ||
public async Task ChatPromptAsync() | ||
{ | ||
Assert.NotNull(TestConfiguration.Ollama.ModelId); | ||
|
||
StringBuilder chatPrompt = new(""" | ||
<message role="system">You are a librarian, expert about books</message> | ||
<message role="user">Hi, I'm looking for book suggestions</message> | ||
"""); | ||
|
||
var kernel = Kernel.CreateBuilder() | ||
.AddOllamaChatCompletion( | ||
endpoint: new Uri(TestConfiguration.Ollama.Endpoint ?? "http://localhost:11434"), | ||
modelId: TestConfiguration.Ollama.ModelId) | ||
.Build(); | ||
|
||
var reply = await kernel.InvokePromptAsync(chatPrompt.ToString()); | ||
|
||
chatPrompt.AppendLine($"<message role=\"assistant\"><![CDATA[{reply}]]></message>"); | ||
chatPrompt.AppendLine("<message role=\"user\">I love history and philosophy, I'd like to learn something new about Greece, any suggestion</message>"); | ||
|
||
reply = await kernel.InvokePromptAsync(chatPrompt.ToString()); | ||
|
||
Console.WriteLine(reply); | ||
} | ||
} |
161 changes: 161 additions & 0 deletions
161
dotnet/samples/Concepts/ChatCompletion/Ollama_ChatCompletionStreaming.cs
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,161 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System.Text; | ||
using Microsoft.SemanticKernel; | ||
using Microsoft.SemanticKernel.ChatCompletion; | ||
using Microsoft.SemanticKernel.Connectors.Ollama; | ||
|
||
namespace ChatCompletion; | ||
|
||
/// <summary> | ||
/// These examples demonstrate the ways different content types are streamed by Ollama via the chat completion service. | ||
/// </summary> | ||
public class Ollama_ChatCompletionStreaming(ITestOutputHelper output) : BaseTest(output) | ||
{ | ||
/// <summary> | ||
/// This example demonstrates chat completion streaming using Ollama. | ||
/// </summary> | ||
[Fact] | ||
public Task StreamChatAsync() | ||
{ | ||
Assert.NotNull(TestConfiguration.Ollama.ModelId); | ||
|
||
Console.WriteLine("======== Ollama - Chat Completion Streaming ========"); | ||
|
||
var chatService = new OllamaChatCompletionService( | ||
endpoint: new Uri(TestConfiguration.Ollama.Endpoint), | ||
modelId: TestConfiguration.Ollama.ModelId); | ||
|
||
return this.StartStreamingChatAsync(chatService); | ||
} | ||
|
||
[Fact] | ||
public async Task StreamChatPromptAsync() | ||
{ | ||
Assert.NotNull(TestConfiguration.Ollama.ModelId); | ||
|
||
StringBuilder chatPrompt = new(""" | ||
<message role="system">You are a librarian, expert about books</message> | ||
<message role="user">Hi, I'm looking for book suggestions</message> | ||
"""); | ||
|
||
var kernel = Kernel.CreateBuilder() | ||
.AddOllamaChatCompletion( | ||
endpoint: new Uri(TestConfiguration.Ollama.Endpoint), | ||
modelId: TestConfiguration.Ollama.ModelId) | ||
.Build(); | ||
|
||
var reply = await StreamMessageOutputFromKernelAsync(kernel, chatPrompt.ToString()); | ||
|
||
chatPrompt.AppendLine($"<message role=\"assistant\"><![CDATA[{reply}]]></message>"); | ||
chatPrompt.AppendLine("<message role=\"user\">I love history and philosophy, I'd like to learn something new about Greece, any suggestion</message>"); | ||
|
||
reply = await StreamMessageOutputFromKernelAsync(kernel, chatPrompt.ToString()); | ||
|
||
Console.WriteLine(reply); | ||
} | ||
|
||
/// <summary> | ||
/// This example demonstrates how the chat completion service streams text content. | ||
/// It shows how to access the response update via StreamingChatMessageContent.Content property | ||
/// and alternatively via the StreamingChatMessageContent.Items property. | ||
/// </summary> | ||
[Fact] | ||
public async Task StreamTextFromChatAsync() | ||
{ | ||
Assert.NotNull(TestConfiguration.Ollama.ModelId); | ||
|
||
Console.WriteLine("======== Stream Text from Chat Content ========"); | ||
|
||
// Create chat completion service | ||
var chatService = new OllamaChatCompletionService( | ||
endpoint: new Uri(TestConfiguration.Ollama.Endpoint), | ||
modelId: TestConfiguration.Ollama.ModelId); | ||
|
||
// Create chat history with initial system and user messages | ||
ChatHistory chatHistory = new("You are a librarian, an expert on books."); | ||
chatHistory.AddUserMessage("Hi, I'm looking for book suggestions."); | ||
chatHistory.AddUserMessage("I love history and philosophy. I'd like to learn something new about Greece, any suggestion?"); | ||
|
||
// Start streaming chat based on the chat history | ||
await foreach (StreamingChatMessageContent chatUpdate in chatService.GetStreamingChatMessageContentsAsync(chatHistory)) | ||
{ | ||
// Access the response update via StreamingChatMessageContent.Content property | ||
Console.Write(chatUpdate.Content); | ||
|
||
// Alternatively, the response update can be accessed via the StreamingChatMessageContent.Items property | ||
Console.Write(chatUpdate.Items.OfType<StreamingTextContent>().FirstOrDefault()); | ||
} | ||
} | ||
|
||
private async Task StartStreamingChatAsync(IChatCompletionService chatCompletionService) | ||
{ | ||
Console.WriteLine("Chat content:"); | ||
Console.WriteLine("------------------------"); | ||
|
||
var chatHistory = new ChatHistory("You are a librarian, expert about books"); | ||
this.OutputLastMessage(chatHistory); | ||
|
||
// First user message | ||
chatHistory.AddUserMessage("Hi, I'm looking for book suggestions"); | ||
this.OutputLastMessage(chatHistory); | ||
|
||
// First assistant message | ||
await StreamMessageOutputAsync(chatCompletionService, chatHistory, AuthorRole.Assistant); | ||
|
||
// Second user message | ||
chatHistory.AddUserMessage("I love history and philosophy, I'd like to learn something new about Greece, any suggestion?"); | ||
this.OutputLastMessage(chatHistory); | ||
|
||
// Second assistant message | ||
await StreamMessageOutputAsync(chatCompletionService, chatHistory, AuthorRole.Assistant); | ||
} | ||
|
||
private async Task StreamMessageOutputAsync(IChatCompletionService chatCompletionService, ChatHistory chatHistory, AuthorRole authorRole) | ||
{ | ||
bool roleWritten = false; | ||
string fullMessage = string.Empty; | ||
|
||
await foreach (var chatUpdate in chatCompletionService.GetStreamingChatMessageContentsAsync(chatHistory)) | ||
{ | ||
if (!roleWritten && chatUpdate.Role.HasValue) | ||
{ | ||
Console.Write($"{chatUpdate.Role.Value}: {chatUpdate.Content}"); | ||
roleWritten = true; | ||
} | ||
|
||
if (chatUpdate.Content is { Length: > 0 }) | ||
{ | ||
fullMessage += chatUpdate.Content; | ||
Console.Write(chatUpdate.Content); | ||
} | ||
} | ||
|
||
Console.WriteLine("\n------------------------"); | ||
chatHistory.AddMessage(authorRole, fullMessage); | ||
} | ||
|
||
private async Task<string> StreamMessageOutputFromKernelAsync(Kernel kernel, string prompt) | ||
{ | ||
bool roleWritten = false; | ||
string fullMessage = string.Empty; | ||
|
||
await foreach (var chatUpdate in kernel.InvokePromptStreamingAsync<StreamingChatMessageContent>(prompt)) | ||
{ | ||
if (!roleWritten && chatUpdate.Role.HasValue) | ||
{ | ||
Console.Write($"{chatUpdate.Role.Value}: {chatUpdate.Content}"); | ||
roleWritten = true; | ||
} | ||
|
||
if (chatUpdate.Content is { Length: > 0 }) | ||
{ | ||
fullMessage += chatUpdate.Content; | ||
Console.Write(chatUpdate.Content); | ||
} | ||
} | ||
|
||
Console.WriteLine("\n------------------------"); | ||
return fullMessage; | ||
} | ||
} |
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.