Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 4 additions & 4 deletions dotnet/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<PackageVersion Include="System.Text.Json" Version="8.0.4" />
<PackageVersion Include="System.Threading.Tasks.Extensions" Version="4.5.4" />
<PackageVersion Include="System.ValueTuple" Version="4.5.0" />
<PackageVersion Include="OllamaSharp" Version="2.0.10" />
<PackageVersion Include="OllamaSharp" Version="2.1.1" />
<!-- Tokenizers -->
<PackageVersion Include="Microsoft.ML.Tokenizers" Version="0.22.0-preview.24271.1" />
<PackageVersion Include="Microsoft.DeepDev.TokenizerLib" Version="1.3.3" />
Expand Down Expand Up @@ -135,8 +135,8 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<!-- OnnxRuntimeGenAI -->
<PackageVersion Include="Microsoft.ML.OnnxRuntimeGenAI" Version="0.3.0"/>
<PackageVersion Include="Microsoft.ML.OnnxRuntimeGenAI.Cuda" Version="0.3.0"/>
<PackageVersion Include="Microsoft.ML.OnnxRuntimeGenAI.DirectML" Version="0.3.0"/>
<PackageVersion Include="Microsoft.ML.OnnxRuntimeGenAI" Version="0.3.0" />
<PackageVersion Include="Microsoft.ML.OnnxRuntimeGenAI.Cuda" Version="0.3.0" />
<PackageVersion Include="Microsoft.ML.OnnxRuntimeGenAI.DirectML" Version="0.3.0" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ public async Task ShouldSendPromptToServiceAsync()
httpClient: this._httpClient);

//Act
await sut.GenerateEmbeddingsAsync(new List<string> { "fake-text" });
await sut.GenerateEmbeddingsAsync(["fake-text"]);

//Assert
var requestPayload = JsonSerializer.Deserialize<GenerateEmbeddingRequest>(this._messageHandlerStub.RequestContent);
Assert.NotNull(requestPayload);
Assert.Equal("fake-text", requestPayload.Prompt);
Assert.Equal("fake-text", requestPayload.Input[0]);
}

[Fact]
Expand All @@ -90,9 +90,10 @@ public async Task ShouldHandleServiceResponseAsync()

//Assert
Assert.NotNull(contents);
Assert.Equal(2, contents.Count);

var content = contents.SingleOrDefault();
Assert.Equal(8, content.Length);
var content = contents[0];
Assert.Equal(5, content.Length);
}

public void Dispose()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
{
"embedding": [
-0.08541165292263031,
0.08639130741357803,
-0.12805694341659546,
-0.2877824902534485,
0.2114177942276001,
-0.29374566674232483,
-0.10496602207422256,
0.009402364492416382
],
"model": "fake-model"
"model": "fake-model",
"embeddings": [
[
0.020765934,
0.007495159,
0.01268963,
0.013938076,
-0.04621073
],
[
0.025005031,
0.009804744,
-0.016960088,
-0.024823941,
-0.02756831
]
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.Extensions.Logging;
using Microsoft.SemanticKernel.Connectors.Ollama.Core;
using Microsoft.SemanticKernel.Embeddings;
using Microsoft.SemanticKernel.Services;
using OllamaSharp;
using OllamaSharp.Models;

Expand Down Expand Up @@ -58,20 +59,20 @@ public async Task<IList<ReadOnlyMemory<float>>> GenerateEmbeddingsAsync(
Kernel? kernel = null,
CancellationToken cancellationToken = default)
{
var tasks = new List<Task<GenerateEmbeddingResponse>>();
foreach (var prompt in data)
var request = new GenerateEmbeddingRequest
{
tasks.Add(this._client.GenerateEmbeddings(prompt, cancellationToken: cancellationToken));
}
Model = this.GetModelId()!,
Input = data.ToList()
};

var response = await this._client.GenerateEmbeddings(request, cancellationToken: cancellationToken).ConfigureAwait(false);

await Task.WhenAll(tasks.ToArray()).ConfigureAwait(false);
List<ReadOnlyMemory<float>> embeddings = [];
foreach (var embedding in response.Embeddings)
{
embeddings.Add(embedding.Select(@decimal => (float)@decimal).ToArray());
}

return new List<ReadOnlyMemory<float>>(
tasks.Select(
task => new ReadOnlyMemory<float>(task.Result.Embedding
.Select(@decimal => (float)@decimal).ToArray()
)
)
);
return embeddings;
}
}