前言

本文通过Codeblaze.SemanticKernel这个项目,学习如何实现ITextEmbeddingGenerationService接口,接入本地嵌入模型。

项目地址:https://github.com/BLaZeKiLL/Codeblaze.SemanticKernel

实践

SemanticKernel初看以为只支持OpenAI的各种模型,但其实也提供了强大的抽象能力,可以通过自己实现接口,来实现接入不兼容OpenAI格式的模型。

Codeblaze.SemanticKernel这个项目实现了ITextGenerationService、IChatCompletionService与ITextEmbeddingGenerationService接口,由于现在Ollama的对话已经支持了OpenAI格式,因此可以不用实现ITextGenerationService和IChatCompletionService来接入Ollama中的模型了,但目前Ollama的嵌入还没有兼容OpenAI的格式,因此可以通过实现ITextEmbeddingGenerationService接口,接入Ollama中的嵌入模型。

查看ITextEmbeddingGenerationService接口:

代表了一种生成浮点类型文本嵌入的生成器。

再看看IEmbeddingGenerationService<string, float>接口:

[Experimental("SKEXP0001")]
public interface IEmbeddingGenerationService<TValue, TEmbedding> : IAIService where TEmbedding : unmanaged
{
     Task<IList<ReadOnlyMemory<TEmbedding>>> GenerateEmbeddingsAsync(IList<TValue> data, Kernel? kernel = null, CancellationToken cancellationToken = default(CancellationToken));
}

再看看IAIService接口:

说明我们只要实现了

Task<IList<ReadOnlyMemory<TEmbedding>>> GenerateEmbeddingsAsync(IList<TValue> data, Kernel? kernel = null, CancellationToken cancellationToken = default(CancellationToken));

IReadOnlyDictionary<string, object?> Attributes { get; }

这个方法和属性就行。

学习Codeblaze.SemanticKernel中是怎么做的。

添加OllamaBase类:

 public interface IOllamaBase
{
    Task PingOllamaAsync(CancellationToken cancellationToken = new());
}
public abstract class OllamaBase<T> : IOllamaBase where T : OllamaBase<T>
{
    public IReadOnlyDictionary<string, object?> Attributes => _attributes;
    private readonly Dictionary<string, object?> _attributes = new();
    protected readonly HttpClient Http;
    protected readonly ILogger<T> Logger;

    protected OllamaBase(string modelId, string baseUrl, HttpClient http, ILoggerFactory? loggerFactory)
    {
        _attributes.Add("model_id", modelId);
        _attributes.Add("base_url", baseUrl);

        Http = http;
        Logger = loggerFactory is not null ? loggerFactory.CreateLogger<T>() : NullLogger<T>.Instance;
    }

    /// <summary>
    /// Ping Ollama instance to check if the required llm model is available at the instance
    /// </summary>
    /// <param name="cancellationToken"></param>
    public async Task PingOllamaAsync(CancellationToken cancellationToken = new())
    {
        var data = new
        {
            name = Attributes["model_id"]
        };

        var response = await Http.PostAsJsonAsync($"{Attributes["base_url"]}/api/show", data, cancellationToken).ConfigureAwait(false);

        ValidateOllamaResponse(response);

        Logger.LogInformation("Connected to Ollama at {url} with model {model}", Attributes["base_url"], Attributes["model_id"]);
    }

    protected void ValidateOllamaResponse(HttpResponseMessage? response)
    {
        try
        {
            response.EnsureSuccessStatusCode();
        }
        catch (HttpRequestException)
        {
            Logger.LogError("Unable to connect to ollama at {url} with model {model}", Attributes["base_url"], Attributes["model_id"]);
        }
    }
}

注意这个

public IReadOnlyDictionary<string, object?> Attributes => _attributes;

实现了接口中的属性。

添加OllamaTextEmbeddingGeneration类:

