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.025706132873892784, -0.00652786111459136, -0.05880141258239746]
[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
Info
This driver requires the drivers-embedding-amazon-bedrock extra.
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.10888671875, 0.291015625, 0.2265625]
[-0.234375, -0.024902344, -0.14941406]
Info
This driver requires the drivers-embedding-google extra.
The GoogleEmbeddingDriver uses the Google Embeddings API.
from griptape.drivers.embedding.google import GoogleEmbeddingDriver embeddings = GoogleEmbeddingDriver().embed("Hello world!") # display the first 3 embeddings print(embeddings[:3])
[0.05886331, 0.0033929434, -0.072810836]
[0.0588633, 0.0033929371, -0.072810836]
Hugging Face Hub
Info
This driver requires the drivers-embedding-huggingface extra.
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])
[-0.02038687840104103, 0.02528087981045246, -0.0005662207258865237]
Ollama
Info
This driver requires the drivers-embedding-ollama extra.
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.
Info
This driver requires the drivers-embedding-amazon-sagemaker extra.