|
| 1 | +""" |
| 2 | +This module provides functionality for managing NL search models in Typesense. |
| 3 | +
|
| 4 | +Classes: |
| 5 | + - NLSearchModels: Handles operations related to NL search models. |
| 6 | +
|
| 7 | +Methods: |
| 8 | + - __init__: Initializes the NLSearchModels object. |
| 9 | + - __getitem__: Retrieves or creates an NLSearchModel object for a given model_id. |
| 10 | + - create: Creates a new NL search model. |
| 11 | + - retrieve: Retrieves all NL search models. |
| 12 | +
|
| 13 | +Attributes: |
| 14 | + - resource_path: The API resource path for NL search models operations. |
| 15 | +
|
| 16 | +The NLSearchModels class interacts with the Typesense API to manage |
| 17 | +NL search model operations. |
| 18 | +
|
| 19 | +It provides methods to create and retrieve NL search models, as well as access |
| 20 | +individual NLSearchModel objects. |
| 21 | +
|
| 22 | +This module uses type hinting and is compatible with Python 3.11+ as well as earlier |
| 23 | +versions through the use of the typing_extensions library. |
| 24 | +""" |
| 25 | + |
| 26 | +import sys |
| 27 | + |
| 28 | +from typesense.api_call import ApiCall |
| 29 | +from typesense.types.nl_search_model import ( |
| 30 | + NLSearchModelCreateSchema, |
| 31 | + NLSearchModelSchema, |
| 32 | + NLSearchModelsRetrieveSchema, |
| 33 | +) |
| 34 | + |
| 35 | +if sys.version_info > (3, 11): |
| 36 | + import typing |
| 37 | +else: |
| 38 | + import typing_extensions as typing |
| 39 | + |
| 40 | +from typesense.nl_search_model import NLSearchModel |
| 41 | + |
| 42 | + |
| 43 | +class NLSearchModels(object): |
| 44 | + """ |
| 45 | + Class for managing NL search models in Typesense. |
| 46 | +
|
| 47 | + This class provides methods to interact with NL search models, including |
| 48 | + creating, retrieving, and accessing individual models. |
| 49 | +
|
| 50 | + Attributes: |
| 51 | + resource_path (str): The API resource path for NL search models operations. |
| 52 | + api_call (ApiCall): The API call object for making requests. |
| 53 | + nl_search_models (Dict[str, NLSearchModel]): |
| 54 | + A dictionary of NLSearchModel objects. |
| 55 | + """ |
| 56 | + |
| 57 | + resource_path: typing.Final[str] = "/nl_search_models" |
| 58 | + |
| 59 | + def __init__(self, api_call: ApiCall) -> None: |
| 60 | + """ |
| 61 | + Initialize the NLSearchModels object. |
| 62 | +
|
| 63 | + Args: |
| 64 | + api_call (ApiCall): The API call object for making requests. |
| 65 | + """ |
| 66 | + self.api_call = api_call |
| 67 | + self.nl_search_models: typing.Dict[str, NLSearchModel] = {} |
| 68 | + |
| 69 | + def __getitem__(self, model_id: str) -> NLSearchModel: |
| 70 | + """ |
| 71 | + Get or create an NLSearchModel object for a given model_id. |
| 72 | +
|
| 73 | + Args: |
| 74 | + model_id (str): The ID of the NL search model. |
| 75 | +
|
| 76 | + Returns: |
| 77 | + NLSearchModel: The NLSearchModel object for the given ID. |
| 78 | + """ |
| 79 | + if model_id not in self.nl_search_models: |
| 80 | + self.nl_search_models[model_id] = NLSearchModel( |
| 81 | + self.api_call, |
| 82 | + model_id, |
| 83 | + ) |
| 84 | + return self.nl_search_models[model_id] |
| 85 | + |
| 86 | + def create(self, model: NLSearchModelCreateSchema) -> NLSearchModelSchema: |
| 87 | + """ |
| 88 | + Create a new NL search model. |
| 89 | +
|
| 90 | + Args: |
| 91 | + model (NLSearchModelCreateSchema): |
| 92 | + The schema for creating the NL search model. |
| 93 | +
|
| 94 | + Returns: |
| 95 | + NLSearchModelSchema: The created NL search model. |
| 96 | + """ |
| 97 | + response = self.api_call.post( |
| 98 | + endpoint=NLSearchModels.resource_path, |
| 99 | + entity_type=NLSearchModelSchema, |
| 100 | + as_json=True, |
| 101 | + body=model, |
| 102 | + ) |
| 103 | + return response |
| 104 | + |
| 105 | + def retrieve(self) -> NLSearchModelsRetrieveSchema: |
| 106 | + """ |
| 107 | + Retrieve all NL search models. |
| 108 | +
|
| 109 | + Returns: |
| 110 | + NLSearchModelsRetrieveSchema: A list of all NL search models. |
| 111 | + """ |
| 112 | + response: NLSearchModelsRetrieveSchema = self.api_call.get( |
| 113 | + endpoint=NLSearchModels.resource_path, |
| 114 | + entity_type=NLSearchModelsRetrieveSchema, |
| 115 | + as_json=True, |
| 116 | + ) |
| 117 | + return response |
0 commit comments