code: from typing import Any, List, Optional, Dict
  from ._base import Record, VectorStore
  from ._embeddings import Embeddings
  
  VECTOR_COLUMN_NAME = "_vector"
  
  
  class LanceDB(VectorStore):
      def __init__(self, db_uri, embeddings: Embeddings = None) -> None:
          super().__init__()
  
          try:
              import pyarrow as pa
              pa.__version__
          except ImportError as exc:
              raise ImportError(
                  "Could not import pyarrow python package. "
                  "Please install it with `pip install pyarrow`."
              ) from exc
  
          try:
              import lancedb as lancedb
  
              # disable diagnostics
              lancedb.utils.CONFIG['diagnostics'] = False
          except ImportError as exc:
              raise ImportError(
                  "Could not import lancedb python package. "
                  "Please install it with `pip install lancedb`."
              ) from exc
  
          self._db_uri = db_uri
          self._embeddings = embeddings
  
          self._db = lancedb.connect(self._db_uri)
  
          self._tables = {}
  
      def create_collection(self, collection: str, schema: Dict[str, type] = None, mode="create"):
          if schema is None:
              raise ValueError("Invalid schema to create LanceDB table.")
  
          s = self._convert_schema(schema=schema)
  
          self._db.create_table(collection, schema=s, mode=mode)
  
      def add(
              self,
              collection: str,
              records: List[Record],
              embeddings_columns: List[str] = None,
              vectors: List[List[float]] = None,
              **kwargs
          ):
          tbl = self._db.open_table(collection)
  
          if not vectors:
              text_to_embed = []
              for r in records:
                  text_to_embed.append(Record.values_to_text(r, props=embeddings_columns))
  
              vectors = self._embeddings.embed_batch(texts=text_to_embed)
  
          if len(vectors) != len(records):
              raise ValueError("The length of records must be the same as the length of vecotors.")
  
          for i in range(len(records)):
              records[i][VECTOR_COLUMN_NAME] = vectors[i]
  
          tbl.add(records)
  
      def delete(self, collection: str, query: str):
          tbl = self._db.open_table(collection)
          tbl.delete(query)
  
      def search(
              self,
              collection: str = None,
              query: str = None,
              vector: List = None,
              filter: Any = None,
              limit: int = 20,
              columns: Optional[List[str]] = None,
              with_vector: bool = False,
              with_distance: bool = False,
              **kwargs
      ) -> List[Record]:
          if not query and not vector:
              raise ValueError("LanceDB search must provide query or vector.")
  
          if query and not vector and self._embeddings:
              vector = self._embeddings.embed(text=query)
          if not vector:
              raise ValueError(
                  "LanceDB search must provide Embeddings function.")
  
          tbl = self._db.open_table(collection)
  
          query = tbl.search(vector, vector_column_name=VECTOR_COLUMN_NAME)
          if filter:
              query = query.where(filter)
  
          if columns:
              query = query.select(columns=columns)
  
          results = query.limit(limit=limit).to_list()
          for v in results:
              if not with_vector:
                  del v[VECTOR_COLUMN_NAME]
              if not with_distance:
                  del v['_distance']
  
          return results
  
      def _convert_schema(self, schema: Dict[str, type]):
          try:
              import pyarrow as pa
          except ImportError as exc:
              raise ImportError(
                  "Could not import pyarrow python package. "
                  "Please install it with `pip install pyarrow`."
              ) from exc
  
          dims = len(self._embeddings.embed(""))
          columns = [
              pa.field(VECTOR_COLUMN_NAME, pa.list_(pa.float32(), dims)),
          ]
  
          for k, v in schema.items():
              t = pa.string()
  
              if isinstance(v, float):
                  t = pa.float64()
              if isinstance(v, int):
                  t = pa.int64()
              if isinstance(v, bool):
                  t = pa.bool_()
  
              columns.append(
                  pa.field(k, t)
              )
  
          s = pa.schema(columns)
  
          return s
  
apis: lancedb.connect
extract_api: [((1012, 1041), 'lancedb.connect', 'lancedb.connect', (['self._db_uri'], {}), '(self._db_uri)\n', (1027, 1041), True, 'import lancedb as lancedb\n'), ((4319, 4337), 'pyarrow.schema', 'pa.schema', (['columns'], {}), '(columns)\n', (4328, 4337), True, 'import pyarrow as pa\n'), ((4016, 4027), 'pyarrow.string', 'pa.string', ([], {}), '()\n', (4025, 4027), True, 'import pyarrow as pa\n'), ((4086, 4098), 'pyarrow.float64', 'pa.float64', ([], {}), '()\n', (4096, 4098), True, 'import pyarrow as pa\n'), ((4154, 4164), 'pyarrow.int64', 'pa.int64', ([], {}), '()\n', (4162, 4164), True, 'import pyarrow as pa\n'), ((4221, 4231), 'pyarrow.bool_', 'pa.bool_', ([], {}), '()\n', (4229, 4231), True, 'import pyarrow as pa\n'), ((4277, 4291), 'pyarrow.field', 'pa.field', (['k', 't'], {}), '(k, t)\n', (4285, 4291), True, 'import pyarrow as pa\n'), ((3931, 3943), 'pyarrow.float32', 'pa.float32', ([], {}), '()\n', (3941, 3943), True, 'import pyarrow as pa\n')]