Methods

Embeddings

Embeddings is the engine that delivers semantic search. Data is transformed into embeddings vectors where similar concepts will produce similar vectors. Indexes both large and small are built with these vectors. The indexes are used to find results that have the same meaning, not necessarily the same keywords.

Source code in txtai/embeddings/base.py

  1. class Embeddings:
  2. """
  3. Embeddings is the engine that delivers semantic search. Data is transformed into embeddings vectors where similar concepts
  4. will produce similar vectors. Indexes both large and small are built with these vectors. The indexes are used to find results
  5. that have the same meaning, not necessarily the same keywords.
  6. """
  7. # pylint: disable = W0231
  8. def __init__(self, config=None, models=None, **kwargs):
  9. """
  10. Creates a new embeddings index. Embeddings indexes are thread-safe for read operations but writes must be synchronized.
  11. Args:
  12. config: embeddings configuration
  13. models: models cache, used for model sharing between embeddings
  14. kwargs: additional configuration as keyword args
  15. """
  16. # Index configuration
  17. self.config = None
  18. # Dimensionality reduction - word vectors only
  19. self.reducer = None
  20. # Dense vector model - transforms data into similarity vectors
  21. self.model = None
  22. # Approximate nearest neighbor index
  23. self.ann = None
  24. # Document database
  25. self.database = None
  26. # Resolvable functions
  27. self.functions = None
  28. # Graph network
  29. self.graph = None
  30. # Sparse vectors
  31. self.scoring = None
  32. # Query model
  33. self.query = None
  34. # Index archive
  35. self.archive = None
  36. # Subindexes for this embeddings instance
  37. self.indexes = None
  38. # Models cache
  39. self.models = models
  40. # Merge configuration into single dictionary
  41. config = {**config, **kwargs} if config and kwargs else kwargs if kwargs else config
  42. # Set initial configuration
  43. self.configure(config)
  44. def score(self, documents):
  45. """
  46. Builds a term weighting scoring index. Only used by word vectors models.
  47. Args:
  48. documents: iterable of (id, data, tags), (id, data) or data
  49. """
  50. # Build scoring index for word vectors term weighting
  51. if self.isweighted():
  52. self.scoring.index(Stream(self)(documents))
  53. def index(self, documents, reindex=False):
  54. """
  55. Builds an embeddings index. This method overwrites an existing index.
  56. Args:
  57. documents: iterable of (id, data, tags), (id, data) or data
  58. reindex: if this is a reindex operation in which case database creation is skipped, defaults to False
  59. """
  60. # Initialize index
  61. self.initindex(reindex)
  62. # Create transform and stream
  63. transform = Transform(self, Action.REINDEX if reindex else Action.INDEX)
  64. stream = Stream(self, Action.REINDEX if reindex else Action.INDEX)
  65. with tempfile.NamedTemporaryFile(mode="wb", suffix=".npy") as buffer:
  66. # Load documents into database and transform to vectors
  67. ids, dimensions, embeddings = transform(stream(documents), buffer)
  68. if embeddings is not None:
  69. # Build LSA model (if enabled). Remove principal components from embeddings.
  70. if self.config.get("pca"):
  71. self.reducer = Reducer(embeddings, self.config["pca"])
  72. self.reducer(embeddings)
  73. # Normalize embeddings
  74. self.normalize(embeddings)
  75. # Save index dimensions
  76. self.config["dimensions"] = dimensions
  77. # Create approximate nearest neighbor index
  78. self.ann = self.createann()
  79. # Add embeddings to the index
  80. self.ann.index(embeddings)
  81. # Save indexids-ids mapping for indexes with no database, except when this is a reindex
  82. if ids and not reindex and not self.database:
  83. self.config["ids"] = ids
  84. # Index scoring, if necessary
  85. # This must occur before graph index in order to be available to the graph
  86. if self.issparse():
  87. self.scoring.index()
  88. # Index subindexes, if necessary
  89. if self.indexes:
  90. self.indexes.index()
  91. # Index graph, if necessary
  92. if self.graph:
  93. self.graph.index(Search(self, True), self.batchsimilarity)
  94. def upsert(self, documents):
  95. """
  96. Runs an embeddings upsert operation. If the index exists, new data is
  97. appended to the index, existing data is updated. If the index doesn't exist,
  98. this method runs a standard index operation.
  99. Args:
  100. documents: iterable of (id, data, tags), (id, data) or data
  101. """
  102. # Run standard insert if index doesn't exist or it has no records
  103. if not self.count():
  104. self.index(documents)
  105. return
  106. # Create transform and stream
  107. transform = Transform(self, Action.UPSERT)
  108. stream = Stream(self, Action.UPSERT)
  109. with tempfile.NamedTemporaryFile(mode="wb", suffix=".npy") as buffer:
  110. # Load documents into database and transform to vectors
  111. ids, _, embeddings = transform(stream(documents), buffer)
  112. if embeddings is not None:
  113. # Remove principal components from embeddings, if necessary
  114. if self.reducer:
  115. self.reducer(embeddings)
  116. # Normalize embeddings
  117. self.normalize(embeddings)
  118. # Append embeddings to the index
  119. self.ann.append(embeddings)
  120. # Save indexids-ids mapping for indexes with no database
  121. if ids and not self.database:
  122. self.config["ids"] = self.config["ids"] + ids
  123. # Scoring upsert, if necessary
  124. # This must occur before graph upsert in order to be available to the graph
  125. if self.issparse():
  126. self.scoring.upsert()
  127. # Subindexes upsert, if necessary
  128. if self.indexes:
  129. self.indexes.upsert()
  130. # Graph upsert, if necessary
  131. if self.graph:
  132. self.graph.upsert(Search(self, True), self.batchsimilarity)
  133. def delete(self, ids):
  134. """
  135. Deletes from an embeddings index. Returns list of ids deleted.
  136. Args:
  137. ids: list of ids to delete
  138. Returns:
  139. list of ids deleted
  140. """
  141. # List of internal indices for each candidate id to delete
  142. indices = []
  143. # List of deleted ids
  144. deletes = []
  145. if self.database:
  146. # Retrieve indexid-id mappings from database
  147. ids = self.database.ids(ids)
  148. # Parse out indices and ids to delete
  149. indices = [i for i, _ in ids]
  150. deletes = sorted(set(uid for _, uid in ids))
  151. # Delete ids from database
  152. self.database.delete(deletes)
  153. elif self.ann or self.scoring:
  154. # Lookup indexids from config for indexes with no database
  155. indexids = self.config["ids"]
  156. # Find existing ids
  157. for uid in ids:
  158. indices.extend([index for index, value in enumerate(indexids) if uid == value])
  159. # Clear config ids
  160. for index in indices:
  161. deletes.append(indexids[index])
  162. indexids[index] = None
  163. # Delete indices for all indexes and data stores
  164. if indices:
  165. # Delete ids from ann
  166. if self.isdense():
  167. self.ann.delete(indices)
  168. # Delete ids from scoring
  169. if self.issparse():
  170. self.scoring.delete(indices)
  171. # Delete ids from subindexes
  172. if self.indexes:
  173. self.indexes.delete(indices)
  174. # Delete ids from graph
  175. if self.graph:
  176. self.graph.delete(indices)
  177. return deletes
  178. def reindex(self, config, columns=None, function=None):
  179. """
  180. Recreates the approximate nearest neighbor (ann) index using config. This method only works if document
  181. content storage is enabled.
  182. Args:
  183. config: new config
  184. columns: optional list of document columns used to rebuild data
  185. function: optional function to prepare content for indexing
  186. """
  187. if self.database:
  188. # Keep content and objects parameters to ensure database is preserved
  189. config["content"] = self.config["content"]
  190. if "objects" in self.config:
  191. config["objects"] = self.config["objects"]
  192. # Reset configuration
  193. self.configure(config)
  194. # Reset function references
  195. if self.functions:
  196. self.functions.reset()
  197. # Reindex
  198. if function:
  199. self.index(function(self.database.reindex(columns)), True)
  200. else:
  201. self.index(self.database.reindex(columns), True)
  202. def transform(self, document):
  203. """
  204. Transforms document into an embeddings vector.
  205. Args:
  206. documents: iterable of (id, data, tags), (id, data) or data
  207. Returns:
  208. embeddings vector
  209. """
  210. return self.batchtransform([document])[0]
  211. def batchtransform(self, documents, category=None):
  212. """
  213. Transforms documents into embeddings vectors.
  214. Args:
  215. documents: iterable of (id, data, tags), (id, data) or data
  216. category: category for instruction-based embeddings
  217. Returns:
  218. embeddings vectors
  219. """
  220. # Initialize default parameters, if necessary
  221. self.defaults()
  222. # Convert documents into sentence embeddings
  223. embeddings = self.model.batchtransform(Stream(self)(documents), category)
  224. # Reduce the dimensionality of the embeddings. Scale the embeddings using this
  225. # model to reduce the noise of common but less relevant terms.
  226. if self.reducer:
  227. self.reducer(embeddings)
  228. # Normalize embeddings
  229. self.normalize(embeddings)
  230. return embeddings
  231. def count(self):
  232. """
  233. Total number of elements in this embeddings index.
  234. Returns:
  235. number of elements in this embeddings index
  236. """
  237. if self.ann:
  238. return self.ann.count()
  239. if self.scoring:
  240. return self.scoring.count()
  241. if self.database:
  242. return self.database.count()
  243. if self.config.get("ids"):
  244. return len([uid for uid in self.config["ids"] if uid is not None])
  245. # Default to 0 when no suitable method found
  246. return 0
  247. def search(self, query, limit=None, weights=None, index=None):
  248. """
  249. Finds documents most similar to the input queries. This method will run either an index search
  250. or an index + database search depending on if a database is available.
  251. Args:
  252. query: input query
  253. limit: maximum results
  254. weights: hybrid score weights, if applicable
  255. index: index name, if applicable
  256. Returns:
  257. list of (id, score) for index search, list of dict for an index+database search
  258. """
  259. results = self.batchsearch([query], limit, weights, index)
  260. return results[0] if results else results
  261. def batchsearch(self, queries, limit=None, weights=None, index=None):
  262. """
  263. Finds documents most similar to the input queries. This method will run either an index search
  264. or an index + database search depending on if a database is available.
  265. Args:
  266. queries: input queries
  267. limit: maximum results
  268. weights: hybrid score weights, if applicable
  269. index: index name, if applicable
  270. Returns:
  271. list of (id, score) per query for index search, list of dict per query for an index+database search
  272. """
  273. return Search(self)(queries, limit, weights, index)
  274. def similarity(self, query, data):
  275. """
  276. Computes the similarity between query and list of data. Returns a list of
  277. (id, score) sorted by highest score, where id is the index in data.
  278. Args:
  279. query: input query
  280. data: list of data
  281. Returns:
  282. list of (id, score)
  283. """
  284. return self.batchsimilarity([query], data)[0]
  285. def batchsimilarity(self, queries, data):
  286. """
  287. Computes the similarity between list of queries and list of data. Returns a list
  288. of (id, score) sorted by highest score per query, where id is the index in data.
  289. Args:
  290. queries: input queries
  291. data: list of data
  292. Returns:
  293. list of (id, score) per query
  294. """
  295. # Convert queries to embedding vectors
  296. queries = self.batchtransform(((None, query, None) for query in queries), "query")
  297. data = self.batchtransform(((None, row, None) for row in data), "data")
  298. # Dot product on normalized vectors is equal to cosine similarity
  299. scores = np.dot(queries, data.T).tolist()
  300. # Add index and sort desc based on score
  301. return [sorted(enumerate(score), key=lambda x: x[1], reverse=True) for score in scores]
  302. def explain(self, query, texts=None, limit=None):
  303. """
  304. Explains the importance of each input token in text for a query.
  305. Args:
  306. query: input query
  307. texts: optional list of (text|list of tokens), otherwise runs search query
  308. limit: optional limit if texts is None
  309. Returns:
  310. list of dict per input text where a higher token scores represents higher importance relative to the query
  311. """
  312. results = self.batchexplain([query], texts, limit)
  313. return results[0] if results else results
  314. def batchexplain(self, queries, texts=None, limit=None):
  315. """
  316. Explains the importance of each input token in text for a list of queries.
  317. Args:
  318. queries: input queries
  319. texts: optional list of (text|list of tokens), otherwise runs search queries
  320. limit: optional limit if texts is None
  321. Returns:
  322. list of dict per input text per query where a higher token scores represents higher importance relative to the query
  323. """
  324. return Explain(self)(queries, texts, limit)
  325. def terms(self, query):
  326. """
  327. Extracts keyword terms from a query.
  328. Args:
  329. query: input query
  330. Returns:
  331. query reduced down to keyword terms
  332. """
  333. return self.batchterms([query])[0]
  334. def batchterms(self, queries):
  335. """
  336. Extracts keyword terms from a list of queries.
  337. Args:
  338. queries: list of queries
  339. Returns:
  340. list of queries reduced down to keyword term strings
  341. """
  342. return Terms(self)(queries)
  343. def exists(self, path=None, cloud=None, **kwargs):
  344. """
  345. Checks if an index exists at path.
  346. Args:
  347. path: input path
  348. cloud: cloud storage configuration
  349. kwargs: additional configuration as keyword args
  350. Returns:
  351. True if index exists, False otherwise
  352. """
  353. # Check if this exists in a cloud instance
  354. cloud = self.createcloud(cloud=cloud, **kwargs)
  355. if cloud:
  356. return cloud.exists(path)
  357. # Check if this is an archive file and exists
  358. path, apath = self.checkarchive(path)
  359. if apath:
  360. return os.path.exists(apath)
  361. # Return true if path has a config or config.json file and an embeddings (dense) or scoring (sparse) file
  362. return (
  363. path
  364. and (os.path.exists(f"{path}/config") or os.path.exists(f"{path}/config.json"))
  365. and (os.path.exists(f"{path}/embeddings") or os.path.exists(f"{path}/scoring"))
  366. )
  367. def load(self, path=None, cloud=None, **kwargs):
  368. """
  369. Loads an existing index from path.
  370. Args:
  371. path: input path
  372. cloud: cloud storage configuration
  373. kwargs: additional configuration as keyword args
  374. """
  375. # Load from cloud, if configured
  376. cloud = self.createcloud(cloud=cloud, **kwargs)
  377. if cloud:
  378. path = cloud.load(path)
  379. # Check if this is an archive file and extract
  380. path, apath = self.checkarchive(path)
  381. if apath:
  382. self.archive.load(apath)
  383. # Load index configuration
  384. self.config = self.loadconfig(path)
  385. # Approximate nearest neighbor index - stores dense vectors
  386. self.ann = self.createann()
  387. if self.ann:
  388. self.ann.load(f"{path}/embeddings")
  389. # Dimensionality reduction model - word vectors only
  390. if self.config.get("pca"):
  391. self.reducer = Reducer()
  392. self.reducer.load(f"{path}/lsa")
  393. # Document database - stores document content
  394. self.database = self.createdatabase()
  395. if self.database:
  396. self.database.load(f"{path}/documents")
  397. # Sparse vectors - stores term sparse arrays
  398. self.scoring = self.createscoring()
  399. if self.scoring:
  400. self.scoring.load(f"{path}/scoring")
  401. # Subindexes
  402. self.indexes = self.createindexes()
  403. if self.indexes:
  404. self.indexes.load(f"{path}/indexes")
  405. # Graph network - stores relationships
  406. self.graph = self.creategraph()
  407. if self.graph:
  408. self.graph.load(f"{path}/graph")
  409. # Dense vectors - transforms data to embeddings vectors
  410. self.model = self.loadvectors()
  411. # Query model
  412. self.query = self.loadquery()
  413. def save(self, path, cloud=None, **kwargs):
  414. """
  415. Saves an index in a directory at path unless path ends with tar.gz, tar.bz2, tar.xz or zip.
  416. In those cases, the index is stored as a compressed file.
  417. Args:
  418. path: output path
  419. cloud: cloud storage configuration
  420. kwargs: additional configuration as keyword args
  421. """
  422. if self.config:
  423. # Check if this is an archive file
  424. path, apath = self.checkarchive(path)
  425. # Create output directory, if necessary
  426. os.makedirs(path, exist_ok=True)
  427. # Copy sentence vectors model
  428. if self.config.get("storevectors"):
  429. shutil.copyfile(self.config["path"], os.path.join(path, os.path.basename(self.config["path"])))
  430. self.config["path"] = os.path.basename(self.config["path"])
  431. # Save index configuration
  432. self.saveconfig(path)
  433. # Save approximate nearest neighbor index
  434. if self.ann:
  435. self.ann.save(f"{path}/embeddings")
  436. # Save dimensionality reduction model (word vectors only)
  437. if self.reducer:
  438. self.reducer.save(f"{path}/lsa")
  439. # Save document database
  440. if self.database:
  441. self.database.save(f"{path}/documents")
  442. # Save scoring index
  443. if self.scoring:
  444. self.scoring.save(f"{path}/scoring")
  445. # Save subindexes
  446. if self.indexes:
  447. self.indexes.save(f"{path}/indexes")
  448. # Save graph
  449. if self.graph:
  450. self.graph.save(f"{path}/graph")
  451. # If this is an archive, save it
  452. if apath:
  453. self.archive.save(apath)
  454. # Save to cloud, if configured
  455. cloud = self.createcloud(cloud=cloud, **kwargs)
  456. if cloud:
  457. cloud.save(apath if apath else path)
  458. def close(self):
  459. """
  460. Closes this embeddings index and frees all resources.
  461. """
  462. self.ann, self.config, self.graph, self.archive = None, None, None, None
  463. self.reducer, self.query, self.model, self.models = None, None, None, None
  464. # Close database connection if open
  465. if self.database:
  466. self.database.close()
  467. self.database, self.functions = None, None
  468. # Close scoring instance if open
  469. if self.scoring:
  470. self.scoring.close()
  471. self.scoring = None
  472. # Close indexes if open
  473. if self.indexes:
  474. self.indexes.close()
  475. self.indexes = None
  476. def info(self):
  477. """
  478. Prints the current embeddings index configuration.
  479. """
  480. if self.config:
  481. # Copy and edit config
  482. config = self.config.copy()
  483. # Remove ids array if present
  484. config.pop("ids", None)
  485. # Print configuration
  486. print(json.dumps(config, sort_keys=True, default=str, indent=2))
  487. def issparse(self):
  488. """
  489. Checks if this instance has an associated scoring instance with term indexing enabled.
  490. Returns:
  491. True if term index is enabled, False otherwise
  492. """
  493. return self.scoring and self.scoring.hasterms()
  494. def isdense(self):
  495. """
  496. Checks if this instance has an associated ANN instance.
  497. Returns:
  498. True if this instance has an associated ANN, False otherwise
  499. """
  500. return self.ann is not None
  501. def isweighted(self):
  502. """
  503. Checks if this instance has an associated scoring instance with term weighting enabled.
  504. Returns:
  505. True if term weighting is enabled, False otherwise
  506. """
  507. return self.scoring and not self.scoring.hasterms()
  508. def configure(self, config):
  509. """
  510. Sets the configuration for this embeddings index and loads config-driven models.
  511. Args:
  512. config: embeddings configuration
  513. """
  514. # Configuration
  515. self.config = config
  516. # Dimensionality reduction model
  517. self.reducer = None
  518. # Create scoring instance for word vectors term weighting
  519. scoring = self.config.get("scoring") if self.config else None
  520. self.scoring = self.createscoring() if scoring and (not isinstance(scoring, dict) or not scoring.get("terms")) else None
  521. # Dense vectors - transforms data to embeddings vectors
  522. self.model = self.loadvectors() if self.config else None
  523. # Query model
  524. self.query = self.loadquery() if self.config else None
  525. def initindex(self, reindex):
  526. """
  527. Initialize new index.
  528. Args:
  529. reindex: if this is a reindex operation in which case database creation is skipped, defaults to False
  530. """
  531. # Initialize default parameters, if necessary
  532. self.defaults()
  533. # Create document database, if necessary
  534. if not reindex:
  535. self.database = self.createdatabase()
  536. # Reset archive since this is a new index
  537. self.archive = None
  538. # Create scoring only if term indexing is enabled
  539. scoring = self.config.get("scoring")
  540. if scoring and isinstance(scoring, dict) and self.config["scoring"].get("terms"):
  541. self.scoring = self.createscoring()
  542. # Create subindexes, if necessary
  543. self.indexes = self.createindexes()
  544. # Create graph, if necessary
  545. self.graph = self.creategraph()
  546. def defaults(self):
  547. """
  548. Apply default parameters to current configuration.
  549. Returns:
  550. configuration with default parameters set
  551. """
  552. self.config = self.config if self.config else {}
  553. # Expand sparse index shortcuts
  554. if not self.config.get("scoring") and any(self.config.get(key) for key in ["keyword", "hybrid"]):
  555. self.config["scoring"] = {"method": "bm25", "terms": True, "normalize": True}
  556. # Check if default model should be loaded
  557. if not self.model and self.defaultallowed():
  558. self.config["path"] = "sentence-transformers/all-MiniLM-L6-v2"
  559. # Load dense vectors model
  560. self.model = self.loadvectors()
  561. def defaultallowed(self):
  562. """
  563. Tests if this embeddings instance can use a default model if not otherwise provided.
  564. Returns:
  565. True if a default model is allowed, False otherwise
  566. """
  567. params = [("keyword", False), ("defaults", True)]
  568. return all(self.config.get(key, default) == default for key, default in params)
  569. def loadconfig(self, path):
  570. """
  571. Loads index configuration. This method supports both config pickle files and config.json files.
  572. Args:
  573. path: path to directory
  574. Returns:
  575. dict
  576. """
  577. # Configuration
  578. config = None
  579. # Determine if config is json or pickle
  580. jsonconfig = os.path.exists(f"{path}/config.json")
  581. # Set config file name
  582. name = "config.json" if jsonconfig else "config"
  583. # Load configuration
  584. with open(f"{path}/{name}", "r" if jsonconfig else "rb") as handle:
  585. config = json.load(handle) if jsonconfig else pickle.load(handle)
  586. # Build full path to embedding vectors file
  587. if config.get("storevectors"):
  588. config["path"] = os.path.join(path, config["path"])
  589. return config
  590. def saveconfig(self, path):
  591. """
  592. Saves index configuration. This method saves to JSON if possible, otherwise it falls back to pickle.
  593. Args:
  594. path: path to directory
  595. Returns:
  596. dict
  597. """
  598. # Default to pickle config
  599. jsonconfig = self.config.get("format", "pickle") == "json"
  600. # Set config file name
  601. name = "config.json" if jsonconfig else "config"
  602. # Write configuration
  603. with open(f"{path}/{name}", "w" if jsonconfig else "wb", encoding="utf-8" if jsonconfig else None) as handle:
  604. if jsonconfig:
  605. # Write config as JSON
  606. json.dump(self.config, handle, default=str, indent=2)
  607. else:
  608. # Write config as pickle format
  609. pickle.dump(self.config, handle, protocol=__pickle__)
  610. def loadvectors(self):
  611. """
  612. Loads a vector model set in config.
  613. Returns:
  614. vector model
  615. """
  616. # Create model cache if subindexes are enabled
  617. if "indexes" in self.config and self.models is None:
  618. self.models = {}
  619. # Model path
  620. path = self.config.get("path")
  621. # Check if model is cached
  622. if self.models and path in self.models:
  623. return self.models[path]
  624. # Load and store uncached model
  625. model = VectorsFactory.create(self.config, self.scoring)
  626. if self.models is not None and path:
  627. self.models[path] = model
  628. return model
  629. def loadquery(self):
  630. """
  631. Loads a query model set in config.
  632. Returns:
  633. query model
  634. """
  635. if "query" in self.config:
  636. return Query(**self.config["query"])
  637. return None
  638. def checkarchive(self, path):
  639. """
  640. Checks if path is an archive file.
  641. Args:
  642. path: path to check
  643. Returns:
  644. (working directory, current path) if this is an archive, original path otherwise
  645. """
  646. # Create archive instance, if necessary
  647. self.archive = ArchiveFactory.create()
  648. # Check if path is an archive file
  649. if self.archive.isarchive(path):
  650. # Return temporary archive working directory and original path
  651. return self.archive.path(), path
  652. return path, None
  653. def createcloud(self, **cloud):
  654. """
  655. Creates a cloud instance from config.
  656. Args:
  657. cloud: cloud configuration
  658. """
  659. # Merge keyword args and keys under the cloud parameter
  660. config = cloud
  661. if "cloud" in config and config["cloud"]:
  662. config.update(config.pop("cloud"))
  663. # Create cloud instance from config and return
  664. return CloudFactory.create(config) if config else None
  665. def createann(self):
  666. """
  667. Creates an ANN from config.
  668. Returns:
  669. new ANN, if enabled in config
  670. """
  671. return ANNFactory.create(self.config) if self.config.get("path") or self.defaultallowed() else None
  672. def createdatabase(self):
  673. """
  674. Creates a database from config. This method will also close any existing database connection.
  675. Returns:
  676. new database, if enabled in config
  677. """
  678. # Free existing database resources
  679. if self.database:
  680. self.database.close()
  681. config = self.config.copy()
  682. # Create references to callable functions
  683. self.functions = Functions(self) if "functions" in config else None
  684. if self.functions:
  685. config["functions"] = self.functions(config)
  686. # Create database from config and return
  687. return DatabaseFactory.create(config)
  688. def creategraph(self):
  689. """
  690. Creates a graph from config.
  691. Returns:
  692. new graph, if enabled in config
  693. """
  694. if "graph" in self.config:
  695. # Get or create graph configuration
  696. config = self.config["graph"] if self.config["graph"] else {}
  697. # Create configuration with custom columns, if necessary
  698. config = self.columns(config)
  699. return GraphFactory.create(config)
  700. return None
  701. def createindexes(self):
  702. """
  703. Creates subindexes from config.
  704. Returns:
  705. list of subindexes
  706. """
  707. # Load subindexes
  708. if "indexes" in self.config:
  709. indexes = {}
  710. for index, config in self.config["indexes"].items():
  711. # Create index with shared model cache
  712. indexes[index] = Embeddings(config, models=self.models)
  713. # Wrap as Indexes object
  714. return Indexes(self, indexes)
  715. return None
  716. def createscoring(self):
  717. """
  718. Creates a scoring from config.
  719. Returns:
  720. new scoring, if enabled in config
  721. """
  722. # Free existing resources
  723. if self.scoring:
  724. self.scoring.close()
  725. if "scoring" in self.config:
  726. # Expand scoring to a dictionary, if necessary
  727. config = self.config["scoring"]
  728. config = config if isinstance(config, dict) else {"method": config}
  729. # Create configuration with custom columns, if necessary
  730. config = self.columns(config)
  731. return ScoringFactory.create(config)
  732. return None
  733. def columns(self, config):
  734. """
  735. Adds custom text/object column information if it's provided.
  736. Args:
  737. config: input configuration
  738. Returns:
  739. config with column information added
  740. """
  741. # Add text/object columns if custom
  742. if "columns" in self.config:
  743. # Work on copy of configuration
  744. config = config.copy()
  745. # Copy columns to config
  746. config["columns"] = self.config["columns"]
  747. return config
  748. def normalize(self, embeddings):
  749. """
  750. Normalizes embeddings using L2 normalization. Operation applied directly on array.
  751. Args:
  752. embeddings: input embeddings matrix
  753. """
  754. # Calculation is different for matrices vs vectors
  755. if len(embeddings.shape) > 1:
  756. embeddings /= np.linalg.norm(embeddings, axis=1)[:, np.newaxis]
  757. else:
  758. embeddings /= np.linalg.norm(embeddings)

