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 kernelIKernelBuilder 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 itstring 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 examplefloat[] 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必须被更改。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/40416.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

idea中maven全局配置

配置了就不需要每次创建项目都来设置maven仓库了。 1.先把项目全关了 2. 进入全局设置 3.设置maven的仓库就可以了

SpringBoot实现多数据源切换

1. 概述 随着项目规模的扩大和业务需求的复杂化&#xff0c;单一数据源已经不能满足实际开发中的需求。在许多情况下&#xff0c;我们需要同时操作多个数据库&#xff0c;或者需要将不同类型的数据存储在不同的数据库中。这时&#xff0c;多数据源场景成为必不可少的解决方案。…

【CentOS7.6】docker部署EMQX教程,本地镜像直接导入(附下载链接),没法在云服务器上魔法拉取镜像的快来

总览 先把下载链接放在这里吧&#xff0c;这是 EMQX 的 tar 包&#xff0c;能够直接导入 CentOS 的 docker&#xff1a; 链接&#xff1a;https://pan.baidu.com/s/1rSGSLoVvj83ai6d5oolg8Q?pwd0108 提取码&#xff1a;0108 一、安装配置教程 1.将 EMQX-latest.tar 包导入…

服务器重装系统时数据丢失?有哪些方法可以避免

为了避免在服务器重装系统时数据丢失&#xff0c;可以采取以下预防措施&#xff1a; 1. 数据备份&#xff1a;在重装系统之前&#xff0c;备份所有重要的数据和配置文件。备份可以通过以下方式进行&#xff1a; - 使用外部存储设备(如USB硬盘、NAS等)进行备份。 - 利用备份软件…

学习成绩总是上不去?中学生把握好这5个环节,助你提高成绩

在中学时代&#xff0c;考试我们并不陌生。每隔一段时间&#xff0c;学校就会安排我们参加考试。学生时代&#xff0c;我们参加的考试有很多。对于中学生来说&#xff0c;考试成绩是我们一直关心的事情。很多学生非常努力的学习&#xff0c;成绩却上不去。这是非常可惜的&#…

[图解]企业应用架构模式2024新译本讲解19-数据映射器1

1 00:00:01,720 --> 00:00:03,950 下一个我们要讲的就是 2 00:00:04,660 --> 00:00:07,420 数据映射器这个模式 3 00:00:09,760 --> 00:00:13,420 这个也是在数据源模式里面 4 00:00:13,430 --> 00:00:14,820 用得最广泛的 5 00:00:16,250 --> 00:00:19,170…

【软件工程中的喷泉模型及其优缺点】

文章目录 一、喷泉模型是什么&#xff1f;二、喷泉模型的优点1. 灵活性和适应性2. 迭代开发3. 风险控制 三、喷泉模型的缺点1. 需求不明确性2. 可能造成资源浪费3. 需要良好的沟通与协作 一、喷泉模型是什么&#xff1f; 喷泉模型是一种迭代增量开发模型&#xff0c;其核心理念…

链篦机回转窑球团生产工艺

生球在回转窑氧化焙烧&#xff0c;回转窑头部设有燃烧器&#xff0c;燃料可以采用气体、固体、液体。 来自环冷机一冷却段的高温废气作为二次风进入窑内参与燃烧&#xff0c;烧成成品球进入环冷机。 环冷机采用鼓风冷却&#xff0c;热风风箱分为四段&#xff1a; 一段气体引至…

无人机有哪些关键技术?

一、控制技术 无人机的核心还是在控制上&#xff0c;飞控系统的可靠性、稳定性及可扩展性是其中重要的指标。可靠性上&#xff0c;除了器件选型之外&#xff0c;目前主要靠多余度来增加&#xff1b;稳定性主要体现在多场景下仍能保持良好的工作状态&#xff0c;主要靠算法来进…

QML-各类布局

