本文介紹如何通過百川智能向量化模型將文本轉換為向量,併入庫至向量檢索服務DashVector中進行向量檢索。
前提條件
DashVector:
已建立Cluster:建立Cluster
已獲得API-KEY:API-KEY管理
已安裝最新版SDK:安裝DashVector SDK
百川智能:
已獲得API密鑰:百川智能向量化模型
百川智能向量化模型
簡介
模型名稱 | 向量維度 | 度量方式 | 向量資料類型 | 備忘 |
Baichuan-Text-Embedding | 1024 | Cosine | Float32 |
|
說明
關於百川智能向量化模型更多資訊請參考:百川智能向量化模型。
使用樣本
說明
需要進行如下替換代碼才能正常運行:
DashVector api-key替換樣本中的{your-dashvector-api-key}
DashVector Cluster Endpoint替換樣本中的{your-dashvector-cluster-endpoint}
百川智能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)