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.pswp.cn/web/40416.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/40416.shtml
英文地址,請注明出處:http://en.pswp.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;以其專業的技術實力和優質的服…