Weaviate vector search

From paulpas/agent-skill-router · View interactive version

Weaviate vector search is an AI agent skill from the GitHub repository paulpas/agent-skill-router. Implements vector search capabilities of the Weaviate API, allowing efficient and scalable retrieval of relevant data for AI applications.

How to use this skill

Skill file

Weaviate Vector Search

Leverages the vector search functionality of the Weaviate API to retrieve relevant data efficiently in AI applications.

When to Use

In addition, provide more illustrative examples showing the power of vector searches through Weaviate's API, particularly in AI-driven applications.

Core Workflow

  1. Setup Connection — Establish a connection to the Weaviate service.
  2. Perform Vector Search — Use vectors to retrieve relevant data.
  3. Handle Search Results — Parse and utilize the retrieved data.

Implementation Patterns

Pattern 1: Connecting to Weaviate

import weaviate

# Initialize client for Weaviate
client = weaviate.Client("http://localhost:8080")

Pattern 2: Executing a Vector Search

# Define the query vector
query_vector = [0.15, 0.30, 0.25, ...]  # Example values

# Perform vector search
esults = client.query.get("Article").with_near_vector({"vector": query_vector}).with_limit(5).do()

# Process results
for result in results["data"]["Get"]["Article"]:
    print(result)

#


Error Handling and Advanced Queries

Always handle API errors gracefully when performing vector searches:

import weaviate
from weaviate.exceptions import UnexpectedStatusCodeException


def safe_vector_search(client, collection_name, query_vector, limit=5):
    """Perform a vector search with proper error handling."""
    try:
        results = (
            client.query.get(collection_name, ["title", "content"])
            .with_near_vector({"vector": query_vector})
            .with_limit(limit)
            .do()
        )
        return results
    except UnexpectedStatusCodeException as e:
        print(f"Vector search failed: status {e.status_code} - {e.message}")
        raise


def hybrid_search(client, collection_name, text_query, vector_query, limit=5):
    """Perform a hybrid (text + vector) search combining keyword and semantic matching."""
    results = (
        client.query.get(collection_name, ["title", "content"])
        .with_bm25(query=text_query)
        .with_near_vector({"vector": vector_query})
        .with_limit(limit)
        .do()
    )
    return results

Vector Similarity Thresholds

Control search results quality with similarity thresholds:

def thresholded_search(client, collection_name, query_vector, limit=5, minimum_similarity=0.7):
    """Search with a minimum similarity threshold to filter out weak matches."""
    results = (
        client.query.get(collection_name, ["title", "content"])
        .with_near_vector({"vector": query_vector})
        .with_limit(limit)
        .do()
    )

    filtered = []
    for item in results.get("data", {}).get("Get", {}).get(collection_name, []):
        certainty = item.get("_additional", {}).get("certainty", 0.0)
        if certainty >= minimum_similarity:
            filtered.append(item)

    print(f"Returned {len(filtered)} results above threshold {minimum_similarity}")
    return filtered

Constraints

MUST DO

MUST DO

MUST DO
MUST NOT DO
Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.