Embeddings

embeddings 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.

The following code snippet shows how to build and search an embeddings index.

  1. from txtai.embeddings import Embeddings
  2. # Create embeddings model, backed by sentence-transformers & transformers
  3. embeddings = Embeddings({"path": "sentence-transformers/nli-mpnet-base-v2"})
  4. data = [
  5. "US tops 5 million confirmed virus cases",
  6. "Canada's last fully intact ice shelf has suddenly collapsed, " +
  7. "forming a Manhattan-sized iceberg",
  8. "Beijing mobilises invasion craft along coast as Taiwan tensions escalate",
  9. "The National Park Service warns against sacrificing slower friends " +
  10. "in a bear attack",
  11. "Maine man wins $1M from $25 lottery ticket",
  12. "Make huge profits without work, earn up to $100,000 a day"
  13. ]
  14. # Create an index for the list of text
  15. embeddings.index([(uid, text, None) for uid, text in enumerate(data)])
  16. print("%-20s %s" % ("Query", "Best Match"))
  17. print("-" * 50)
  18. # Run an embeddings search for each query
  19. for query in ("feel good story", "climate change", "public health story", "war",
  20. "wildlife", "asia", "lucky", "dishonest junk"):
  21. # Extract uid of first result
  22. # search result format: (uid, score)
  23. uid = embeddings.search(query, 1)[0][0]
  24. # Print text
  25. print("%-20s %s" % (query, data[uid]))

Build

An embeddings instance can be created as follows:

  1. embeddings = Embeddings({"path": "sentence-transformers/nli-mpnet-base-v2"})

The example above builds a transformers-based embeddings instance. In this case, when loading and searching for data, a transformers model is used to vectorize data.

The embeddings instance is configuration-driven based on what is passed in the constructor. Embeddings indexes store vectors and can optionally store content. Content storage enables additional filtering and data retrieval options.

Index

After creating a new embeddings instance, the next step is adding data to it.

  1. embeddings.index([(uid, text, None) for uid, text in enumerate(data)])

The index method takes an iterable collection of tuples with three values.

ElementDescription
idunique record id
datainput data to index, can be text, a dictionary or object
tagsoptional tags string, used to mark/label data as it’s indexed

When the data element is a dictionary and it has a field named text, that will be used for indexing.

The input iterable can be a list or generator. Generators help with indexing very large datasets as only portions of the data is in memory at any given time.

More information on indexing can be found in the index guide.

Once data is indexed, it is ready for search.

  1. embeddings.search(query, limit)

The search method takes two parameters, the query and query limit. The results format is different based on whether content is stored or not.

  • List of (id, score) when content is not stored
  • List of {**query columns} when content is stored

Both natural language and SQL queries are supported. More information can be found in the query guide.

More examples

See this link for a full list of embeddings examples.