VoltAgent
VoltAgent 是一个基于 TypeScript 的开源框架,旨在开发支持模块化工具集成、LLM 协调和适应性多智能体架构的 AI 智能体。该框架包括一个类似于 n8n 的集成可观察性仪表板,可实现对智能体操作的可视化监控、动作跟踪和简化的调试功能。
安装
创建一个集成 Qdrant 的新 VoltAgent 项目
npm create voltagent-app@latest -- --example with-qdrant
cd with-qdrant
此命令生成一个完全配置的项目,结合了 VoltAgent 和 Qdrant,包括示例数据和两种不同的智能体实现模式。
安装依赖项
npm install
环境设置
创建包含配置的 .env 文件
# Qdrant URL
# docker run -p 6333:6333 qdrant/qdrant
QDRANT_URL=https://:6333
# Qdrant API key (Optional)
QDRANT_API_KEY=your-qdrant-api-key-here
# OpenAI API key for embeddings and LLM
OPENAI_API_KEY=your-openai-api-key-here
启动 VoltAgent 应用程序
npm run dev
请参阅此处示例的源代码:这里。
工作原理
以下部分将演示此示例的构建方式,并提供有关如何根据您的需求进行调整的指导。
创建 Qdrant 检索器
创建 src/retriever/index.ts
import { BaseRetriever, type BaseMessage, type RetrieveOptions } from "@voltagent/core";
import { QdrantClient } from "@qdrant/js-client-rest";
// Initialize Qdrant client
const qdrant = new QdrantClient({
url: process.env.QDRANT_URL || "https://:6333",
apiKey: process.env.QDRANT_API_KEY,
});
const collectionName = "voltagent-knowledge-base";
关键组件解释:
- Qdrant 客户端:连接到 Qdrant 的 REST API
- 集合 (Collection):Qdrant 中用于存储向量的命名容器
- 开源与云:可本地使用或作为托管服务使用
初始化集合和示例数据
提供的示例处理 Qdrant 集合的自动创建和数据初始化
async function initializeCollection() {
try {
// Check if collection exists
let exists = false;
try {
await qdrant.getCollection(collectionName);
exists = true;
console.log(`📋 Collection "${collectionName}" already exists`);
} catch (error) {
console.log(`📋 Creating new collection "${collectionName}"...`);
}
// Create collection if it doesn't exist
if (!exists) {
await qdrant.createCollection(collectionName, {
vectors: { size: 1536, distance: "Cosine" },
});
console.log(`✅ Collection "${collectionName}" created successfully`);
}
// Check if we need to populate with sample data
const stats = await qdrant.count(collectionName);
if (stats.count === 0) {
console.log("📚 Populating collection with sample documents...");
// Generate embeddings for sample documents using OpenAI
const OpenAI = await import("openai");
const openai = new OpenAI.default({
apiKey: process.env.OPENAI_API_KEY!,
});
const points = [];
for (const record of sampleRecords) {
try {
const embeddingResponse = await openai.embeddings.create({
model: "text-embedding-3-small",
input: record.payload.text,
});
points.push({
id: record.id,
vector: embeddingResponse.data[0].embedding,
payload: record.payload,
});
} catch (error) {
console.error(`Error generating embedding for ${record.id}:`, error);
}
}
if (points.length > 0) {
await qdrant.upsert(collectionName, { points });
console.log(`✅ Successfully upserted ${points.length} documents to collection`);
}
} else {
console.log(`📊 Collection already contains ${stats.count} documents`);
}
} catch (error) {
console.error("Error initializing Qdrant collection:", error);
}
}
作用:
- 创建具有余弦相似度的 Qdrant 集合
- 使用 OpenAI API 生成嵌入
- 将嵌入和有效负载添加到 Qdrant
实现检索器类
实现用于向量搜索功能的主要检索器类
// Retriever function
async function retrieveDocuments(query: string, topK = 3) {
try {
// Generate embedding for the query
const OpenAI = await import("openai");
const openai = new OpenAI.default({
apiKey: process.env.OPENAI_API_KEY!,
});
const embeddingResponse = await openai.embeddings.create({
model: "text-embedding-3-small",
input: query,
});
const queryVector = embeddingResponse.data[0].embedding;
// Perform search in Qdrant
const searchResults = (
await qdrant.query(collectionName, {
query: queryVector,
limit: topK,
with_payload: true,
})
).points;
// Format results
return (
searchResults.map((match: any) => ({
content: match.payload?.text || "",
metadata: match.payload || {},
score: match.score || 0,
id: match.id,
})) || []
);
} catch (error) {
console.error("Error retrieving documents from Qdrant:", error);
return [];
}
}
/**
* Qdrant-based retriever implementation for VoltAgent
*/
export class QdrantRetriever extends BaseRetriever {
/**
* Retrieve documents from Qdrant based on semantic similarity
* @param input - The input to use for retrieval (string or BaseMessage[])
* @param options - Configuration and context for the retrieval
* @returns Promise resolving to a formatted context string
*/
async retrieve(input: string | BaseMessage[], options: RetrieveOptions): Promise<string> {
// Convert input to searchable string
let searchText = "";
if (typeof input === "string") {
searchText = input;
} else if (Array.isArray(input) && input.length > 0) {
const lastMessage = input[input.length - 1];
if (Array.isArray(lastMessage.content)) {
const textParts = lastMessage.content
.filter((part: any) => part.type === "text")
.map((part: any) => part.text);
searchText = textParts.join(" ");
} else {
searchText = lastMessage.content as string;
}
}
// Perform semantic search using Qdrant
const results = await retrieveDocuments(searchText, 3);
// Add references to userContext if available
if (options.userContext && results.length > 0) {
const references = results.map((doc: any, index: number) => ({
id: doc.id,
title: doc.metadata.topic || `Document ${index + 1}`,
source: "Qdrant Knowledge Base",
score: doc.score,
category: doc.metadata.category,
}));
options.userContext.set("references", references);
}
// Return the concatenated content for the LLM
if (results.length === 0) {
return "No relevant documents found in the knowledge base.";
}
return results
.map(
(doc: any, index: number) =>
`Document ${index + 1} (ID: ${doc.id}, Score: ${doc.score.toFixed(4)}, Category: ${doc.metadata.category}):\n${doc.content}`
)
.join("\n\n---\n\n");
}
}
// Create retriever instance
export const retriever = new QdrantRetriever();
创建你的智能体
在 src/index.ts 中配置具有各种检索策略的智能体
import { openai } from "@ai-sdk/openai";
import { Agent, VoltAgent } from "@voltagent/core";
import { createPinoLogger } from "@voltagent/logger";
import { VercelAIProvider } from "@voltagent/vercel-ai";
import { retriever } from "./retriever/index.js";
// Agent 1: Using retriever directly
const agentWithRetriever = new Agent({
name: "Assistant with Retriever",
description:
"A helpful assistant that can retrieve information from the Qdrant knowledge base using semantic search to provide better answers. I automatically search for relevant information when needed.",
llm: new VercelAIProvider(),
model: openai("gpt-4o-mini"),
retriever: retriever,
});
// Agent 2: Using retriever as tool
const agentWithTools = new Agent({
name: "Assistant with Tools",
description:
"A helpful assistant that can search the Qdrant knowledge base using tools. The agent will decide when to search for information based on user questions.",
llm: new VercelAIProvider(),
model: openai("gpt-4o-mini"),
tools: [retriever.tool],
});
// Create logger
const logger = createPinoLogger({
name: "with-qdrant",
level: "info",
});
new VoltAgent({
agents: {
agentWithRetriever,
agentWithTools,
},
logger,
});