voyage-law-2, a legal-domain model.
Setup guide
In this guide, you use the Voyage Embedding API endpoint to generate text embeddings for terms of service and consumer contract documents, and then index those embeddings in Pinecone Database. This is a common combination for building retrieval-augmented generation (RAG), semantic search, question-answering, code assistants, and other applications that rely on NLP and search over a large corpus of text data.Set up the environment
Start by installing the Voyage and Pinecone clients, along with Hugging Face Datasets for downloading the LegalBench: Consumer Contracts QA (
mteb/legalbench_consumer_contracts_qa) dataset used in this guide:Shell
Create embeddings
Sign up for an API key at Voyage AI and then use it to initialize your connection.Load the LegalBench: Consumer Contracts QA dataset, which contains 154 consumer contract documents and 396 labeled queries about these documents.Each document in Check the dimensionality of the returned vectors. Save the embedding dimensionality, because you need it when you create your Pinecone index later.In this example, you can see that for each of the
Python
Python
mteb/legalbench_consumer_contracts_qa contains a text field, which you embed with the Voyage AI client.Python
Python
Response
154 documents, the code creates a 1024-dimensional embedding with the Voyage AI voyage-law-2 model.Store the embeddings
Now that you have your embeddings, you can move on to indexing them in Pinecone Database. For this, you need a Pinecone API key. Sign up for one.First, initialize your connection to Pinecone, and then create a new index called Now you can begin populating the index with your embeddings. Pinecone expects you to provide a list of tuples in the format (You can see from
voyageai-pinecone-legalbench for storing the embeddings. When you create the index, specify the cosine similarity metric to align with Voyage AI’s embeddings, and pass the embedding dimensionality of 1024.Python
id, vector, metadata), where the metadata field is an optional extra field where you can store anything you want in a dictionary format. For this example, you’ll store the original text of the embeddings.Upload the data in batches to avoid pushing too much data at once.Python
Response
index.describe_index_stats that you have a 1024-dimensional index populated with 154 embeddings.Run a semantic search
Now that you have your indexed vectors, you can perform a few search queries. To search, first embed your query with The response from Pinecone includes your original text in the The semantic search pipeline with Voyage AI and Pinecone can identify the relevant consumer contract documents to answer the user query.
voyage-law-2, and then search Pinecone with the returned vector.Python
metadata field. Print the top_k most similar questions and their similarity scores. The dataset’s text is missing apostrophes and dashes, so the output is missing them too.Python
Response