|
| 1 | +# AI Search Feature Implementation Guide |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +The AI Search feature has been successfully implemented using Typesense's conversational `multi_search` API. This feature allows users to ask questions to an AI assistant and receive both AI-generated answers and relevant search results from the documentation. |
| 6 | + |
| 7 | +## Features |
| 8 | + |
| 9 | +### 1. **Conversational Interface** |
| 10 | +- Modal dialog that opens when clicking the AI Search button |
| 11 | +- Clean, intuitive UI for asking questions |
| 12 | +- Real-time message display with user and assistant messages |
| 13 | +- Automatic scrolling to latest messages |
| 14 | + |
| 15 | +### 2. **AI Assistant Responses** |
| 16 | +- Powered by Typesense's conversational model (`conv-model-1`) |
| 17 | +- Generates contextual answers based on documentation |
| 18 | +- Displays answers prominently in the conversation |
| 19 | + |
| 20 | +### 3. **Search Results Integration** |
| 21 | +- Displays relevant search results alongside AI answers |
| 22 | +- Shows document titles, snippets, and links |
| 23 | +- Results are clickable and navigate to the full documentation |
| 24 | + |
| 25 | +### 4. **Conversation Persistence** |
| 26 | +- Maintains `conversation_id` across multiple messages |
| 27 | +- Allows follow-up questions within the same conversation |
| 28 | +- Context is preserved for coherent multi-turn conversations |
| 29 | + |
| 30 | +## Component Structure |
| 31 | + |
| 32 | +### TypesenseConversation.mjs |
| 33 | + |
| 34 | +The main component file located at: |
| 35 | +``` |
| 36 | +MyApp/wwwroot/mjs/components/TypesenseConversation.mjs |
| 37 | +``` |
| 38 | + |
| 39 | +#### Key Components: |
| 40 | + |
| 41 | +1. **AISearchDialog Component** |
| 42 | + - Modal dialog for the search interface |
| 43 | + - Manages conversation state and messages |
| 44 | + - Handles user input and API calls |
| 45 | + |
| 46 | +2. **TypesenseConversation Component** |
| 47 | + - Main component exported as default |
| 48 | + - Renders the AI Search button |
| 49 | + - Manages dialog visibility |
| 50 | + - Initializes collection on mount |
| 51 | + |
| 52 | +#### Key Functions: |
| 53 | + |
| 54 | +- `ensureGlobals()` - Initializes global utilities (get, post, BaseUrl, H) |
| 55 | +- `clean(s)` - Sanitizes text by removing zero-width spaces and normalizing whitespace |
| 56 | +- `listCollections()` - Fetches available Typesense collections |
| 57 | +- `multiSearch(collection, message, conversationId)` - Performs conversational search |
| 58 | + |
| 59 | +## Usage |
| 60 | + |
| 61 | +### For Users |
| 62 | + |
| 63 | +1. **Opening AI Search** |
| 64 | + - Click the AI Search button (chat icon) in the header |
| 65 | + - The modal dialog will open with an input field |
| 66 | + |
| 67 | +2. **Asking Questions** |
| 68 | + - Type your question in the input field |
| 69 | + - Press Enter or click the Send button |
| 70 | + - Wait for the AI assistant to respond |
| 71 | + |
| 72 | +3. **Following Up** |
| 73 | + - Ask follow-up questions in the same conversation |
| 74 | + - The conversation_id is automatically maintained |
| 75 | + - Context from previous messages is preserved |
| 76 | + |
| 77 | +4. **Viewing Results** |
| 78 | + - AI response appears in the conversation |
| 79 | + - Search results are displayed below the response |
| 80 | + - Click on any result to navigate to the full documentation |
| 81 | + |
| 82 | +### For Developers |
| 83 | + |
| 84 | +#### Integration Points |
| 85 | + |
| 86 | +The component is already integrated into the main application: |
| 87 | + |
| 88 | +1. **Header Component** (`Pages/Shared/Header.cshtml`) |
| 89 | + - The `<typesense></typesense>` component is rendered in the header |
| 90 | + - Includes both the regular search and AI search buttons |
| 91 | + |
| 92 | +2. **App Initialization** (`wwwroot/mjs/app.mjs`) |
| 93 | + - TypesenseConversation is registered as a component |
| 94 | + - Automatically mounted when the page loads |
| 95 | + |
| 96 | +#### API Configuration |
| 97 | + |
| 98 | +The component uses the following Typesense configuration: |
| 99 | + |
| 100 | +```javascript |
| 101 | +const BaseUrl = "https://search.docs.servicestack.net" |
| 102 | +const H = { |
| 103 | + search: { |
| 104 | + "accept": "application/json", |
| 105 | + "x-typesense-api-key": "N4N8bF0XwyvzwCGwm3CKB0QcnwyWtygo" |
| 106 | + }, |
| 107 | + admin: { |
| 108 | + "accept": "application/json", |
| 109 | + "x-typesense-api-key": "N4N8bF0XwyvzwCGwm3CKB0QcnwyWtygo" |
| 110 | + } |
| 111 | +} |
| 112 | +``` |
| 113 | + |
| 114 | +#### Multi-Search API Call |
| 115 | + |
| 116 | +The component makes POST requests to: |
| 117 | +``` |
| 118 | +https://search.docs.servicestack.net/multi_search |
| 119 | +``` |
| 120 | + |
| 121 | +With parameters: |
| 122 | +- `q` - The user's question |
| 123 | +- `conversation` - Set to `true` to enable conversation mode |
| 124 | +- `conversation_model_id` - Set to `'conv-model-1'` |
| 125 | +- `conversation_id` - (Optional) ID from previous message for follow-ups |
| 126 | + |
| 127 | +Request body: |
| 128 | +```json |
| 129 | +{ |
| 130 | + "searches": [{ |
| 131 | + "collection": "typesense_docs_v1", |
| 132 | + "query_by": "embedding", |
| 133 | + "exclude_fields": "embedding" |
| 134 | + }] |
| 135 | +} |
| 136 | +``` |
| 137 | + |
| 138 | +## Response Structure |
| 139 | + |
| 140 | +The API returns a response with the following structure: |
| 141 | + |
| 142 | +```javascript |
| 143 | +{ |
| 144 | + conversation: { |
| 145 | + answer: "AI-generated answer text", |
| 146 | + conversation_id: "conv-123-456", |
| 147 | + query: "Original user query" |
| 148 | + }, |
| 149 | + results: [{ |
| 150 | + found: 2, |
| 151 | + out_of: 100, |
| 152 | + page: 1, |
| 153 | + hits: [ |
| 154 | + { |
| 155 | + document: { |
| 156 | + url: "/docs/page/", |
| 157 | + anchor: "section", |
| 158 | + content: "Full content", |
| 159 | + hierarchy: { |
| 160 | + lvl0: "Title", |
| 161 | + lvl1: "Subtitle", |
| 162 | + lvl2: null, |
| 163 | + lvl3: null |
| 164 | + } |
| 165 | + }, |
| 166 | + vector_distance: 0.15, |
| 167 | + highlights: [{ |
| 168 | + snippet: "Relevant snippet..." |
| 169 | + }] |
| 170 | + } |
| 171 | + ] |
| 172 | + }] |
| 173 | +} |
| 174 | +``` |
| 175 | + |
| 176 | +## Styling |
| 177 | + |
| 178 | +The component uses Tailwind CSS classes for styling: |
| 179 | + |
| 180 | +- **Modal**: `fixed inset-0 z-50 flex bg-black/25` |
| 181 | +- **Dialog**: `max-w-2xl rounded-lg shadow-lg` |
| 182 | +- **Messages**: |
| 183 | + - User: `bg-blue-500 text-white` |
| 184 | + - Assistant: `bg-gray-100 dark:bg-gray-700` |
| 185 | +- **Results**: `bg-gray-50 dark:bg-gray-900 rounded-lg p-3` |
| 186 | +- **Input**: `border border-gray-300 dark:border-gray-600 rounded-lg` |
| 187 | + |
| 188 | +## Testing |
| 189 | + |
| 190 | +A comprehensive test suite is included at: |
| 191 | +``` |
| 192 | +MyApp/wwwroot/mjs/components/TypesenseConversation.test.mjs |
| 193 | +``` |
| 194 | + |
| 195 | +Run tests with: |
| 196 | +```bash |
| 197 | +node MyApp/wwwroot/mjs/components/TypesenseConversation.test.mjs |
| 198 | +``` |
| 199 | + |
| 200 | +Tests verify: |
| 201 | +1. Global utilities initialization |
| 202 | +2. Text cleaning function |
| 203 | +3. Conversation ID preservation |
| 204 | +4. Component structure |
| 205 | + |
| 206 | +## Browser Compatibility |
| 207 | + |
| 208 | +The component uses modern JavaScript features: |
| 209 | +- ES6 modules |
| 210 | +- Async/await |
| 211 | +- Fetch API |
| 212 | +- Vue 3 Composition API |
| 213 | + |
| 214 | +Supported browsers: |
| 215 | +- Chrome/Edge 88+ |
| 216 | +- Firefox 78+ |
| 217 | +- Safari 14+ |
| 218 | + |
| 219 | +## Performance Considerations |
| 220 | + |
| 221 | +1. **Debouncing**: Messages are sent immediately on Enter or button click |
| 222 | +2. **Scrolling**: Auto-scroll to latest message uses `nextTick()` for smooth UX |
| 223 | +3. **Loading State**: Disabled input during API calls to prevent duplicate submissions |
| 224 | +4. **Error Handling**: Graceful error messages displayed in conversation |
| 225 | + |
| 226 | +## Future Enhancements |
| 227 | + |
| 228 | +Potential improvements: |
| 229 | +1. Add message editing/deletion |
| 230 | +2. Implement conversation history/saving |
| 231 | +3. Add typing indicators |
| 232 | +4. Support for code snippets in responses |
| 233 | +5. Conversation export functionality |
| 234 | +6. User feedback (thumbs up/down) on responses |
| 235 | +7. Rate limiting and usage analytics |
| 236 | + |
| 237 | +## Troubleshooting |
| 238 | + |
| 239 | +### Modal doesn't open |
| 240 | +- Check browser console for errors |
| 241 | +- Verify TypesenseConversation component is mounted |
| 242 | +- Check that collection is initialized |
| 243 | + |
| 244 | +### No search results |
| 245 | +- Verify Typesense API key is valid |
| 246 | +- Check network tab for API response |
| 247 | +- Ensure collection name matches expected format |
| 248 | + |
| 249 | +### Conversation_id not persisting |
| 250 | +- Check that response includes `conversation_id` |
| 251 | +- Verify it's being stored in component state |
| 252 | +- Check that it's passed in subsequent requests |
| 253 | + |
| 254 | +## Files Modified |
| 255 | + |
| 256 | +1. **MyApp/wwwroot/mjs/components/TypesenseConversation.mjs** |
| 257 | + - Complete rewrite with AI Search modal |
| 258 | + - Added AISearchDialog component |
| 259 | + - Added global utility functions |
| 260 | + - Enhanced multiSearch function |
| 261 | + |
| 262 | +2. **MyApp/wwwroot/mjs/components/TypesenseConversation.test.mjs** (New) |
| 263 | + - Comprehensive test suite |
| 264 | + - Tests for all major functions |
| 265 | + |
| 266 | +## Support |
| 267 | + |
| 268 | +For issues or questions about the AI Search feature, refer to: |
| 269 | +- Typesense Documentation: https://typesense.org/docs/ |
| 270 | +- Vue 3 Documentation: https://vuejs.org/ |
| 271 | +- ServiceStack Documentation: https://docs.servicestack.net/ |
| 272 | + |
0 commit comments