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

本文介绍如何通过百川智能向量化模型文本转换为向量 ,并入库至向量检索服务DashVector中进行向量检索。

前提条件

  • DashVector:已创建Cluster已获得API-KEY已安装最新版SDK
  • 百川智能:已获得API密钥:百川智能向量化模型

百川智能向量化模型

简介

说明

关于百川智能向量化模型更多信息请参考:百川智能向量化模型

使用示例

说明

需要进行如下替换代码才能正常运行:

  1. DashVector api-key替换示例中的{your-dashvector-api-key}
  2. DashVector Cluster Endpoint替换示例中的{your-dashvector-cluster-endpoint}
  3. 百川智能api-key替换示例中的{your-baichuan-api-key}

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)

#数据库##大模型##人工智能##AI#
全部评论

相关推荐

10-22 19:44
门头沟学院 Java
面了100年面试不知...:那我得去剪个头
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务