Skip to main content

Embed

With our embed-endpoint you can embed any prompt into vector space.

Code Example

import os
from aleph_alpha_client import Client, Prompt, EmbeddingRequest
# If you are using a Windows machine, you must install the python-dotenv package and run the two below lines as well.
# from dotenv import load_dotenv
# load_dotenv()

client = Client(token=os.getenv("AA_TOKEN"))
prompt_text = "An apple a day keeps the doctor away."
params = {
"prompt": Prompt.from_text(prompt_text),
"pooling": ["mean"],
"layers": [-1]
}
request = EmbeddingRequest(**params)
response = client.embed(request=request, model="luminous-extended")
embedding = list(response.embeddings.values())[0]

If you need more information on the parameters you can use, please checkout our HTTP API.

tip

Should I Use Embeddings or Semantic Embeddings?

Technically, you can work with both types of embeddings. Our embedding endpoint should be used as a feature input for downstream models (e.g., classifiers). There are cases where the semantic meaning of a text matters. These could be, but are not limited to, semantic search systems or Q&A applications. In these scenarios, we recommend using our semantic embeddings endpoint.

What is the Difference Between Embeddings and Semantic Embeddings?

Our embeddings are the pooled activations for the input tokens at the layer of your choice. While the embedding representation has some semantic meaning, the semantic embeddings have been optimized to find semantic relationships between two inputs (text and image). This was achieved by training additional layers using datasets curated for this task and a contrastive loss. There is an optional dimensionality reduction layer that reduces the embedding dimension to a 128-dimensional vector, making it more efficient to compare two vectors.

Code Example Attention Manipulation

You can also create embeddings with our attention manipulation method, AtMan. Let's say you want to suppress attention on the word "doctor". Then you would do the following:

import os
import re
from aleph_alpha_client import Client, Prompt, EmbeddingRequest, TextControl

# If you are using a Windows machine, you must install the python-dotenv package and run the two below lines as well.
# from dotenv import load_dotenv
# load_dotenv()

client = Client(token=os.getenv("AA_TOKEN"))
prompt_text = "An apple a day keeps the doctor away."

matching_string = re.search("doctor", prompt_text)

begin_match = matching_string.regs[0][0]
end_match = matching_string.regs[0][1]

control = TextControl(start=begin_match, length=end_match - begin_match, factor=0.01)

params = {
"prompt": Prompt.from_text(prompt_text, controls=[control]),
"pooling": ["mean"],
"layers": [-1],
}
request = EmbeddingRequest(**params)
response = client.embed(request=request, model="luminous-extended")
embedding = list(response.embeddings.values())[0]