Implementation:Run llama Llama index BaseImageRetriever
Overview
BaseImageRetriever is an abstract base class that defines the interface for image retrieval in LlamaIndex. It supports two retrieval modes -- text-to-image and image-to-image -- each with both synchronous and asynchronous variants. Subclasses must implement four abstract methods to provide the actual retrieval logic.
Source file: llama-index-core/llama_index/core/image_retriever.py (112 lines)
Class Hierarchy
PromptMixin, DispatcherSpanMixin └── BaseImageRetriever
BaseImageRetriever inherits from:
PromptMixin-- provides prompt management capabilitiesDispatcherSpanMixin-- provides instrumentation and span tracking
Retrieval Modes
The class supports two distinct retrieval paradigms:
| Mode | Description | Input |
|---|---|---|
| Text-to-Image | Retrieve images based on a text query | Text string or QueryBundle
|
| Image-to-Image | Retrieve images similar to a given image | Image path string or QueryBundle
|
Public Methods
text_to_image_retrieve
def text_to_image_retrieve(
self, str_or_query_bundle: QueryType
) -> List[NodeWithScore]:
Retrieves image nodes given a text query. Accepts either a plain string or a QueryBundle object. If a string is provided, it is automatically wrapped in a QueryBundle with the string set as query_str. Delegates to the abstract _text_to_image_retrieve method.
image_to_image_retrieve
def image_to_image_retrieve(
self, str_or_query_bundle: QueryType
) -> List[NodeWithScore]:
Retrieves image nodes given an input image. Accepts either a file path string or a QueryBundle. If a string is provided, wraps it in a QueryBundle with query_str set to empty and image_path set to the provided string. Delegates to the abstract _image_to_image_retrieve method.
atext_to_image_retrieve
async def atext_to_image_retrieve(
self, str_or_query_bundle: QueryType
) -> List[NodeWithScore]:
Async version of text_to_image_retrieve. Performs the same input normalization and delegates to _atext_to_image_retrieve.
aimage_to_image_retrieve
async def aimage_to_image_retrieve(
self, str_or_query_bundle: QueryType
) -> List[NodeWithScore]:
Async version of image_to_image_retrieve. Performs the same input normalization and delegates to _aimage_to_image_retrieve.
Abstract Methods (Must Be Implemented by Subclasses)
| Method | Signature | Description |
|---|---|---|
_text_to_image_retrieve |
(query_bundle: QueryBundle) -> List[NodeWithScore] |
Sync text-to-image retrieval logic |
_image_to_image_retrieve |
(query_bundle: QueryBundle) -> List[NodeWithScore] |
Sync image-to-image retrieval logic |
_atext_to_image_retrieve |
async (query_bundle: QueryBundle) -> List[NodeWithScore] |
Async text-to-image retrieval logic |
_aimage_to_image_retrieve |
async (query_bundle: QueryBundle) -> List[NodeWithScore] |
Async image-to-image retrieval logic |
All four methods receive a normalized QueryBundle (never a raw string) and must return a list of NodeWithScore objects.
Input Handling Pattern
Both the sync and async public methods follow the same pattern:
- Accept
QueryType(which isUnion[str, QueryBundle]). - If the input is a string, wrap it in an appropriate
QueryBundle. - Delegate to the corresponding abstract method.
For text-to-image, the string is used as query_str. For image-to-image, the string is used as image_path with an empty query_str.
Dependencies
llama_index.core.indices.query.schema-- providesQueryBundleandQueryTypellama_index.core.schema-- providesNodeWithScorellama_index.core.instrumentation-- providesDispatcherSpanMixinllama_index.core.prompts.mixin-- providesPromptMixin