Source code for now.executor.gateway.bff.app.v1.routers.search

import base64
import logging
import os
from typing import Any, Dict, List

from fastapi import APIRouter, Body

from now.data_loading.create_dataclass import create_dataclass
from now.executor.gateway.bff.app.settings import GlobalUserInput
from now.executor.gateway.bff.app.v1.models.search import (
    SearchRequestModel,
    SearchResponseModel,
)
from now.executor.gateway.bff.app.v1.routers.helper import (
    field_dict_to_mm_doc,
    jina_client_post,
)
from now.executor.gateway.hubble_report import report_search_usage
from now.utils.docarray.helpers import (
    get_chunk_by_field_name,
    modality_string_to_docarray_typing,
)

logger = logging.getLogger(__file__)
logger.setLevel(os.environ.get('JINA_LOG_LEVEL', 'INFO'))

search_examples = {
    'working_text': {
        'summary': 'A working example: search with text',
        'description': 'A working example which can be tried out. Search with text on the best artworks dataset.',
        'value': {
            'limit': 10,
            'query': [
                {
                    'name': 'query_text_0',
                    'modality': 'text',
                    'value': 'cute cats',
                }
            ],
            'create_temp_link': False,
            'get_score_breakdown': True,
        },
    },
    'working_text_image': {
        'summary': 'A working example: search with text and image',
        'description': 'A working example which can be tried out. Search with text and image on the best artworks dataset.',
        'value': {
            'limit': 10,
            'query': [
                {
                    'name': 'query_text_0',
                    'modality': 'text',
                    'value': 'cute cats',
                },
                {
                    'name': 'query_image_0',
                    'modality': 'image',
                    'value': 'https://images.pexels.com/photos/45201/kitty-cat-kitten-pet-45201.jpeg',
                },
            ],
            'create_temp_link': False,
            'get_score_breakdown': True,
        },
    },
    'dummy': {
        'summary': 'A dummy example',
        'description': 'A dummy example,  do not run. For parameter reference only.',
        'value': {
            'limit': 10,
            'filters': {
                'color': ['blue', 'red'],
                'price': {'lte': 100, 'gte': 50},
            },
            'query': [
                {
                    'name': 'query_text_0',
                    'modality': 'text',
                    'value': 'cute cats',
                }
            ],
            'create_temp_link': False,
            'score_calculation': [['query_text_0', 'title', 'encoderclip', 1.0]],
        },
    },
}


router = APIRouter()





[docs]def get_score_calculation( data: SearchRequestModel, field_names_to_dataclass_fields: Dict[str, str] ) -> List[List[Any]]: """ Extract and process the score calculation from the request model to the format expected by the indexer. This includes converting the field names to the dataclass field names, for the query and for the index fields. :param data: the request model :param field_names_to_dataclass_fields: a mapping from the field names in the request model to the field names in the dataclass :return: the score calculation in the format expected by the indexer. Example: [['query_text', 'my_product_image', 'encoderclip', 1], ['query_text', 'my_product_text', 'bm25', 1]] """ score_calculation = [] user_input_in_bff = GlobalUserInput.user_input_in_bff for scr_calc in data.score_calculation: scr_calc[0] = field_names_to_dataclass_fields[scr_calc[0]] try: scr_calc[1] = user_input_in_bff.field_names_to_dataclass_fields[scr_calc[1]] except KeyError: raise KeyError( f'Field {scr_calc[1]} not found in dataclass. Please select possible values: ' f'{user_input_in_bff.field_names_to_dataclass_fields.keys()}' ) score_calculation.append(scr_calc) return score_calculation