Skip to content

Documentation for Index

lexy_py.index.models.Index

Bases: IndexModel

Index model

Source code in sdk-python/lexy_py/index/models.py
class Index(IndexModel):
    __doc__ = IndexModel.__doc__
    _client: Optional["LexyClient"] = PrivateAttr(default=None)

    def __init__(self, **data: Any):
        super().__init__(**data)
        self._client = data.pop("client", None)

    @property
    def client(self) -> "LexyClient":
        if not self._client:
            raise ValueError("API client has not been set.")
        return self._client

    def query(
        self,
        query_text: str = None,
        query_image: Image.Image | str = None,
        query_field: str = "embedding",
        k: int = 5,
        return_fields: list[str] = None,
        return_document: bool = False,
        embedding_model: str = None,
    ) -> list[dict]:
        """Synchronously query an index.

        Args:
            query_text (str): The query text.
            query_image (Image.Image | str): The query image. Can be a PIL Image object
                or a path to an image.
            query_field (str, optional): The field to query. Defaults to "embedding".
            k (int, optional): The number of records to return. Defaults to 5.
            return_fields (list[str], optional): The fields to return. Defaults to
                None, which returns all fields. To return fields from the linked
                document, use "document.<field_name>".
            return_document (bool, optional): Whether to return the document object.
                Defaults to False.
            embedding_model (str, optional): The name of the embedding model to use.
                Defaults to None, which uses the embedding model associated with
                `index_id.query_field`.

        Returns:
            Results: A list of query results.
        """
        return self.client.index.query_index(
            query_text=query_text,
            query_image=query_image,
            index_id=self.index_id,
            query_field=query_field,
            k=k,
            return_fields=return_fields,
            return_document=return_document,
            embedding_model=embedding_model,
        )

    def list_records(self, document_id: Optional[str] = None) -> list[dict]:
        """Synchronously list all records in the index.

        Args:
            document_id (str, optional): The document ID to filter by. Defaults to None.

        Returns:
            list[dict]: A list of records.
        """
        return self.client.index.list_index_records(
            self.index_id, document_id=document_id
        )

list_records(document_id=None)

Synchronously list all records in the index.

Parameters:

Name Type Description Default
document_id str

The document ID to filter by. Defaults to None.

None

Returns:

Type Description
list[dict]

list[dict]: A list of records.

Source code in sdk-python/lexy_py/index/models.py
def list_records(self, document_id: Optional[str] = None) -> list[dict]:
    """Synchronously list all records in the index.

    Args:
        document_id (str, optional): The document ID to filter by. Defaults to None.

    Returns:
        list[dict]: A list of records.
    """
    return self.client.index.list_index_records(
        self.index_id, document_id=document_id
    )

query(query_text=None, query_image=None, query_field='embedding', k=5, return_fields=None, return_document=False, embedding_model=None)

Synchronously query an index.

Parameters:

Name Type Description Default
query_text str

The query text.

None
query_image Image | str

The query image. Can be a PIL Image object or a path to an image.

None
query_field str

The field to query. Defaults to "embedding".

'embedding'
k int

The number of records to return. Defaults to 5.

5
return_fields list[str]

The fields to return. Defaults to None, which returns all fields. To return fields from the linked document, use "document.".

None
return_document bool

Whether to return the document object. Defaults to False.

False
embedding_model str

The name of the embedding model to use. Defaults to None, which uses the embedding model associated with index_id.query_field.

None

Returns:

Name Type Description
Results list[dict]

A list of query results.

Source code in sdk-python/lexy_py/index/models.py
def query(
    self,
    query_text: str = None,
    query_image: Image.Image | str = None,
    query_field: str = "embedding",
    k: int = 5,
    return_fields: list[str] = None,
    return_document: bool = False,
    embedding_model: str = None,
) -> list[dict]:
    """Synchronously query an index.

    Args:
        query_text (str): The query text.
        query_image (Image.Image | str): The query image. Can be a PIL Image object
            or a path to an image.
        query_field (str, optional): The field to query. Defaults to "embedding".
        k (int, optional): The number of records to return. Defaults to 5.
        return_fields (list[str], optional): The fields to return. Defaults to
            None, which returns all fields. To return fields from the linked
            document, use "document.<field_name>".
        return_document (bool, optional): Whether to return the document object.
            Defaults to False.
        embedding_model (str, optional): The name of the embedding model to use.
            Defaults to None, which uses the embedding model associated with
            `index_id.query_field`.

    Returns:
        Results: A list of query results.
    """
    return self.client.index.query_index(
        query_text=query_text,
        query_image=query_image,
        index_id=self.index_id,
        query_field=query_field,
        k=k,
        return_fields=return_fields,
        return_document=return_document,
        embedding_model=embedding_model,
    )

