文档

通过百川智能向量化模型将文本转换为向量

更新时间:

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

前提条件

百川智能向量化模型

简介

模型名称

向量维度

度量方式

向量数据类型

备注

Baichuan-Text-Embedding

1024

Cosine

Float32

  • 输入最长token:512 个 ,超出自动截断

  • 批量最大大小:16

说明

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

使用示例

说明

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

  1. DashVector api-key替换示例中的{your-dashvector-api-key}

  2. DashVector Cluster Endpoint替换示例中的{your-dashvector-cluster-endpoint}

  3. 百川智能api-key替换示例中的{your-baichuan-api-key}

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)
  • 本页导读 (0)
文档反馈