Embeddings (Memory)

The embeddings are a simple way to create a long term memory and simplify the use of extracted information.

The embeddings were previously called Memories. You might still find some places which reference memories instead of embeddings. The name is different but it is exactly the same thing.

Embeddings allows to store information and send it to a model via a embeddings vector search. This means the model only receives the important parts of this information.

Key Points

  1. Not for Summarizing: Embeddings aren't designed to summarize data.
  2. Selective Data Use: Some parts not judged relevant will not be sent to the model even when the prompt is small. This is based on the similarity of each data in the embeddings to the prompt
  3. Limit with Large Data: Summarizing extensive documents might not work with too much stored data (beyond 2000 tokens).
  4. Large inputs will be split up: In order for it to still be usable with large inputs, the inputs are split up in parts of max 512 tokens. The selection is then made on each part.
  5. Ideal Uses: Best suited for specific searches or clear questions.


Using Embeddings in JavaScript:

The most basic way of using Embeddings:

// In React
const { data: { Embeddings } } = usePolyfire();
// In other environments:
const { data: { Embeddings } } = polyfire;

const embeddings = Embeddings();
embeddings.add("The secret word is: banana42");
const secret = await polyfire.generate("What's the secret?", { embeddings });
console.log(secret); // Outputs: The secret word is "banana42".



type EmbeddingsOptions = {
  id?: string, // This cannot be set at the same time as public since it gets an existing embedding collection instead of creating a new one.
  public?: boolean, // Whether the content of your new embedding collection should be public (defaults to false)
} | string;
function Embeddings(options?: EmbeddingsOptions): embeddings;
async function embeddings.getId(): Promise<string>;
async function embeddings.add(input: string): Promise<void>;
async function embeddings.search(input: string): Promise<{ content: string, similarity: number }[]>
  • Accessing a Previously Created Embeddings: If you've made embeddings before and want to access it again, you can do so using its ID.
const existingEmbeddings = Embeddings("yourEmbeddingsCollectionID");
// or
const existingEmbeddings = Embeddings({ id: "yourEmbeddingsCollectionID" });
  • Retrieving the ID of a Embeddings: To get the unique identifier of a particular embeddings collection instance, use the following:
const id = await embeddings.getId();
console.log(id);  // Outputs the ID of the embedding collection.
  • Storing Data in Embeddings: You can easily add new information to your embeddings collection using the add method.
embeddings.add("My house is yellow");
  • Looking for semantically similar data in a embeddings collection:
const [{ content }] = await embeddings.search("My house is pink");

console.log(content); // My house is yellow