Weaviate and Cohere (Integration Guide)
Weaviate is an open source vector search engine that stores both objects and vectors, allowing for combining vector search with structured filtering. Here, we’ll create a Weaviate Cluster to index your data with Cohere Embed, and process it with Rerank and Command.
Here are the steps involved:
- Create the Weaviate cluster (see this post for more detail.)
- Once the cluster is created, you will receive the cluster URL and API key.
- Use the provided URL and API key to connect to your Weaviate cluster.
- Use the Weaviate Python client to create your collection to store data
Getting Set up
First, let’s handle the imports, the URLs, and the pip installs.
Embed
Now, we’ll create a new collection named "Healthcare_Compliance"
in the Weaviate database.
You’ll see something like this:
Next, we’ll define the list of healthcare compliance documents, retrieve the "Healthcare_Compliance"
collection from the Weaviate client, and use a dynamic batch process to add multiple documents to the collection efficiently.
Now, we’ll iterate over the objects we’ve retrieved and print their results:
The output will look something like this (NOTE: a lower value for a Distance
means that two vectors are closer to one another than those with a higher value):
Embed + Rerank
Now, we’ll add in Cohere Rerank to surface more relevant results. This will involve some more set up:
And here we’ll create a "Legal_Docs"
collection in the Weaviate database:
Now, we’ll need to define a searh query:
This code snippet imports the MetadataQuery
class from weaviate.classes.query
to handle metadata in queries, iterates over the retrieved objects, and prints their details:
The output will look something like this:
This code sets up Rerank infrastructure:
Here’s what the output looks like:
Based on the rerank scores, it’s clear that the Bankruptcy Law Overview is the most relevant result, while the other two documents (Tax Law for Businesses and Consumer Protection Laws) have significantly lower scores, indicating they are less relevant to the query. Therefore, we should focus only on the most relevant result and can skip the other two.
Embed + Rerank + Command
Finally, we’ll add Command into the mix. This handles imports and creates a fresh "Legal_Docs"
in the Weaviate database.
You should see something like that:
This retrieves "Legal_Docs_RAG"
from Weaviate:
As before, we’ll iterate over the object and print the result:
You’ll see something like this:
Conclusion
This integration guide has demonstrated how to effectively combine Cohere’s powerful AI capabilities with Weaviate’s vector database to create sophisticated search and retrieval systems. We’ve covered three key approaches:
-
Basic Vector Search: Using Cohere’s Embed model with Weaviate to perform semantic search, enabling natural language queries to find relevant documents based on meaning rather than just keywords.
-
Enhanced Search with Rerank: Adding Cohere’s Rerank model to improve search results by reordering them based on relevance, ensuring the most pertinent documents appear first.
-
Full RAG Pipeline: Implementing a complete Retrieval-Augmented Generation (RAG) system that combines embedding, reranking, and Cohere’s Command model to not only find relevant information but also generate contextual responses.
The integration showcases how these technologies work together to create more intelligent and accurate search systems. Whether you’re building a healthcare compliance database, legal document system, or any other knowledge base, this combination provides a powerful foundation for semantic search and AI-powered content generation.
The flexibility of this integration allows you to adapt it to various use cases while maintaining high performance and accuracy in your search and retrieval operations.