首页 > 其他分享 >中文环境下使用 huggingface 模型替换 OpenAI的Embedding 接口

中文环境下使用 huggingface 模型替换 OpenAI的Embedding 接口

时间:2023-05-29 10:38:43浏览次数:47  
标签:embeddings string huggingface System OpenAI Embedding using 文本 response

OpenAI的文本嵌入衡量文本字符串的相关性。嵌入通常用于:

  • 搜索(其中结果按与查询字符串的相关性排名)
  • 聚类(其中文本字符串按相似性分组)
  • 推荐(推荐具有相关文本字符串的项目)
  • 异常检测(识别出相关性不大的异常值)
  • 多样性测量(分析相似性分布)
  • 分类(其中文本字符串按其最相似的标签分类)

嵌入是浮点数的向量(列表)。两个向量之间的距离衡量它们的相关性。小距离表示高相关性,大距离表示低相关性。 但是OpenAI的文本嵌入接口对中文的支持并不好,社区经过实践,对中文支持比较好的模型是Hugging face上的 ganymedenil/text2vec-large-chinese。具体可以参见:https://huggingface.co/GanymedeNil/text2vec-large-chinese/discussions/3 ,作者采用的训练数据集是 中文STS-B数据集。它将句子映射到 768 维密集向量空间,可用于任务 如句子嵌入、文本匹配或语义搜索。

在Semantic Kernel 的Sample 下有一个 hugging-face-http-server:https://github.com/microsoft/semantic-kernel/tree/main/samples/apps/hugging-face-http-server ,通过这个示例项目,我们可以在本地运行Huggingface模型。

我们首先构建一个Docker,执行命令 docker image build -t hf_model_server . ,最新的构建会有问题,我把它独立成一个repo :https://github.com/mlnethub/hugging-face-http-server

运行容器

docker run -p 5000:5000 -d hf_model_server

中文环境下使用 huggingface 模型替换 OpenAI的Embedding 接口_System

访问http://localhost:5000

中文环境下使用 huggingface 模型替换 OpenAI的Embedding 接口_Endpoint_02

通过从0.14版本引入的 Nuget 包 Microsoft.SemanticKernel.Connectors.AI.HuggingFace:https://www.nuget.org/packages/Microsoft.SemanticKernel.Connectors.AI.HuggingFace/0.14.547.1-preview#versions-body-tab 

具体用法参考单元测试代码HuggingFaceEmbeddingGenerationTests

using System;

using System.Collections.Generic;

using System.Linq;

using System.Net;

using System.Net.Http;

using System.Threading.Tasks;

using Microsoft.SemanticKernel.Connectors.AI.HuggingFace.TextEmbedding;

using Xunit;namespace SemanticKernel.Connectors.UnitTests.HuggingFace.TextEmbedding;
/// <summary>

/// Unit tests for <see cref="HuggingFaceTextEmbeddingGeneration"/> class.

/// </summary>

public class HuggingFaceEmbeddingGenerationTests : IDisposable

{
     private const string Endpoint = "http://localhost:5000/embeddings";     private const string Model = @"GanymedeNil/text2vec-large-chinese";    private readonly HttpResponseMessage _response = new()
     {
         StatusCode = HttpStatusCode.OK,
     };    /// <summary>
     /// Verifies that <see cref="HuggingFaceTextEmbeddingGeneration.GenerateEmbeddingsAsync"/>
     /// returns expected list of generated embeddings without errors.
     /// </summary>
     [Fact]
     public async Task ItReturnsEmbeddingsCorrectlyAsync()
     {
         // Arrange
         const int ExpectedEmbeddingCount = 1;
         const int ExpectedVectorCount = 8;
         List<string> data = new() { "test_string_1", "test_string_2", "test_string_3" };        using var service = this.CreateService(HuggingFaceTestHelper.GetTestResponse("embeddings_test_response.json"));
        // Act
         var embeddings = await service.GenerateEmbeddingsAsync(data);        // Assert
         Assert.NotNull(embeddings);
         Assert.Equal(ExpectedEmbeddingCount, embeddings.Count);
         Assert.Equal(ExpectedVectorCount, embeddings.First().Count);
     }    /// <summary>
     /// Initializes <see cref="HuggingFaceTextEmbeddingGeneration"/> with mocked <see cref="HttpClientHandler"/>.
     /// </summary>
     /// <param name="testResponse">Test response for <see cref="HttpClientHandler"/> to return.</param>
     private HuggingFaceTextEmbeddingGeneration CreateService(string testResponse)
     {
         this._response.Content = new StringContent(testResponse);        var httpClientHandler = HuggingFaceTestHelper.GetHttpClientHandlerMock(this._response);
        return new HuggingFaceTextEmbeddingGeneration(new Uri(Endpoint), Model, httpClientHandler);
     }    public void Dispose()
     {
         this.Dispose(true);
         GC.SuppressFinalize(this);
     }    protected virtual void Dispose(bool disposing)
     {
         if (disposing)
         {
             this._response.Dispose();
         }
     }

}

