You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Semantic Kernel本地调用Nomic嵌入模型遇JSON转换异常求助

问题排查:Semantic Kernel + LM Studio本地嵌入模型JsonException错误

问题描述

参考Stephen Toub的博客基于Semantic Kernel构建.NET控制台聊天应用,计划用Microsoft Phi 3和Nomic文本嵌入模型替代OpenAI。已通过LM Studio在本地部署两个模型,使用HuggingFace插件复现部分示例时,执行memory.SaveInformationAsync代码触发JsonException,提示「无法将JSON值转换为Microsoft.SemanticKernel.Connectors.HuggingFace.Core.TextEmbeddingResponse」。

实现代码

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)
    {
        // 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]}");
    }
}

已安装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

解决方法

1. 替换连接器类型

LM Studio提供的是OpenAI兼容API,而非HuggingFace Inference API格式,所以应该使用Semantic Kernel的OpenAI文本嵌入连接器,而非HuggingFace连接器:

  • 安装NuGet包Microsoft.SemanticKernel.Connectors.OpenAI(版本匹配1.15.0)
  • 修改嵌入服务注册代码:
    var kernel = kernelBuilder
        .AddOpenAITextEmbeddingGeneration(
            modelId: "nomic-ai/nomic-embed-text-v1.5-GGUF/nomic-embed-text-v1.5.Q8_0.gguf",
            apiKey: "lm-studio",
            endpoint: new Uri("http://localhost:1234/v1")
        )
        .Build();
    

2. 统一NuGet包版本

当前混合使用正式版、alpha版和preview版,存在兼容性风险:

  • 移除Microsoft.SemanticKernel.Plugins.Memory的1.15.0-alpha版本,安装正式版1.15.0
  • 移除Microsoft.SemanticKernel.Connectors.HuggingFace预览版,改用OpenAI连接器包

3. 验证LM Studio响应格式

通过curl或Postman调用嵌入接口,确认返回格式符合OpenAI规范:

curl http://localhost:1234/v1/embeddings \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer lm-studio" \
  -d '{
    "input": "test",
    "model": "nomic-ai/nomic-embed-text-v1.5-GGUF/nomic-embed-text-v1.5.Q8_0.gguf"
  }'

预期响应应包含data数组,每个元素含embedding字段,这是与HuggingFace格式的核心差异。

4. 简化内存构建代码(可选)

无需手动构建MemoryBuilder,直接从Kernel服务获取:

var memory = kernel.Services.GetRequiredService<ISemanticTextMemory>();

内容的提问来源于stack exchange,提问作者Robert

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 18:45:59