__init__(self, config=None, models=None, **kwargs) special

Creates a new embeddings index. Embeddings indexes are thread-safe for read operations but writes must be synchronized.

Parameters:

NameTypeDescriptionDefault
config

embeddings configuration

None
models

models cache, used for model sharing between embeddings

None
kwargs

additional configuration as keyword args

{}

Source code in txtai/embeddings/base.py

  1. def __init__(self, config=None, models=None, **kwargs):
  2. """
  3. Creates a new embeddings index. Embeddings indexes are thread-safe for read operations but writes must be synchronized.
  4. Args:
  5. config: embeddings configuration
  6. models: models cache, used for model sharing between embeddings
  7. kwargs: additional configuration as keyword args
  8. """
  9. # Index configuration
  10. self.config = None
  11. # Dimensionality reduction - word vectors only
  12. self.reducer = None
  13. # Dense vector model - transforms data into similarity vectors
  14. self.model = None
  15. # Approximate nearest neighbor index
  16. self.ann = None
  17. # Document database
  18. self.database = None
  19. # Resolvable functions
  20. self.functions = None
  21. # Graph network
  22. self.graph = None
  23. # Sparse vectors
  24. self.scoring = None
  25. # Query model
  26. self.query = None
  27. # Index archive
  28. self.archive = None
  29. # Subindexes for this embeddings instance
  30. self.indexes = None
  31. # Models cache
  32. self.models = models
  33. # Merge configuration into single dictionary
  34. config = {**config, **kwargs} if config and kwargs else kwargs if kwargs else config
  35. # Set initial configuration
  36. self.configure(config)