Colunm布局 Column{id:colspacing: 30Repeater{id:repmodel: ListModel{}Button{width: 100height: 50text: "btn"index}}//开始时候移动move: Transition {NumberAnimation { properties: "x,y"; easing.type: Easing.OutBounce }}//添加时变化add:Transi…

【Nginx】docker运行Nginx及配置

Nginx镜像的获取 直接从Docker Hub拉取Nginx镜像通过Dockerfile构建Nginx镜像后拉取 二者区别 主要区别在于定制化程度和构建过程的控制&#xff1a; 直接拉取Nginx镜像&#xff1a; 简便性&#xff1a;直接使用docker pull nginx命令可以快速拉取官方的Nginx镜像。这个过程…

通透!手把教你如何从头构建一个机器学习模型

目录 1.业务理解 2.数据收集和准备 数据采集 探索性数据分析 (EDA) 和数据清理 特征选择 3.建立机器学习模型 选择正确的模型 分割数据 训练模型 模型评估 4.模型优化 5.部署模型 今天我将带领大家一步步的来构建一个机器学习模型。 我们将按照以下步骤开发客户流失…

赛博解压板

目录 开头程序程序的流程图程序的解压效果(暂无&#xff0c;但可以运行一下上面的代码)结尾 开头 大家好&#xff0c;我叫这是我58。今天&#xff0c;我们要看关于赛博解压板的一些东西。 程序 #define _CRT_SECURE_NO_WARNINGS 1 #define ROW 6//ROW表示行数&#xff0c;可…

【ARM 常见汇编指令学习 7.1 -- LDRH 半字读取指令】

请阅读【嵌入式开发学习必备专栏】 文章目录 LDRH 使用介绍LDRH&#xff08;Load Register Half-word&#xff09;总结 LDRH 使用介绍 在ARMv9架构中&#xff0c;汇编指令LDRH用于从内存中载入数据到寄存器的指令&#xff0c;下面将分别对它进行详细介绍&#xff1a; LDRH&am…

【基础算法】UE中实现轮播

本期作者&#xff1a;尼克 易知微3D引擎技术负责人 当前N 总数M 从0到M-1 从1到M 感谢阅读&#xff0c;以上内容均由易知微3D引擎团队原创设计&#xff0c;以及易知微版权所有&#xff0c;转载请注明出处&#xff0c;违者必究&#xff0c;谢谢您的合作。申请转载授权后台回复【…

【WebKit屏幕方向API全解析】掌握现代Web应用的方向感应

标题&#xff1a;【WebKit屏幕方向API全解析】掌握现代Web应用的方向感应 WebKit作为许多现代浏览器的内核&#xff0c;提供了对HTML5和CSS3的广泛支持&#xff0c;包括对屏幕方向的控制。屏幕方向API&#xff08;Screen Orientation API&#xff09;允许Web应用知道屏幕的方向…

左耳听风_114_113_Go编程模式修饰器

你好&#xff0c;我是陈浩&#xff0c;我名多尔多house.之前呢我写过一篇文章叫做python修饰器的函数式编程。 那这种模式呢可以很轻松的把一些函数啊装配到另外一些函数上。 让你的代码呢更加简单&#xff0c;也可以让一些小功能性的代码复用性更高。 让代码中的函数呢可以…

掌握XD数字设计:打造令人惊艳的用户体验

xd是adobe旗下一款主打UI界面设计-建立原型的软件&#xff0c;它可以将wireframe、design、以及prototype等UI/UX设计流程整合到一个软件中&#xff0c;算是一款与sketch对打的软件。 与PS相比&#xff0c;在UI设计方面&#xff0c;Adobe XD有非常突出的3个优点&#xff1a;能…

从0到1手写vue源码

模版引擎 数组join法&#xff08;字符串&#xff09; es6反引号法&#xff08;模版字符串换行&#xff09; mustache (小胡子) 引入mustache 模版引擎的使用 mustache.render(templatestr,data) mustache.render 循环简单数组 循环复杂数组 循环单项数组 数组的嵌套 musta…

江苏徐州SAP代理商有哪些?怎么选择?

在数字化浪潮席卷全球的今天&#xff0c;企业对于高效、智能的管理系统需求日益迫切。SAP作为全球领先的企业管理软件解决方案提供商&#xff0c;其产品在市场上享有极高的声誉。而在江苏徐州&#xff0c;哲讯智能科技作为SAP的代理商&#xff0c;以其专业的技术实力和优质的服…