Perform semantic search on documents for RAG implementations
Perform semantic search to find the most relevant text chunks from a collection of documents based on a query. This method is designed for RAG (Retrieval-Augmented Generation) implementations.
Array of documents to search through. Each document has content and optional meta fields.
documents: [ { content: "London is the capital of England...", meta: { filename: "geography.md", source: "notes" } }, { content: "Paris is the capital of France...", meta: { filename: "cities.md", category: "europe" } }]
Relevance score (cosine similarity) between the query and chunk. Higher scores indicate better matches.Typically ranges from -1 to 1, with 1 being most similar.
const documents = [ { content: "London is the capital city of England and the United Kingdom. It is located on the River Thames.", meta: { source: "geography.txt", category: "cities" } }, { content: "Paris is the capital and most populous city of France. It is situated on the Seine River.", meta: { source: "geography.txt", category: "cities" } }];const results = await aiProviders.retrieve({ query: "What is the capital of England?", documents: documents, embeddingProvider: aiProviders.providers[0]});// Results are sorted by relevanceresults.forEach(result => { console.log(`Score: ${result.score}`); console.log(`Content: ${result.content}`); console.log(`Source: ${result.document.meta?.source}`);});
The retrieve method automatically splits large documents into smaller chunks:
const largeDocuments = [ { content: ` Chapter 1: Introduction to Machine Learning Machine learning is a subset of artificial intelligence that focuses on algorithms and statistical models. Chapter 2: Types of Machine Learning There are three main types: supervised learning, unsupervised learning, and reinforcement learning. Chapter 3: Neural Networks Neural networks are computing systems inspired by biological neural networks. `, meta: { title: "ML Textbook", chapter: "1-3" } }];const results = await aiProviders.retrieve({ query: "What are the types of machine learning?", documents: largeDocuments, embeddingProvider: aiProviders.providers[0]});// The method will find the most relevant chunk about ML typesconsole.log(results[0].content);// "There are three main types: supervised learning, unsupervised learning, and reinforcement learning."