|
| 1 | +# Product Search |
| 2 | + |
| 3 | +This document describes the semantic product search implementation in Cognito. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +Cognito uses semantic search powered by Weaviate vector database to find products based on natural language queries. The system extracts search keywords from conversation context and performs vector similarity search. |
| 8 | + |
| 9 | +## Architecture |
| 10 | + |
| 11 | +``` |
| 12 | +User Message |
| 13 | + ↓ |
| 14 | +Router Node (LLM) |
| 15 | + ↓ routes to "products" |
| 16 | +Products Node |
| 17 | + ↓ |
| 18 | +Query Extraction (LLM) |
| 19 | + ↓ |
| 20 | +Weaviate Vector Search |
| 21 | + ↓ |
| 22 | +MongoDB Product Fetch |
| 23 | + ↓ |
| 24 | +Formatted Response |
| 25 | +``` |
| 26 | + |
| 27 | +## Components |
| 28 | + |
| 29 | +### 1. Router Node |
| 30 | + |
| 31 | +Location: `agents/graph/nodes/routerNode.ts` |
| 32 | + |
| 33 | +The router analyzes user messages and routes product-related queries to the products agent: |
| 34 | + |
| 35 | +- `"products"` - for product search/browsing |
| 36 | +- `"product"` - for specific product details |
| 37 | +- `"chat"` - for general conversation |
| 38 | + |
| 39 | +### 2. Products Node |
| 40 | + |
| 41 | +Location: `agents/graph/nodes/productsNode.ts` |
| 42 | + |
| 43 | +Handles product search workflow: |
| 44 | + |
| 45 | +1. **Query Extraction** - Uses LLM to extract search keywords from conversation |
| 46 | +2. **Vector Search** - Queries Weaviate for semantically similar products |
| 47 | +3. **Product Fetch** - Retrieves full product data from MongoDB |
| 48 | +4. **Response Formatting** - Formats products into user-friendly response |
| 49 | + |
| 50 | +### 3. Query Extraction Prompt |
| 51 | + |
| 52 | +Location: `agents/prompts/productsPrompts.ts` |
| 53 | + |
| 54 | +The LLM extracts search keywords from the entire conversation context, not just the last message. This enables multi-turn conversations: |
| 55 | + |
| 56 | +``` |
| 57 | +User: I need something for gaming |
| 58 | +Assistant: PC or console? |
| 59 | +User: PC, with good graphics |
| 60 | +→ "gaming PC graphics GPU computer" |
| 61 | +``` |
| 62 | + |
| 63 | +### 4. Weaviate Product Model |
| 64 | + |
| 65 | +Location: `models/products/weaviateProductsModel.ts` |
| 66 | + |
| 67 | +Products are indexed in Weaviate with text vectorization: |
| 68 | + |
| 69 | +```typescript |
| 70 | +interface IWeaviateProduct { |
| 71 | + mongoId: string; |
| 72 | + name: string; |
| 73 | + description: string; |
| 74 | + category: string; |
| 75 | + price: number; |
| 76 | + sku: string; |
| 77 | + stock: number; |
| 78 | + imageUrl?: string; |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +Search uses `nearText` query on `text_vector` target. |
| 83 | + |
| 84 | +## Data Flow |
| 85 | + |
| 86 | +### Indexing Products |
| 87 | + |
| 88 | +When a product is created: |
| 89 | + |
| 90 | +1. Product saved to MongoDB |
| 91 | +2. Product indexed in Weaviate with vector embedding |
| 92 | + |
| 93 | +```typescript |
| 94 | +await createProduct(productData); // MongoDB |
| 95 | +await addProductToWeaviate(client, product); // Weaviate |
| 96 | +``` |
| 97 | + |
| 98 | +### Searching Products |
| 99 | + |
| 100 | +When user searches: |
| 101 | + |
| 102 | +1. LLM extracts keywords from conversation |
| 103 | +2. Weaviate returns product IDs by vector similarity |
| 104 | +3. MongoDB fetches full product details |
| 105 | +4. Only active, non-deleted products are returned |
| 106 | + |
| 107 | +```typescript |
| 108 | +const query = await extractSearchQuery(messages, locale); |
| 109 | +const productIds = await searchProductIdsInWeaviate(client, query, limit); |
| 110 | +const products = await Promise.all( |
| 111 | + productIds.map(id => getProductById(db, id)) |
| 112 | +); |
| 113 | +``` |
| 114 | + |
| 115 | +## Configuration |
| 116 | + |
| 117 | +### Environment Variables |
| 118 | + |
| 119 | +```bash |
| 120 | +# Weaviate |
| 121 | +WEAVIATE_HTTP_HOST=localhost |
| 122 | +WEAVIATE_HTTP_PORT=8080 |
| 123 | +WEAVIATE_GRPC_HOST=localhost |
| 124 | +WEAVIATE_GRPC_PORT=50051 |
| 125 | +WEAVIATE_API_KEY=your-api-key |
| 126 | + |
| 127 | +# Ollama (for LLM) |
| 128 | +OLLAMA_URL=http://localhost:11434/v1 |
| 129 | +OLLAMA_MODEL=mistral-small3.2:24b-instruct-2506-q8_0 |
| 130 | +``` |
| 131 | + |
| 132 | +### Search Parameters |
| 133 | + |
| 134 | +| Parameter | Value | Location | |
| 135 | +|-----------|-------|----------| |
| 136 | +| Search limit | 5 | `productsNode.ts` | |
| 137 | +| Query extraction temperature | 0.1 | `productsNode.ts` | |
| 138 | +| Query extraction max tokens | 100 | `productsNode.ts` | |
| 139 | +| Max context messages | 10 | `productsNode.ts` | |
| 140 | + |
| 141 | +## Translations |
| 142 | + |
| 143 | +Product search responses are translated based on locale: |
| 144 | + |
| 145 | +| Key | EN | PL | |
| 146 | +|-----|----|----| |
| 147 | +| `noQueryDetected` | No product query detected... | Nie wykryto zapytania... | |
| 148 | +| `noProductsFound` | No products found... | Nie znaleziono produktów... | |
| 149 | +| `foundProducts` | Found {count} products: | Znaleziono {count} produktów: | |
| 150 | +| `inStock` | In stock | Dostępny | |
| 151 | +| `outOfStock` | Out of stock | Niedostępny | |
| 152 | + |
| 153 | +Translations: `messages/en.json`, `messages/pl.json` |
| 154 | + |
| 155 | +## Testing |
| 156 | + |
| 157 | +### Unit Tests |
| 158 | + |
| 159 | +```bash |
| 160 | +TEST_LOCALE=en npm test |
| 161 | +``` |
| 162 | + |
| 163 | +Tests in `agents/graph/chatGraph.test.ts` cover: |
| 164 | +- Router routing to products agent |
| 165 | +- Query extraction |
| 166 | +- Product filtering (deleted, inactive) |
| 167 | +- Response formatting |
| 168 | + |
| 169 | +### Evaluation Tests |
| 170 | + |
| 171 | +```bash |
| 172 | +TEST_LOCALE=en npm run test:eval |
| 173 | +``` |
| 174 | + |
| 175 | +LLM-as-judge evaluation tests in `agents/__tests__/evaluation/`: |
| 176 | +- Single-turn product searches |
| 177 | +- Multi-turn conversations |
| 178 | +- Edge cases (greetings, ambiguous queries) |
| 179 | + |
| 180 | +See `docs/JENKINS_EVAL.md` for CI setup. |
| 181 | + |
| 182 | +## Limitations |
| 183 | + |
| 184 | +Current implementation is a simple semantic search: |
| 185 | + |
| 186 | +- No filtering by price range |
| 187 | +- No filtering by category |
| 188 | +- No filtering by brand |
| 189 | +- No sorting options |
| 190 | +- Returns top 5 results by semantic similarity |
| 191 | + |
| 192 | +These features are planned for future iterations. |
0 commit comments