Methods

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, weights=None, index=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. weights = self.weights(request.query_params.get("weights") if request and hasattr(request, "query_params") else weights)
  18. index = request.query_params.get("index") if request and hasattr(request, "query_params") else index
  19. if self.cluster:
  20. return self.cluster.search(query, limit, weights, index)
  21. return super().search(query, limit, weights, index)
  22. def batchsearch(self, queries, limit=None, weights=None, index=None):
  23. if self.cluster:
  24. return self.cluster.batchsearch(queries, self.limit(limit), weights, index)
  25. return super().batchsearch(queries, limit, weights, index)
  26. def add(self, documents):
  27. """
  28. Adds a batch of documents for indexing.
  29. Downstream applications can override this method to also store full documents in an external system.
  30. Args:
  31. documents: list of {id: value, text: value}
  32. Returns:
  33. unmodified input documents
  34. """
  35. if self.cluster:
  36. self.cluster.add(documents)
  37. else:
  38. super().add(documents)
  39. return documents
  40. def index(self):
  41. """
  42. Builds an embeddings index for previously batched documents.
  43. """
  44. if self.cluster:
  45. self.cluster.index()
  46. else:
  47. super().index()
  48. def upsert(self):
  49. """
  50. Runs an embeddings upsert operation for previously batched documents.
  51. """
  52. if self.cluster:
  53. self.cluster.upsert()
  54. else:
  55. super().upsert()
  56. def delete(self, ids):
  57. """
  58. Deletes from an embeddings index. Returns list of ids deleted.
  59. Args:
  60. ids: list of ids to delete
  61. Returns:
  62. ids deleted
  63. """
  64. if self.cluster:
  65. return self.cluster.delete(ids)
  66. return super().delete(ids)
  67. def reindex(self, config, function=None):
  68. """
  69. Recreates this embeddings index using config. This method only works if document content storage is enabled.
  70. Args:
  71. config: new config
  72. function: optional function to prepare content for indexing
  73. """
  74. if self.cluster:
  75. self.cluster.reindex(config, function)
  76. else:
  77. super().reindex(config, function)
  78. def count(self):
  79. """
  80. Total number of elements in this embeddings index.
  81. Returns:
  82. number of elements in embeddings index
  83. """
  84. if self.cluster:
  85. return self.cluster.count()
  86. return super().count()
  87. def limit(self, limit):
  88. """
  89. Parses the number of results to return from the request. Allows range of 1-250, with a default of 10.
  90. Args:
  91. limit: limit parameter
  92. Returns:
  93. bounded limit
  94. """
  95. # Return between 1 and 250 results, defaults to 10
  96. return max(1, min(250, int(limit) if limit else 10))
  97. def weights(self, weights):
  98. """
  99. Parses the weights parameter from the request.
  100. Args:
  101. weights: weights parameter
  102. Returns:
  103. weights
  104. """
  105. return float(weights) if weights else weights

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, weights=None, index=None)

Finds documents most similar to the input queries. This method will run either an index search or an index + database search depending on if a database is available.

Parameters:

NameTypeDescriptionDefault
queries

input queries

required
limit

maximum results

None
weights

hybrid score weights, if applicable

None
index

index name, if applicable

None

Returns:

TypeDescription
list of {id

value, score: value} per query for index search, list of dict per query for an index + database search

Source code in txtai/api/base.py

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

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()

limit(self, limit)

Parses the number of results to return from the request. Allows range of 1-250, with a default of 10.

Parameters:

NameTypeDescriptionDefault
limit

limit parameter

required

Returns:

TypeDescription

bounded limit

Source code in txtai/api/base.py

  1. def limit(self, limit):
  2. """
  3. Parses the number of results to return from the request. Allows range of 1-250, with a default of 10.
  4. Args:
  5. limit: limit parameter
  6. Returns:
  7. bounded limit
  8. """
  9. # Return between 1 and 250 results, defaults to 10
  10. return max(1, min(250, int(limit) if limit else 10))

reindex(self, config, function=None)

Recreates this embeddings index using config. This method only works if document content storage is enabled.

Parameters:

NameTypeDescriptionDefault
config

new config

required
function

optional function to prepare content for indexing

None

Source code in txtai/api/base.py

  1. def reindex(self, config, function=None):
  2. """
  3. Recreates this embeddings index using config. This method only works if document content storage is enabled.
  4. Args:
  5. config: new config
  6. function: optional function to prepare content for indexing
  7. """
  8. if self.cluster:
  9. self.cluster.reindex(config, function)
  10. else:
  11. super().reindex(config, function)

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

Finds documents most similar to the input query. This method will run either an index search or an index + database search depending on if a database is available.

Parameters:

NameTypeDescriptionDefault
query

input query

required
limit

maximum results

None
weights

hybrid score weights, if applicable

None
index

index name, if applicable

None

Returns:

TypeDescription
list of {id

value, score: value} for index search, list of dict for an index + database search

Source code in txtai/api/base.py

  1. def search(self, query, limit=None, weights=None, index=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. weights = self.weights(request.query_params.get("weights") if request and hasattr(request, "query_params") else weights)
  6. index = request.query_params.get("index") if request and hasattr(request, "query_params") else index
  7. if self.cluster:
  8. return self.cluster.search(query, limit, weights, index)
  9. return super().search(query, limit, weights, index)

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()

weights(self, weights)

Parses the weights parameter from the request.

Parameters:

NameTypeDescriptionDefault
weights

weights parameter

required

Returns:

TypeDescription

weights

Source code in txtai/api/base.py

  1. def weights(self, weights):
  2. """
  3. Parses the weights parameter from the request.
  4. Args:
  5. weights: weights parameter
  6. Returns:
  7. weights
  8. """
  9. return float(weights) if weights else weights