|
| 1 | +--- |
| 2 | +title: Semantic Search with Hugging Face Inference Endpoints - Meilisearch documentation |
| 3 | +description: This guide will walk you through the process of setting up Meilisearch with Hugging Face Inference Endpoints. |
| 4 | +--- |
| 5 | + |
| 6 | +# Semantic search with Hugging Face Inference Endpoints |
| 7 | + |
| 8 | +## Introduction |
| 9 | + |
| 10 | +This guide will walk you through the process of setting up a Meilisearch REST embedder with [Hugging Face Inference Endpoints](https://ui.endpoints.huggingface.co/) to enable semantic search capabilities. |
| 11 | + |
| 12 | +<Capsule intent="note" title="`rest` or `huggingface`?"> |
| 13 | +You can use Hugging Face and Meilisearch in two ways: running the model locally by setting the embedder source to `huggingface`, or remotely in Hugging Face's servers by setting the embeder source to `rest`. |
| 14 | +</Capsule> |
| 15 | + |
| 16 | +## Requirements |
| 17 | + |
| 18 | +To follow this guide, you'll need: |
| 19 | + |
| 20 | +- A [Meilisearch Cloud](https://www.meilisearch.com/cloud) project running version 1.10 or above with the Vector store activated |
| 21 | +- A [Hugging Face account](https://huggingface.co/) with a deployed inference endpoint |
| 22 | +- The endpoint URL and API key of the deployed model on your Hugging Face account |
| 23 | + |
| 24 | +## Configure the embedder |
| 25 | + |
| 26 | +Set up an embedder using the update settings endpoint: |
| 27 | + |
| 28 | +```json |
| 29 | +{ |
| 30 | + "hf-inference": { |
| 31 | + "source": "rest", |
| 32 | + "url": "ENDPOINT_URL", |
| 33 | + "apiKey": "API_KEY", |
| 34 | + "dimensions": 384, |
| 35 | + "documentTemplate": "CUSTOM_LIQUID_TEMPLATE", |
| 36 | + "request": { |
| 37 | + "inputs": ["{{text}}", "{{..}}"], |
| 38 | + "model": "baai/bge-small-en-v1.5" |
| 39 | + }, |
| 40 | + "response": ["{{embedding}}", "{{..}}"] |
| 41 | + } |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +In this configuration: |
| 46 | + |
| 47 | +- `source`: declares Meilisearch should connect to this embedder via its REST API |
| 48 | +- `url`: replace `ENDPOINT_URL` with the address of your Hugging Face model endpoint |
| 49 | +- `apiKey`: replace `API_KEY` with your Hugging Face API key |
| 50 | +- `dimensions`: specifies the dimensions of the embeddings, which are 384 for `baai/bge-small-en-v1.5` |
| 51 | +- `documentTemplate`: an optional but recommended [template](/learn/ai_powered_search/getting_started_with_ai_search) for the data you will send the embedder |
| 52 | +- `request`: defines the structure and parameters of the request Meilisearch will send to the embedder |
| 53 | +- `response`: defines the structure of the embedder's response |
| 54 | + |
| 55 | +Once you've configured the embedder, Meilisearch will automatically generate embeddings for your documents. Monitor the task using the Cloud UI or the [get task endpoint](/reference/api/tasks). |
| 56 | + |
| 57 | +<Capsule intent="note"> |
| 58 | +This example uses [BAAI/bge-small-en-v1.5](https://huggingface.co/BAAI/bge-small-en-v1.5) as its model, but Hugging Face offers [other options that may fit your dataset better](https://ui.endpoints.huggingface.co/catalog?task=sentence-embeddings). |
| 59 | +</Capsule> |
| 60 | + |
| 61 | +## Perform a semantic search |
| 62 | + |
| 63 | +With the embedder set up, you can now perform semantic searches. Make a search request with the `hybrid` search parameter, setting `semanticRatio` to `1`: |
| 64 | + |
| 65 | +```json |
| 66 | +{ |
| 67 | + "q": "QUERY_TERMS", |
| 68 | + "hybrid": { |
| 69 | + "semanticRatio": 1, |
| 70 | + "embedder": "hf-inference" |
| 71 | + } |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +In this request: |
| 76 | + |
| 77 | +- `q`: the search query |
| 78 | +- `hybrid`: enables AI-powered search functionality |
| 79 | + - `semanticRatio`: controls the balance between semantic search and full-text search. Setting it to `1` means you will only receive semantic search results |
| 80 | + - `embedder`: the name of the embedder used for generating embeddings |
| 81 | + |
| 82 | +## Conclusion |
| 83 | + |
| 84 | +You have set up with an embedder using Hugging Face Inference Endpoints. This allows you to use pure semantic search capabilities in your application. |
| 85 | + |
| 86 | +Consult the [embedder setting documentation](/reference/api/settings#embedders-experimental) for more information on other embedder configuration options. |
0 commit comments