batchexplain(self, queries, texts=None, limit=None)

Explains the importance of each input token in text for a list of queries.

Parameters:

NameTypeDescriptionDefault
queries

input queries

required
texts

optional list of (text|list of tokens), otherwise runs search queries

None
limit

optional limit if texts is None

None

Returns:

TypeDescription

list of dict per input text per query where a higher token scores represents higher importance relative to the query

Source code in txtai/embeddings/base.py

  1. def batchexplain(self, queries, texts=None, limit=None):
  2. """
  3. Explains the importance of each input token in text for a list of queries.
  4. Args:
  5. queries: input queries
  6. texts: optional list of (text|list of tokens), otherwise runs search queries
  7. limit: optional limit if texts is None
  8. Returns:
  9. list of dict per input text per query where a higher token scores represents higher importance relative to the query
  10. """
  11. return Explain(self)(queries, texts, limit)

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, score) per query for index search, list of dict per query for an index+database search

Source code in txtai/embeddings/base.py

  1. def batchsearch(self, queries, limit=None, weights=None, index=None):
  2. """
  3. Finds documents most similar to the input queries. This method will run either an index search
  4. or an index + database search depending on if a database is available.
  5. Args:
  6. queries: input queries
  7. limit: maximum results
  8. weights: hybrid score weights, if applicable
  9. index: index name, if applicable
  10. Returns:
  11. list of (id, score) per query for index search, list of dict per query for an index+database search
  12. """
  13. return Search(self)(queries, limit, weights, index)

