Create vector embeddings
To use Aerospike Vector Search (AVS), you must build an application that generates vector embeddings. This page outlines some general approaches for generating vector embeddings using Python and a machine learning model.
Generate embeddings using a hosted service
Using a hosted model like OpenAI provides ease of use, quick deployment, and access to cutting-edge technology without the need for significant infrastructure investment. It ensures scalability, automatic updates, and maintenance, allowing organizations to focus on application development rather than managing the underlying model infrastructure.
-
Install the OpenAI Python client library if you haven’t already:
Terminal window pip install openai -
Use the following Python code to generate a vector embedding:
import openai# Set your OpenAI API keyopenai.api_key = 'your-api-key-here'# Define the text chunk for which you want to generate an embeddingtext_chunk = "OpenAI's GPT-4 is a powerful language model capable of performing a wide range of natural language processing tasks."# Generate the embeddingresponse = openai.Embedding.create(input=text_chunk,model="text-embedding-ada-002")# Extract the embedding vectorembedding_vector = response['data'][0]['embedding']# Print the embedding vectorprint(embedding_vector)
Self-host an open-source model
Self-hosting a machine learning model offers enhanced data privacy, security, and control over the environment, making it easier to comply with regulatory requirements and optimize performance. It can also be more cost-effective for high usage scenarios, eliminating dependency on third-party providers and reducing latency.
The following example shows how you can generate a vector embedding from a chunk of text using the LLaMA model. You can use the generated vector for various downstream tasks such as similarity searches and other vector computations.
-
Install the required libraries:
Terminal window pip install transformerspip install torch -
Use the following Python code to generate a vector embedding:
from transformers import AutoTokenizer, AutoModelimport torch# Load the LLaMA model and tokenizermodel_name = "facebook/llama-7b" # Replace with the correct model nametokenizer = AutoTokenizer.from_pretrained(model_name)model = AutoModel.from_pretrained(model_name)# Define the text chunk for which you want to generate an embeddingtext_chunk = "LLaMA is a powerful language model capable of performing a wide range of natural language processing tasks."# Tokenize the text chunkinputs = tokenizer(text_chunk, return_tensors="pt")# Generate the embeddingswith torch.no_grad():outputs = model(**inputs)# The embeddings are typically in the 'last_hidden_state' tensorembeddings = outputs.last_hidden_state# Average the token embeddings to get a single vector representationembedding_vector = torch.mean(embeddings, dim=1).squeeze().numpy()# Print the embedding vectorprint(embedding_vector)