|
| 1 | +--- |
| 2 | +title: Getting started with conversational search |
| 3 | +sidebarTitle: Getting started with chat |
| 4 | +description: Learn how to implement AI-powered conversational search in your application |
| 5 | +--- |
| 6 | + |
| 7 | +import { Warning, Note } from '/snippets/notice_tag.mdx' |
| 8 | + |
| 9 | +This guide walks you through implementing Meilisearch's chat feature to create conversational search experiences in your application. |
| 10 | + |
| 11 | +<Warning> |
| 12 | +The chat feature is experimental and must be enabled before use. See [experimental features](/reference/api/experimental_features) for activation instructions. |
| 13 | +</Warning> |
| 14 | + |
| 15 | +## Prerequisites |
| 16 | + |
| 17 | +Before starting, ensure you have: |
| 18 | +- Meilisearch instance running (v1.11 or later) |
| 19 | +- An API key from an LLM provider (OpenAI or Mistral) |
| 20 | +- At least one index with searchable content |
| 21 | +- The chat experimental feature enabled |
| 22 | + |
| 23 | +## Quick start |
| 24 | + |
| 25 | +### 1. Enable the chat feature |
| 26 | + |
| 27 | +First, enable the chat experimental feature: |
| 28 | + |
| 29 | +```bash |
| 30 | +curl \ |
| 31 | + -X PATCH 'http://localhost:7700/experimental-features' \ |
| 32 | + -H 'Authorization: Bearer MASTER_KEY' \ |
| 33 | + -H 'Content-Type: application/json' \ |
| 34 | + --data-binary '{ |
| 35 | + "chat": true |
| 36 | + }' |
| 37 | +``` |
| 38 | + |
| 39 | +### 2. Configure a chat workspace |
| 40 | + |
| 41 | +Create a workspace with your LLM provider settings: |
| 42 | + |
| 43 | +```bash |
| 44 | +curl \ |
| 45 | + -X PUT 'http://localhost:7700/chats/my-assistant/settings' \ |
| 46 | + -H 'Authorization: Bearer MASTER_KEY' \ |
| 47 | + -H 'Content-Type: application/json' \ |
| 48 | + --data-binary '{ |
| 49 | + "provider": "openai", |
| 50 | + "model": "gpt-3.5-turbo", |
| 51 | + "apiKey": "sk-...", |
| 52 | + "prompt": "You are a helpful assistant. Answer questions based only on the provided context." |
| 53 | + }' |
| 54 | +``` |
| 55 | + |
| 56 | +### 3. Send your first chat request |
| 57 | + |
| 58 | +Now you can start a conversation: |
| 59 | + |
| 60 | +```bash |
| 61 | +curl \ |
| 62 | + -X POST 'http://localhost:7700/chats/my-assistant/chat/completions' \ |
| 63 | + -H 'Authorization: Bearer DEFAULT_CHAT_KEY' \ |
| 64 | + -H 'Content-Type: application/json' \ |
| 65 | + --data-binary '{ |
| 66 | + "model": "gpt-3.5-turbo", |
| 67 | + "messages": [ |
| 68 | + { |
| 69 | + "role": "user", |
| 70 | + "content": "What is Meilisearch?" |
| 71 | + } |
| 72 | + ], |
| 73 | + "stream": true |
| 74 | + }' |
| 75 | +``` |
| 76 | + |
| 77 | +## Understanding workspaces |
| 78 | + |
| 79 | +Workspaces allow you to create isolated chat configurations for different use cases: |
| 80 | + |
| 81 | +- **Customer support**: Configure with support-focused prompts |
| 82 | +- **Product search**: Optimize for e-commerce queries |
| 83 | +- **Documentation**: Tune for technical Q&A |
| 84 | + |
| 85 | +Each workspace maintains its own: |
| 86 | +- LLM provider configuration |
| 87 | +- System prompt |
| 88 | +- Access permissions |
| 89 | + |
| 90 | +## Building a chat interface |
| 91 | + |
| 92 | +Here's a simple example using JavaScript to create an interactive chat: |
| 93 | + |
| 94 | +```javascript |
| 95 | +async function sendMessage(message) { |
| 96 | + const response = await fetch('http://localhost:7700/chats/my-assistant/chat/completions', { |
| 97 | + method: 'POST', |
| 98 | + headers: { |
| 99 | + 'Authorization': 'Bearer YOUR_API_KEY', |
| 100 | + 'Content-Type': 'application/json' |
| 101 | + }, |
| 102 | + body: JSON.stringify({ |
| 103 | + model: 'gpt-3.5-turbo', |
| 104 | + messages: [ |
| 105 | + { |
| 106 | + role: 'user', |
| 107 | + content: message |
| 108 | + } |
| 109 | + ], |
| 110 | + stream: true |
| 111 | + }) |
| 112 | + }); |
| 113 | + |
| 114 | + const reader = response.body.getReader(); |
| 115 | + const decoder = new TextDecoder(); |
| 116 | + let assistantResponse = ''; |
| 117 | + |
| 118 | + while (true) { |
| 119 | + const { done, value } = await reader.read(); |
| 120 | + if (done) break; |
| 121 | + |
| 122 | + const chunk = decoder.decode(value); |
| 123 | + const lines = chunk.split('\n'); |
| 124 | + |
| 125 | + for (const line of lines) { |
| 126 | + if (line.startsWith('data: ') && line !== 'data: [DONE]') { |
| 127 | + try { |
| 128 | + const data = JSON.parse(line.slice(6)); |
| 129 | + const content = data.choices[0]?.delta?.content || ''; |
| 130 | + assistantResponse += content; |
| 131 | + // Update your UI here |
| 132 | + } catch (e) { |
| 133 | + // Handle parsing errors |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + return assistantResponse; |
| 140 | +} |
| 141 | +``` |
| 142 | +
|
| 143 | +## Managing conversations |
| 144 | +
|
| 145 | +Since the chat API is stateless, you need to maintain conversation history client-side: |
| 146 | +
|
| 147 | +```javascript |
| 148 | +class ChatConversation { |
| 149 | + constructor() { |
| 150 | + this.messages = []; |
| 151 | + } |
| 152 | + |
| 153 | + async sendMessage(content) { |
| 154 | + // Add user message to history |
| 155 | + this.messages.push({ role: 'user', content }); |
| 156 | + |
| 157 | + // Send entire conversation |
| 158 | + const response = await fetch('http://localhost:7700/chats/my-assistant/chat/completions', { |
| 159 | + method: 'POST', |
| 160 | + headers: { |
| 161 | + 'Authorization': 'Bearer YOUR_API_KEY', |
| 162 | + 'Content-Type': 'application/json' |
| 163 | + }, |
| 164 | + body: JSON.stringify({ |
| 165 | + model: 'gpt-3.5-turbo', |
| 166 | + messages: this.messages, |
| 167 | + stream: true |
| 168 | + }) |
| 169 | + }); |
| 170 | + |
| 171 | + // Process streaming response |
| 172 | + const assistantMessage = await this.processStream(response); |
| 173 | + |
| 174 | + // Add assistant response to history |
| 175 | + this.messages.push({ role: 'assistant', content: assistantMessage }); |
| 176 | + |
| 177 | + return assistantMessage; |
| 178 | + } |
| 179 | + |
| 180 | + async processStream(response) { |
| 181 | + // Stream processing logic here (see previous example) |
| 182 | + } |
| 183 | +} |
| 184 | +``` |
| 185 | +
|
| 186 | +## Best practices |
| 187 | +
|
| 188 | +### 1. Craft effective system prompts |
| 189 | +
|
| 190 | +Your system prompt shapes how the assistant responds: |
| 191 | +
|
| 192 | +```javascript |
| 193 | +const prompts = { |
| 194 | + customerSupport: `You are a helpful customer support assistant. |
| 195 | + Answer questions based only on the provided product documentation. |
| 196 | + If you don't know the answer, say so politely and suggest contacting support.`, |
| 197 | + |
| 198 | + technicalDocs: `You are a technical documentation assistant. |
| 199 | + Provide accurate, concise answers with code examples when relevant. |
| 200 | + Always cite the source document.`, |
| 201 | + |
| 202 | + ecommerce: `You are a shopping assistant. |
| 203 | + Help users find products based on their needs. |
| 204 | + Mention key features and benefits.` |
| 205 | +}; |
| 206 | +``` |
| 207 | +
|
| 208 | +### 2. Handle streaming responses properly |
| 209 | +
|
| 210 | +Always implement proper error handling for streaming: |
| 211 | +
|
| 212 | +```javascript |
| 213 | +async function processStreamWithErrorHandling(response) { |
| 214 | + if (!response.ok) { |
| 215 | + throw new Error(`HTTP error! status: ${response.status}`); |
| 216 | + } |
| 217 | + |
| 218 | + const reader = response.body.getReader(); |
| 219 | + const decoder = new TextDecoder(); |
| 220 | + |
| 221 | + try { |
| 222 | + while (true) { |
| 223 | + const { done, value } = await reader.read(); |
| 224 | + if (done) break; |
| 225 | + |
| 226 | + // Process chunks |
| 227 | + } |
| 228 | + } catch (error) { |
| 229 | + console.error('Streaming error:', error); |
| 230 | + reader.releaseLock(); |
| 231 | + throw error; |
| 232 | + } |
| 233 | +} |
| 234 | +``` |
| 235 | +
|
| 236 | +### 3. Implement conversation limits |
| 237 | +
|
| 238 | +Manage token usage by limiting conversation length: |
| 239 | +
|
| 240 | +```javascript |
| 241 | +function trimConversation(messages, maxMessages = 10) { |
| 242 | + if (messages.length <= maxMessages) return messages; |
| 243 | + |
| 244 | + // Keep system message if present |
| 245 | + const systemMessage = messages.find(m => m.role === 'system'); |
| 246 | + const recentMessages = messages.slice(-maxMessages); |
| 247 | + |
| 248 | + return systemMessage |
| 249 | + ? [systemMessage, ...recentMessages.filter(m => m.role !== 'system')] |
| 250 | + : recentMessages; |
| 251 | +} |
| 252 | +``` |
| 253 | +
|
| 254 | +## Security considerations |
| 255 | +
|
| 256 | +### API key management |
| 257 | +
|
| 258 | +<Note> |
| 259 | +Never expose your master key or LLM API keys in client-side code. Use server-side proxies or secure key management. |
| 260 | +</Note> |
| 261 | +
|
| 262 | +Recommended approach: |
| 263 | +
|
| 264 | +1. Store LLM API keys server-side |
| 265 | +2. Create workspace configurations using your master key |
| 266 | +3. Use restricted API keys or tenant tokens for client access |
| 267 | +
|
| 268 | +### Multi-tenant applications |
| 269 | +
|
| 270 | +For multi-tenant scenarios, use tenant tokens: |
| 271 | +
|
| 272 | +```javascript |
| 273 | +// Server-side: Generate tenant token |
| 274 | +const searchRules = { |
| 275 | + 'products': { 'filter': 'tenant_id = 123' } |
| 276 | +}; |
| 277 | + |
| 278 | +const tenantToken = await generateTenantToken( |
| 279 | + apiKeyUid, |
| 280 | + searchRules, |
| 281 | + { apiKey: signingKey } |
| 282 | +); |
| 283 | + |
| 284 | +// Client-side: Use tenant token |
| 285 | +const response = await fetch('http://localhost:7700/chats/support/chat/completions', { |
| 286 | + headers: { |
| 287 | + 'Authorization': `Bearer ${tenantToken}` |
| 288 | + }, |
| 289 | + // ... rest of the request |
| 290 | +}); |
| 291 | +``` |
| 292 | +
|
| 293 | +## Common use cases |
| 294 | +
|
| 295 | +### Customer support chatbot |
| 296 | +
|
| 297 | +```javascript |
| 298 | +const supportConfig = { |
| 299 | + provider: 'openai', |
| 300 | + model: 'gpt-3.5-turbo', |
| 301 | + prompt: `You are a customer support assistant for ACME Corp. |
| 302 | + Use the knowledge base to answer customer questions. |
| 303 | + Be polite, helpful, and concise. |
| 304 | + If you cannot find an answer, offer to escalate to human support.` |
| 305 | +}; |
| 306 | +``` |
| 307 | +
|
| 308 | +### Documentation assistant |
| 309 | +
|
| 310 | +```javascript |
| 311 | +const docsConfig = { |
| 312 | + provider: 'openai', |
| 313 | + model: 'gpt-4', |
| 314 | + prompt: `You are a technical documentation assistant. |
| 315 | + Provide clear, accurate answers with code examples. |
| 316 | + Always mention the source section of the documentation. |
| 317 | + If multiple interpretations exist, clarify the context.` |
| 318 | +}; |
| 319 | +``` |
| 320 | +
|
| 321 | +### E-commerce product finder |
| 322 | +
|
| 323 | +```javascript |
| 324 | +const commerceConfig = { |
| 325 | + provider: 'openai', |
| 326 | + model: 'gpt-3.5-turbo', |
| 327 | + prompt: `You are a shopping assistant. |
| 328 | + Help users find products based on their needs and preferences. |
| 329 | + Focus on benefits and use cases. |
| 330 | + Suggest alternatives when exact matches aren't available.` |
| 331 | +}; |
| 332 | +``` |
| 333 | +
|
| 334 | +## Troubleshooting |
| 335 | +
|
| 336 | +### Common issues |
| 337 | +
|
| 338 | +1. **"Feature not enabled" error** |
| 339 | + - Ensure experimental features are activated |
| 340 | + - Restart Meilisearch after enabling |
| 341 | +
|
| 342 | +2. **"Unauthorized" errors** |
| 343 | + - Verify API key has chat permissions |
| 344 | + - Check workspace exists and is properly configured |
| 345 | +
|
| 346 | +3. **Streaming interruptions** |
| 347 | + - Implement reconnection logic |
| 348 | + - Handle network timeouts gracefully |
| 349 | +
|
| 350 | +4. **Empty responses** |
| 351 | + - Verify indexes contain searchable content |
| 352 | + - Check API key has access to relevant indexes |
| 353 | + - Review system prompt for conflicts |
| 354 | +
|
| 355 | +## Next steps |
| 356 | +
|
| 357 | +- Explore [advanced chat API features](/reference/api/chats) |
| 358 | +- Learn about [conversational search concepts](/learn/ai_powered_search/conversational_search_with_chat) |
| 359 | +- Review [security best practices](/learn/security/basic_security) |
0 commit comments