crewai-ts
Version:
TypeScript port of crewAI for agent-based workflows
93 lines (92 loc) • 3.89 kB
JavaScript
/**
* PDFSearchTool implementation
* Provides PDF document search capabilities for agents using RAG
* Optimized for performance, memory efficiency, and large document handling
*/
import { z } from 'zod';
import { createStructuredTool } from '../StructuredTool.js';
import { Knowledge } from '../../knowledge/index.js';
import { KnowledgeStorage } from '../../knowledge/index.js';
// Input schema for PDF search operations
const pdfSearchSchema = z.object({
query: z.string().min(1, "Query cannot be empty"),
pdfPath: z.string().min(1, "PDF path cannot be empty").or(z.array(z.string().min(1, "PDF path cannot be empty"))),
resultCount: z.number().int().positive().default(5).optional(),
similarityThreshold: z.number().min(0).max(1).default(0.7).optional(),
pageNumbers: z.array(z.number().int().nonnegative()).optional(),
pageRange: z.object({
start: z.number().int().nonnegative(),
end: z.number().int().nonnegative(),
}).optional(),
metadata: z.record(z.any()).optional(),
});
/**
* Creates an optimized PDF search tool
*/
export function createPDFSearchTool(options = {}) {
// Create or use provided knowledge base
let knowledgeBase = options.knowledgeBase;
// If no knowledge base provided, create one with optimized storage
if (!knowledgeBase) {
knowledgeBase = new Knowledge({
collectionName: 'pdf_search_knowledge',
storage: new KnowledgeStorage({
collectionName: 'pdf_search_collection',
embedder: {
model: options.embeddingModelName || 'all-MiniLM-L6-v2',
provider: 'fastembed'
}
}),
maxConcurrency: 4,
enableCache: true
});
}
return createStructuredTool({
name: "pdf_search",
description: "Search for information in PDF documents. Provide a query and path(s) to PDF file(s).",
inputSchema: pdfSearchSchema,
cacheResults: options.cacheResults,
timeout: options.timeoutMs,
maxRetries: options.maxRetries,
func: async (input) => {
try {
// Check for NodeJS environment since PDFLoader requires Node
// This is a placeholder for the actual implementation that would:
// 1. Load the PDF(s) using a PDF parser library
// 2. Process and chunk the content
// 3. Add to the knowledge base
// 4. Perform search and return results
// For now, return a placeholder result
return {
query: input.query,
results: [
{
content: "PDF search functionality requires NodeJS environment and PDF parsing libraries.",
score: 1.0,
metadata: {
fileName: typeof input.pdfPath === 'string' ? input.pdfPath : input.pdfPath[0],
}
}
],
error: "PDF search implementation is a placeholder. Real implementation would load PDFs, chunk content, and perform RAG search."
};
}
catch (error) {
return {
query: input.query,
results: [],
error: `Error searching PDF: ${error instanceof Error ? error.message : String(error)}`
};
}
}
});
}
/**
* Real implementation would include PDF parsing and processing
* This would require importing libraries such as pdf-parse or pdfjs
* And implementing functions for:
* - Loading and parsing PDFs
* - Chunking text content
* - Creating embeddings
* - Optimized search with hot/warm/cold tiered storage
*/