<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/google-ai-edge/mediapipe/blob/master/mediapipe/tasks/python/retrieval/semantic_retriever.py#L339-L744) |

Class that manages a vector index and retrieves relevant media records.

```python
mp.tasks.retrieval.SemanticRetriever(
    lib: serial_dispatcher.SerialDispatcher, handle: ctypes.c_void_p
) -> None
```

## Methods

### `close`

[View source](https://github.com/google-ai-edge/mediapipe/blob/master/mediapipe/tasks/python/retrieval/semantic_retriever.py#L729-L735)

```python
close() -> None
```

Shuts down and releases the retriever.

### `create_from_options`

[View source](https://github.com/google-ai-edge/mediapipe/blob/master/mediapipe/tasks/python/retrieval/semantic_retriever.py#L353-L364)

```python
@classmethod
create_from_options(
    options: `mp.tasks.retrieval.SemanticRetrieverOptions`
) -> 'SemanticRetriever'
```

Creates the `SemanticRetriever` object from options.

### `delete_all`

[View source](https://github.com/google-ai-edge/mediapipe/blob/master/mediapipe/tasks/python/retrieval/semantic_retriever.py#L725-L727)

```python
delete_all() -> None
```

Deletes all records from database.

### `delete_record`

[View source](https://github.com/google-ai-edge/mediapipe/blob/master/mediapipe/tasks/python/retrieval/semantic_retriever.py#L689-L694)

```python
delete_record(
    record_id: str
) -> None
```

Deletes record with the specified ID.

### `delete_with_metadata_filter`

[View source](https://github.com/google-ai-edge/mediapipe/blob/master/mediapipe/tasks/python/retrieval/semantic_retriever.py#L696-L708)

```python
delete_with_metadata_filter(
    metadata_filter: Dict[str, str]
) -> None
```

Deletes records matching the specified metadata filter.

### `get_all_record_ids`

[View source](https://github.com/google-ai-edge/mediapipe/blob/master/mediapipe/tasks/python/retrieval/semantic_retriever.py#L710-L723)

```python
get_all_record_ids() -> List[str]
```

Returns list of all active record IDs.

### `insert_audio`

[View source](https://github.com/google-ai-edge/mediapipe/blob/master/mediapipe/tasks/python/retrieval/semantic_retriever.py#L550-L593)

```python
insert_audio(
    audio_id: str,
    audio_path: str,
    audio_data: Optional[List[float]] = None,
    metadata: Optional[Dict[str, str]] = None
) -> None
```

Inserts an audio record into the vector store.

If `audio_data` is provided, the audio is loaded from those raw PCM samples
directly. Otherwise, if `audio_data` is not passed, the audio is
automatically loaded and decoded from `audio_path` on-demand.

<br />

<br />

<br />

| Args ||
|---|---|
| `audio_id` | The unique identifier for the audio record. |
| `audio_path` | The file path to the WAV file. Must be specified. |
| `audio_data` | Optional list of raw mono PCM float samples. |
| `metadata` | Optional dictionary of key-value metadata to associate. |

<br />

### `insert_content`

[View source](https://github.com/google-ai-edge/mediapipe/blob/master/mediapipe/tasks/python/retrieval/semantic_retriever.py#L595-L626)

```python
insert_content(
    record_id: str,
    parts: Sequence[Union[ContentPart, Dict[str, Any]]],
    metadata: Optional[Dict[str, str]] = None
) -> None
```

Inserts multi-modal parts with optional metadata.

<br />

<br />

<br />

| Args ||
|---|---|
| `record_id` | The unique identifier for the content record. |
| `parts` | A sequence of `TextPart`, `ImagePart`, or `AudioPart` instances (or legacy part dictionaries). |
| `metadata` | Optional dictionary of key-value metadata to associate. |

<br />

### `insert_document`

[View source](https://github.com/google-ai-edge/mediapipe/blob/master/mediapipe/tasks/python/retrieval/semantic_retriever.py#L485-L505)

```python
insert_document(
    doc_id: str, text: str, metadata: Optional[Dict[str, str]] = None
) -> None
```

Inserts text document with optional metadata.

### `insert_image`

[View source](https://github.com/google-ai-edge/mediapipe/blob/master/mediapipe/tasks/python/retrieval/semantic_retriever.py#L507-L548)

```python
insert_image(
    image_id: str,
    image_path: str,
    image_bytes: Optional[bytes] = None,
    metadata: Optional[Dict[str, str]] = None
) -> None
```

Inserts an image into the vector store.

If `image_bytes` is provided, the image is loaded from those raw bytes
directly. Otherwise, if `image_bytes` is not passed, the image is
automatically loaded and decoded from `image_path` on-demand.

<br />

<br />

<br />

| Args ||
|---|---|
| `image_id` | The unique identifier for the image record. |
| `image_path` | The file path or URI for the image. Must be specified. |
| `image_bytes` | Optional raw encoded image bytes (e.g. JPEG, PNG). |
| `metadata` | Optional dictionary of key-value metadata to associate. |

<br />

### `retrieve`

[View source](https://github.com/google-ai-edge/mediapipe/blob/master/mediapipe/tasks/python/retrieval/semantic_retriever.py#L628-L687)

```python
retrieve(
    query: Union[str, Sequence[Union[ContentPart, Dict[str, Any]]]],
    limit: int,
    min_similarity: float = 0.5,
    metadata_filter: Optional[Dict[str, str]] = None
) -> List[`mp.tasks.retrieval.RetrievalRecord`]
```

Retrieves nearest matching documents from the vector store.

<br />

<br />

<br />

| Args ||
|---|---|
| `query` | Either a plain text query string, or a sequence of `TextPart`, `ImagePart`, or `AudioPart` instances (or legacy part dictionaries) for multimodal retrieval. |
| `limit` | The maximum number of nearest records to return. |
| `min_similarity` | The minimum similarity score threshold. |
| `metadata_filter` | Optional dictionary of key-value pairs used to filter candidate records. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| A list of `RetrievalResult` items ordered by similarity score. ||

<br />

### `__enter__`

[View source](https://github.com/google-ai-edge/mediapipe/blob/master/mediapipe/tasks/python/retrieval/semantic_retriever.py#L737-L738)

```python
__enter__() -> 'SemanticRetriever'
```

### `__exit__`

[View source](https://github.com/google-ai-edge/mediapipe/blob/master/mediapipe/tasks/python/retrieval/semantic_retriever.py#L740-L741)

```python
__exit__(
    exc_type, exc_val, exc_tb
) -> None
```