Hybrid Search Embedding 001
Hybrid Search with Gemini
This recipe will show you how to run hybrid search with embeddings from Google's Gemini.
WARNING: Ignoring invalid distribution ~eaviate-client (/usr/local/lib/python3.11/site-packages) Name: weaviate-client Version: 4.10.4 Summary: A python native Weaviate client Home-page: https://github.com/weaviate/weaviate-python-client Author: Weaviate Author-email: hello@weaviate.io, License: BSD 3-clause Location: /usr/local/lib/python3.11/site-packages Requires: authlib, grpcio, grpcio-health-checking, grpcio-tools, httpx, pydantic, validators Required-by: crewai-tools, goldenverba, langchain-weaviate, llama-index-vector-stores-weaviate, vector-etl
Requirements
- Weaviate cluster
- You can create a 14-day free sandbox on WCD
- Embedded Weaviate
- Local deployment
- Other options
- Gemini API Key or Vertex AI token
Connect to Weaviate
Only choose one option from the below.
You can access Gemini through Vertex AI or AI Studio. Depending on which you choose, you will need to change the header when connecting to Weaviate (X-Goog-Vertex-Api-Key or X-Goog-Studio-Api-Key)
Weaviate Cloud Deployment
True
Local Instance
Expired Google Cloud Token
If you're using Vertex AI, you will need to refresh your token every 60 minutes.
If you're using AI Studio, ignore the below portion.
The Google Cloud's OAuth 2.0 access tokens only have a one hour lifetime. This means you have to replace the expired token with a valid one and it to Weaviate by re-instantiating the client.
Option 1: With Google Cloud CLI
Then you could run the below cell periodically.
Then run the below periodically:
Create a collection
Collection stores your data and vector embeddings.
Successfully created collection: JeopardyQuestion.
Import Data
Insert complete.
10
Hybrid Search
The alpha parameter determines the weight given to the sparse and dense search methods. alpha = 0 is pure sparse (bm25) search, whereas alpha = 1 is pure dense (vector) search.
Alpha is an optional parameter. The default is set to 0.75.
Hybrid Search only
The below query is finding Jeopardy questions about animals and is limiting the output to only two results. Notice alpha is set to 0.80, which means it is weighing the vector search results more than bm25. If you were to set alpha = 0.25, you would get different results.
ID: d2d97c7d-4ae6-44f3-9c50-57d84ee75699
Data: {
"answer": "species",
"question": "2000 news: the Gunnison sage grouse isn't just another northern sage grouse, but a new one of this classification",
"category": "SCIENCE"
}
ID: 0b488aa7-8e47-47ee-9362-8614dbbd919f
Data: {
"answer": "the diamondback rattler",
"question": "Heaviest of all poisonous snakes is this North American rattlesnake",
"category": "ANIMALS"
}
ID: 014219e8-03d4-4218-bfc5-37e237259353
Data: {
"answer": "Antelope",
"question": "Weighing around a ton, the eland is the largest species of this animal in Africa",
"category": "ANIMALS"
}
Hybrid Search on a specific property
The properties parameter allows you to list the properties that you want bm25 to search on.
ID: d2d97c7d-4ae6-44f3-9c50-57d84ee75699
Data: {
"answer": "species",
"question": "2000 news: the Gunnison sage grouse isn't just another northern sage grouse, but a new one of this classification",
"category": "SCIENCE"
}
ID: 0b488aa7-8e47-47ee-9362-8614dbbd919f
Data: {
"answer": "the diamondback rattler",
"question": "Heaviest of all poisonous snakes is this North American rattlesnake",
"category": "ANIMALS"
}
ID: 014219e8-03d4-4218-bfc5-37e237259353
Data: {
"answer": "Antelope",
"question": "Weighing around a ton, the eland is the largest species of this animal in Africa",
"category": "ANIMALS"
}
Hybrid Search with a where filter
Find Jeopardy questions about elephants, where the category is set to Animals.
ID: 0b488aa7-8e47-47ee-9362-8614dbbd919f
Data: {
"answer": "the diamondback rattler",
"question": "Heaviest of all poisonous snakes is this North American rattlesnake",
"category": "ANIMALS"
}
ID: 014219e8-03d4-4218-bfc5-37e237259353
Data: {
"answer": "Antelope",
"question": "Weighing around a ton, the eland is the largest species of this animal in Africa",
"category": "ANIMALS"
}
ID: 7ce9505e-d0bf-4620-981a-63811f3320a3
Data: {
"answer": "the nose or snout",
"question": "The gavial looks very much like a crocodile except for this bodily feature",
"category": "ANIMALS"
}
Hybrid Search with a custom vector
You can pass in your own vector as input into the hybrid query, by using the vector parameter.