Agent with RAG

medium10 minrag-agent.ts

Instead of stuffing documents into the prompt, the agent should search the knowledge base when it needs to.

What it shows

  • Turn a pipeline into a tool with createSearchTool() and give it to an agent

The code

rag-agent.ts
import { Agent, Cogitator, tool } from '@cogitator-ai/core';
import { GoogleEmbeddingService, InMemoryEmbeddingAdapter } from '@cogitator-ai/memory';
import { RAGPipelineBuilder, TextLoader, createSearchTool } from '@cogitator-ai/rag';
import { mkdir, writeFile } from 'node:fs/promises';
import { join } from 'node:path';

const apiKey = process.env.GOOGLE_API_KEY;
if (!apiKey) throw new Error('Set GOOGLE_API_KEY');

const docsDir = join(process.cwd(), 'knowledge');
await mkdir(docsDir, { recursive: true });
await writeFile(
  join(docsDir, 'memory.txt'),
  'The @cogitator-ai/memory package has adapters for Redis, PostgreSQL, SQLite, MongoDB and Qdrant.'
);
await writeFile(
  join(docsDir, 'rag.txt'),
  'The @cogitator-ai/rag package loads text, markdown, JSON, CSV, HTML, PDF and web pages.'
);

const pipeline = new RAGPipelineBuilder()
  .withLoader(new TextLoader())
  .withEmbeddingService(new GoogleEmbeddingService({ apiKey }))
  .withEmbeddingAdapter(new InMemoryEmbeddingAdapter())
  .withConfig({
    chunking: { strategy: 'recursive', chunkSize: 400, chunkOverlap: 50 },
    retrieval: { strategy: 'similarity', topK: 3, threshold: 0.3 },
  })
  .build();
await pipeline.ingest(docsDir);

const cog = new Cogitator({ llm: { providers: { google: { apiKey } } } });

const assistant = new Agent({
  name: 'docs-assistant',
  model: 'google/gemini-3.5-flash-lite',
  instructions:
    'Use the rag_search tool to find information before answering. Cite what you found. Be concise.',
  tools: [tool(createSearchTool(pipeline))],
  temperature: 0.3,
  maxIterations: 5,
});

const result = await cog.run(assistant, { input: 'Which databases can Cogitator memory use?' });
console.log(result.output);
console.log('Tools:', result.toolCalls.map((call) => call.name));

await cog.close();

Run it

install · bash
pnpm add @cogitator-ai/core @cogitator-ai/memory @cogitator-ai/rag
EnvironmentGOOGLE_API_KEY
run · bash
GOOGLE_API_KEY=your-key npx tsx rag-agent.ts

Or run the full example from a clone of the repository (keys in .env):

repository · bash
npx tsx examples/rag/03-agent-with-rag.ts

Learn more