标签:embeddings,string,huggingface,System,OpenAI,Embedding,using,文本,response
From: https://blog.51cto.com/shanyou/6368143

相关文章

  • C#使用词嵌入向量与向量数据库为大语言模型(LLM)赋能长期记忆实现私域问答机器人落地
    ------------恢复内容开始------------在上一篇文章中我们大致讲述了一下如何通过词嵌入向量的方式为大语言模型增加长期记忆,用于落地在私域场景的问题。其中涉及到使用openai的接口进行词嵌入向量的生成以及chat模型的调用由于众所周知的原因,国内调用openai接口并不友好,所以今......
  • Graph Embedding:LINE算法
    背景 如上图所示,结点6和7是相邻结点,他们应该是相似结点,结点5和6虽然不是相邻结点,但是它们有共同的相邻的结点,因此它们也应该是相似结点。基于词观察,LINE算法提出了一阶相似性算法和二阶相似性算法  First-order我们首先如如下公式来计算结点i和j的联合概率分布:其中ui,u......
  • HuggingFace
    Pipeline  pipeline模块把所有东西都封装死了,只需要传进去原始输入,就能得到输出. 例:遮掩词填空,可以看出pipelinefunction给出了5个备选答案  fromtransformersimportpipelineclassifier=pipeline("fill-mask")y_pred=classifier("Ilove<mask>......
  • 中文环境下使用 huggingface 模型替换 OpenAI的Embedding 接口
    OpenAI的文本嵌入衡量文本字符串的相关性。嵌入通常用于:搜索(其中结果按与查询字符串的相关性排名)聚类(其中文本字符串按相似性分组)推荐(推荐具有相关文本字符串的项目)异常检测(识别出相关性不大的异常值)多样性测量(分析相似性分布)分类(其中文本字符串按其最相似的标签分类)嵌入是浮......
  • OpenAI 官宣首个 ChatGPT iOS 应用
    最近,OpenAI宣布推出官方iOS应用,允许用户随时随地访问其高人气AI聊天机器人,此举也打破了近几个月内苹果AppStore上充斥似是而非的山寨服务的窘境。该应用程序是ChatGPT的首个官方移动应用程序。ChatGPT软件程序在去年推出后迅速获得了超过1亿用户,这也让技术行业火速......
  • 矩池云教程|体验 OpenAI 最近推出的 3D 生成模型 Shap-E!
    Shap-E是由OpenAI最近推出的3D生成模型,使用者可以通过简单的文字或图像进行三维模型的生成,OpenAI认为相比于点云的生成模型Point-E,Shap-E的收敛更快。本文将展示如何在矩池云上体验3D模型生成。Shap-E:https://github.com/openai/shap-e矩池云:https://matpool.com/1、安装......
  • BFT 最前线 | WPS AI接入四大办公软件;OpenAI准备开源新模型;OpenAI CEO首次出证国会;百
    原创|文BFT机器人AI视界TECHNOLOGYNEWS01WPSAI宣布接入文字、表格、PPT、PDF四大办公软件,AI能力大升级5月16日,金山办公旗下人工智能应用WPSAI,宣布四大办公组件全面升级:包括文字、表格、PPT和PDF,相比4月只有文字组件升级,这次升级涵盖了更广范围,并展示了WPSAI在阅读理解和人......
  • 使用 openai 翻译器
    1.下载openai翻译器 https://github.com/yetone/openai-translator  我在release界面下下载.msi安装包,下载好双击安装即可2.申请openaiAPIkey https://platform.openai.com/account/api-keys要获取openaiAPIkey相对容易。但是免费试用只有一个月,如何升级成付......
  • 调用openAI API出现429错误
    如果不是请求太频繁的话,请检查一下自己账号Usage的期限 openAI给每个账号18美刀、三个月的免费用量,所以过期了就会报429错误......
  • openai模型个性化训练Embedding和fine-tuning区别
    现在基于自然语言和文档进行对话的背后都是使用的基于嵌入的向量搜索。OpenAI在这方面做的很好,它的Cookbook(github.com/openai/openai-cookbook)上有很多案例,最近他们对文档做了一些更新。GPT擅长回答问题,但是只能回答它以前被训练过的问题,如果是没有训练过的数据,比如一些私有数据......