上升阶段
Qdrant 支持与来自 上演 的 Solar Embeddings API 一起工作。
太阳嵌入 API 提供了用户查询和文档嵌入的双模型功能,这些模型在统一的向量空间内运行,专为高效的文本处理而设计。
您可以从上台控制台生成一个API密钥来验证请求。
设置Qdrant客户端和Upstage会话
import requests
from qdrant_client import QdrantClient
UPSTAGE_BASE_URL = "https://api.upstage.ai/v1/solar/embeddings"
UPSTAGE_API_KEY = "<YOUR_API_KEY>"
upstage_session = requests.Session()
client = QdrantClient(url="http://localhost:6333")
headers = {
"Authorization": f"Bearer {UPSTAGE_API_KEY}",
"Accept": "application/json",
}
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 UPSTAGE_BASE_URL = "https://api.upstage.ai/v1/solar/embeddings"
const UPSTAGE_API_KEY = "<YOUR_API_KEY>"
const client = new QdrantClient({ url: 'http://localhost:6333' });
const headers = {
"Authorization": "Bearer " + UPSTAGE_API_KEY,
"Accept": "application/json",
"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.",
]
以下示例展示了如何使用推荐的solar-embedding-1-large-passage和solar-embedding-1-large-query模型嵌入文档,这些模型生成大小为4096的句子嵌入。
嵌入文档
body = {
"input": texts,
"model": "solar-embedding-1-large-passage",
}
response_body = upstage_session.post(
UPSTAGE_BASE_URL, headers=headers, json=body
).json()
let body = {
"input": texts,
"model": "solar-embedding-1-large-passage",
}
let response = await fetch(UPSTAGE_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=data["embedding"],
payload={"text": text},
)
for idx, (data, text) in enumerate(zip(response_body["data"], 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"
client.create_collection(
collection_name,
vectors_config=VectorParams(
size=4096,
distance=Distance.COSINE,
),
)
client.upsert(collection_name, points)
const COLLECTION_NAME = "example_collection"
await client.createCollection(COLLECTION_NAME, {
vectors: {
size: 4096,
distance: 'Cosine',
}
});
await client.upsert(COLLECTION_NAME, {
wait: true,
points
})
使用Qdrant搜索文档
一旦所有文档都添加完毕,您可以搜索最相关的文档。
body = {
"input": "What is the best to use for vector search scaling?",
"model": "solar-embedding-1-large-query",
}
response_body = upstage_session.post(
UPSTAGE_BASE_URL, headers=headers, json=body
).json()
client.search(
collection_name=collection_name,
query_vector=response_body["data"][0]["embedding"],
)
body = {
"input": "What is the best to use for vector search scaling?",
"model": "solar-embedding-1-large-query",
}
response = await fetch(UPSTAGE_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,
});
