主题
通过 Google 的 google-genai 包,LlamaIndex 提供了 GoogleGenAIEmbedding 类,允许你使用最新的 gemini-embedding-2-preview 模型,通过 Gemini 和 Vertex AI 两个 API 的 Google GenAI 模型嵌入文本。
如果你在 Colab 中打开这个 Notebook,可能需要先安装 LlamaIndex 🦙。
%pip install llama-index-embeddings-google-genai1
import os
os.environ["GOOGLE_API_KEY"] = "..."1
2
3
2
3
设置
GoogleGenAIEmbedding 是围绕 google-genai 包的封装,这意味着它开箱即支持 Gemini 和 Vertex AI 两个 API。
你可以直接传入 api_key,或传入 vertexai_config 以使用 Vertex AI API。
其他选项包括 embed_batch_size、model_name 和 embedding_config。
from llama_index.embeddings.google_genai import GoogleGenAIEmbedding
from google.genai.types import EmbedContentConfig
embed_model = GoogleGenAIEmbedding(
model_name="gemini-embedding-2-preview",
embed_batch_size=100,
# can pass in the api key directly
# api_key="...",
# or pass in a vertexai_config
# vertexai_config={
# "project": "...",
# "location": "...",
# }
# can also pass in an embedding_config
# embedding_config=EmbedContentConfig(...)
)1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
用量
同步
embeddings = embed_model.get_text_embedding("Google Gemini Embeddings.")
print(embeddings[:5])
print(f"Dimension of embeddings: {len(embeddings)}")1
2
3
2
3
[0.031099992, 0.02192731, -0.06523498, 0.016788177, 0.0392835]
Dimension of embeddings: 7681
2
2
embeddings = embed_model.get_query_embedding("Query Google Gemini Embeddings.")
print(embeddings[:5])
print(f"Dimension of embeddings: {len(embeddings)}")1
2
3
2
3
[0.022199392, 0.03671178, -0.06874573, 0.02195774, 0.05475164]
Dimension of embeddings: 7681
2
2
embeddings = embed_model.get_text_embedding_batch(
[
"Google Gemini Embeddings.",
"Google is awesome.",
"Llamaindex is awesome.",
]
)
print(f"Got {len(embeddings)} embeddings")
print(f"Dimension of embeddings: {len(embeddings[0])}")1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
Got 3 embeddings
Dimension of embeddings: 7681
2
2
异步
embeddings = await embed_model.aget_text_embedding("Google Gemini Embeddings.")
print(embeddings[:5])
print(f"Dimension of embeddings: {len(embeddings)}")1
2
3
2
3
[0.031099992, 0.02192731, -0.06523498, 0.016788177, 0.0392835]
Dimension of embeddings: 7681
2
2
embeddings = await embed_model.aget_query_embedding(
"Query Google Gemini Embeddings."
)
print(embeddings[:5])
print(f"Dimension of embeddings: {len(embeddings)}")1
2
3
4
5
2
3
4
5
[0.022199392, 0.03671178, -0.06874573, 0.02195774, 0.05475164]
Dimension of embeddings: 7681
2
2
embeddings = await embed_model.aget_text_embedding_batch(
[
"Google Gemini Embeddings.",
"Google is awesome.",
"Llamaindex is awesome.",
]
)
print(f"Got {len(embeddings)} embeddings")
print(f"Dimension of embeddings: {len(embeddings[0])}")1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
Got 3 embeddings
Dimension of embeddings: 7681
2
2