llm.embed

llm.embed

Generates numerical embeddings for given text inputs for allowing context-sensitive AI interactions.

The llm.embed function is used to generate embeddings for a given text or a list of texts. Embeddings are numerical representations of text data and are often used for measuring semantic similarities and helping in context based AI interaction.

llm.embed uses OpenAI's Create Embedding API under the hood.

Syntax

const { embeddings } = await llm.embed({
  input: "Embed me please!", // single string
});
const { embeddings } = await llm.embed({
  input: "Embed me please!", // single string
});

OR

const { embeddings } = await llm.embed({
  input: paragraphs, // list of strings
});
const { embeddings } = await llm.embed({
  input: paragraphs, // list of strings
});

Parameters

  • input (String or Array of Strings): The text or texts to be embedded.
  • user (optional, String): An optional parameter representing the users question.

Return Value

The embed function is an asynchronous function that returns a Promise, which resolves to an object containing the key 'embeddings'. The value of 'embeddings' is a list of vectors, one for each input text.

{ embeddings: number[][] }
{ embeddings: number[][] }

Example

Here's an example usage of llm.embed function (live demo):

"use client";
import useLLM from "usellm";
import React, { useState } from "react";
 
export default function EmbedDemoPage() {
  const [documentText, setDocumentText] = useState("");
  const llm = useLLM({ serviceUrl: "https://usellm.org/api/llm" });
 
  async function handleEmbedding() {
    const paragraphs = documentText.split("\n").filter((p) => p.length > 0);
    const { embeddings } = await llm.embed({ input: paragraphs });
 
    // For illustration purpose, print the first embedding to the console
    console.log("Embedding of the first paragraph: ", embeddings[0]);
  }
 
  return (
    <div style={{ padding: 8 }}>
      <textarea
        maxLength={2000}
        rows={10}
        value={documentText}
        onChange={(e) => setDocumentText(e.target.value)}
        placeholder="Enter a document..."
      />
      <button onClick={handleEmbedding}>Generate Embeddings</button>
    </div>
  );
}
"use client";
import useLLM from "usellm";
import React, { useState } from "react";
 
export default function EmbedDemoPage() {
  const [documentText, setDocumentText] = useState("");
  const llm = useLLM({ serviceUrl: "https://usellm.org/api/llm" });
 
  async function handleEmbedding() {
    const paragraphs = documentText.split("\n").filter((p) => p.length > 0);
    const { embeddings } = await llm.embed({ input: paragraphs });
 
    // For illustration purpose, print the first embedding to the console
    console.log("Embedding of the first paragraph: ", embeddings[0]);
  }
 
  return (
    <div style={{ padding: 8 }}>
      <textarea
        maxLength={2000}
        rows={10}
        value={documentText}
        onChange={(e) => setDocumentText(e.target.value)}
        placeholder="Enter a document..."
      />
      <button onClick={handleEmbedding}>Generate Embeddings</button>
    </div>
  );
}

In this example, when the "Generate Embeddings" button is clicked, the handleEmbedding function is triggered. This function splits the input document into separate paragraphs, each of which is then passed to the llm.embed function. The resulting embeddings are then displayed on the screen.

To see a real world example of embedding a document and using it for questions refer to the doc of Score Embeddings.