lexy_py.index.models.IndexModel

Bases: BaseModel

Index model

Parameters:

Name Type Description Default
index_id str

The ID of the index.

required
description str | None
None
index_table_schema dict[str, Any] | None
{}
index_fields dict[str, Any] | None
{}
created_at datetime | None
None
updated_at datetime | None
None
index_table_name str | None
None
Source code in sdk-python/lexy_py/index/models.py
class IndexModel(BaseModel):
    """Index model"""

    index_id: str = Field(
        default=...,
        min_length=1,
        max_length=56,
        pattern="^[a-z_][a-z0-9_]{0,55}$",
        description="The ID of the index.",
    )
    description: Optional[str] = None
    index_table_schema: Optional[dict[str, Any]] = Field(default={})
    index_fields: Optional[dict[str, Any]] = Field(default={})
    created_at: Optional[datetime] = None
    updated_at: Optional[datetime] = None
    index_table_name: Optional[str] = None

    def __repr__(self):
        return f"<Index('{self.index_id}', description='{self.description}')>"

lexy_py.index.client.IndexClient

This class is used to interact with the Lexy Indexes API.

Attributes:

Name Type Description
aclient AsyncClient

Asynchronous API client.

client Client

Synchronous API client.

Source code in sdk-python/lexy_py/index/client.py
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
class IndexClient:
    """
    This class is used to interact with the Lexy Indexes API.

    Attributes:
        aclient (httpx.AsyncClient): Asynchronous API client.
        client (httpx.Client): Synchronous API client.
    """

    def __init__(self, lexy_client: "LexyClient") -> None:
        self._lexy_client = lexy_client

    @property
    def aclient(self) -> httpx.AsyncClient:
        return self._lexy_client.aclient

    @property
    def client(self) -> httpx.Client:
        return self._lexy_client.client

    def list_indexes(self) -> list[Index]:
        """Synchronously get a list of all indexes.

        Returns:
            list[Index]: A list of all indexes.
        """
        r = self.client.get("/indexes")
        handle_response(r)
        return [Index(**index, client=self._lexy_client) for index in r.json()]

    async def alist_indexes(self) -> list[Index]:
        """Asynchronously get a list of all indexes.

        Returns:
            list[Index]: A list of all indexes.
        """
        r = await self.aclient.get("/indexes")
        handle_response(r)
        return [Index(**index, client=self._lexy_client) for index in r.json()]

    def get_index(self, index_id: str) -> Index:
        """Synchronously get an index.

        Args:
            index_id (str): The ID of the index to get.

        Returns:
            Index: The index.
        """
        r = self.client.get(f"/indexes/{index_id}")
        handle_response(r)
        return Index(**r.json(), client=self._lexy_client)

    async def aget_index(self, index_id: str) -> Index:
        """Asynchronously get an index.

        Args:
            index_id (str): The ID of the index to get.

        Returns:
            Index: The index.
        """
        r = await self.aclient.get(f"/indexes/{index_id}")
        handle_response(r)
        return Index(**r.json(), client=self._lexy_client)

    def create_index(
        self,
        index_id: str,
        description: Optional[str] = None,
        index_table_schema: Optional[dict[str, Any]] = None,
        index_fields: Optional[dict[str, Any]] = None,
    ) -> Index:
        """Synchronously create an index.

        Args:
            index_id (str): The ID of the index to create.
            description (str, optional): A description of the index. Defaults to None.
            index_table_schema (dict[str, Any]): The schema of the index table.
                Defaults to None.
            index_fields (dict[str, Any]): The index fields that are created in the
                index table. These are typically the fields you want to populate using
                transformers. Defaults to None.

        Returns:
            Index: The index.

        Examples:
            >>> from lexy_py import LexyClient
            >>> lx = LexyClient()
            >>> code_index_fields = {
            ...     "code": {"type": "text"},
            ...     "code_embedding": {"type": "embedding", "extras": {"dims": 384, "model": "text.embeddings.minilm"}},
            ...     "n_lines": {"type": "int"},
            ... }
            >>> index = lx.index.create_index(index_id="code_index",
            ...                               description="Code embedding index",
            ...                               index_fields=code_index_fields)
        """
        if index_table_schema is None:
            index_table_schema = {}
        if index_fields is None:
            index_fields = {}
        data = {
            "index_id": index_id,
            "description": description,
            "index_table_schema": index_table_schema,
            "index_fields": index_fields,
        }
        r = self.client.post("/indexes", json=data)
        handle_response(r)
        return Index(**r.json(), client=self._lexy_client)

    async def acreate_index(
        self,
        index_id: str,
        description: Optional[str] = None,
        index_table_schema: Optional[dict[str, Any]] = None,
        index_fields: Optional[dict[str, Any]] = None,
    ) -> Index:
        """Asynchronously create an index.

        Args:
            index_id (str): The ID of the index to create.
            description (str, optional): A description of the index. Defaults to None.
            index_table_schema (dict[str, Any]): The schema of the index table.
                Defaults to None.
            index_fields (dict[str, Any]): The index fields that are created in the
                index table. These are typically the fields you want to populate using
                transformers. Defaults to None.

        Returns:
            Index: The index.

        Examples:
            >>> from lexy_py import LexyClient
            >>> lx = LexyClient()
            >>> code_index_fields = {
            ...     "code": {"type": "text"},
            ...     "code_embedding": {"type": "embedding", "extras": {"dims": 384, "model": "text.embeddings.minilm"}},
            ...     "n_lines": {"type": "int"},
            ... }
            >>> index = await lx.index.acreate_index(index_id="code_index",
            ...                                      description="Code embedding index",
            ...                                      index_fields=code_index_fields)
        """
        if index_table_schema is None:
            index_table_schema = {}
        if index_fields is None:
            index_fields = {}
        data = {
            "index_id": index_id,
            "description": description,
            "index_table_schema": index_table_schema,
            "index_fields": index_fields,
        }
        r = await self.aclient.post("/indexes", json=data)
        handle_response(r)
        return Index(**r.json(), client=self._lexy_client)

    def delete_index(self, index_id: str, drop_table: bool = False) -> dict:
        """Synchronously delete an index.

        Args:
            index_id (str): The ID of the index to delete.
            drop_table (bool, optional): Whether to drop the index table from the
                database. Defaults to False.
        """
        r = self.client.delete(
            f"/indexes/{index_id}", params={"drop_table": drop_table}
        )
        handle_response(r)
        return r.json()

    async def adelete_index(self, index_id: str, drop_table: bool = False) -> dict:
        """Asynchronously delete an index.

        Args:
            index_id (str): The ID of the index to delete.
            drop_table (bool, optional): Whether to drop the index table from the
                database. Defaults to False.
        """
        r = await self.aclient.delete(
            f"/indexes/{index_id}", params={"drop_table": drop_table}
        )
        handle_response(r)
        return r.json()

    def update_index(
        self,
        index_id: str,
        description: Optional[str] = None,
        index_table_schema: Optional[dict[str, Any]] = None,
        index_fields: Optional[dict[str, Any]] = None,
    ) -> Index:
        """Synchronously update an index.

        Args:
            index_id (str): The ID of the index to update.
            description (str, optional): The new description of the index.
            index_table_schema (dict[str, Any], optional): The new schema of the index
                table.
            index_fields (dict[str, Any], optional): The new value for index fields
                that are created in the index table.

        Returns:
            Index: The updated index.
        """
        index = IndexUpdate(
            description=description,
            index_table_schema=index_table_schema,
            index_fields=index_fields,
        )
        r = self.client.patch(
            f"/indexes/{index_id}", json=index.model_dump(exclude_none=True)
        )
        handle_response(r)
        return Index(**r.json(), client=self._lexy_client)

    async def aupdate_index(
        self,
        index_id: str,
        description: Optional[str] = None,
        index_table_schema: Optional[dict[str, Any]] = None,
        index_fields: Optional[dict[str, Any]] = None,
    ) -> Index:
        """Asynchronously update an index.

        Args:
            index_id (str): The ID of the index to update.
            description (str, optional): The new description of the index.
            index_table_schema (dict[str, Any], optional): The new schema of the index
                table.
            index_fields (dict[str, Any], optional): The new value for index fields
                that are created in the index table.

        Returns:
            Index: The updated index.
        """
        index = IndexUpdate(
            description=description,
            index_table_schema=index_table_schema,
            index_fields=index_fields,
        )
        r = await self.aclient.patch(
            f"/indexes/{index_id}", json=index.model_dump(exclude_none=True)
        )
        handle_response(r)
        return Index(**r.json(), client=self._lexy_client)

    def list_index_records(
        self, index_id: str, document_id: Optional[str] = None
    ) -> list[dict]:
        """Synchronously get a list of all index records for an index.

        Args:
            index_id (str): The ID of the index to get records for.
            document_id (str, optional): The ID of a document to get records for.

        Returns:
            list[dict]: A list of all index records for an index.
        """
        params = {}
        if document_id:
            params["document_id"] = document_id
        r = self.client.get(f"/indexes/{index_id}/records", params=params)
        handle_response(r)
        return r.json()

    async def alist_index_records(
        self, index_id: str, document_id: Optional[str] = None
    ) -> list[dict]:
        """Asynchronously get a list of all index records for an index.

        Args:
            index_id (str): The ID of the index to get records for.
            document_id (str, optional): The ID of a document to get records for.

        Returns:
            list[dict]: A list of all index records for an index.
        """
        params = {}
        if document_id:
            params["document_id"] = document_id
        r = await self.aclient.get(f"/indexes/{index_id}/records", params=params)
        handle_response(r)
        return r.json()

    def query_index(
        self,
        query_text: str = None,
        query_image: Image.Image | str = None,
        index_id: str = "default_text_embeddings",
        query_field: str = "embedding",
        k: int = 5,
        return_fields: list[str] = None,
        return_document: bool = False,
        embedding_model: str = None,
    ) -> list[dict]:
        """Synchronously query an index.

        Args:
            query_text (str): The query text.
            query_image (Image.Image | str): The query image. Can be a PIL Image object
                or a path to an image.
            index_id (str): The ID of the index to query. Defaults to
                "default_text_embeddings".
            query_field (str, optional): The field to query. Defaults to "embedding".
            k (int, optional): The number of records to return. Defaults to 5.
            return_fields (list[str], optional): The fields to return. Defaults to
                None, which returns all fields. To return fields from the linked
                document, use "document.<field_name>".
            return_document (bool, optional): Whether to return the document object.
                Defaults to False.
            embedding_model (str, optional): The name of the embedding model to use.
                Defaults to None, which uses the embedding model associated with
                `index_id.query_field`.

        Returns:
            list[dict]: The query results.

        Examples:
            Query an index using text:

            >>> from lexy_py import LexyClient
            >>> lx = LexyClient()
            >>> lx.query_index(query_text="Test Query")

            Query an index using text and return specific fields:

            >>> lx.query_index(
            ...     query_text="Test Query",
            ...     return_fields=["my_index_field", "document.content"]
            ... )

            Query an index using an image file:

            >>> lx.query_index(query_image="test_image.jpg", index_id="my_image_index")

            Query an index using a PIL Image object:

            >>> import httpx
            >>> from PIL import Image
            >>> img_url = 'https://getlexy.com/assets/images/dalle-agi.jpeg'
            >>> image = Image.open(httpx.get(img_url))
            >>> lx.query_index(query_image=image, index_id="my_image_index", k=3)
        """
        files, params = self._process_query_params(
            query_text=query_text,
            query_image=query_image,
            query_field=query_field,
            k=k,
            return_fields=return_fields,
            return_document=return_document,
            embedding_model=embedding_model,
        )

        r = self.client.post(
            f"/indexes/{index_id}/records/query", files=files, params=params
        )
        handle_response(r)
        search_results = r.json()["search_results"]
        if return_document:
            for result in search_results:
                result["document"] = Document(
                    **result["document"], client=self._lexy_client
                )
        return search_results

    async def aquery_index(
        self,
        query_text: str = None,
        query_image: Image.Image | str = None,
        index_id: str = "default_text_embeddings",
        query_field: str = "embedding",
        k: int = 5,
        return_fields: list[str] = None,
        return_document: bool = False,
        embedding_model: str = None,
    ) -> list[dict]:
        """Asynchronously query an index.

        Args:
            query_text (str): The query text.
            query_image (Image.Image | str): The query image. Can be a PIL Image object
                or a path to an image.
            index_id (str): The ID of the index to query. Defaults to
                "default_text_embeddings".
            query_field (str, optional): The field to query. Defaults to "embedding".
            k (int, optional): The number of records to return. Defaults to 5.
            return_fields (list[str], optional): The fields to return. Defaults to
                None, which returns all fields. To return fields from the linked
                document, use "document.<field_name>".
            return_document (bool, optional): Whether to return the document object.
                Defaults to False.
            embedding_model (str, optional): The name of the embedding model to use.
                Defaults to None, which uses the embedding model associated with
                `index_id.query_field`.

        Returns:
            list[dict]: The query results.
        """
        files, params = self._process_query_params(
            query_text=query_text,
            query_image=query_image,
            query_field=query_field,
            k=k,
            return_fields=return_fields,
            return_document=return_document,
            embedding_model=embedding_model,
        )

        r = await self.aclient.post(
            f"/indexes/{index_id}/records/query", files=files, params=params
        )
        handle_response(r)
        search_results = r.json()["search_results"]
        if return_document:
            for result in search_results:
                result["document"] = Document(
                    **result["document"], client=self._lexy_client
                )
        return search_results

    @staticmethod
    def _process_query_params(
        query_text: str,
        query_image: Image.Image | str,
        query_field: str,
        k: int,
        return_fields: list[str],
        return_document: bool,
        embedding_model: str,
    ) -> tuple[dict, dict]:
        files = {}

        if query_text and query_image:
            raise LexyClientError(
                "Please submit either 'query_text' or 'query_image', not both."
            )
        elif query_text:
            files["query_text"] = (None, query_text)
        elif query_image:
            if isinstance(query_image, str):
                image = Image.open(query_image)
                filename = os.path.basename(query_image)
            else:
                image = query_image
                filename = (
                    f"image.{image.format.lower()}" if image.format else "image.jpg"
                )
            buffer = io.BytesIO()
            image_format = image.format or "jpg"
            image.save(buffer, format=image_format)
            buffer.seek(0)
            mime_type = mimetypes.types_map.get(
                f".{image_format.lower()}", "application/octet-stream"
            )
            files["query_image"] = (filename, buffer, mime_type)
        else:
            raise LexyClientError("Please submit either 'query_text' or 'query_image'.")

        if return_fields is None:
            return_fields = []
        params = {
            "query_field": query_field,
            "k": k,
            "return_fields": return_fields,
            "return_document": return_document,
        }
        if embedding_model:
            params["embedding_model"] = embedding_model

        return files, params

