This article walks you through implementing Meilisearch’s chat completions feature to create conversational search experiences in your application.
To successfully implement a conversational search interface you must follow three steps: configure indexes for chat usage, create a chat workspaces, and build a chat interface.
Conversational search is still in early development. Conversational agents may occasionally hallucinate inaccurate and misleading information, so it is important to closely monitor it in production environments.
When Meilisearch runs with a master key on an instance created after v1.15.1, it automatically generates a “Default Chat API Key” with chatCompletions and search permissions on all indexes. Check if you have the key using:
Copy
curl \ -X GET 'MEILISEARCH_URL/keys' \ -H 'Authorization: Bearer MASTER_KEY'
Look for the key with the description “Default Chat API Key”.
After activating the /chats route and obtaining an API key with chat permissions, configure the chat settings for each index you want to be searchable via chat UI:
Copy
curl \ -X PATCH 'MEILISEARCH_URL/indexes/INDEX_NAME/settings' \ -H 'Authorization: Bearer MEILISEARCH_KEY' \ -H 'Content-Type: application/json' \ --data-binary '{ "chat": { "description": "A comprehensive database of TYPE_OF_DOCUMENT containing titles, descriptions, genres, and release dates to help users searching for TYPE_OF_DOCUMENT", "documentTemplate": "{% for field in fields %}{% if field.is_searchable and field.value != nil %}{{ field.name }}: {{ field.value }}\n{% endif %}{% endfor %}", "documentTemplateMaxBytes": 400 } }'
description gives the initial context of the conversation to the LLM. A good description improves relevance of the chat’s answers
documentTemplate defines the document data Meilisearch sends to the AI provider. This template outputs all searchable fields in your documents, which may not be ideal if your documents have many fields. Consult the best document template best practices article for more guidance
documentTemplateMaxBytes establishes a size limit for the document templates. Documents bigger than 400 bytes are truncated to ensure a good balance between speed and relevancy
The next step is to create a workspace. Chat completion workspaces are isolated configurations targeting different use cases. Each workspace can:
Use different embedding providers (OpenAI, Azure OpenAI, Mistral, vLLM)
Establish separate conversation contexts via baseline prompts
Access a specific set of indexes
For example, you may have one workspace for publicly visible data, and another for data only available for logged in users.Create a workspace setting your LLM provider as its source:
Copy
curl \ -X PATCH 'MEILISEARCH_URL/chats/WORKSPACE_NAME/settings' \ -H 'Authorization: Bearer MEILISEARCH_KEY' \ -H 'Content-Type: application/json' \ --data-binary '{ "source": "openAi", "apiKey": "PROVIDER_API_KEY", "baseUrl": "PROVIDER_API_URL", "prompts": { "system": "You are a helpful assistant. Answer questions based only on the provided context." } }'
Which fields are mandatory will depend on your chosen provider source. In most cases, you will have to provide an apiKey to access the provider.baseUrl indicates the URL Meilisearch queries when users submit questions to your chat interface. This is only mandatory for Azure OpenAI and vLLM sources.prompts.system gives the conversational search bot the baseline context of your users and their questions. The prompts object accepts a few other fields that provide more information to improve how the agent uses the information it finds via Meilisearch. In real-life scenarios filling these fields would improve the quality of conversational search results.
You have finished configuring your conversational search agent. To test everything is working as expected, send a streaming curl query to the chat completions API route:
In this article, you have seen how to activate the chats completion route, prepare your indexes to serve as a base for your AI agent, and performed your first conversational search.In most cases, that is only the beginning of adding conversational search to your application. Next, you are most likely going to want to add a graphical user interface to your application.
Meilisearch’s chat endpoint was designed to be OpenAI-compatible. This means you can use the official OpenAI SDK in any supported programming language, even if your provider is not OpenAI.Integrating Meilisearch and the OpenAI SDK with JavaScript would look like this:
Take particular note of the last lines, which output the streamed responses to the browser console. In a real-life application, you would instead print the response chunks to the user interface.