xonecas.io
53129
Pa

PAT / commit 2f8ec34

feat: rag system vibe coded initial commit

xonecas2026-01-12 2f8ec34d3304a8753d3b0b55fd9ced3034e78413 parent 50b638b

10 files changed

AGENTS.md+2−0
@@ -15,3 +15,5 @@
1515 - ensure code modularity and separation of concerns
1616 - ensure code respects project guidelines
1717 - ensure the code uses best practices for React and Next.js
18+- **Always** use mcp servers for latest documentation:
19+ - use specific mcp servers if available, otherwise use the content7 server.
drizzle/0008_jittery_talon.sql+10−0
@@ -0,0 +1,10 @@
1+CREATE EXTENSION IF NOT EXISTS vector;--> statement-breakpoint
2+CREATE TABLE "document_chunks" (
3+ "id" serial PRIMARY KEY NOT NULL,
4+ "content" text NOT NULL,
5+ "embedding" vector(768),
6+ "metadata" jsonb,
7+ "created_at" timestamp with time zone DEFAULT now() NOT NULL
8+);
9+--> statement-breakpoint
10+CREATE INDEX "embeddingIndex" ON "document_chunks" USING hnsw ("embedding" vector_cosine_ops);
drizzle/meta/0008_snapshot.json+201−0
@@ -0,0 +1,201 @@
1+{
2+ "id": "c37d31a9-377e-41df-be95-832686d4978c",
3+ "prevId": "ca5189e9-3478-4d36-8016-f20ddea2c568",
4+ "version": "7",
5+ "dialect": "postgresql",
6+ "tables": {
7+ "public.document_chunks": {
8+ "name": "document_chunks",
9+ "schema": "",
10+ "columns": {
11+ "id": {
12+ "name": "id",
13+ "type": "serial",
14+ "primaryKey": true,
15+ "notNull": true
16+ },
17+ "content": {
18+ "name": "content",
19+ "type": "text",
20+ "primaryKey": false,
21+ "notNull": true
22+ },
23+ "embedding": {
24+ "name": "embedding",
25+ "type": "vector(768)",
26+ "primaryKey": false,
27+ "notNull": false
28+ },
29+ "metadata": {
30+ "name": "metadata",
31+ "type": "jsonb",
32+ "primaryKey": false,
33+ "notNull": false
34+ },
35+ "created_at": {
36+ "name": "created_at",
37+ "type": "timestamp with time zone",
38+ "primaryKey": false,
39+ "notNull": true,
40+ "default": "now()"
41+ }
42+ },
43+ "indexes": {
44+ "embeddingIndex": {
45+ "name": "embeddingIndex",
46+ "columns": [
47+ {
48+ "expression": "embedding",
49+ "isExpression": false,
50+ "asc": true,
51+ "nulls": "last",
52+ "opclass": "vector_cosine_ops"
53+ }
54+ ],
55+ "isUnique": false,
56+ "concurrently": false,
57+ "method": "hnsw",
58+ "with": {}
59+ }
60+ },
61+ "foreignKeys": {},
62+ "compositePrimaryKeys": {},
63+ "uniqueConstraints": {},
64+ "policies": {},
65+ "checkConstraints": {},
66+ "isRLSEnabled": false
67+ },
68+ "public.messages": {
69+ "name": "messages",
70+ "schema": "",
71+ "columns": {
72+ "id": {
73+ "name": "id",
74+ "type": "serial",
75+ "primaryKey": true,
76+ "notNull": true
77+ },
78+ "thread_id": {
79+ "name": "thread_id",
80+ "type": "integer",
81+ "primaryKey": false,
82+ "notNull": true
83+ },
84+ "role": {
85+ "name": "role",
86+ "type": "text",
87+ "primaryKey": false,
88+ "notNull": true
89+ },
90+ "content": {
91+ "name": "content",
92+ "type": "text",
93+ "primaryKey": false,
94+ "notNull": true
95+ },
96+ "thinking": {
97+ "name": "thinking",
98+ "type": "text",
99+ "primaryKey": false,
100+ "notNull": false
101+ },
102+ "created_at": {
103+ "name": "created_at",
104+ "type": "timestamp with time zone",
105+ "primaryKey": false,
106+ "notNull": true,
107+ "default": "now()"
108+ }
109+ },
110+ "indexes": {},
111+ "foreignKeys": {
112+ "messages_thread_id_threads_id_fk": {
113+ "name": "messages_thread_id_threads_id_fk",
114+ "tableFrom": "messages",
115+ "tableTo": "threads",
116+ "columnsFrom": [
117+ "thread_id"
118+ ],
119+ "columnsTo": [
120+ "id"
121+ ],
122+ "onDelete": "cascade",
123+ "onUpdate": "no action"
124+ }
125+ },
126+ "compositePrimaryKeys": {},
127+ "uniqueConstraints": {},
128+ "policies": {},
129+ "checkConstraints": {},
130+ "isRLSEnabled": false
131+ },
132+ "public.threads": {
133+ "name": "threads",
134+ "schema": "",
135+ "columns": {
136+ "id": {
137+ "name": "id",
138+ "type": "serial",
139+ "primaryKey": true,
140+ "notNull": true
141+ },
142+ "title": {
143+ "name": "title",
144+ "type": "text",
145+ "primaryKey": false,
146+ "notNull": false
147+ },
148+ "model": {
149+ "name": "model",
150+ "type": "text",
151+ "primaryKey": false,
152+ "notNull": true
153+ },
154+ "max_prompt_length": {
155+ "name": "max_prompt_length",
156+ "type": "integer",
157+ "primaryKey": false,
158+ "notNull": false
159+ },
160+ "user_prompt": {
161+ "name": "user_prompt",
162+ "type": "text",
163+ "primaryKey": false,
164+ "notNull": false
165+ },
166+ "created_at": {
167+ "name": "created_at",
168+ "type": "timestamp with time zone",
169+ "primaryKey": false,
170+ "notNull": true,
171+ "default": "now()"
172+ },
173+ "updated_at": {
174+ "name": "updated_at",
175+ "type": "timestamp with time zone",
176+ "primaryKey": false,
177+ "notNull": true,
178+ "default": "now()"
179+ }
180+ },
181+ "indexes": {},
182+ "foreignKeys": {},
183+ "compositePrimaryKeys": {},
184+ "uniqueConstraints": {},
185+ "policies": {},
186+ "checkConstraints": {},
187+ "isRLSEnabled": false
188+ }
189+ },
190+ "enums": {},
191+ "schemas": {},
192+ "sequences": {},
193+ "roles": {},
194+ "policies": {},
195+ "views": {},
196+ "_meta": {
197+ "columns": {},
198+ "schemas": {},
199+ "tables": {}
200+ }
201+}
drizzle/meta/_journal.json+7−0
@@ -57,6 +57,13 @@
5757 "when": 1766350897443,
5858 "tag": "0007_spotty_maestro",
5959 "breakpoints": true
60+ },
61+ {
62+ "idx": 8,
63+ "version": "7",
64+ "when": 1768243019725,
65+ "tag": "0008_jittery_talon",
66+ "breakpoints": true
6067 }
6168 ]
6269 }
package.json+2−0
@@ -23,6 +23,8 @@
2323 "db:wipe-dev": "./scripts/wipe-dev-db.sh",
2424 "start:docker": "./scripts/start-docker.sh",
2525 "stop:docker": "./scripts/stop-docker.sh",
26+ "rag:update": "tsx scripts/update-vector-data.ts",
27+ "rag:update:force": "tsx scripts/update-vector-data.ts --force",
2628 "prepare": "husky"
2729 },
2830 "dependencies": {
scripts/start-docker.sh+3−3
@@ -28,7 +28,7 @@ SEARXNG_PORT="${SEARXNG_PORT:-8888}"
2828
2929 # Pull images
3030 docker pull valkey/valkey:7.2
31-docker pull postgres:16-alpine
31+docker pull pgvector/pgvector:pg16
3232 docker pull docker.io/searxng/searxng:latest
3333
3434 # Stop and remove existing Valkey container if it exists
@@ -65,7 +65,7 @@ docker run -d \
6565 valkey/valkey:7.2 \
6666 valkey-server --requirepass "${VALKEY_PASSWORD}"
6767
68-# Run PostgreSQL container
68+# Run PostgreSQL container with pgvector extension
6969 docker run -d \
7070 --name postgres \
7171 --restart unless-stopped \
@@ -74,7 +74,7 @@ docker run -d \
7474 -e POSTGRES_PASSWORD="${POSTGRES_PASSWORD}" \
7575 -e POSTGRES_DB="${POSTGRES_DB}" \
7676 -v postgres_data:/var/lib/postgresql/data \
77- postgres:16-alpine
77+ pgvector/pgvector:pg16
7878
7979 # Run SearXNG container
8080 docker run -d \
scripts/update-vector-data.ts+61−0
@@ -0,0 +1,61 @@
1+import { readFile } from "fs/promises";
2+import { join } from "path";
3+import { Document } from "@langchain/core/documents";
4+import { RecursiveCharacterTextSplitter } from "@langchain/textsplitters";
5+import { OllamaEmbeddings } from "@langchain/ollama";
6+import { storeDocuments } from "@/src/lib/rag";
7+import { db } from "@/src/lib/db";
8+import { documentChunks } from "@/src/lib/db/schema";
9+import { count } from "drizzle-orm";
10+
11+const DATA_FILE_PATH = "./data.txt";
12+const CHUNK_SIZE = 1000;
13+const CHUNK_OVERLAP = 200;
14+
15+async function updateVectorData(force: boolean = false): Promise<void> {
16+ try {
17+ // Check if documents are already indexed
18+ const existingCount = await db.select({ count: count() }).from(documentChunks);
19+ if (existingCount[0].count > 0 && !force) {
20+ console.log("RAG vector store already initialized. Use --force to re-index.");
21+ return;
22+ }
23+
24+ if (force && existingCount[0].count > 0) {
25+ console.log("Clearing existing vector data...");
26+ await db.delete(documentChunks);
27+ }
28+
29+ console.log("Loading and chunking data.txt...");
30+
31+ // Load data.txt file
32+ const filePath = join(process.cwd(), DATA_FILE_PATH);
33+ const content = await readFile(filePath, "utf-8");
34+ const document = new Document({
35+ pageContent: content,
36+ metadata: { source: DATA_FILE_PATH },
37+ });
38+
39+ // Split document into chunks
40+ const textSplitter = new RecursiveCharacterTextSplitter({
41+ chunkSize: CHUNK_SIZE,
42+ chunkOverlap: CHUNK_OVERLAP,
43+ });
44+ const chunks = await textSplitter.splitDocuments([document]);
45+
46+ console.log(`Generated ${chunks.length} chunks, generating embeddings...`);
47+
48+ // Generate embeddings for all chunks
49+ const embeddings = new OllamaEmbeddings({ model: "nomic-embed-text" });
50+ await storeDocuments(chunks, embeddings);
51+
52+ console.log("Vector data update complete!");
53+ } catch (error) {
54+ console.error("Failed to update vector data:", error);
55+ process.exit(1);
56+ }
57+}
58+
59+// Parse command line arguments
60+const force = process.argv.includes("--force");
61+updateVectorData(force);
src/lib/chat.ts+34−1
@@ -1,6 +1,7 @@
11 import { db } from "@/src/lib/db";
22 import { messages, threads } from "@/src/lib/db/schema";
33 import { createMessage } from "@/src/lib/messages";
4+import { retrieveRelevantContext } from "@/src/lib/rag";
45 import { executeToolCall, fetchPageTool, webSearchTool } from "@/src/lib/tools";
56 import { asc, eq } from "drizzle-orm";
67 import ollama, { type Tool, type ToolCall } from "ollama";
@@ -129,7 +130,39 @@ export async function generateResponse(
129130 throw new Error("No messages found for this thread");
130131 }
131132
132- const messageHistory = await agentLoop(thread[0].model, messagesList, enqueue);
133+ // Extract latest user message
134+ const latestUserMessage = messagesList.filter((msg) => msg.role === "user").pop();
135+
136+ // Retrieve RAG context if user message exists
137+ let ragContext: string | null = null;
138+ if (latestUserMessage) {
139+ try {
140+ ragContext = await retrieveRelevantContext(latestUserMessage.content);
141+ } catch (error) {
142+ console.error("Failed to retrieve RAG context:", error);
143+ // Continue without RAG context
144+ }
145+ }
146+
147+ // Inject RAG context into message history
148+ const enhancedMessageHistory = [...messagesList];
149+ if (ragContext) {
150+ // Find position after all system messages
151+ const lastSystemIndex =
152+ enhancedMessageHistory
153+ .map((msg, idx) => (msg.role === "system" ? idx : -1))
154+ .filter((idx) => idx !== -1)
155+ .pop() ?? -1;
156+
157+ const ragSystemMessage: MessageHistoryEntry = {
158+ role: "system",
159+ content: `**Relevant Context from Knowledge Base:**\n\n${ragContext}\n\nUse this information to help answer the user's question.`,
160+ };
161+
162+ enhancedMessageHistory.splice(lastSystemIndex + 1, 0, ragSystemMessage);
163+ }
164+
165+ const messageHistory = await agentLoop(thread[0].model, enhancedMessageHistory, enqueue);
133166
134167 // Update the thread with the new message history:
135168 for (const message of messageHistory) {
src/lib/db/schema.ts+26−1
@@ -1,5 +1,14 @@
11 import { relations } from "drizzle-orm";
2-import { integer, pgTable, serial, text, timestamp } from "drizzle-orm/pg-core";
2+import {
3+ index,
4+ integer,
5+ jsonb,
6+ pgTable,
7+ serial,
8+ text,
9+ timestamp,
10+ vector,
11+} from "drizzle-orm/pg-core";
312
413 export const threads = pgTable("threads", {
514 id: serial("id").primaryKey(),
@@ -34,6 +43,22 @@ export const messagesRelations = relations(messages, ({ one }) => ({
3443 }),
3544 }));
3645
46+// Note: Verify that nomic-embed-text produces 768-dimensional embeddings
47+// If using a different embedding model, adjust the dimensions accordingly
48+const EMBEDDING_DIMENSIONS = 768;
49+
50+export const documentChunks = pgTable(
51+ "document_chunks",
52+ {
53+ id: serial("id").primaryKey(),
54+ content: text("content").notNull(),
55+ embedding: vector("embedding", { dimensions: EMBEDDING_DIMENSIONS }),
56+ metadata: jsonb("metadata"),
57+ createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
58+ },
59+ (table) => [index("embeddingIndex").using("hnsw", table.embedding.op("vector_cosine_ops"))]
60+);
61+
3762 export type Thread = typeof threads.$inferSelect;
3863 export type NewThread = typeof threads.$inferInsert;
3964 export type Message = typeof messages.$inferSelect;
src/lib/rag.ts+86−0
@@ -0,0 +1,86 @@
1+import { db } from "@/src/lib/db";
2+import { documentChunks } from "@/src/lib/db/schema";
3+import { OllamaEmbeddings } from "@langchain/ollama";
4+import { Document } from "@langchain/core/documents";
5+import { cosineDistance, desc, sql } from "drizzle-orm";
6+import { getCache, setCache } from "@/src/lib/cache";
7+
8+const EMBEDDING_MODEL = "nomic-embed-text";
9+const CHUNK_COUNT = 4;
10+const CACHE_TTL_MS = 60 * 60 * 1000; // 1 hour
11+
12+function createEmbeddings(): OllamaEmbeddings {
13+ return new OllamaEmbeddings({
14+ model: EMBEDDING_MODEL,
15+ });
16+}
17+
18+export async function storeDocuments(
19+ documents: Document[],
20+ embeddings: OllamaEmbeddings
21+): Promise<void> {
22+ // Generate embeddings for all chunks
23+ const texts = documents.map((doc) => doc.pageContent);
24+ const embeddingsList = await embeddings.embedDocuments(texts);
25+
26+ // Store in database (Drizzle handles vector type conversion automatically)
27+ for (let i = 0; i < documents.length; i++) {
28+ await db.insert(documentChunks).values({
29+ content: documents[i].pageContent,
30+ embedding: embeddingsList[i],
31+ metadata: documents[i].metadata,
32+ });
33+ }
34+}
35+
36+async function similaritySearch(
37+ queryEmbedding: number[],
38+ limit: number
39+): Promise<Array<{ content: string; metadata: unknown; similarity: number }>> {
40+ const similarity = sql<number>`1 - (${cosineDistance(documentChunks.embedding, queryEmbedding)})`;
41+
42+ const results = await db
43+ .select({
44+ content: documentChunks.content,
45+ metadata: documentChunks.metadata,
46+ similarity,
47+ })
48+ .from(documentChunks)
49+ .orderBy(desc(similarity))
50+ .limit(limit);
51+
52+ return results;
53+}
54+
55+export async function retrieveRelevantContext(query: string): Promise<string | null> {
56+ // Check cache first
57+ const cacheKey = `rag:${query.toLowerCase().trim()}`;
58+ const cached = await getCache<string>(cacheKey);
59+ if (cached !== null) {
60+ return cached;
61+ }
62+
63+ try {
64+ const embeddings = createEmbeddings();
65+ const queryEmbedding = await embeddings.embedQuery(query);
66+
67+ const results = await similaritySearch(queryEmbedding, CHUNK_COUNT);
68+
69+ if (results.length === 0) {
70+ return null;
71+ }
72+
73+ // Format chunks into context string
74+ const context = results
75+ .map((result, index) => `[Chunk ${index + 1}]\n${result.content}`)
76+ .join("\n\n");
77+
78+ // Cache the result
79+ await setCache(cacheKey, context, CACHE_TTL_MS);
80+
81+ return context;
82+ } catch (error) {
83+ console.error("RAG retrieval error:", error);
84+ return null;
85+ }
86+}