将 Ollama 与 Qdrant 结合使用
Ollama 为小众应用提供专门的嵌入。Ollama 支持多种嵌入模型,从而可以构建检索增强生成 (RAG) 应用,将文本提示与特定领域的现有文档或其他数据相结合。
安装
您可以使用以下 pip 命令安装所需的软件包
pip install ollama qdrant-client
集成示例
以下代码假设 Ollama 可通过端口 11434
访问,Qdrant 可通过端口 6334
访问。
from qdrant_client import QdrantClient, models
import ollama
COLLECTION_NAME = "NicheApplications"
# Initialize Ollama client
oclient = ollama.Client(host="localhost")
# Initialize Qdrant client
qclient = QdrantClient(host="localhost", port=6333)
# Text to embed
text = "Ollama excels in niche applications with specific embeddings"
# Generate embeddings
response = oclient.embeddings(model="llama3.2", prompt=text)
embeddings = response["embedding"]
# Create a collection if it doesn't already exist
if not qclient.collection_exists(COLLECTION_NAME):
qclient.create_collection(
collection_name=COLLECTION_NAME,
vectors_config=models.VectorParams(
size=len(embeddings), distance=models.Distance.COSINE
),
)
# Upload the vectors to the collection along with the original text as payload
qclient.upsert(
collection_name=COLLECTION_NAME,
points=[models.PointStruct(id=1, vector=embeddings, payload={"text": text})],
)