如何将文本转换为向量?(方法四)

DashVector · · 208 次点击 · · 开始浏览    

本文介绍如何通过[百川智能向量化模型](https://platform.baichuan-ai.com/docs/text-Embedding)将 **文本转换为向量** ,并入库至向量检索服务DashVector中进行向量检索。 前提条件 --------------------- * DashVector: * 已创建Cluster * 已获得API-KEY * 已安装最新版SDK * 百川智能: * 已获得API密钥:[百川智能向量化模型](https://platform.baichuan-ai.com/docs/text-Embedding) **百川智能向量化模型** ------------------------------ ### **简介** ![image.png](https://static.golangjob.cn/240905/75e2aa2b2a3bfa84dff4e75344d6f2cd.png) **说明** 关于百川智能向量化模型更多信息请参考:[百川智能向量化模型](https://platform.baichuan-ai.com/docs/text-Embedding)。 ### **使用示例** **说明** 需要进行如下替换代码才能正常运行: 1. DashVector api-key替换示例中的{your-dashvector-api-key} 2. DashVector Cluster Endpoint替换示例中的{your-dashvector-cluster-endpoint} 3. 百川智能api-key替换示例中的{your-baichuan-api-key} Python ```python from dashvector import Client import requests from typing import List # 调用百川智能向量化模型服务,将文本embedding为向量 def generate_embeddings(texts: List[str]): headers = { 'Content-Type': 'application/json', 'Authorization': 'Bearer {your-baichuan-api-key}' } data = {'input': texts, 'model': 'Baichuan-Text-Embedding'} response = requests.post('http://api.baichuan-ai.com/v1/embeddings', headers=headers, json=data) return [record["embedding"] for record in response.json()["data"]] # 创建DashVector Client client = Client( api_key='{your-dashvector-api-key}', endpoint='{your-dashvector-cluster-endpoint}' ) # 创建DashVector Collection rsp = client.create('baichuan-text-embedding', 1024) assert rsp collection = client.get('baichuan-text-embedding') assert collection # 向量入库DashVector collection.insert( ('ID1', generate_embeddings(['阿里云向量检索服务DashVector是性能、性价比具佳的向量数据库之一'])[0]) ) # 向量检索 docs = collection.query( generate_embeddings(['The best vector database'])[0] ) print(docs) ```

有疑问加站长微信联系(非本文作者))

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

208 次点击  
加入收藏 微博
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传