create_index(index_id, description=None, index_table_schema=None, index_fields=None)

Synchronously create an index.

Parameters:

Name Type Description Default
index_id str

The ID of the index to create.

required
description str

A description of the index. Defaults to None.

None
index_table_schema dict[str, Any]

The schema of the index table. Defaults to None.

None
index_fields dict[str, Any]

The index fields that are created in the index table. These are typically the fields you want to populate using transformers. Defaults to None.

None

Returns:

Name Type Description
Index Index

The index.

Examples:

>>> from lexy_py import LexyClient
>>> lx = LexyClient()
>>> code_index_fields = {
...     "code": {"type": "text"},
...     "code_embedding": {"type": "embedding", "extras": {"dims": 384, "model": "text.embeddings.minilm"}},
...     "n_lines": {"type": "int"},
... }
>>> index = lx.index.create_index(index_id="code_index",
...                               description="Code embedding index",
...                               index_fields=code_index_fields)
Source code in sdk-python/lexy_py/index/client.py
def create_index(
    self,
    index_id: str,
    description: Optional[str] = None,
    index_table_schema: Optional[dict[str, Any]] = None,
    index_fields: Optional[dict[str, Any]] = None,
) -> Index:
    """Synchronously create an index.

    Args:
        index_id (str): The ID of the index to create.
        description (str, optional): A description of the index. Defaults to None.
        index_table_schema (dict[str, Any]): The schema of the index table.
            Defaults to None.
        index_fields (dict[str, Any]): The index fields that are created in the
            index table. These are typically the fields you want to populate using
            transformers. Defaults to None.

    Returns:
        Index: The index.

    Examples:
        >>> from lexy_py import LexyClient
        >>> lx = LexyClient()
        >>> code_index_fields = {
        ...     "code": {"type": "text"},
        ...     "code_embedding": {"type": "embedding", "extras": {"dims": 384, "model": "text.embeddings.minilm"}},
        ...     "n_lines": {"type": "int"},
        ... }
        >>> index = lx.index.create_index(index_id="code_index",
        ...                               description="Code embedding index",
        ...                               index_fields=code_index_fields)
    """
    if index_table_schema is None:
        index_table_schema = {}
    if index_fields is None:
        index_fields = {}
    data = {
        "index_id": index_id,
        "description": description,
        "index_table_schema": index_table_schema,
        "index_fields": index_fields,
    }
    r = self.client.post("/indexes", json=data)
    handle_response(r)
    return Index(**r.json(), client=self._lexy_client)

