Skip to content

Latest commit

 

History

History
885 lines (678 loc) · 25.9 KB

search_troubleshooting.md

File metadata and controls

885 lines (678 loc) · 25.9 KB

Search Troubleshooting

This document covers common issues, debugging techniques, and best practices for the SignalWire Agents SDK search system. For an overview of search concepts, see search_overview.md. For indexing configuration, see search_indexing.md.

Common Issues

No Results Returned

Symptoms:

The search returns empty results for queries that should match content in the index.

Query: "how to authenticate"
Results: []

Diagnosis:

  1. Verify the index exists and has content:

    ls -lh knowledge.swsearch
    sw-search validate ./knowledge.swsearch
  2. Test the query directly via CLI:

    sw-search search ./knowledge.swsearch "how to authenticate" --verbose

    Examine the similarity scores in verbose output.

  3. Check the similarity threshold:

{
    "similarity_threshold": 0.7  # Very strict -- may filter out valid results
}

Common Causes and Solutions:

  1. Similarity threshold too strict: Lower the threshold to allow weaker but valid matches:

    # Problem
    {"similarity_threshold": 0.7}  # Only near-perfect matches
    
    # Solution
    {"similarity_threshold": 0.4}  # More permissive
  2. Content does not exist in the index: Verify and add missing content:

    sw-search search ./knowledge.swsearch "authentication" --verbose --count 10
    # If no content about authentication exists, rebuild with additional sources
    sw-search ./auth-docs --output updated.swsearch
  3. Query phrasing does not match content semantically: Try synonym variations:

    # "how to authenticate" may not match content phrased as "bearer token usage"
    sw-search search ./knowledge.swsearch "bearer token" --verbose
  4. Model mismatch between index build and query: The embedding model used at query time must match the model used when building the index:

    # Built with base model
    # sw-search ./docs --model base --output docs.swsearch
    
    # Query must also use base model
    {"model_name": "base"}  # Must match the build model

Fallback strategy: Try progressively lower thresholds:

def search_with_fallback(self, query):
    """Try multiple thresholds"""
    for threshold in [0.5, 0.4, 0.3]:
        results = self.search(query, threshold=threshold)
        if results:
            return results
    return []

Irrelevant Results

Symptoms:

Results are returned but do not match the query intent.

Query: "Python authentication examples"
Results:
1. "Ruby authentication guide" (similarity: 0.65)
2. "General API overview" (similarity: 0.58)
3. "Installation instructions" (similarity: 0.52)

Diagnosis:

  1. Check similarity scores:

    sw-search search ./knowledge.swsearch "Python authentication examples" --verbose

    Scores below 0.5 indicate weak matches.

  2. Examine the returned content:

    sw-search search ./knowledge.swsearch "Python authentication examples" --verbose --count 10
  3. Check chunking quality by reading the index directly. A .swsearch file is a SQLite database, so its chunks are queryable with sqlite3:

    sqlite3 ./knowledge.swsearch "SELECT filename, section, content FROM chunks"
    # Review chunk boundaries -- are they mixing topics?

Common Causes and Solutions:

  1. Threshold too permissive: Raise the threshold to reject weak matches:

    # Problem
    {"similarity_threshold": 0.2}  # Accepts weak matches
    
    # Solution
    {"similarity_threshold": 0.4}
  2. Poor chunking strategy: Use a strategy that matches your content type:

    # Problem: sentence strategy splits code blocks
    sw-search ./docs --chunking-strategy sentence
    
    # Solution: markdown strategy preserves code blocks
    sw-search ./docs --chunking-strategy markdown

    See search_indexing.md for details on chunking strategies.

  3. Missing metadata and tags: Add tags during indexing to improve hybrid scoring:

    sw-search ./docs --tags python,authentication,examples --output docs.swsearch
  4. Content lacks specificity: If generic content dominates, add more specific documents covering the exact topics users ask about.

Tag-based filtering:

self.add_skill("native_vector_search", {
    "tool_name": "search_python_examples",
    "description": "Search for Python code examples",
    "index_file": "./docs.swsearch",
    "tags": ["python", "code"],
    "similarity_threshold": 0.4
})

Poor Ranking Quality

Symptoms:

The best result appears lower in the list instead of first.

Query: "voice configuration"
Results:
1. "General configuration overview" (similarity: 0.72)
2. "Database configuration" (similarity: 0.68)
3. "Perfect voice config guide" (similarity: 0.85)  # Should be #1

