Using a text embedding model locally with semantic kernel

题意:在本地使用带有语义核(Semantic Kernel)的文本嵌入模型

问题背景:

I've been reading Stephen Toub's blog post about building a simple console-based .NET chat application from the ground up with semantic-kernel. I'm following the examples but instead of OpenAI I want to use microsoft Phi 3 and the nomic embedding model. The first examples in the blog post I could recreate using the semantic kernel huggingface plugin. But I can't seem to run the text embedding example.

我一直在阅读Stephen Toub的博客文章,文章讲述了如何使用语义核(semantic-kernel)从头开始构建一个基于控制台的简单.NET聊天应用程序。我按照示例操作,但我想使用微软的Phi 3和nomic嵌入模型,而不是OpenAI。我能够使用语义核的huggingface插件重现博客文章中的第一个示例。但是,我似乎无法运行文本嵌入的示例。

I've downloaded Phi and nomic embed text and are running them on a local server with lm studio.

我已经下载了Phi和nomic嵌入文本模型,并正在使用lm studio在本地服务器上运行它们。

Here's the code I came up with that uses the huggingface plugin:

这里是我编写的使用huggingface插件的代码

using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Embeddings;
using Microsoft.SemanticKernel.Memory;
using System.Numerics.Tensors;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.SemanticKernel.ChatCompletion;

#pragma warning disable SKEXP0070, SKEXP0003, SKEXP0001, SKEXP0011, SKEXP0052, SKEXP0055, SKEXP0050  // Type is for evaluation purposes only and is subject to change or removal in future updates. 

internal class Program
{
    private static async Task Main(string[] args)
    {
        //Suppress this diagnostic to proceed.
        // Initialize the Semantic kernel
        IKernelBuilder kernelBuilder = Kernel.CreateBuilder();
        kernelBuilder.Services.ConfigureHttpClientDefaults(c => c.AddStandardResilienceHandler());
        var kernel = kernelBuilder
            .AddHuggingFaceTextEmbeddingGeneration("nomic-ai/nomic-embed-text-v1.5-GGUF/nomic-embed-text-v1.5.Q8_0.gguf",
            new Uri("http://localhost:1234/v1"),
            apiKey: "lm-studio",
            serviceId: null)
            .Build();

        var embeddingGenerator = kernel.GetRequiredService<ITextEmbeddingGenerationService>();
        var memoryBuilder = new MemoryBuilder();
        memoryBuilder.WithTextEmbeddingGeneration(embeddingGenerator);
        memoryBuilder.WithMemoryStore(new VolatileMemoryStore());
        var memory = memoryBuilder.Build();
        // Download a document and create embeddings for it
        string input = "What is an amphibian?";
        string[] examples = [ "What is an amphibian?",
                              "Cos'è un anfibio?",
                              "A frog is an amphibian.",
                              "Frogs, toads, and salamanders are all examples.",
                              "Amphibians are four-limbed and ectothermic vertebrates of the class Amphibia.",
                              "They are four-limbed and ectothermic vertebrates.",
                              "A frog is green.",
                              "A tree is green.",
                              "It's not easy bein' green.",
                              "A dog is a mammal.",
                              "A dog is a man's best friend.",
                              "You ain't never had a friend like me.",
                              "Rachel, Monica, Phoebe, Joey, Chandler, Ross"];
        for (int i = 0; i < examples.Length; i++)
            await memory.SaveInformationAsync("net7perf", examples[i], $"paragraph{i}");
        var embed = await embeddingGenerator.GenerateEmbeddingsAsync([input]);
        ReadOnlyMemory<float> inputEmbedding = (embed)[0];
        // Generate embeddings for each chunk.
        IList<ReadOnlyMemory<float>> embeddings = await embeddingGenerator.GenerateEmbeddingsAsync(examples);
        // Print the cosine similarity between the input and each example
        float[] similarity = embeddings.Select(e => TensorPrimitives.CosineSimilarity(e.Span, inputEmbedding.Span)).ToArray();
        similarity.AsSpan().Sort(examples.AsSpan(), (f1, f2) => f2.CompareTo(f1));
        Console.WriteLine("Similarity Example");
        for (int i = 0; i < similarity.Length; i++)
            Console.WriteLine($"{similarity[i]:F6}   {examples[i]}");
    }
}

At the line:   这部分代码存在问题