delete_index(index_id, drop_table=False)

Synchronously delete an index.

Parameters:

Name Type Description Default
index_id str

The ID of the index to delete.

required
drop_table bool

Whether to drop the index table from the database. Defaults to False.

False
Source code in sdk-python/lexy_py/index/client.py
def delete_index(self, index_id: str, drop_table: bool = False) -> dict:
    """Synchronously delete an index.

    Args:
        index_id (str): The ID of the index to delete.
        drop_table (bool, optional): Whether to drop the index table from the
            database. Defaults to False.
    """
    r = self.client.delete(
        f"/indexes/{index_id}", params={"drop_table": drop_table}
    )
    handle_response(r)
    return r.json()

get_index(index_id)

Synchronously get an index.

Parameters:

Name Type Description Default
index_id str

The ID of the index to get.

required

Returns:

Name Type Description
Index Index

The index.

Source code in sdk-python/lexy_py/index/client.py
def get_index(self, index_id: str) -> Index:
    """Synchronously get an index.

    Args:
        index_id (str): The ID of the index to get.

    Returns:
        Index: The index.
    """
    r = self.client.get(f"/indexes/{index_id}")
    handle_response(r)
    return Index(**r.json(), client=self._lexy_client)

list_indexes()

Synchronously get a list of all indexes.

