From de77b79b85d8d0ba6f7427f9eed3864f326746f6 Mon Sep 17 00:00:00 2001 From: Kevin BEAUGRAND Date: Thu, 4 Jan 2024 16:35:35 +0100 Subject: [PATCH] Move to .Net Standard2 --- .../OllamaChatCompletionService.cs | 31 +++++++++---------- ...ze.SemanticKernel.Connectors.Ollama.csproj | 8 +++-- .../OllamaTextEmbeddingGeneration.cs | 2 +- .../OllamaTextGenerationService.cs | 27 +++++++--------- 4 files changed, 32 insertions(+), 36 deletions(-) diff --git a/Codeblaze.SemanticKernel.Connectors.Ollama/ChatCompletion/OllamaChatCompletionService.cs b/Codeblaze.SemanticKernel.Connectors.Ollama/ChatCompletion/OllamaChatCompletionService.cs index e38145b..18c7fa0 100644 --- a/Codeblaze.SemanticKernel.Connectors.Ollama/ChatCompletion/OllamaChatCompletionService.cs +++ b/Codeblaze.SemanticKernel.Connectors.Ollama/ChatCompletion/OllamaChatCompletionService.cs @@ -20,7 +20,7 @@ public async Task> GetChatMessageContentsAsync { var system = string.Join("\n", chatHistory.Where(x => x.Role == AuthorRole.System).Select(x => x.Content)); var user = chatHistory.Last(x => x.Role == AuthorRole.User); - + var data = new { model = Attributes["model_id"] as string, @@ -34,7 +34,7 @@ public async Task> GetChatMessageContentsAsync ValidateOllamaResponse(response); - var json = JsonSerializer.Deserialize(await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false)); + var json = JsonSerializer.Deserialize(await response.Content.ReadAsStringAsync().ConfigureAwait(false)); return new List { new(AuthorRole.Assistant, json!["response"]!.GetValue(), modelId: Attributes["model_id"] as string) }; } @@ -45,7 +45,7 @@ public async IAsyncEnumerable GetStreamingChatMessa { var system = string.Join("\n", chatHistory.Where(x => x.Role == AuthorRole.System).Select(x => x.Content)); var user = chatHistory.Last(x => x.Role == AuthorRole.User); - + var data = new { model = Attributes["model_id"] as string, @@ -58,25 +58,22 @@ public async IAsyncEnumerable GetStreamingChatMessa var response = await Http.PostAsJsonAsync($"{Attributes["base_url"]}/api/generate", data, cancellationToken).ConfigureAwait(false); ValidateOllamaResponse(response); - - var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false); - await using (stream) - { - using var reader = new StreamReader(stream); + using var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false); - var done = false; + using var reader = new StreamReader(stream); - while (!done) - { - var json = JsonSerializer.Deserialize( - await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false) - ); + var done = false; + + while (!done) + { + var json = JsonSerializer.Deserialize( + await response.Content.ReadAsStringAsync().ConfigureAwait(false) + ); - done = json!["done"]!.GetValue(); + done = json!["done"]!.GetValue(); - yield return new StreamingChatMessageContent(AuthorRole.Assistant, json["response"]!.GetValue(), modelId: Attributes["model_id"] as string); - } + yield return new StreamingChatMessageContent(AuthorRole.Assistant, json["response"]!.GetValue(), modelId: Attributes["model_id"] as string); } } } \ No newline at end of file diff --git a/Codeblaze.SemanticKernel.Connectors.Ollama/Codeblaze.SemanticKernel.Connectors.Ollama.csproj b/Codeblaze.SemanticKernel.Connectors.Ollama/Codeblaze.SemanticKernel.Connectors.Ollama.csproj index 9b2d619..8e1c4df 100644 --- a/Codeblaze.SemanticKernel.Connectors.Ollama/Codeblaze.SemanticKernel.Connectors.Ollama.csproj +++ b/Codeblaze.SemanticKernel.Connectors.Ollama/Codeblaze.SemanticKernel.Connectors.Ollama.csproj @@ -1,6 +1,7 @@  - net8.0 + netstandard2.0 + Latest enable enable @@ -15,6 +16,7 @@ runtime; build; native; contentfiles; analyzers; buildtransitive all + @@ -43,8 +45,8 @@ - - + + diff --git a/Codeblaze.SemanticKernel.Connectors.Ollama/EmbeddingGeneration/OllamaTextEmbeddingGeneration.cs b/Codeblaze.SemanticKernel.Connectors.Ollama/EmbeddingGeneration/OllamaTextEmbeddingGeneration.cs index 0d73118..c499b3c 100644 --- a/Codeblaze.SemanticKernel.Connectors.Ollama/EmbeddingGeneration/OllamaTextEmbeddingGeneration.cs +++ b/Codeblaze.SemanticKernel.Connectors.Ollama/EmbeddingGeneration/OllamaTextEmbeddingGeneration.cs @@ -30,7 +30,7 @@ public async Task>> GenerateEmbeddingsAsync(IList(await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false)); + var json = JsonSerializer.Deserialize(await response.Content.ReadAsStringAsync().ConfigureAwait(false)); var embedding = new ReadOnlyMemory(json!["embedding"]?.AsArray().GetValues().ToArray()); diff --git a/Codeblaze.SemanticKernel.Connectors.Ollama/TextGenerationService/OllamaTextGenerationService.cs b/Codeblaze.SemanticKernel.Connectors.Ollama/TextGenerationService/OllamaTextGenerationService.cs index de122e1..de34378 100644 --- a/Codeblaze.SemanticKernel.Connectors.Ollama/TextGenerationService/OllamaTextGenerationService.cs +++ b/Codeblaze.SemanticKernel.Connectors.Ollama/TextGenerationService/OllamaTextGenerationService.cs @@ -27,7 +27,7 @@ public async Task> GetTextContentsAsync(string prompt ValidateOllamaResponse(response); - var json = JsonSerializer.Deserialize(await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false)); + var json = JsonSerializer.Deserialize(await response.Content.ReadAsStringAsync().ConfigureAwait(false)); return new List { new(json!["response"]!.GetValue()) }; } @@ -47,25 +47,22 @@ public async IAsyncEnumerable GetStreamingTextContentsAsyn var response = await Http.PostAsJsonAsync($"{Attributes["base_url"]}/api/generate", data, cancellationToken).ConfigureAwait(false); ValidateOllamaResponse(response); - - var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false); - await using (stream) - { - using var reader = new StreamReader(stream); + using var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false); + + using var reader = new StreamReader(stream); - var done = false; + var done = false; - while (!done) - { - var json = JsonSerializer.Deserialize( - await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false) - ); + while (!done) + { + var json = JsonSerializer.Deserialize( + await response.Content.ReadAsStringAsync().ConfigureAwait(false) + ); - done = json!["done"]!.GetValue(); + done = json!["done"]!.GetValue(); - yield return new StreamingTextContent(json["response"]!.GetValue()); - } + yield return new StreamingTextContent(json["response"]!.GetValue()); } } } \ No newline at end of file