Diagnosis:

  1. Check hybrid scoring via verbose output:

    sw-search search ./knowledge.swsearch "voice configuration" --verbose

    Determine whether the result with higher vector similarity has a lower hybrid score.

  2. Examine metadata on each result:

    sqlite3 ./knowledge.swsearch "SELECT filename, section, tags, metadata FROM chunks"
    # Check if the best result lacks metadata that other results have

Common Causes and Solutions:

  1. Missing metadata on the best result: Results with tags receive keyword boosts in hybrid scoring. Re-export the source documents to JSON, add tags, and rebuild:

    sw-search ./docs --output-format json --output chunks.json
    # Edit chunks.json to add metadata:
    # {"content": "Voice configuration guide...", "metadata": {"tags": ["voice", "configuration", "guide"]}}
    sw-search ./chunks.json --chunking-strategy json --output fixed.swsearch
  2. Keyword matches boosting wrong results: A result containing both query keywords (e.g., "Configuration for voice, database, and API...") may receive a keyword boost even though it is less relevant than a focused document.

  3. Increase result count: Returning more results allows the LLM to select the most relevant one:

{"count": 5}
  1. Custom reranking via formatter callback:
def _format_with_reranking(self, response, agent, query, results, **kwargs):
    """Rerank based on custom logic"""
    if "example" in query.lower():
        results.sort(key=lambda r: r.get('metadata', {}).get('has_code', False), reverse=True)
    return self._build_response(results)

Content Truncation

Symptoms:

Search results appear cut off mid-sentence or are incomplete.

Diagnosis:

  1. Check the content length budget:
{"max_content_length": 16384}  # May be too small for the number of results
  1. Examine individual chunk sizes:

    sqlite3 ./knowledge.swsearch "SELECT filename, LENGTH(content) FROM chunks ORDER BY LENGTH(content) DESC LIMIT 10"
    # Check for very long chunks
  2. Check result count relative to budget:

{
    "count": 10,
    "max_content_length": 16384  # Only ~1.6KB per result
}

Common Causes and Solutions:

  1. max_content_length too low: Increase the budget:
{"max_content_length": 32768}  # 32KB (default)
  1. Individual chunks too long: Rechunk with a strategy that produces smaller pieces, for example by lowering the sentence count per chunk:

    sw-search ./docs \
      --chunking-strategy sentence \
      --max-sentences-per-chunk 3 \
      --output docs_small_chunks.swsearch
  2. Too many results for the budget: Reduce the result count so each result gets more space:

{
    "count": 3,
    "max_content_length": 16384
}

Function Not Being Called by AI

Symptoms:

The agent does not invoke the search function even when the user asks questions that should trigger a search.

Diagnosis:

  1. Verify the function is registered:

    swaig-test agent.py --list-tools
    # Should list search_docs or the configured tool_name
  2. Test the function directly:

    swaig-test agent.py --exec search_docs --query "test"
  3. Check the function description. A vague description makes it less likely the LLM will invoke it.

Common Causes and Solutions:

  1. Vague function description: Provide a specific, detailed description:

    # Problem
    {"description": "Search docs"}
    
    # Solution
    self.add_skill("native_vector_search", {
        "tool_name": "search_product_docs",
        "description": (
            "Search comprehensive product documentation. "
            "Use this for ANY question about: features, configuration, "
            "API usage, troubleshooting, code examples, or technical details. "
            "This is your primary source of truth."
        ),
        "index_file": "./docs.swsearch"
    })
  2. No prompt instructions telling the agent to search: Add explicit guidance:

self.prompt_add_section(
    "Search First Policy",
    bullets=[
        "Before answering ANY technical question, search the knowledge base",
        "Use search_product_docs for product-related questions",
        "Never guess or use general knowledge - always search first",
        "If search returns no results, tell user you don't have that information"
    ]
)
  1. LLM believes it already knows the answer: Strengthen the prompt:
"ALWAYS search before answering, even if you think you know the answer"

See search_integration.md for full details on agent configuration.


Model Loading Errors

Symptoms:

Error loading model: sentence-transformers/all-MiniLM-L6-v2

Diagnosis:

  1. Check that search dependencies are installed:

    pip list | grep sentence-transformers
  2. Check the model cache:

    ls ~/.cache/huggingface/hub/
  3. Check network access to Hugging Face:

    curl -I https://huggingface.co