Returns:

Type Description
list[Index]

list[Index]: A list of all indexes.

Source code in sdk-python/lexy_py/index/client.py
def list_indexes(self) -> list[Index]:
    """Synchronously get a list of all indexes.

    Returns:
        list[Index]: A list of all indexes.
    """
    r = self.client.get("/indexes")
    handle_response(r)
    return [Index(**index, client=self._lexy_client) for index in r.json()]

query_index(query_text=None, query_image=None, index_id='default_text_embeddings', query_field='embedding', k=5, return_fields=None, return_document=False, embedding_model=None)

Synchronously query an index.

Parameters:

Name Type Description Default
query_text str

The query text.

None
query_image Image | str

The query image. Can be a PIL Image object or a path to an image.

None
index_id str

The ID of the index to query. Defaults to "default_text_embeddings".

'default_text_embeddings'
query_field str

The field to query. Defaults to "embedding".

'embedding'
k int

The number of records to return. Defaults to 5.

5
return_fields list[str]

The fields to return. Defaults to None, which returns all fields. To return fields from the linked document, use "document.".

None
return_document bool

Whether to return the document object. Defaults to False.

False
embedding_model str

The name of the embedding model to use. Defaults to None, which uses the embedding model associated with index_id.query_field.

None

Returns:

Type Description
list[dict]

