The OpenAI embedder connects Meilisearch to OpenAI’s embedding API to generate vectors for your documents and queries. This is one of the easiest ways to enable semantic search, as Meilisearch has built-in support for OpenAI through the openAi source.
This guide requires an OpenAI API key.
Choose a model
OpenAI offers three main embedding models:
| Model | Dimensions | Notes |
|---|---|---|
text-embedding-3-small |
1,536 | Cost-effective, good for most use cases |
text-embedding-3-large |
3,072 | Higher accuracy, best for complex datasets |
text-embedding-ada-002 |
1,536 | Legacy model, still supported |
For most applications, text-embedding-3-small provides a good balance between accuracy and cost. Use text-embedding-3-large when you need maximum retrieval quality and can accept higher API costs.
Configure the embedder
Create an embedder object with the openAi source. Open your text editor and build the following configuration:
{
"my-openai": {
"source": "openAi",
"model": "text-embedding-3-small",
"apiKey": "OPEN_AI_API_KEY",
"documentTemplate": "A product named '{{doc.name}}' described as '{{doc.description}}'"
}
}
In this configuration:
source: must be"openAi"to use OpenAI’s built-in integrationmodel: the OpenAI model to use for generating embeddingsapiKey: your OpenAI API keydocumentTemplate: a Liquid template that converts your documents into text for embedding. Keep it short and include only the most important fields
Update your index settings
Send the embedder configuration to Meilisearch using the update settings endpoint:
curl \
-X PATCH 'MEILISEARCH_URL/indexes/INDEX_NAME/settings' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer MEILISEARCH_KEY' \
--data-binary '{
"embedders": {
"my-openai": {
"source": "openAi",
"model": "text-embedding-3-small",
"apiKey": "OPEN_AI_API_KEY",
"documentTemplate": "A product named '\'{{doc.name}}\'' described as '\'{{doc.description}}\''"
}
}
}'
Replace MEILISEARCH_URL with the address of your Meilisearch project, INDEX_NAME with your index name, MEILISEARCH_KEY with your Meilisearch API key, and OPEN_AI_API_KEY with your OpenAI API key. Meilisearch will start generating embeddings for all documents in the index. Monitor progress through the task queue.
Customize dimensions
OpenAI’s text-embedding-3-small and text-embedding-3-large models support custom dimensions. You can reduce the vector size to save storage and improve performance at the cost of some accuracy:
{
"my-openai": {
"source": "openAi",
"model": "text-embedding-3-small",
"apiKey": "OPEN_AI_API_KEY",
"dimensions": 512,
"documentTemplate": "A product named '{{doc.name}}'"
}
}
Lower dimension values reduce storage requirements and can speed up search. However, very low values may decrease result quality.
Never share your OpenAI API key publicly or commit it to version control. Use environment variables or a secrets manager to store it securely.
OpenAI applies rate limits based on your account tier. Free-tier accounts may experience slow indexing. Meilisearch handles rate limiting automatically with a retry strategy, but using at least a Tier 2 key is recommended for production environments.
Test the embedder
Once indexing is complete, perform a search using the hybrid parameter:
{
"q": "something to stir soup with",
"hybrid": {
"semanticRatio": 0.5,
"embedder": "my-openai"
}
}
A semanticRatio of 0.5 returns a balanced mix of keyword and semantic results. Adjust this value based on your needs.
Next steps
[**Choose an embedder**
Compare OpenAI with other embedder providers](/content/docs/capabilities/hybrid_search/how_to/choose_an_embedder/index.html)
[**Document templates**
Optimize which fields are embedded for better results](/content/docs/capabilities/hybrid_search/advanced/document_template_best_practices/index.html)