Common Causes and Solutions:

  1. Search dependencies not installed: Install the appropriate search extras:

    # Base install does not include search
    pip install signalwire-sdk[search]
  2. Network issues preventing model download on first run: Pre-download the model:

    python -c "from sentence_transformers import SentenceTransformer; SentenceTransformer('all-MiniLM-L6-v2')"
  3. Insufficient disk space: Models require approximately 400MB:

    df -h ~/.cache
    # Clean if needed
    rm -rf ~/.cache/huggingface/hub/

Pre-download in Docker builds:

RUN pip install signalwire-sdk[search] && \
    python -c "from sentence_transformers import SentenceTransformer; SentenceTransformer('all-MiniLM-L6-v2')"

Offline mode: If the model is already cached:

export TRANSFORMERS_OFFLINE=1

Query-only mode: If you only need to query existing indexes and do not need the full model:

pip install signalwire-sdk[search-queryonly]

"Illegal Instruction" Error (CPU Compatibility)

Symptoms:

When running sw-search with embedding generation, the process crashes with an "Illegal instruction" error.

Root Cause:

This occurs when PyTorch was compiled with newer CPU instruction sets (such as AVX2 or AVX-512) that are not supported by the host CPU. This is common on older server hardware or certain virtualized environments.

Diagnosis:

  1. Check CPU capabilities:

    cat /proc/cpuinfo | grep -E "(model name|flags)" | head -5

    Look for avx2 and avx512f in the flags. If absent, the CPU does not support these instruction sets.

  2. Check PyTorch version:

    python -c "import torch; print('PyTorch version:', torch.__version__)"
  3. Test model loading to reproduce the error:

    python -c "from sentence_transformers import SentenceTransformer; model = SentenceTransformer('sentence-transformers/all-mpnet-base-v2')"

Solution:

PyTorch chooses many of its CPU kernels at runtime, based on what the processor supports. First, force the most basic kernels with ATEN_CPU_CAPABILITY and run the build again:

ATEN_CPU_CAPABILITY=default sw-search ./docs --output index.swsearch

If it still crashes, the installed binary itself uses instructions the CPU lacks. Install a PyTorch build that supports this CPU and operating system, using the options on the PyTorch installation page. The CPU-only wheels are a common choice, but they don't support every older processor:

pip uninstall torch
pip install torch --index-url https://download.pytorch.org/whl/cpu

Alternative:

Run sw-search on hardware that supports AVX2 and AVX-512, or inside a container built for the host's actual CPU capabilities.


pgvector Connection Issues

Symptoms:

Error: could not connect to PostgreSQL server

Or the extension itself may be missing:

vector type not found in the database

Diagnosis:

  1. Test the connection directly:

    psql "$PGVECTOR_CONNECTION"
  2. Check the connection string format:

    # Required format: postgresql://user:pass@host:port/db
    echo "$PGVECTOR_CONNECTION"
  3. Check whether the pgvector extension is installed:

    SELECT * FROM pg_extension WHERE extname = 'vector';
  4. Check whether PostgreSQL is running:

    sudo systemctl status postgresql
    sudo systemctl start postgresql  # Start if needed

Common Causes and Solutions:

  1. Incomplete connection string:

    # Problem: missing components
    "postgresql://localhost/db"
    
    # Solution: include all components
    "postgresql://user:pass@localhost:5432/db"
  2. pgvector extension not installed:

    # Install the extension package (PostgreSQL 16 example)
    sudo apt-get install postgresql-16-pgvector

    Then enable it inside the target database:

    -- Enable in the database (as superuser)
    CREATE EXTENSION vector;
    
    -- Verify
    \dx

    Alternatively, use the Docker image with pgvector pre-installed:

    docker run -d \
      --name postgres-pgvector \
      -e POSTGRES_USER=signalwire \
      -e POSTGRES_PASSWORD=signalwire123 \
      -e POSTGRES_DB=knowledge \
      -p 5432:5432 \
      pgvector/pgvector:pg16
  3. Database does not exist:

    createdb -U postgres knowledge
  4. Network or firewall blocking the connection:

    telnet postgres-host 5432
  5. Authentication failure: Verify credentials and check pg_hba.conf for allowed connection methods.

  6. Docker container cannot reach host PostgreSQL:

    # Linux: use host networking
    docker run --network host ...
    
    # macOS/Windows: use host.docker.internal
    # postgresql://user:pass@host.docker.internal:5432/knowledge
    
    # Or use a Docker network
    docker network create myapp
    docker run -d --name postgres --network myapp pgvector/pgvector:pg16
    docker run --network myapp -e DB_HOST=postgres ...