list[dict]: The query results.

Examples:

Query an index using text:

>>> from lexy_py import LexyClient
>>> lx = LexyClient()
>>> lx.query_index(query_text="Test Query")

Query an index using text and return specific fields:

>>> lx.query_index(
...     query_text="Test Query",
...     return_fields=["my_index_field", "document.content"]
... )

Query an index using an image file:

>>> lx.query_index(query_image="test_image.jpg", index_id="my_image_index")

Query an index using a PIL Image object:

>>> import httpx
>>> from PIL import Image
>>> img_url = 'https://getlexy.com/assets/images/dalle-agi.jpeg'
>>> image = Image.open(httpx.get(img_url))
>>> lx.query_index(query_image=image, index_id="my_image_index", k=3)
Source code in sdk-python/lexy_py/index/client.py
def query_index(
    self,
    query_text: str = None,
    query_image: Image.Image | str = None,
    index_id: str = "default_text_embeddings",
    query_field: str = "embedding",
    k: int = 5,
    return_fields: list[str] = None,
    return_document: bool = False,
    embedding_model: str = None,
) -> list[dict]:
    """Synchronously query an index.

    Args:
        query_text (str): The query text.
        query_image (Image.Image | str): The query image. Can be a PIL Image object
            or a path to an image.
        index_id (str): The ID of the index to query. Defaults to
            "default_text_embeddings".
        query_field (str, optional): The field to query. Defaults to "embedding".
        k (int, optional): The number of records to return. Defaults to 5.
        return_fields (list[str], optional): The fields to return. Defaults to
            None, which returns all fields. To return fields from the linked
            document, use "document.<field_name>".
        return_document (bool, optional): Whether to return the document object.
            Defaults to False.
        embedding_model (str, optional): The name of the embedding model to use.
            Defaults to None, which uses the embedding model associated with
            `index_id.query_field`.

    Returns:
        list[dict]: The query results.

    Examples:
        Query an index using text:

        >>> from lexy_py import LexyClient
        >>> lx = LexyClient()
        >>> lx.query_index(query_text="Test Query")

        Query an index using text and return specific fields:

        >>> lx.query_index(
        ...     query_text="Test Query",
        ...     return_fields=["my_index_field", "document.content"]
        ... )

        Query an index using an image file:

        >>> lx.query_index(query_image="test_image.jpg", index_id="my_image_index")

        Query an index using a PIL Image object:

        >>> import httpx
        >>> from PIL import Image
        >>> img_url = 'https://getlexy.com/assets/images/dalle-agi.jpeg'
        >>> image = Image.open(httpx.get(img_url))
        >>> lx.query_index(query_image=image, index_id="my_image_index", k=3)
    """
    files, params = self._process_query_params(
        query_text=query_text,
        query_image=query_image,
        query_field=query_field,
        k=k,
        return_fields=return_fields,
        return_document=return_document,
        embedding_model=embedding_model,
    )

    r = self.client.post(
        f"/indexes/{index_id}/records/query", files=files, params=params
    )
    handle_response(r)
    search_results = r.json()["search_results"]
    if return_document:
        for result in search_results:
            result["document"] = Document(
                **result["document"], client=self._lexy_client
            )
    return search_results