#pragma warning disable SKEXP0001
   public class OllamaTextEmbeddingGeneration(string modelId, string baseUrl, HttpClient http, ILoggerFactory? loggerFactory)
      : OllamaBase<OllamaTextEmbeddingGeneration>(modelId, baseUrl, http, loggerFactory),
           ITextEmbeddingGenerationService
  {
       public async Task<IList<ReadOnlyMemory<float>>> GenerateEmbeddingsAsync(IList<string> data, Kernel? kernel = null,
           CancellationToken cancellationToken = new())
      {
           var result = new List<ReadOnlyMemory<float>>(data.Count);

           foreach (var text in data)
          {
               var request = new
              {
                   model = Attributes["model_id"],
                   prompt = text
              };

               var response = await Http.PostAsJsonAsync($"{Attributes["base_url"]}/api/embeddings", request, cancellationToken).ConfigureAwait(false);

               ValidateOllamaResponse(response);

               var json = JsonSerializer.Deserialize<JsonNode>(await response.Content.ReadAsStringAsync().ConfigureAwait(false));

               var embedding = new ReadOnlyMemory<float>(json!["embedding"]?.AsArray().GetValues<float>().ToArray());

               result.Add(embedding);
          }

           return result;
      }
  }

注意实现了GenerateEmbeddingsAsync方法。实现的思路就是向Ollama中的嵌入接口发送请求,获得embedding数组。

为了在MemoryBuilder中能用还需要添加扩展方法:

#pragma warning disable SKEXP0001
   public static class OllamaMemoryBuilderExtensions
  {
       /// <summary>
       /// Adds Ollama as the text embedding generation backend for semantic memory
       /// </summary>
       /// <param name="builder">kernel builder</param>
       /// <param name="modelId">Ollama model ID to use</param>
       /// <param name="baseUrl">Ollama base url</param>
       /// <returns></returns>
       public static MemoryBuilder WithOllamaTextEmbeddingGeneration(
           this MemoryBuilder builder,
           string modelId,
           string baseUrl
      )
      {
           builder.WithTextEmbeddingGeneration((logger, http) => new OllamaTextEmbeddingGeneration(
               modelId,
               baseUrl,
               http,
               logger
          ));

           return builder;
      }      
  }

开始使用

 public async Task<ISemanticTextMemory> GetTextMemory3()
{
    var builder = new MemoryBuilder();
    var embeddingEndpoint = "http://localhost:11434";
    var cancellationTokenSource = new System.Threading.CancellationTokenSource();
    var cancellationToken = cancellationTokenSource.Token;
    builder.WithHttpClient(new HttpClient());
    builder.WithOllamaTextEmbeddingGeneration("mxbai-embed-large:335m", embeddingEndpoint);
    IMemoryStore memoryStore = await SqliteMemoryStore.ConnectAsync("memstore.db");
    builder.WithMemoryStore(memoryStore);
    var textMemory = builder.Build();
    return textMemory;
}
  builder.WithOllamaTextEmbeddingGeneration("mxbai-embed-large:335m", embeddingEndpoint);

实现了WithOllamaTextEmbeddingGeneration这个扩展方法,因此可以这么写,使用的是mxbai-embed-large:335m这个向量模型。

我使用WPF简单做了个界面,来试试效果。

找了一个新闻嵌入:

文本向量化存入数据库中:

现在测试RAG效果:

回答的效果也还可以。

大模型使用的是在线api的Qwen/Qwen2-72B-Instruct,嵌入模型使用的是本地Ollama中的mxbai-embed-large:335m。

 