Validate connection string programmatically:

import os
conn = os.getenv("PGVECTOR_CONNECTION")
assert conn.startswith("postgresql://"), "Invalid connection string"
assert "@" in conn, "Missing credentials"
assert ":" in conn.split("@")[1], "Missing port"

Test connection:

from sqlalchemy import create_engine

engine = create_engine(os.getenv("PGVECTOR_CONNECTION"))
with engine.connect() as conn:
    result = conn.execute("SELECT 1")
    print("Connected successfully")

See search_deployment.md for full pgvector deployment guidance.


Slow Queries

Symptoms:

Search queries take much longer than expected.

Diagnosis:

  1. Profile query time:

    time sw-search search ./knowledge.swsearch "test query"
  2. Check index size:

    ls -lh knowledge.swsearch
    
    # For pgvector
    # SELECT count(*) FROM knowledge_chunks;
  3. Check which model is being used:

{"model_name": "base"}  # base is slower than mini

Common Causes and Solutions:

  1. Using the base model: Switch to the mini model for faster queries with minimal quality loss:
{"model_name": "mini"}
  1. No vector index on pgvector: Create an IVFFlat index:

    -- For small datasets (< 100k rows)
    CREATE INDEX ON knowledge_chunks USING ivfflat (embedding vector_cosine_ops)
      WITH (lists = 100);
    
    -- For larger datasets
    CREATE INDEX ON knowledge_chunks USING ivfflat (embedding vector_cosine_ops)
      WITH (lists = 1000);
  2. Too many results requested: Reduce the result count:

{"count": 3}  # Faster than requesting 10+
  1. Remote pgvector with high network latency: For single-agent deployments, consider using a local SQLite index instead:
{"index_file": "./local.swsearch"}
  1. PostgreSQL not tuned for vector operations:

    SET work_mem = '256MB';
    -- Or globally:
    ALTER SYSTEM SET work_mem = '256MB';
  2. Analyze query plans to identify bottlenecks:

    EXPLAIN (ANALYZE, BUFFERS)
    SELECT * FROM your_collection
    ORDER BY embedding <=> '[...]'::vector
    LIMIT 5;

Connection pool exhaustion (multi-agent deployments): use pgbouncer for connection pooling:

# /etc/pgbouncer/pgbouncer.ini
[databases]
knowledge = host=localhost port=5432 dbname=knowledge

[pgbouncer]
pool_mode = transaction
max_client_conn = 1000
default_pool_size = 25

Point agents at pgbouncer instead of PostgreSQL directly:

# Connect through pgbouncer (default port 6432)
# postgresql://user:pass@localhost:6432/knowledge

Debugging

Debug Mode

Enable verbose logging to see detailed search internals:

import os
os.environ['SIGNALWIRE_LOG_LEVEL'] = 'DEBUG'

This exposes similarity scores, hybrid scoring details, and timing information in the agent logs.

Inspecting Chunks

A .swsearch file is a SQLite database, so query it directly to inspect what was indexed:

sqlite3 ./knowledge.swsearch "SELECT filename, section, content FROM chunks"

Review the output to verify:

  • Chunk boundaries are sensible and do not split related content
  • Metadata and tags are present on chunks that need them
  • Chunk sizes are appropriate (not too large, not too small)

Checking Embeddings

Verify that the embedding model loads correctly and produces vectors:

python -c "from sentence_transformers import SentenceTransformer; m = SentenceTransformer('all-MiniLM-L6-v2'); print(m.encode('test').shape)"

If this fails, see the earlier Model Loading Errors and "Illegal Instruction" Error sections.

Score Analysis

Use verbose CLI output to analyze similarity scores:

sw-search search ./knowledge.swsearch "your query" --verbose --count 10

Interpret the scores:

  • 0.7 and above: Strong semantic match
  • 0.5 to 0.7: Moderate match, usually relevant
  • 0.3 to 0.5: Weak match, may or may not be relevant
  • Below 0.3: Poor match, likely irrelevant

A typical --similarity-threshold of 0.4 provides a good balance between recall and precision.

Verbose CLI Output

The --verbose flag on sw-search search provides:

  • Similarity scores for each result
  • The NLP backend and embedding model used for the query
  • How many chunks and files the loaded index contains

For example:

sw-search search ./knowledge.swsearch "query" --verbose

Debugging Checklist