update_index(index_id, description=None, index_table_schema=None, index_fields=None)

Synchronously update an index.

Parameters:

Name Type Description Default
index_id str

The ID of the index to update.

required
description str

The new description of the index.

None
index_table_schema dict[str, Any]

The new schema of the index table.

None
index_fields dict[str, Any]

The new value for index fields that are created in the index table.

None

Returns:

Name Type Description
Index Index

The updated index.

Source code in sdk-python/lexy_py/index/client.py
def update_index(
    self,
    index_id: str,
    description: Optional[str] = None,
    index_table_schema: Optional[dict[str, Any]] = None,
    index_fields: Optional[dict[str, Any]] = None,
) -> Index:
    """Synchronously update an index.

    Args:
        index_id (str): The ID of the index to update.
        description (str, optional): The new description of the index.
        index_table_schema (dict[str, Any], optional): The new schema of the index
            table.
        index_fields (dict[str, Any], optional): The new value for index fields
            that are created in the index table.

    Returns:
        Index: The updated index.
    """
    index = IndexUpdate(
        description=description,
        index_table_schema=index_table_schema,
        index_fields=index_fields,
    )
    r = self.client.patch(
        f"/indexes/{index_id}", json=index.model_dump(exclude_none=True)
    )
    handle_response(r)
    return Index(**r.json(), client=self._lexy_client)

acreate_index(index_id, description=None, index_table_schema=None, index_fields=None) async

Asynchronously create an index.

Parameters:

Name Type Description Default
index_id str

The ID of the index to create.

required
description str

A description of the index. Defaults to None.

None
index_table_schema dict[str, Any]

The schema of the index table. Defaults to None.

None
index_fields dict[str, Any]

The index fields that are created in the index table. These are typically the fields you want to populate using transformers. Defaults to None.

None

Returns:

Name Type Description
Index Index

The index.

Examples:

