Skip to content
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

.Net: ONNX 0.5.0 Add Ogahandle resource managment to Service [WIP] #9644

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 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
Expand Up @@ -34,7 +34,7 @@ public async Task ServicePromptAsync()

Console.WriteLine("======== Onnx - Chat Completion ========");

var chatService = new OnnxRuntimeGenAIChatCompletionService(
using var chatService = new OnnxRuntimeGenAIChatCompletionService(
modelId: TestConfiguration.Onnx.ModelId,
modelPath: TestConfiguration.Onnx.ModelPath);

Expand Down Expand Up @@ -105,5 +105,7 @@ public async Task ChatPromptAsync()
reply = await kernel.InvokePromptAsync(chatPrompt.ToString());

Console.WriteLine(reply);

(kernel.GetRequiredService<IChatCompletionService>() as IDisposable)?.Dispose();
RogerBarreto marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ public async Task StreamChatPromptAsync()
reply = await StreamMessageOutputFromKernelAsync(kernel, chatPrompt.ToString());

Console.WriteLine(reply);

(kernel.GetRequiredService<IChatCompletionService>() as IDisposable)?.Dispose();
}

/// <summary>
Expand Down Expand Up @@ -135,7 +137,7 @@ public async Task StreamTextFromChatAsync()
}
}

private async Task StartStreamingChatAsync(IChatCompletionService chatCompletionService)
private async Task StartStreamingChatAsync(OnnxRuntimeGenAIChatCompletionService chatCompletionService)
{
Console.WriteLine("Chat content:");
Console.WriteLine("------------------------");
Expand All @@ -156,9 +158,11 @@ private async Task StartStreamingChatAsync(IChatCompletionService chatCompletion

// Second assistant message
await StreamMessageOutputAsync(chatCompletionService, chatHistory, AuthorRole.Assistant);

chatCompletionService.Dispose();
}

private async Task StreamMessageOutputAsync(IChatCompletionService chatCompletionService, ChatHistory chatHistory, AuthorRole authorRole)
private async Task StreamMessageOutputAsync(OnnxRuntimeGenAIChatCompletionService chatCompletionService, ChatHistory chatHistory, AuthorRole authorRole)
{
bool roleWritten = false;
string fullMessage = string.Empty;
Expand Down
3 changes: 2 additions & 1 deletion dotnet/samples/Demos/OnnxSimpleRAG/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.InMemory;
using Microsoft.SemanticKernel.Connectors.Onnx;
using Microsoft.SemanticKernel.Data;
using Microsoft.SemanticKernel.Embeddings;
using Microsoft.SemanticKernel.PromptTemplates.Handlebars;
Expand All @@ -38,7 +39,7 @@
var kernel = builder.Build();

// Get the instances of the services
var chatService = kernel.GetRequiredService<IChatCompletionService>();
using var chatService = kernel.GetRequiredService<IChatCompletionService>() as OnnxRuntimeGenAIChatCompletionService;
var embeddingService = kernel.GetRequiredService<ITextEmbeddingGenerationService>();

// Create a vector store and a collection to store information
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public sealed class OnnxRuntimeGenAIChatCompletionService : IChatCompletionServi
private readonly JsonSerializerOptions? _jsonSerializerOptions;
private Model? _model;
private Tokenizer? _tokenizer;
private readonly OgaHandle _ogaHandle = new();

private Dictionary<string, object?> AttributesInternal { get; } = new();

Expand Down Expand Up @@ -212,5 +213,6 @@ public void Dispose()
{
this._tokenizer?.Dispose();
this._model?.Dispose();
this._ogaHandle.Dispose();
}
}
Loading