batchsimilarity(self, queries, data)

Computes the similarity between list of queries and list of data. Returns a list of (id, score) sorted by highest score per query, where id is the index in data.

Parameters:

NameTypeDescriptionDefault
queries

input queries

required
data

list of data

required

Returns:

TypeDescription

list of (id, score) per query

Source code in txtai/embeddings/base.py

  1. def batchsimilarity(self, queries, data):
  2. """
  3. Computes the similarity between list of queries and list of data. Returns a list
  4. of (id, score) sorted by highest score per query, where id is the index in data.
  5. Args:
  6. queries: input queries
  7. data: list of data
  8. Returns:
  9. list of (id, score) per query
  10. """
  11. # Convert queries to embedding vectors
  12. queries = self.batchtransform(((None, query, None) for query in queries), "query")
  13. data = self.batchtransform(((None, row, None) for row in data), "data")
  14. # Dot product on normalized vectors is equal to cosine similarity
  15. scores = np.dot(queries, data.T).tolist()
  16. # Add index and sort desc based on score
  17. return [sorted(enumerate(score), key=lambda x: x[1], reverse=True) for score in scores]

batchterms(self, queries)

Extracts keyword terms from a list of queries.

Parameters:

NameTypeDescriptionDefault
queries

list of queries

required

Returns:

