|
| 1 | +--- |
| 2 | +description: |
| 3 | +globs: |
| 4 | +alwaysApply: false |
| 5 | +--- |
| 6 | +# Knowledge Base |
| 7 | + |
| 8 | +This file explains the Knowledge Base feature and how it's implemented. |
| 9 | + |
| 10 | +The knowledge base helps users store and manage information that can be used to help draft responses to emails. It acts as a personal database of information that can be referenced when composing replies. |
| 11 | + |
| 12 | +## Overview |
| 13 | + |
| 14 | +Users can create, edit, and delete knowledge base entries. Each entry consists of: |
| 15 | + |
| 16 | +- A title for quick reference |
| 17 | +- Content that contains the actual information |
| 18 | +- Metadata like creation and update timestamps |
| 19 | + |
| 20 | +## Database Schema |
| 21 | + |
| 22 | +The `Knowledge` model in Prisma: |
| 23 | + |
| 24 | +```prisma |
| 25 | +model Knowledge { |
| 26 | + id String @id @default(cuid()) |
| 27 | + createdAt DateTime @default(now()) |
| 28 | + updatedAt DateTime @updatedAt |
| 29 | + title String |
| 30 | + content String |
| 31 | + |
| 32 | + userId String |
| 33 | + user User @relation(fields: [userId], references: [id], onDelete: Cascade) |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +Each knowledge entry belongs to a specific user and is automatically deleted if the user is deleted (cascade). |
| 38 | + |
| 39 | +## Main Files and Directories |
| 40 | + |
| 41 | +The knowledge base functionality is implemented in: |
| 42 | + |
| 43 | +- `apps/web/app/(app)/automation/knowledge/KnowledgeBase.tsx` - Main UI component |
| 44 | +- `apps/web/app/(app)/automation/knowledge/KnowledgeForm.tsx` - Form for creating/editing entries |
| 45 | +- `apps/web/utils/actions/knowledge.ts` - Server actions for CRUD operations |
| 46 | +- `apps/web/utils/actions/knowledge.validation.ts` - Zod validation schemas |
| 47 | +- `apps/web/app/api/knowledge/route.ts` - API route for fetching entries |
| 48 | + |
| 49 | +### AI Integration Files |
| 50 | + |
| 51 | +- `apps/web/utils/ai/knowledge/extract.ts` - Extract relevant knowledge from knowledge base entries |
| 52 | +- `apps/web/utils/ai/knowledge/extract-from-email-history.ts` - Extract context from previous emails |
| 53 | +- `apps/web/utils/ai/reply/draft-with-knowledge.ts` - Generate email drafts using extracted knowledge |
| 54 | +- `apps/web/utils/reply-tracker/generate-reply.ts` - Coordinates the extraction and drafting process |
| 55 | +- `apps/web/utils/llms/model-selector.ts` - Economy LLM selection for high-volume tasks |
| 56 | + |
| 57 | +## Features |
| 58 | + |
| 59 | +- **Create**: Users can add new knowledge entries with a title and content |
| 60 | +- **Read**: Entries are displayed in a table with title and last updated date |
| 61 | +- **Update**: Users can edit existing entries |
| 62 | +- **Delete**: Entries can be deleted with a confirmation dialog |
| 63 | + |
| 64 | +## Usage in Email Responses |
| 65 | + |
| 66 | +The knowledge base entries are used to help draft responses to emails. When composing a reply, the system can reference these entries to include relevant information, ensuring consistent and accurate responses. |
| 67 | + |
| 68 | +When drafting responses, we use two LLMs: |
| 69 | + |
| 70 | +1. A cheaper LLM that can process a lot of data (e.g. Google Gemini 2 Flash) |
| 71 | +2. A more expensive LLM to draft the response (e.g. Anthropic Sonnet 3.7) |
| 72 | + |
| 73 | +The cheaper LLM is an agent that extracts the key information needed for the drafter LLM. |
| 74 | +For example, the knowledge base may include 100 pages of content, and the LLM extracts half a page of knowledge to pass to the more expensive drafter LLM. |
| 75 | + |
| 76 | +## Dual LLM Architecture |
| 77 | + |
| 78 | +The dual LLM approach is implemented as follows: |
| 79 | + |
| 80 | +1. **Knowledge Extraction (Economy LLM)**: |
| 81 | + |
| 82 | + - Uses a more cost-efficient model like Gemini Flash for processing large volumes of knowledge base content |
| 83 | + - Analyzes all knowledge entries and extracts only relevant information based on the email content |
| 84 | + - Configured via environment variables (`ECONOMY_LLM_PROVIDER` and `ECONOMY_LLM_MODEL`) |
| 85 | + - If no specific economy model is configured, defaults to Gemini Flash when Google API key is available |
| 86 | + |
| 87 | +2. **Email Draft Generation (Core LLM)**: |
| 88 | + - Uses the default model (e.g., Anthropic Claude 3.7 Sonnet) for high-quality content generation |
| 89 | + - Receives the extracted relevant knowledge from the economy LLM |
| 90 | + - Generates the final email draft based on the provided context |
| 91 | + |
| 92 | +This architecture optimizes for both cost efficiency (using cheaper models for high-volume tasks) and quality (using premium models for user-facing content). |
0 commit comments