Prerequisites
Before starting, ensure you have:- A secure Meilisearch >= v1.15.1 project
- An API key from an LLM provider
- At least one index with searchable content
Setup
Enable the chat completions feature
First, enable the chat completions experimental feature:Find your chat API key
When Meilisearch runs with a master key on an instance created after v1.15.1, it automatically generates a “Default Chat API Key” withchatCompletions and search permissions on all indexes. Check if you have the key using:
Troubleshooting: Missing default chat API key
If your instance does not have a Default Chat API Key, create one manually:Configure your indexes
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:
descriptiongives the initial context of the conversation to the LLM. A good description improves relevance of the chat’s answersdocumentTemplatedefines 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 guidancedocumentTemplateMaxBytesestablishes a size limit for the document templates. Documents bigger than 400 bytes are truncated to ensure a good balance between speed and relevancy
Configure a chat completions workspace
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, Gemini, vLLM)
- Establish separate conversation contexts via baseline prompts
- Access a specific set of indexes
source:
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.
Send your first chat completions request
You have finished configuring your conversational search agent. To test everything is working as expected, send a streamingcurl query to the chat completions API route:
modelis mandatory and must indicate a model supported by your chosensourcemessagescontains the messages exchanged between the conversational search agent and the usertoolssets up two optional but highly recommended tools:_meiliSearchProgress: shows users what searches are being performed_meiliSearchSources: displays the actual documents used to generate responses
Next steps
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.Building a chat interface using the OpenAI SDK
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:JavaScript
Troubleshooting
Common issues and solutions
Empty reply from server (curl error 52)
Causes:- Meilisearch not started with a master key
- Experimental features not enabled
- Missing authentication in requests
- Restart Meilisearch with a master key:
meilisearch --master-key yourKey - Enable experimental features (see setup instructions above)
- Include Authorization header in all requests
”Invalid API key” error
Cause: Using the wrong type of API key Solution:- Use either the master key or the “Default Chat API Key”
- Don’t use search or admin API keys for chat endpoints
- Find your chat key:
curl MEILISEARCH_URL/keys -H "Authorization: Bearer MEILISEARCH_KEY"
”Socket connection closed unexpectedly”
Cause: Usually means the OpenAI API key is missing or invalid in workspace settings Solution:-
Check workspace configuration:
-
Update with valid API key:
Chat not searching the database
Cause: Missing Meilisearch tools in the request Solution:- Include
_meiliSearchProgressand_meiliSearchSourcestools in your request - Ensure indexes have proper chat descriptions configured