TypeDescription

list of queries reduced down to keyword term strings

Source code in txtai/embeddings/base.py

  1. def batchterms(self, queries):
  2. """
  3. Extracts keyword terms from a list of queries.
  4. Args:
  5. queries: list of queries
  6. Returns:
  7. list of queries reduced down to keyword term strings
  8. """
  9. return Terms(self)(queries)

batchtransform(self, documents, category=None)

Transforms documents into embeddings vectors.

Parameters:

NameTypeDescriptionDefault
documents

iterable of (id, data, tags), (id, data) or data

required
category

category for instruction-based embeddings

None

Returns:

TypeDescription

embeddings vectors

Source code in txtai/embeddings/base.py

  1. def batchtransform(self, documents, category=None):
  2. """
  3. Transforms documents into embeddings vectors.
  4. Args:
  5. documents: iterable of (id, data, tags), (id, data) or data
  6. category: category for instruction-based embeddings
  7. Returns:
  8. embeddings vectors
  9. """
  10. # Initialize default parameters, if necessary
  11. self.defaults()
  12. # Convert documents into sentence embeddings
  13. embeddings = self.model.batchtransform(Stream(self)(documents), category)
  14. # Reduce the dimensionality of the embeddings. Scale the embeddings using this
  15. # model to reduce the noise of common but less relevant terms.
  16. if self.reducer:
  17. self.reducer(embeddings)
  18. # Normalize embeddings
  19. self.normalize(embeddings)
  20. return embeddings

close(self)

Closes this embeddings index and frees all resources.

Source code in txtai/embeddings/base.py

  1. def close(self):
  2. """
  3. Closes this embeddings index and frees all resources.
  4. """
  5. self.ann, self.config, self.graph, self.archive = None, None, None, None
  6. self.reducer, self.query, self.model, self.models = None, None, None, None
  7. # Close database connection if open
  8. if self.database:
  9. self.database.close()
  10. self.database, self.functions = None, None
  11. # Close scoring instance if open
  12. if self.scoring:
  13. self.scoring.close()
  14. self.scoring = None
  15. # Close indexes if open
  16. if self.indexes:
  17. self.indexes.close()
  18. self.indexes = None

columns(self, config)

Adds custom text/object column information if it’s provided.

Parameters:

NameTypeDescriptionDefault
config

input configuration

required

Returns:

TypeDescription

config with column information added

Source code in txtai/embeddings/base.py

  1. def columns(self, config):
  2. """
  3. Adds custom text/object column information if it's provided.
  4. Args:
  5. config: input configuration
  6. Returns:
  7. config with column information added
  8. """
  9. # Add text/object columns if custom
  10. if "columns" in self.config:
  11. # Work on copy of configuration
  12. config = config.copy()
  13. # Copy columns to config
  14. config["columns"] = self.config["columns"]
  15. return config

count(self)

Total number of elements in this embeddings index.

Returns:

TypeDescription

number of elements in this embeddings index

