釋放 AI 的強大力量

100 萬免費代幣

88% 價格優惠

立即啟用
本文由簡體中文內容自動轉碼而成。阿里雲不保證此自動轉碼的準確性、完整性及時效性。本文内容請以簡體中文版本為準。

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

更新時間:2024-07-13 00:21

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

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)
  • 本頁導讀 (1, M)
  • 前提條件
  • 百川智能向量化模型
  • 簡介
  • 使用樣本
文檔反饋
phone 聯絡我們

立即和Alibaba Cloud在線服務人員進行交談,獲取您想了解的產品信息以及最新折扣。

alicare alicarealicarealicare