Embedding#
Overview#
Embeddings in Gen AI Builder are multidimensional representations of text or image data. Embeddings carry semantic information, making them powerful for use-cases like text or image similarity search in a RAG Engine .
Embedding Drivers#
OpenAI#
The OpenAiEmbeddingDriver uses the OpenAI Embeddings API .
from griptape.drivers.embedding.openai import OpenAiEmbeddingDriver
embeddings = OpenAiEmbeddingDriver().embed("Hello Griptape!")
# display the first 3 embeddings
print(embeddings[:3])
[0.0017853748286142945, 0.006118456833064556, -0.005811543669551611]
OpenAI Compatible#
Many services such as LMStudio and OhMyGPT provide
OpenAI-compatible APIs. You can use the OpenAiEmbeddingDriver to interact with
these services. Simply set the base_url to the service’s API
endpoint and the model to the model name. If the service requires an
API key, you can set it in the api_key field.
from griptape.drivers.embedding.openai import OpenAiEmbeddingDriver
embedding_driver = OpenAiEmbeddingDriver(
base_url="http://127.0.0.1:1234/v1",
model="nomic-ai/nomic-embed-text-v1.5-GGUF/nomic-embed-text-v1.5.Q2_K",
)
embeddings = embedding_driver.embed("Hello world!")
# display the first 3 embeddings
print(embeddings[:3])
Tip
Make sure to include v1 at the end of the base_url to match the OpenAI API endpoint.
Azure OpenAI#
The AzureOpenAiEmbeddingDriver uses the same parameters as OpenAiEmbeddingDriver
with updated defaults.
Bedrock Titan#
The AmazonBedrockTitanEmbeddingDriver uses the Amazon Bedrock Embeddings API .
from griptape.drivers.embedding.amazon_bedrock import AmazonBedrockTitanEmbeddingDriver
from griptape.loaders import ImageLoader
embedding_driver = AmazonBedrockTitanEmbeddingDriver()
embeddings = embedding_driver.embed("Hello world!")
print(embeddings[:3])
# Some models support images!
multi_modal_embedding_driver = AmazonBedrockTitanEmbeddingDriver(model="amazon.titan-embed-image-v1")
image = ImageLoader().load("tests/resources/cow.png")
image_embeddings = multi_modal_embedding_driver.embed(image)
print(image_embeddings[:3])
[-0.234375, -0.024902344, -0.14941406]
Google#
The GoogleEmbeddingDriver uses the Google Embeddings API <Optional title for the content. Only works with `retrieval_document task type.>` .
from griptape.drivers.embedding.google import GoogleEmbeddingDriver
embeddings = GoogleEmbeddingDriver().embed("Hello world!")
# display the first 3 embeddings
print(embeddings[:3])
[0.0588633, 0.0033929371, -0.072810836]
Hugging Face Hub#
The HuggingFaceHubEmbeddingDriver connects to the Hugging Face Hub API . It supports models with the following tasks:
feature-extraction
import os
from griptape.drivers.embedding.huggingface_hub import HuggingFaceHubEmbeddingDriver
from griptape.tokenizers import HuggingFaceTokenizer
driver = HuggingFaceHubEmbeddingDriver(
api_token=os.environ["HUGGINGFACE_HUB_ACCESS_TOKEN"],
model="sentence-transformers/all-MiniLM-L6-v2",
tokenizer=HuggingFaceTokenizer(
model="sentence-transformers/all-MiniLM-L6-v2",
max_output_tokens=512,
),
)
embeddings = driver.embed("Hello world!")
# display the first 3 embeddings
print(embeddings[:3])
Ollama#
The OllamaEmbeddingDriver uses the Ollama Embeddings API .
from griptape.drivers.embedding.ollama import OllamaEmbeddingDriver
driver = OllamaEmbeddingDriver(
model="all-minilm",
)
results = driver.embed("Hello world!")
# display the first 3 embeddings
print(results[:3])
Amazon SageMaker Jumpstart#
The AmazonSageMakerJumpstartEmbeddingDriver uses the Amazon SageMaker Endpoints to generate embeddings on AWS.
import os
from griptape.drivers.embedding.amazon_sagemaker_jumpstart import AmazonSageMakerJumpstartEmbeddingDriver
driver = AmazonSageMakerJumpstartEmbeddingDriver(
endpoint=os.environ["SAGEMAKER_ENDPOINT"],
model=os.environ["SAGEMAKER_TENSORFLOW_HUB_MODEL"],
)
embeddings = driver.embed("Hello world!")
# display the first 3 embeddings
print(embeddings[:3])
VoyageAI#
The VoyageAiEmbeddingDriver uses the VoyageAI Embeddings API .
import os
from griptape.drivers.embedding.voyageai import VoyageAiEmbeddingDriver
from griptape.loaders import ImageLoader
embedding_driver = VoyageAiEmbeddingDriver(api_key=os.environ["VOYAGE_API_KEY"])
embeddings = embedding_driver.embed("Hello world!")
print(embeddings[:3])
# Some models support images!
multi_modal_embedding_driver = VoyageAiEmbeddingDriver(
api_key=os.environ["VOYAGE_API_KEY"], model="voyage-multimodal-3"
)
image = ImageLoader().load("tests/resources/cow.png")
image_embeddings = multi_modal_embedding_driver.embed(image)
print(image_embeddings[:3])
Cohere#
The CohereEmbeddingDriver uses the Cohere Embeddings API .
import os
from griptape.drivers.embedding.cohere import CohereEmbeddingDriver
embedding_driver = CohereEmbeddingDriver(
model="embed-english-v3.0",
api_key=os.environ["COHERE_API_KEY"],
input_type="search_document",
)
embeddings = embedding_driver.embed("Hello world!")
# display the first 3 embeddings
print(embeddings[:3])
Nvidia NIM#
The NvidiaNimEmbeddingDriver uses the Nvidia NIM API .
Override Default Structure Embedding Driver#
Here is how you can override the Embedding Driver that is used by default in Structures.
from griptape.configs import Defaults
from griptape.configs.drivers import DriversConfig
from griptape.drivers.embedding.voyageai import VoyageAiEmbeddingDriver
from griptape.drivers.prompt.openai import OpenAiChatPromptDriver
from griptape.structures import Agent
from griptape.tools import PromptSummaryTool, WebScraperTool
Defaults.drivers_config = DriversConfig(
prompt_driver=OpenAiChatPromptDriver(model="gpt-4.1"),
embedding_driver=VoyageAiEmbeddingDriver(),
)
Defaults.drivers_config = DriversConfig(
prompt_driver=OpenAiChatPromptDriver(model="gpt-4.1"),
embedding_driver=VoyageAiEmbeddingDriver(),
)
agent = Agent(
tools=[WebScraperTool(off_prompt=True), PromptSummaryTool(off_prompt=False)],
)
agent.run("based on https://www.griptape.ai/, tell me what Griptape is")