import cohere
from lambdadb import LambdaDB
class WikipediaSearchExamples:
def __init__(self, cohere_api_key, lambdadb_api_key):
"""Initialize the search examples with API credentials"""
self.co = cohere.Client(cohere_api_key)
self.lambda_db = LambdaDB(
server_url="<PLAYGROUND_PROJECT_URL>",
project_api_key=lambdadb_api_key
)
self.collection_name = "cohere-wikipedia-en-100k"
def generate_embedding(self, text):
"""Generate embedding for search queries"""
try:
response = self.co.embed(
texts=[text],
model="embed-multilingual-v3.0",
input_type="search_query"
)
return response.embeddings[0]
except Exception as e:
print(f"Error generating embedding: {e}")
return None
def run_hybrid_search(self):
"""Example 1: Hybrid Search with RRF"""
user_query = "When is Taylor Lautner, who appeared in Twilight,'s birthday?"
query_vector = self.generate_embedding(user_query)
if not query_vector:
return None
query = {
"rrf": [
{
"queryString": {
"query": user_query,
"defaultField": "text",
"skipSyntax": True
}
},
{
"knn": {
"field": "vector",
"k": 5,
"queryVector": query_vector
}
}
]
}
return self.lambda_db.collections.query(
collection_name=self.collection_name,
size=3,
query=query
)
def run_keyword_filtering(self):
"""Example 2: Keyword Filtering with Vector Search"""
user_query = "I want to know the list of global movie theater chains."
query_vector = self.generate_embedding(user_query)
if not query_vector:
return None
query = {
"rrf": [
{
"bool": [
{
"queryString": {
"query": user_query,
"defaultField": "text",
"skipSyntax": True
},
"occur": "should"
},
{
"queryString": {
"query": "List*",
"defaultField": "title" },
"occur": "must"
}
]
},
{
"knn": {
"field": "vector",
"k": 5,
"queryVector": query_vector
}
}
]
}
return self.lambda_db.collections.query(
collection_name=self.collection_name,
size=3,
query=query
)
def run_exact_match(self):
"""Example 3: Exact Match Filtering with Vector Search"""
target_url = "https://en.wikipedia.org/wiki/The%20Top%20Ten%20Club"
user_query = "How many times did The Beatles perform at the Top Ten Club?"
query_vector = self.generate_embedding(user_query)
if not query_vector:
return None
query = {
"knn": {
"filter": {
"queryString": {
"query": target_url,
"defaultField": "url",
"skipSyntax": True
},
"occur": "must"
},
"field": "vector",
"k": 5,
"queryVector": query_vector
}
}
return self.lambda_db.collections.query(
collection_name=self.collection_name,
size=3,
query=query
)
def display_results(self, results, title, description=""):
"""Display search results in a consistent format"""
if not results:
print(f"β Failed to get results for {title}")
return
print(f"\n{'='*60}")
print(f"π {title}")
if description:
print(f"π {description}")
print(f"π Found {len(results.docs)} results")
print(f"{'='*60}")
for i, result in enumerate(results.docs, 1):
print(f"\n--- Result {i} ---")
print(f"Title: {result.doc['title']}")
print(f"URL: {result.doc['url']}")
print(f"Score: {result.score}")
print(f"Text Preview: {result.doc['text'][:200]}...")
print("-" * 50)
def run_all_examples(self):
"""Run all three search examples"""
print("π Running LambdaDB Playground Search Examples")
print("=" * 60)
# Example 1: Hybrid Search
hybrid_results = self.run_hybrid_search()
self.display_results(
hybrid_results,
"Hybrid Search with RRF",
"Combines keyword and semantic search for comprehensive results"
)
# Example 2: Keyword Filtering
filtered_results = self.run_keyword_filtering()
self.display_results(
filtered_results,
"Keyword Filtering + Vector Search",
"Filters by document type, then ranks semantically"
)
# Example 3: Exact Match
exact_results = self.run_exact_match()
self.display_results(
exact_results,
"Exact Match Filtering + Vector Search",
"Searches within a specific document using semantic ranking"
)
return hybrid_results, filtered_results, exact_results
# Usage Example
if __name__ == "__main__":
# Initialize with your API keys
examples = WikipediaSearchExamples(
cohere_api_key="YOUR_COHERE_API_KEY",
lambdadb_api_key="YOUR_LAMBDADB_API_KEY"
)
# Run all examples
try:
results = examples.run_all_examples()
print("\nβ
All examples completed successfully!")
except Exception as e:
print(f"β Error running examples: {e}")