Source code in txtai/embeddings/base.py

  1. def count(self):
  2. """
  3. Total number of elements in this embeddings index.
  4. Returns:
  5. number of elements in this embeddings index
  6. """
  7. if self.ann:
  8. return self.ann.count()
  9. if self.scoring:
  10. return self.scoring.count()
  11. if self.database:
  12. return self.database.count()
  13. if self.config.get("ids"):
  14. return len([uid for uid in self.config["ids"] if uid is not None])
  15. # Default to 0 when no suitable method found
  16. return 0

createann(self)

Creates an ANN from config.

Returns:

TypeDescription

new ANN, if enabled in config

Source code in txtai/embeddings/base.py

  1. def createann(self):
  2. """
  3. Creates an ANN from config.
  4. Returns:
  5. new ANN, if enabled in config
  6. """
  7. return ANNFactory.create(self.config) if self.config.get("path") or self.defaultallowed() else None

createindexes(self)

Creates subindexes from config.

Returns:

TypeDescription

list of subindexes

Source code in txtai/embeddings/base.py

  1. def createindexes(self):
  2. """
  3. Creates subindexes from config.
  4. Returns:
  5. list of subindexes
  6. """
  7. # Load subindexes
  8. if "indexes" in self.config:
  9. indexes = {}
  10. for index, config in self.config["indexes"].items():
  11. # Create index with shared model cache
  12. indexes[index] = Embeddings(config, models=self.models)
  13. # Wrap as Indexes object
  14. return Indexes(self, indexes)
  15. return None

createscoring(self)

Creates a scoring from config.

Returns:

TypeDescription

new scoring, if enabled in config

Source code in txtai/embeddings/base.py

  1. def createscoring(self):
  2. """
  3. Creates a scoring from config.
  4. Returns:
  5. new scoring, if enabled in config
  6. """
  7. # Free existing resources
  8. if self.scoring:
  9. self.scoring.close()
  10. if "scoring" in self.config:
  11. # Expand scoring to a dictionary, if necessary
  12. config = self.config["scoring"]
  13. config = config if isinstance(config, dict) else {"method": config}
  14. # Create configuration with custom columns, if necessary
  15. config = self.columns(config)
  16. return ScoringFactory.create(config)
  17. return None

defaultallowed(self)

Tests if this embeddings instance can use a default model if not otherwise provided.

Returns:

TypeDescription

True if a default model is allowed, False otherwise

Source code in txtai/embeddings/base.py

  1. def defaultallowed(self):
  2. """
  3. Tests if this embeddings instance can use a default model if not otherwise provided.
  4. Returns:
  5. True if a default model is allowed, False otherwise
  6. """
  7. params = [("keyword", False), ("defaults", True)]
  8. return all(self.config.get(key, default) == default for key, default in params)

delete(self, ids)

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

Parameters:

NameTypeDescriptionDefault
ids

list of ids to delete

required

Returns:

TypeDescription

list of ids deleted

