Voyage AI
Qdrant 支持使用 Voyage AI 嵌入。支持的模型列表可以在此处找到。
您可以从 Voyage AI 控制面板生成 API 密钥来验证请求。
设置 Qdrant 和 Voyage 客户端
from qdrant_client import QdrantClient
import voyageai
VOYAGE_API_KEY = "<YOUR_VOYAGEAI_API_KEY>"
qclient = QdrantClient(":memory:")
vclient = voyageai.Client(api_key=VOYAGE_API_KEY)
texts = [
"Qdrant is the best vector search engine!",
"Loved by Enterprises and everyone building for low latency, high performance, and scale.",
]
import {QdrantClient} from '@qdrant/js-client-rest';
const VOYAGEAI_BASE_URL = "https://api.voyageai.com/v1/embeddings"
const VOYAGEAI_API_KEY = "<YOUR_VOYAGEAI_API_KEY>"
const client = new QdrantClient({ url: 'http://localhost:6333' });
const headers = {
"Authorization": "Bearer " + VOYAGEAI_API_KEY,
"Content-Type": "application/json"
}
const texts = [
"Qdrant is the best vector search engine!",
"Loved by Enterprises and everyone building for low latency, high performance, and scale.",
]
以下示例展示了如何使用voyage-large-2
模型嵌入文档,该模型生成大小为 1536 的句子嵌入。
嵌入文档
response = vclient.embed(texts, model="voyage-large-2", input_type="document")
let body = {
"input": texts,
"model": "voyage-large-2",
"input_type": "document",
}
let response = await fetch(VOYAGEAI_BASE_URL, {
method: "POST",
body: JSON.stringify(body),
headers
});
let response_body = await response.json();
将模型输出转换为 Qdrant 点
from qdrant_client.models import PointStruct
points = [
PointStruct(
id=idx,
vector=embedding,
payload={"text": text},
)
for idx, (embedding, text) in enumerate(zip(response.embeddings, texts))
]
let points = response_body.data.map((data, i) => {
return {
id: i,
vector: data.embedding,
payload: {
text: texts[i]
}
}
});
创建一个集合来插入文档
from qdrant_client.models import VectorParams, Distance
COLLECTION_NAME = "example_collection"
qclient.create_collection(
COLLECTION_NAME,
vectors_config=VectorParams(
size=1536,
distance=Distance.COSINE,
),
)
qclient.upsert(COLLECTION_NAME, points)
const COLLECTION_NAME = "example_collection"
await client.createCollection(COLLECTION_NAME, {
vectors: {
size: 1536,
distance: 'Cosine',
}
});
await client.upsert(COLLECTION_NAME, {
wait: true,
points
});
使用 Qdrant 搜索文档
添加文档后,您可以搜索最相关的文档。
response = vclient.embed(
["What is the best to use for vector search scaling?"],
model="voyage-large-2",
input_type="query",
)
qclient.search(
collection_name=COLLECTION_NAME,
query_vector=response.embeddings[0],
)
body = {
"input": ["What is the best to use for vector search scaling?"],
"model": "voyage-large-2",
"input_type": "query",
};
response = await fetch(VOYAGEAI_BASE_URL, {
method: "POST",
body: JSON.stringify(body),
headers
});
response_body = await response.json();
await client.search(COLLECTION_NAME, {
vector: response_body.data[0].embedding,
});