Tutorial: Build a RAG Agent
Learn how to give your agent long-term memory and access to private documentation using Vector Stores.
What we'll build#
We will build a "Knowledge Base Agent" that can answer questions about a fictional product manual. We'll use:
- OpenAI Embeddings for vectorizing text.
- In-Memory Vector Store for simplicity (swappable for Pinecone/PgVector).
- RAG Tool to retrieve context.
1
Setup Knowledge Base
First, we need a way to store and retrieve documents. We'll define a simple VectorStore interface.
typescript
import { Tool } from '@akios/core'
import { z } from 'zod'
// Mock vector store (in production, use Pinecone/Weaviate)
const knowledgeBase = [
{ id: 1, text: "The ACME 2000 has a battery life of 12 hours.", vector: [...] },
{ id: 2, text: "To reset the device, hold the power button for 5s.", vector: [...] },
]
async function search(query: string) {
// Simulate semantic search
console.log(`Searching for: ${query}`)
return knowledgeBase.map(k => k.text).join('\n')
}2
Create Retrieval Tool
We wrap our search function in a Tool so the agent knows how and when to use it.
typescript
const retrievalTool = new Tool({
name: 'search_manual',
description: 'Search the product manual for technical specifications and troubleshooting.',
schema: z.object({
query: z.string().describe('The semantic search query')
}),
handler: async ({ query }) => {
const results = await search(query)
return results || "No relevant information found."
}
})3
Initialize Agent
Now we give the tool to the agent. Notice the system prompt encourages using the tool.
typescript
const agent = new Agent({
name: 'SupportBot',
model: 'gpt-4-turbo',
tools: [retrievalTool],
systemPrompt: `
You are a technical support assistant for ACME Corp.
ALWAYS search the manual before answering technical questions.
If the answer isn't in the manual, say "I don't know".
`
})4
Test It
Run the agent with a question that requires external knowledge.
typescript
const response = await agent.run("How do I perform a hard reset?")
console.log(response.text)> Searching for: perform a hard reset
> Tool Output: "To reset the device, hold the power button for 5s."
> Agent: You can perform a hard reset by holding the power button for 5 seconds.
> Tool Output: "To reset the device, hold the power button for 5s."
> Agent: You can perform a hard reset by holding the power button for 5 seconds.
Pro Tip
For large datasets, use the
@akios/rag package (coming soon) which handles chunking, embedding, and re-ranking automatically.