Source code in txtai/embeddings/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. list of ids deleted
  8. """
  9. # List of internal indices for each candidate id to delete
  10. indices = []
  11. # List of deleted ids
  12. deletes = []
  13. if self.database:
  14. # Retrieve indexid-id mappings from database
  15. ids = self.database.ids(ids)
  16. # Parse out indices and ids to delete
  17. indices = [i for i, _ in ids]
  18. deletes = sorted(set(uid for _, uid in ids))
  19. # Delete ids from database
  20. self.database.delete(deletes)
  21. elif self.ann or self.scoring:
  22. # Lookup indexids from config for indexes with no database
  23. indexids = self.config["ids"]
  24. # Find existing ids
  25. for uid in ids:
  26. indices.extend([index for index, value in enumerate(indexids) if uid == value])
  27. # Clear config ids
  28. for index in indices:
  29. deletes.append(indexids[index])
  30. indexids[index] = None
  31. # Delete indices for all indexes and data stores
  32. if indices:
  33. # Delete ids from ann
  34. if self.isdense():
  35. self.ann.delete(indices)
  36. # Delete ids from scoring
  37. if self.issparse():
  38. self.scoring.delete(indices)
  39. # Delete ids from subindexes
  40. if self.indexes:
  41. self.indexes.delete(indices)
  42. # Delete ids from graph
  43. if self.graph:
  44. self.graph.delete(indices)
  45. return deletes

exists(self, path=None, cloud=None, **kwargs)

Checks if an index exists at path.

Parameters:

NameTypeDescriptionDefault
path

input path

None
cloud

cloud storage configuration

None
kwargs

additional configuration as keyword args

{}

Returns:

TypeDescription

True if index exists, False otherwise

Source code in txtai/embeddings/base.py

  1. def exists(self, path=None, cloud=None, **kwargs):
  2. """
  3. Checks if an index exists at path.
  4. Args:
  5. path: input path
  6. cloud: cloud storage configuration
  7. kwargs: additional configuration as keyword args
  8. Returns:
  9. True if index exists, False otherwise
  10. """
  11. # Check if this exists in a cloud instance
  12. cloud = self.createcloud(cloud=cloud, **kwargs)
  13. if cloud:
  14. return cloud.exists(path)
  15. # Check if this is an archive file and exists
  16. path, apath = self.checkarchive(path)
  17. if apath:
  18. return os.path.exists(apath)
  19. # Return true if path has a config or config.json file and an embeddings (dense) or scoring (sparse) file
  20. return (
  21. path
  22. and (os.path.exists(f"{path}/config") or os.path.exists(f"{path}/config.json"))
  23. and (os.path.exists(f"{path}/embeddings") or os.path.exists(f"{path}/scoring"))
  24. )

explain(self, query, texts=None, limit=None)

Explains the importance of each input token in text for a query.

Parameters:

NameTypeDescriptionDefault
query

input query

required
texts

optional list of (text|list of tokens), otherwise runs search query

None
limit

optional limit if texts is None

None

Returns:

TypeDescription

list of dict per input text where a higher token scores represents higher importance relative to the query

Source code in txtai/embeddings/base.py

  1. def explain(self, query, texts=None, limit=None):
  2. """
  3. Explains the importance of each input token in text for a query.
  4. Args:
  5. query: input query
  6. texts: optional list of (text|list of tokens), otherwise runs search query
  7. limit: optional limit if texts is None
  8. Returns:
  9. list of dict per input text where a higher token scores represents higher importance relative to the query
  10. """
  11. results = self.batchexplain([query], texts, limit)
  12. return results[0] if results else results

index(self, documents, reindex=False)

Builds an embeddings index. This method overwrites an existing index.

Parameters:

NameTypeDescriptionDefault
documents

iterable of (id, data, tags), (id, data) or data

required
reindex

if this is a reindex operation in which case database creation is skipped, defaults to False

False

Source code in txtai/embeddings/base.py

  1. def index(self, documents, reindex=False):
  2. """
  3. Builds an embeddings index. This method overwrites an existing index.
  4. Args:
  5. documents: iterable of (id, data, tags), (id, data) or data
  6. reindex: if this is a reindex operation in which case database creation is skipped, defaults to False
  7. """
  8. # Initialize index
  9. self.initindex(reindex)
  10. # Create transform and stream
  11. transform = Transform(self, Action.REINDEX if reindex else Action.INDEX)
  12. stream = Stream(self, Action.REINDEX if reindex else Action.INDEX)
  13. with tempfile.NamedTemporaryFile(mode="wb", suffix=".npy") as buffer:
  14. # Load documents into database and transform to vectors
  15. ids, dimensions, embeddings = transform(stream(documents), buffer)
  16. if embeddings is not None:
  17. # Build LSA model (if enabled). Remove principal components from embeddings.
  18. if self.config.get("pca"):
  19. self.reducer = Reducer(embeddings, self.config["pca"])
  20. self.reducer(embeddings)
  21. # Normalize embeddings
  22. self.normalize(embeddings)
  23. # Save index dimensions
  24. self.config["dimensions"] = dimensions
  25. # Create approximate nearest neighbor index
  26. self.ann = self.createann()
  27. # Add embeddings to the index
  28. self.ann.index(embeddings)
  29. # Save indexids-ids mapping for indexes with no database, except when this is a reindex
  30. if ids and not reindex and not self.database:
  31. self.config["ids"] = ids
  32. # Index scoring, if necessary
  33. # This must occur before graph index in order to be available to the graph
  34. if self.issparse():
  35. self.scoring.index()
  36. # Index subindexes, if necessary
  37. if self.indexes:
  38. self.indexes.index()
  39. # Index graph, if necessary
  40. if self.graph:
  41. self.graph.index(Search(self, True), self.batchsimilarity)

info(self)

Prints the current embeddings index configuration.

Source code in txtai/embeddings/base.py

  1. def info(self):
  2. """
  3. Prints the current embeddings index configuration.
  4. """
  5. if self.config:
  6. # Copy and edit config
  7. config = self.config.copy()
  8. # Remove ids array if present
  9. config.pop("ids", None)
  10. # Print configuration
  11. print(json.dumps(config, sort_keys=True, default=str, indent=2))

initindex(self, reindex)

Initialize new index.

Parameters:

NameTypeDescriptionDefault
reindex

if this is a reindex operation in which case database creation is skipped, defaults to False

required

Source code in txtai/embeddings/base.py

  1. def initindex(self, reindex):
  2. """
  3. Initialize new index.
  4. Args:
  5. reindex: if this is a reindex operation in which case database creation is skipped, defaults to False
  6. """
  7. # Initialize default parameters, if necessary
  8. self.defaults()
  9. # Create document database, if necessary
  10. if not reindex:
  11. self.database = self.createdatabase()
  12. # Reset archive since this is a new index
  13. self.archive = None
  14. # Create scoring only if term indexing is enabled
  15. scoring = self.config.get("scoring")
  16. if scoring and isinstance(scoring, dict) and self.config["scoring"].get("terms"):
  17. self.scoring = self.createscoring()
  18. # Create subindexes, if necessary
  19. self.indexes = self.createindexes()
  20. # Create graph, if necessary
  21. self.graph = self.creategraph()

isdense(self)

Checks if this instance has an associated ANN instance.

Returns:

TypeDescription

True if this instance has an associated ANN, False otherwise

Source code in txtai/embeddings/base.py

  1. def isdense(self):
  2. """
  3. Checks if this instance has an associated ANN instance.
  4. Returns:
  5. True if this instance has an associated ANN, False otherwise
  6. """
  7. return self.ann is not None

issparse(self)

Checks if this instance has an associated scoring instance with term indexing enabled.

Returns:

TypeDescription

True if term index is enabled, False otherwise

Source code in txtai/embeddings/base.py

  1. def issparse(self):
  2. """
  3. Checks if this instance has an associated scoring instance with term indexing enabled.
  4. Returns:
  5. True if term index is enabled, False otherwise
  6. """
  7. return self.scoring and self.scoring.hasterms()

isweighted(self)

Checks if this instance has an associated scoring instance with term weighting enabled.

Returns:

TypeDescription

True if term weighting is enabled, False otherwise

Source code in txtai/embeddings/base.py

  1. def isweighted(self):
  2. """
  3. Checks if this instance has an associated scoring instance with term weighting enabled.
  4. Returns:
  5. True if term weighting is enabled, False otherwise
  6. """
  7. return self.scoring and not self.scoring.hasterms()

load(self, path=None, cloud=None, **kwargs)

Loads an existing index from path.

Parameters:

NameTypeDescriptionDefault
path

input path

None
cloud

cloud storage configuration

None
kwargs

additional configuration as keyword args

{}

Source code in txtai/embeddings/base.py

  1. def load(self, path=None, cloud=None, **kwargs):
  2. """
  3. Loads an existing index from path.
  4. Args:
  5. path: input path
  6. cloud: cloud storage configuration
  7. kwargs: additional configuration as keyword args
  8. """
  9. # Load from cloud, if configured
  10. cloud = self.createcloud(cloud=cloud, **kwargs)
  11. if cloud:
  12. path = cloud.load(path)
  13. # Check if this is an archive file and extract
  14. path, apath = self.checkarchive(path)
  15. if apath:
  16. self.archive.load(apath)
  17. # Load index configuration
  18. self.config = self.loadconfig(path)
  19. # Approximate nearest neighbor index - stores dense vectors
  20. self.ann = self.createann()
  21. if self.ann:
  22. self.ann.load(f"{path}/embeddings")
  23. # Dimensionality reduction model - word vectors only
  24. if self.config.get("pca"):
  25. self.reducer = Reducer()
  26. self.reducer.load(f"{path}/lsa")
  27. # Document database - stores document content
  28. self.database = self.createdatabase()
  29. if self.database:
  30. self.database.load(f"{path}/documents")
  31. # Sparse vectors - stores term sparse arrays
  32. self.scoring = self.createscoring()
  33. if self.scoring:
  34. self.scoring.load(f"{path}/scoring")
  35. # Subindexes
  36. self.indexes = self.createindexes()
  37. if self.indexes:
  38. self.indexes.load(f"{path}/indexes")
  39. # Graph network - stores relationships
  40. self.graph = self.creategraph()
  41. if self.graph:
  42. self.graph.load(f"{path}/graph")
  43. # Dense vectors - transforms data to embeddings vectors
  44. self.model = self.loadvectors()
  45. # Query model
  46. self.query = self.loadquery()

reindex(self, config, columns=None, function=None)

Recreates the approximate nearest neighbor (ann) index using config. This method only works if document content storage is enabled.

Parameters:

NameTypeDescriptionDefault
config

new config

required
columns

optional list of document columns used to rebuild data

None
function

optional function to prepare content for indexing

None

Source code in txtai/embeddings/base.py

  1. def reindex(self, config, columns=None, function=None):
  2. """
  3. Recreates the approximate nearest neighbor (ann) index using config. This method only works if document
  4. content storage is enabled.
  5. Args:
  6. config: new config
  7. columns: optional list of document columns used to rebuild data
  8. function: optional function to prepare content for indexing
  9. """
  10. if self.database:
  11. # Keep content and objects parameters to ensure database is preserved
  12. config["content"] = self.config["content"]
  13. if "objects" in self.config:
  14. config["objects"] = self.config["objects"]
  15. # Reset configuration
  16. self.configure(config)
  17. # Reset function references
  18. if self.functions:
  19. self.functions.reset()
  20. # Reindex
  21. if function:
  22. self.index(function(self.database.reindex(columns)), True)
  23. else:
  24. self.index(self.database.reindex(columns), True)

save(self, path, cloud=None, **kwargs)

Saves an index in a directory at path unless path ends with tar.gz, tar.bz2, tar.xz or zip. In those cases, the index is stored as a compressed file.

Parameters:

NameTypeDescriptionDefault
path

output path

required
cloud

cloud storage configuration

None
kwargs

additional configuration as keyword args

{}

Source code in txtai/embeddings/base.py

  1. def save(self, path, cloud=None, **kwargs):
  2. """
  3. Saves an index in a directory at path unless path ends with tar.gz, tar.bz2, tar.xz or zip.
  4. In those cases, the index is stored as a compressed file.
  5. Args:
  6. path: output path
  7. cloud: cloud storage configuration
  8. kwargs: additional configuration as keyword args
  9. """
  10. if self.config:
  11. # Check if this is an archive file
  12. path, apath = self.checkarchive(path)
  13. # Create output directory, if necessary
  14. os.makedirs(path, exist_ok=True)
  15. # Copy sentence vectors model
  16. if self.config.get("storevectors"):
  17. shutil.copyfile(self.config["path"], os.path.join(path, os.path.basename(self.config["path"])))
  18. self.config["path"] = os.path.basename(self.config["path"])
  19. # Save index configuration
  20. self.saveconfig(path)
  21. # Save approximate nearest neighbor index
  22. if self.ann:
  23. self.ann.save(f"{path}/embeddings")
  24. # Save dimensionality reduction model (word vectors only)
  25. if self.reducer:
  26. self.reducer.save(f"{path}/lsa")
  27. # Save document database
  28. if self.database:
  29. self.database.save(f"{path}/documents")
  30. # Save scoring index
  31. if self.scoring:
  32. self.scoring.save(f"{path}/scoring")
  33. # Save subindexes
  34. if self.indexes:
  35. self.indexes.save(f"{path}/indexes")
  36. # Save graph
  37. if self.graph:
  38. self.graph.save(f"{path}/graph")
  39. # If this is an archive, save it
  40. if apath:
  41. self.archive.save(apath)
  42. # Save to cloud, if configured
  43. cloud = self.createcloud(cloud=cloud, **kwargs)
  44. if cloud:
  45. cloud.save(apath if apath else path)

score(self, documents)

Builds a term weighting scoring index. Only used by word vectors models.

Parameters:

NameTypeDescriptionDefault
documents

iterable of (id, data, tags), (id, data) or data

required

Source code in txtai/embeddings/base.py

  1. def score(self, documents):
  2. """
  3. Builds a term weighting scoring index. Only used by word vectors models.
  4. Args:
  5. documents: iterable of (id, data, tags), (id, data) or data
  6. """
  7. # Build scoring index for word vectors term weighting
  8. if self.isweighted():
  9. self.scoring.index(Stream(self)(documents))

search(self, query, 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
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, score) for index search, list of dict for an index+database search

Source code in txtai/embeddings/base.py

  1. def search(self, query, limit=None, weights=None, index=None):
  2. """
  3. Finds documents most similar to the input queries. This method will run either an index search
  4. or an index + database search depending on if a database is available.
  5. Args:
  6. query: input query
  7. limit: maximum results
  8. weights: hybrid score weights, if applicable
  9. index: index name, if applicable
  10. Returns:
  11. list of (id, score) for index search, list of dict for an index+database search
  12. """
  13. results = self.batchsearch([query], limit, weights, index)
  14. return results[0] if results else results

similarity(self, query, data)

Computes the similarity between query and list of data. Returns a list of (id, score) sorted by highest score, where id is the index in data.

Parameters:

NameTypeDescriptionDefault
query

input query

required
data

list of data

required

Returns:

TypeDescription

list of (id, score)

Source code in txtai/embeddings/base.py

  1. def similarity(self, query, data):
  2. """
  3. Computes the similarity between query and list of data. Returns a list of
  4. (id, score) sorted by highest score, where id is the index in data.
  5. Args:
  6. query: input query
  7. data: list of data
  8. Returns:
  9. list of (id, score)
  10. """
  11. return self.batchsimilarity([query], data)[0]

terms(self, query)

Extracts keyword terms from a query.

Parameters:

NameTypeDescriptionDefault
query

input query

required

Returns:

TypeDescription

query reduced down to keyword terms

Source code in txtai/embeddings/base.py

  1. def terms(self, query):
  2. """
  3. Extracts keyword terms from a query.
  4. Args:
  5. query: input query
  6. Returns:
  7. query reduced down to keyword terms
  8. """
  9. return self.batchterms([query])[0]

transform(self, document)

Transforms document into an embeddings vector.

Parameters:

NameTypeDescriptionDefault
documents

iterable of (id, data, tags), (id, data) or data

required

Returns:

TypeDescription

embeddings vector

Source code in txtai/embeddings/base.py

  1. def transform(self, document):
  2. """
  3. Transforms document into an embeddings vector.
  4. Args:
  5. documents: iterable of (id, data, tags), (id, data) or data
  6. Returns:
  7. embeddings vector
  8. """
  9. return self.batchtransform([document])[0]

upsert(self, documents)

Runs an embeddings upsert operation. If the index exists, new data is appended to the index, existing data is updated. If the index doesn’t exist, this method runs a standard index operation.

Parameters:

NameTypeDescriptionDefault
documents

iterable of (id, data, tags), (id, data) or data

required

Source code in txtai/embeddings/base.py

  1. def upsert(self, documents):
  2. """
  3. Runs an embeddings upsert operation. If the index exists, new data is
  4. appended to the index, existing data is updated. If the index doesn't exist,
  5. this method runs a standard index operation.
  6. Args:
  7. documents: iterable of (id, data, tags), (id, data) or data
  8. """
  9. # Run standard insert if index doesn't exist or it has no records
  10. if not self.count():
  11. self.index(documents)
  12. return
  13. # Create transform and stream
  14. transform = Transform(self, Action.UPSERT)
  15. stream = Stream(self, Action.UPSERT)
  16. with tempfile.NamedTemporaryFile(mode="wb", suffix=".npy") as buffer:
  17. # Load documents into database and transform to vectors
  18. ids, _, embeddings = transform(stream(documents), buffer)
  19. if embeddings is not None:
  20. # Remove principal components from embeddings, if necessary
  21. if self.reducer:
  22. self.reducer(embeddings)
  23. # Normalize embeddings
  24. self.normalize(embeddings)
  25. # Append embeddings to the index
  26. self.ann.append(embeddings)
  27. # Save indexids-ids mapping for indexes with no database
  28. if ids and not self.database:
  29. self.config["ids"] = self.config["ids"] + ids
  30. # Scoring upsert, if necessary
  31. # This must occur before graph upsert in order to be available to the graph
  32. if self.issparse():
  33. self.scoring.upsert()
  34. # Subindexes upsert, if necessary
  35. if self.indexes:
  36. self.indexes.upsert()
  37. # Graph upsert, if necessary
  38. if self.graph:
  39. self.graph.upsert(Search(self, True), self.batchsimilarity)