Methods

API (Application)

Base API template. The API is an extended txtai application, adding the ability to cluster API instances together.

Downstream applications can extend this base template to add/modify functionality.

Source code in txtai/api/base.py

  1. class API(Application):
  2. """
  3. Base API template. The API is an extended txtai application, adding the ability to cluster API instances together.
  4. Downstream applications can extend this base template to add/modify functionality.
  5. """
  6. def __init__(self, config, loaddata=True):
  7. super().__init__(config, loaddata)
  8. # Embeddings cluster
  9. self.cluster = None
  10. if self.config.get("cluster"):
  11. self.cluster = Cluster(self.config["cluster"])
  12. # pylint: disable=W0221
  13. def search(self, query, limit=None, request=None):
  14. # When search is invoked via the API, limit is set from the request
  15. # When search is invoked directly, limit is set using the method parameter
  16. limit = self.limit(request.query_params.get("limit") if request and hasattr(request, "query_params") else limit)
  17. if self.cluster:
  18. return self.cluster.search(query, limit)
  19. return super().search(query, limit)
  20. def batchsearch(self, queries, limit=None):
  21. if self.cluster:
  22. return self.cluster.batchsearch(queries, self.limit(limit))
  23. return super().batchsearch(queries, limit)
  24. def add(self, documents):
  25. """
  26. Adds a batch of documents for indexing.
  27. Downstream applications can override this method to also store full documents in an external system.
  28. Args:
  29. documents: list of {id: value, text: value}
  30. Returns:
  31. unmodified input documents
  32. """
  33. if self.cluster:
  34. self.cluster.add(documents)
  35. else:
  36. super().add(documents)
  37. return documents
  38. def index(self):
  39. """
  40. Builds an embeddings index for previously batched documents.
  41. """
  42. if self.cluster:
  43. self.cluster.index()
  44. else:
  45. super().index()
  46. def upsert(self):
  47. """
  48. Runs an embeddings upsert operation for previously batched documents.
  49. """
  50. if self.cluster:
  51. self.cluster.upsert()
  52. else:
  53. super().upsert()
  54. def delete(self, ids):
  55. """
  56. Deletes from an embeddings index. Returns list of ids deleted.
  57. Args:
  58. ids: list of ids to delete
  59. Returns:
  60. ids deleted
  61. """
  62. if self.cluster:
  63. return self.cluster.delete(ids)
  64. return super().delete(ids)
  65. def count(self):
  66. """
  67. Total number of elements in this embeddings index.
  68. Returns:
  69. number of elements in embeddings index
  70. """
  71. if self.cluster:
  72. return self.cluster.count()
  73. return super().count()
  74. def limit(self, limit):
  75. """
  76. Parses the number of results to return from the request. Allows range of 1-250, with a default of 10.
  77. Args:
  78. limit: limit parameter
  79. Returns:
  80. bounded limit
  81. """
  82. # Return between 1 and 250 results, defaults to 10
  83. return max(1, min(250, int(limit) if limit else 10))

add(self, documents)

Adds a batch of documents for indexing.

Downstream applications can override this method to also store full documents in an external system.

Parameters:

NameTypeDescriptionDefault
documents

list of {id: value, text: value}

required

Returns:

TypeDescription

unmodified input documents

Source code in txtai/api/base.py

  1. def add(self, documents):
  2. """
  3. Adds a batch of documents for indexing.
  4. Downstream applications can override this method to also store full documents in an external system.
  5. Args:
  6. documents: list of {id: value, text: value}
  7. Returns:
  8. unmodified input documents
  9. """
  10. if self.cluster:
  11. self.cluster.add(documents)
  12. else:
  13. super().add(documents)
  14. return documents

batchsearch(self, queries, limit=None)

Finds documents in the embeddings model most similar to the input queries. Returns a list of {id: value, score: value} sorted by highest score per query, where id is the document id in the embeddings model.

Parameters:

NameTypeDescriptionDefault
queries

queries text

required
limit

maximum results

None

Returns:

TypeDescription
list of {id

value, score: value} per query

Source code in txtai/api/base.py

  1. def batchsearch(self, queries, limit=None):
  2. if self.cluster:
  3. return self.cluster.batchsearch(queries, self.limit(limit))
  4. return super().batchsearch(queries, limit)

count(self)

Total number of elements in this embeddings index.

Returns:

TypeDescription

number of elements in embeddings index

Source code in txtai/api/base.py

  1. def count(self):
  2. """
  3. Total number of elements in this embeddings index.
  4. Returns:
  5. number of elements in embeddings index
  6. """
  7. if self.cluster:
  8. return self.cluster.count()
  9. return super().count()

delete(self, ids)

Deletes from an embeddings index. Returns list of ids deleted.

Parameters:

NameTypeDescriptionDefault
ids

list of ids to delete

required

Returns:

TypeDescription

ids deleted

Source code in txtai/api/base.py

  1. def delete(self, ids):
  2. """
  3. Deletes from an embeddings index. Returns list of ids deleted.
  4. Args:
  5. ids: list of ids to delete
  6. Returns:
  7. ids deleted
  8. """
  9. if self.cluster:
  10. return self.cluster.delete(ids)
  11. return super().delete(ids)

index(self)

Builds an embeddings index for previously batched documents.

Source code in txtai/api/base.py

  1. def index(self):
  2. """
  3. Builds an embeddings index for previously batched documents.
  4. """
  5. if self.cluster:
  6. self.cluster.index()
  7. else:
  8. super().index()

search(self, query, limit=None, request=None)

Finds documents in the embeddings model most similar to the input query. Returns a list of {id: value, score: value} sorted by highest score, where id is the document id in the embeddings model.

Parameters:

NameTypeDescriptionDefault
query

query text

required
limit

maximum results, used if request is None

None

Returns:

TypeDescription
list of {id

value, score: value}

Source code in txtai/api/base.py

  1. def search(self, query, limit=None, request=None):
  2. # When search is invoked via the API, limit is set from the request
  3. # When search is invoked directly, limit is set using the method parameter
  4. limit = self.limit(request.query_params.get("limit") if request and hasattr(request, "query_params") else limit)
  5. if self.cluster:
  6. return self.cluster.search(query, limit)
  7. return super().search(query, limit)

upsert(self)

Runs an embeddings upsert operation for previously batched documents.

Source code in txtai/api/base.py

  1. def upsert(self):
  2. """
  3. Runs an embeddings upsert operation for previously batched documents.
  4. """
  5. if self.cluster:
  6. self.cluster.upsert()
  7. else:
  8. super().upsert()