The embedding service in the Gemini API generates state-of-the-art embeddings for words, phrases, and sentences. The resulting embeddings can then be used for natural language processing (NLP) tasks, such as semantic search, text classification and clustering among many others.
This page briefly explains embeddings and highlights some key use cases for the embedding service to help you get started.
What are embeddings?
Text embeddings are a natural language processing (NLP) technique that converts text into numerical coordinates (called vectors) that can be plotted in an n-dimensional space. This approach lets you treat pieces of text as bits of relational data, which we can then train models on.
Embeddings capture semantic meaning and context which results in text with similar meanings having closer embeddings. For example, the sentence "I took my dog to the vet" and "I took my cat to the vet" would have embeddings that are close to each other in the vector space since they both describe a similar context.
You can use embeddings to compare different texts and understand how they relate. For example, if the embeddings of the text "cat" and "dog" are close together you can infer that these words are similar in meaning or context or both. This ability allows a variety of use cases described in the next section.
Use cases
Text embeddings are used in a variety of NLP use cases, such as:
- Information retrieval: You can use embeddings to retrieve semantically
similar text given a piece of input text. A variety of applications can be
supported by an information retrieval system such as semantic search,
answering questions, or summarization.
- Tutorial: Document search
- Classification: You can train a model using embeddings to classify
documents into categories. For example, if you want to classify user
comments as negative or positive, you can use the embeddings service to get
the vector representation of each comment to train the classifier.
- Tutorial: Train a text classifier
- Clustering: Comparing vectors of text can show how similar or different
they are. This feature can be used to train a clustering model that groups
similar text or documents together and to detect anomalies in your data.
- Tutorials: Train a clustering model, Detect data anomalies
- Vector database: You can store your generated embeddings in a vector
database to improve the accuracy and efficiency of your NLP application.
Refer to the following tutorial to learn how to use a vector database to
translate text prompts into numerical vectors.
- Tutorials: Work with vector embeddings, Semantic retrieval
Gemini embeddings models
The Gemini API offers two models that generate text embeddings: Text Embeddings and Embeddings. Text Embeddings is an updated version of the Embedding model that offers elastic embedding sizes under 768 dimensions. Elastic embeddings generate smaller output dimensions and potentially save computing and storage costs with minor performance loss.
Use Text Embeddings for new projects or applications. Your application should only use the Embedding model if you can't migrate it to use Text Embeddings.
Generate embeddings with the Gemini API
Use the embedContent
method to generate text embeddings:
Python
result = genai.embed_content(
model="models/text-embedding-004",
content="What is the meaning of life?",
task_type="retrieval_document",
title="Embedding of single string")
# 1 input > 1 vector output
print(str(result['embedding'])[:50], '... TRIMMED]')
Visit our getting started tutorial for the complete runnable example.
Go
ctx := context.Background()
// Access your API key as an environment variable (see our Getting Started tutorial)
client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("API_KEY")))
if err != nil {
log.Fatal(err)
}
defer client.Close()
// For embeddings, use the Text Embeddings model
em := client.EmbeddingModel("text-embedding-004")
res, err := em.EmbedContent(ctx, genai.Text("The quick brown fox jumps over the lazy dog."))
if err != nil {
panic(err)
}
fmt.Println(res.Embedding.Values)
Visit our getting started tutorial for the complete runnable example.
Node.js
const { GoogleGenerativeAI } = require("@google/generative-ai");
// Access your API key as an environment variable (see our Getting Started tutorial)
const genAI = new GoogleGenerativeAI(process.env.API_KEY);
async function run() {
// For embeddings, use the Text Embeddings model
const model = genAI.getGenerativeModel({ model: "text-embedding-004"});
const text = "The quick brown fox jumps over the lazy dog."
const result = await model.embedContent(text);
const embedding = result.embedding;
console.log(embedding.values);
}
run();
Visit our getting started tutorial for the complete runnable example.
Dart (Flutter)
final model = GenerativeModel(model: 'text-embedding-004', apiKey: apiKey);
final content = Content.text('The quick brown fox jumps over the lazy dog.');
final result = await model.embedContent(content);
print(result.embedding.values);
Visit our getting started tutorial for the complete runnable example.
curl
curl "https://generativelanguage.googleapis.com/v1beta/models/text-embedding-004:embedContent?key=$GOOGLE_API_KEY" \
-H 'Content-Type: application/json' \
-d '{"model": "models/text-embedding-004",
"content": {
"parts":[{
"text": "Hello world"}]}, }' 2> /dev/null | head
Visit our Embedding Quickstart with REST cookbook on GitHub for the complete runnable example.
What's next
- If you want to learn more about embeddings, visit Dale Markowitz's post on the Google Cloud blog for more information.
- If you're ready to start developing, you can find complete runnable code in the quickstarts for Python, Go, Node.js, and Dart (Flutter).