全部產品
Search
文件中心

DashVector:通過百川智能向量化模型將文本轉換為向量

更新時間:Jul 13, 2024

本文介紹如何通過百川智能向量化模型文本轉換為向量,併入庫至向量檢索服務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)