SemanticKernel/C#:实现接口,接入本地嵌入模型的更多相关文章

  1. 循序渐进BootstrapVue,开发公司门户网站(5)--- 使用实际数据接口代替本地Mock数据

    在我们开发一些门户网站功能的时候,有时候我们需要快速的创建数据模型来进行数据展示,因为数据结构可能处于不断的修正变化之中,因此服务端的接口我们可以暂时不开发,当我们基本完成数据结构和界面展示的时候,就 ...

  2. 微信SDK开发——接口接入

    园子里面很多关于微信接口开发的文章,Github也一堆的开源代码. 官方文档地址:http://mp.weixin.qq.com/wiki/home/index.html 接下来主要以代码为主,接口说 ...

  3. ACM MM | 中山大学等提出HSE:基于层次语义嵌入模型的精细化物体分类

    细粒度识别一般需要模型识别非常精细的子类别,它基本上就是同时使用图像全局信息和局部信息的分类任务.在本论文中,研究者们提出了一种新型层次语义框架,其自顶向下地由全局图像关注局部特征或更具判别性的区域. ...

  4. webpack正式、测试环境接口地址本地运行及打包命令配置

    声明:本文由w3h5原创,转载请注明出处:<webpack正式.测试环境接口地址本地运行及打包命令配置> https://www.w3h5.com/post/521.html 为了方便开发 ...

  5. 配置交换机Trunk接口流量本地优先转发(集群/堆叠)

    组网图形 Eth-Trunk接口流量本地优先转发简介 在设备集群/堆叠情况下,为了保证流量的可靠传输,流量的出接口设置为Eth-Trunk接口.那么Eth-Trunk接口中必定存在跨框成员口.当集群/ ...

  6. 华为S5300交换机配置基于接口的本地端口镜像

    配置思路 1.  将Ethernet0/0/20接口配置为观察端口(监控端口) 2.  将Ethernet0/0/1----Ethernet0/0/10接口配置为镜像端口 配置步骤 1.  配置观察端 ...

  7. xddpay.com 个人支付接口接入流程

    作为一个独立开发者产品需要支付接口是挺麻烦的,支付宝微信都不对个人开放,注册公司维护成本太高,市面上各种收款工具要么手续费太高,要么到账很慢,体验很不好. 看到 「小叮当支付」 这个收款工具,挺有意思 ...

  8. php短信验证码接口接入流程及代码示例

    对于绝大部分企业来说,所使用的短信验证码接口都是第三方短信服务商所提供,目前市场上短信服务商有很多,在此向大家推荐一家动力思维乐信,运营13年,值得信赖! 就拿动力思维乐信短信验证码接口为例,详细介绍 ...

  9. BufPay.com 个人收款接口 接入步骤

    作为独立开发者产品需要收款是非常麻烦的,注册公司维护成本太高,市面上各种收款工具要么手续费太高,要么到账很慢,体验很不好. 看到 「BufPay.com 个人收款」 这个收款工具,挺有意思的.原理是监 ...

  10. 用PHP调用证件识别接口识别本地图片

    前置条件 在开始前,请作如下准备:1.学会用PHP输出“Hello World” 2.去聚合数据申请证件识别专用的KEY:https://www.juhe.cn/docs/api/id/153 操作步 ...

随机推荐

  1. Java对称加解密算法AES

    Java对称加解密算法AES import org.apache.commons.codec.binary.Base64; import org.apache.commons.lang3.String ...

  2. nacos v2.2 k8s部署启动报错:nacos server did not start because dumpservice bean construction failure. errMsg102, errllsg dataSource or tableName is null

    背景 最近搭建个nacos环境,用的镜像是2.2版本的,yaml如下: nacos-conf apiVersion: v1 kind: ConfigMap metadata: name: nacos- ...

  3. hive第二课:Hive3.1.2分区与排序以及分桶(内置函数)

    Hive3.1.2分区与排序(内置函数) 1.Hive分区(十分重要!!) 分区的目的:避免全表扫描,加快查询速度! 在大数据中,最常见的一种思想就是分治,我们可以把大的文件切割划分成一个个的小的文件 ...

  4. Ubuntu访问samba共享文件

    Ubuntu访问samba共享文件 参考:https://www.cnblogs.com/Wolf-Dreams/p/11241198.html 做法 安装samba-client.cifs-util ...

  5. Xilinx SDK 开发Linux APP

    Xilinx SDK 开发Linux APP 步骤 配置环境变量 将工具链需要的程序的所在目录添加到 系统环境变量中,例如: D:\Xilinx_201803\SDK\2018.3\gnu\micro ...

  6. QT学习:02 界面布局管理

    --- title: framework-cpp-qt-02-界面布局管理 EntryName: framework-cpp-qt-02-ui-layout-manage date: 2020-04- ...

  7. vue大型电商项目尚品汇(前台篇)day03

    堆积了两天一起发的,先祝大家节日快乐 后面任务很繁重,还有登录注册组件还有后台管理页面,真的繁重,我现在感觉每天全天时间都在学都不一定学得完,主要想在六月一号之前把整个项目过一遍.看看能不能创造奇迹 ...

  8. rust项目中通过log4rs将日志写入文件

    java项目中使用最广泛的日志系统应该是log4j(2)了.如果你也是一个Java程序员,可能在写rust的时候会想怎么能顺手地平移日志编写习惯到rust中来. log4rs就是干这个的.从名字就能看 ...

  9. JVM是如何创建一个对象的?

    哈喽,大家好,我是世杰. 本文我为大家介绍面试官经常考察的「Java对象创建流程」 照例在开头留一些面试考察内容~~ 面试连环call Java对象创建的流程是什么样? JVM执行new关键字时都有哪 ...

  10. 动态分配内存new和delete

    #include<iostream> /* 动态分配内存用new关键字,语法:new 变量类型(初始值) C++11支持{} new int(5) ---- 申请了一个整型内存,并赋初值为 ...