>>> from lexy_py import LexyClient
>>> lx = LexyClient()
>>> code_index_fields = {
...     "code": {"type": "text"},
...     "code_embedding": {"type": "embedding", "extras": {"dims": 384, "model": "text.embeddings.minilm"}},
...     "n_lines": {"type": "int"},
... }
>>> index = await lx.index.acreate_index(index_id="code_index",
...                                      description="Code embedding index",
...                                      index_fields=code_index_fields)
Source code in sdk-python/lexy_py/index/client.py
async def acreate_index(
    self,
    index_id: str,
    description: Optional[str] = None,
    index_table_schema: Optional[dict[str, Any]] = None,
    index_fields: Optional[dict[str, Any]] = None,
) -> Index:
    """Asynchronously create an index.

    Args:
        index_id (str): The ID of the index to create.
        description (str, optional): A description of the index. Defaults to None.
        index_table_schema (dict[str, Any]): The schema of the index table.
            Defaults to None.
        index_fields (dict[str, Any]): The index fields that are created in the
            index table. These are typically the fields you want to populate using
            transformers. Defaults to None.

    Returns:
        Index: The index.

    Examples:
        >>> from lexy_py import LexyClient
        >>> lx = LexyClient()
        >>> code_index_fields = {
        ...     "code": {"type": "text"},
        ...     "code_embedding": {"type": "embedding", "extras": {"dims": 384, "model": "text.embeddings.minilm"}},
        ...     "n_lines": {"type": "int"},
        ... }
        >>> index = await lx.index.acreate_index(index_id="code_index",
        ...                                      description="Code embedding index",
        ...                                      index_fields=code_index_fields)
    """
    if index_table_schema is None:
        index_table_schema = {}
    if index_fields is None:
        index_fields = {}
    data = {
        "index_id": index_id,
        "description": description,
        "index_table_schema": index_table_schema,
        "index_fields": index_fields,
    }
    r = await self.aclient.post("/indexes", json=data)
    handle_response(r)
    return Index(**r.json(), client=self._lexy_client)

adelete_index(index_id, drop_table=False) async

Asynchronously delete an index.

Parameters:

Name Type Description Default
index_id str

The ID of the index to delete.

required
drop_table bool

Whether to drop the index table from the database. Defaults to False.

False
Source code in sdk-python/lexy_py/index/client.py
async def adelete_index(self, index_id: str, drop_table: bool = False) -> dict:
    """Asynchronously delete an index.

    Args:
        index_id (str): The ID of the index to delete.
        drop_table (bool, optional): Whether to drop the index table from the
            database. Defaults to False.
    """
    r = await self.aclient.delete(
        f"/indexes/{index_id}", params={"drop_table": drop_table}
    )
    handle_response(r)
    return r.json()

aget_index(index_id) async

Asynchronously get an index.

Parameters:

Name Type Description Default
index_id str

The ID of the index to get.

required

Returns:

Name Type Description
Index Index

The index.

Source code in sdk-python/lexy_py/index/client.py
async def aget_index(self, index_id: str) -> Index:
    """Asynchronously get an index.

    Args:
        index_id (str): The ID of the index to get.

    Returns:
        Index: The index.
    """
    r = await self.aclient.get(f"/indexes/{index_id}")
    handle_response(r)
    return Index(**r.json(), client=self._lexy_client)

alist_indexes() async

Asynchronously get a list of all indexes.

Returns:

Type Description
list[Index]

list[Index]: A list of all indexes.

Source code in sdk-python/lexy_py/index/client.py
async def alist_indexes(self) -> list[Index]:
    """Asynchronously get a list of all indexes.

    Returns:
        list[Index]: A list of all indexes.
    """
    r = await self.aclient.get("/indexes")
    handle_response(r)
    return [Index(**index, client=self._lexy_client) for index in r.json()]

aupdate_index(index_id, description=None, index_table_schema=None, index_fields=None) async

Asynchronously update an index.

Parameters:

Name Type Description Default
index_id str

The ID of the index to update.

required
description str

The new description of the index.

None
index_table_schema dict[str, Any]

The new schema of the index table.

None
index_fields dict[str, Any]

The new value for index fields that are created in the index table.

None

Returns:

Name Type Description
Index Index

The updated index.

Source code in sdk-python/lexy_py/index/client.py
async def aupdate_index(
    self,
    index_id: str,
    description: Optional[str] = None,
    index_table_schema: Optional[dict[str, Any]] = None,
    index_fields: Optional[dict[str, Any]] = None,
) -> Index:
    """Asynchronously update an index.

    Args:
        index_id (str): The ID of the index to update.
        description (str, optional): The new description of the index.
        index_table_schema (dict[str, Any], optional): The new schema of the index
            table.
        index_fields (dict[str, Any], optional): The new value for index fields
            that are created in the index table.

    Returns:
        Index: The updated index.
    """
    index = IndexUpdate(
        description=description,
        index_table_schema=index_table_schema,
        index_fields=index_fields,
    )
    r = await self.aclient.patch(
        f"/indexes/{index_id}", json=index.model_dump(exclude_none=True)
    )
    handle_response(r)
    return Index(**r.json(), client=self._lexy_client)