for (int i = 0; i < examples.Length; i++)
    await memory.SaveInformationAsync("net7perf", examples[i], $"paragraph{i}");

I get the following exception:        得到了下面的异常信息

JsonException: The JSON value could not be converted to Microsoft.SemanticKernel.Connectors.HuggingFace.Core.TextEmbeddingResponse

Does anybody know what I'm doing wrong?        有人知道我错在哪里吗?

I've downloaded the following nuget packages into the project:

我已经将以下NuGet包下载到项目中:

IdVersionsProjectName
Microsoft.SemanticKernel.Core{1.15.0}LocalLlmApp
Microsoft.SemanticKernel.Plugins.Memory{1.15.0-alpha}LocalLlmApp
Microsoft.Extensions.Http.Resilience{8.6.0}LocalLlmApp
Microsoft.Extensions.Logging{8.0.0}LocalLlmApp
Microsoft.SemanticKernel.Connectors.HuggingFace{1.15.0-preview}LocalLlmApp
Newtonsoft.Json{13.0.3}LocalLlmApp
Microsoft.Extensions.Logging.Console{8.0.0}LocalLlmApp

问题解决:

I think you cannot use AddHuggingFaceTextEmbeddingGeneration with an embedding model from LM Studio out of the box. The reason is that the HuggingFaceClient internally changes the url and adds:

我认为你不能直接使用AddHuggingFaceTextEmbeddingGeneration与LM Studio中的嵌入模型,因为HuggingFaceClient内部会更改URL并添加:

pipeline/feature-extraction/

private Uri GetEmbeddingGenerationEndpoint(string modelId)
     => new($"{this.Endpoint}{this.Separator}pipeline/feature-extraction/{modelId}");

that's the same as the Error Message I get in the LM Studio Console:

这与我在LM Studio控制台中收到的错误信息相同:

[2024-07-03 22:18:19.898] [ERROR] Unexpected endpoint or method. (POST /v1/embedding/pipeline/feature-extraction/nomic-ai/nomic-embed-text-v1.5-GGUF/nomic-embed-text-v1.5.Q5_K_M.gguf). Returning 200 anyway

In order to get this working the url would have to be changed.

为了使这个工作正常进行,URL必须被更改。

  • 38
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Sure, here's an example of how you can design a PyTorch model using embeddings: ``` import torch import torch.nn as nn class EmbeddingModel(nn.Module): def __init__(self, vocab_size, embedding_dim, hidden_size, num_classes): super(EmbeddingModel, self).__init__() self.embedding = nn.Embedding(vocab_size, embedding_dim) self.rnn = nn.LSTM(embedding_dim, hidden_size, batch_first=True) self.fc = nn.Linear(hidden_size, num_classes) def forward(self, inputs): # inputs shape: (batch_size, seq_len) embedded = self.embedding(inputs) # embedded shape: (batch_size, seq_len, embedding_dim) output, _ = self.rnn(embedded) # output shape: (batch_size, seq_len, hidden_size) logits = self.fc(output[:, -1, :]) # logits shape: (batch_size, num_classes) return logits ``` In this example, we're creating a model that takes in sequences of integers (representing words in a sentence) and outputs a classification. The `EmbeddingModel` class inherits from `nn.Module` and defines three layers: 1. An `Embedding` layer that creates a learned embedding for each word in the vocabulary. The `vocab_size` parameter specifies the number of unique words in the vocabulary, and `embedding_dim` specifies the size of the learned embeddings. 2. An `LSTM` layer that takes the embedded input sequences and outputs a sequence of hidden states. The `hidden_size` parameter specifies the number of hidden units in the LSTM. 3. A fully connected `Linear` layer that takes the final hidden state of the LSTM and produces the output logits. `num_classes` specifies the number of classes we're trying to classify. In the `forward` method, we first pass the input sequences through the embedding layer to get the learned embeddings. Then we pass the embedded sequences through the LSTM layer to get a sequence of hidden states. Finally, we take the last hidden state (corresponding to the end of the sequence) and pass it through the fully connected layer to get the final logits. Note that we're using the `batch_first=True` parameter in the LSTM layer so that the input and output shapes are `(batch_size, seq_len, embedding_dim)` and `(batch_size, seq_len, hidden_size)` instead of `(seq_len, batch_size, embedding_dim)` and `(seq_len, batch_size, hidden_size)`. This is just a matter of personal preference, but it can make the code easier to read and write.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

营赢盈英

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值