When troubleshooting search issues, work through this checklist in order:

  • Verify the index file exists and passes validation (sw-search validate)
  • Test the query with the CLI tool (sw-search search --verbose)
  • Check the distance threshold (0.4 is a typical starting value)
  • Verify the model matches between build and query time
  • Examine chunk quality by exporting and reviewing the index
  • Check that the function description is specific and clear
  • Verify prompt instructions tell the agent when to use search
  • Monitor search latency
  • Check for missing Python dependencies
  • Test pgvector connection if using that backend
  • Review verbose output for similarity scores
  • Verify metadata and tags are present on indexed chunks

Best Practices Summary

Index Building

Follow these practices when building an index:

  • Choose the right chunking strategy for your content type:
    • Technical docs with code: markdown
    • FAQ/support content: qa
    • General articles/blogs: paragraph
    • Academic papers: semantic
    • Mixed content: start with markdown
    • Custom needs: JSON workflow for manual curation
  • Add tags during indexing to improve hybrid search scoring:
    sw-search ./docs --tags documentation,api,code --output docs.swsearch
  • Use the mini model for most use cases. Only upgrade to base or large if quality is insufficient.
  • Validate indexes after building to catch issues early:
    sw-search validate ./knowledge.swsearch
  • Test with representative queries before deploying:
    sw-search search ./knowledge.swsearch "expected user question" --verbose

Agent Configuration

Follow these practices when configuring the skill on an agent:

  • Write specific, detailed function descriptions: The LLM uses the description to decide when to call the search function. Generic descriptions like "Search docs" lead to the function not being invoked.
  • Add explicit prompt instructions telling the agent to search before answering:
self.prompt_add_section("Instructions", bullets=[
    "Always search before answering technical questions",
    "Base answers on search results, not general knowledge",
    "If no results, say you don't have that information"
])
  • Match the model name between the index build and the agent configuration.
  • Set a balanced distance threshold: Start with 0.4 and adjust based on testing.
  • Tune result count and content length together to avoid truncation:
{
    "count": 3,
    "max_content_length": 32768
}

Production Deployment

Follow these practices when deploying to production:

  • Use search-queryonly in production to save approximately 400MB of dependencies:
    pip install signalwire-sdk[search-queryonly]
  • Use search-full for building indexes in your CI/CD pipeline or development environment.
  • Switch to pgvector for multi-agent deployments: SQLite is appropriate for single-agent setups; pgvector handles concurrent access and shared knowledge bases.
  • Pre-download models in Docker builds to avoid first-run delays:
    RUN pip install signalwire-sdk[search] && \
        python -c "from sentence_transformers import SentenceTransformer; SentenceTransformer('all-MiniLM-L6-v2')"
  • Create pgvector indexes on the embedding column for large datasets:
    CREATE INDEX ON knowledge_chunks USING ivfflat (embedding vector_cosine_ops)
      WITH (lists = 100);
  • Use connection pooling (pgbouncer) for multi-agent deployments sharing a pgvector database.
  • Monitor search performance: Track query latency, result counts, and no-result queries to identify content gaps and performance regressions.

Common Mistakes to Avoid

  1. Not telling the agent to search: Adding the search skill without prompt instructions means the LLM may never invoke it. Always pair add_skill() with prompt instructions.

  2. Using the wrong chunking strategy: Sentence-based chunking splits code blocks and destroys their meaning. Use the markdown strategy for technical documentation.

  3. Setting the distance threshold too strictly or too loosely:

    • Too strict (0.7): returns no results for valid queries
    • Too permissive (0.2): returns irrelevant results
    • Recommended starting point: 0.4
  4. Mismatched models: Building the index with one model and querying with a different model produces meaningless similarity scores. Always match the model.

  5. Ignoring metadata: Indexes built without tags lose the keyword boosting benefits of hybrid search. Add relevant tags during indexing.

  6. Deploying without testing: Always test with the CLI and swaig-test before deploying:

    sw-search search ./knowledge.swsearch "test queries"
    swaig-test agent.py --exec search_docs --query "test"

Getting Help

CLI reference:

sw-search --help
sw-search search --help
sw-search validate --help

Enable debug logging:

import os
os.environ['SIGNALWIRE_LOG_LEVEL'] = 'DEBUG'

Test with CLI:

sw-search search ./knowledge.swsearch "query" --verbose

Inspect index contents directly:

sqlite3 ./knowledge.swsearch "SELECT filename, section, content FROM chunks"

GitHub Issues: https://github.com/signalwire/signalwire-python/issues

GitHub Discussions: https://github.com/signalwire/signalwire-python/discussions