agentcourse / CSV_SEARCH_IMPLEMENTATION.md
chimestream's picture
Enhanced agent with CSV knowledge base search priority - Added CSV search tool with TF-IDF semantic search, modified workflow to prioritize CSV before other tools, updated system prompt, added scikit-learn dependency, created documentation. Benefits: faster responses, consistent answers, reduced API calls, better accuracy.
e951f82
|
Raw History Blame Contribute Delete
3.56 kB

A newer version of the Gradio SDK is available: 6.30.0

Upgrade

CSV Knowledge Base Search Implementation

Overview

The agent has been enhanced to prioritize searching a CSV knowledge base before using other tools. This ensures faster, more consistent responses for questions that have been previously answered.

Key Changes Made

1. New CSV Search Tool (agent.py)

  • Function: search_csv_knowledge_base(query: str, top_k: int = 3)
  • Technology: Uses TF-IDF vectorization and cosine similarity for semantic search
  • Features:
    • Searches through questions and answers in supabase_docs.csv
    • Returns top-k most similar results with similarity scores
    • Extracts questions and answers from the content format
    • Filters results by minimum similarity threshold (0.1)

2. Modified Agent Workflow (agent.py)

  • Priority System: CSV search is now the first tool used for any question
  • Enhanced Assistant Function:
    • Automatically searches CSV before other tools
    • Uses CSV results as foundation for answers
    • Falls back to other tools if no relevant CSV results found
  • Tool Order: CSV search tool is placed first in the tools list

3. Updated System Prompt (system_prompt.txt)

  • New Instructions: Always start with CSV knowledge base search
  • Clear Priority: CSV search is explicitly marked as first priority
  • Workflow: Updated reasoning process to include CSV search as step 1

4. Dependencies (requirements.txt)

  • Added: scikit-learn for TF-IDF vectorization and similarity calculations

CSV File Structure

The CSV contains:

  • content: Questions and answers in format "Question : [question text] Final answer : [answer]"
  • metadata: Task metadata (not used in search)
  • embedding: Vector embeddings (not used in current implementation)

How It Works

1. User asks a question

2. Agent automatically searches CSV knowledge base first

csv_result = search_csv_knowledge_base.invoke({
    'query': user_question, 
    'top_k': 3
})

3. If relevant results found:

  • Uses CSV information as foundation
  • May use additional tools for verification/supplementation
  • Provides comprehensive answer

4. If no relevant results found:

  • Proceeds with normal tool usage (web search, calculations, etc.)

Benefits

βœ… Faster Responses: No need to search external APIs for known questions βœ… Consistency: Same questions get same answers based on previous knowledge βœ… Cost Efficiency: Reduced API calls to external services βœ… Accuracy: Domain-specific questions answered from verified knowledge base βœ… Fallback: Still uses other tools when CSV doesn't have relevant information

Testing

Test Scripts Created:

  1. test_csv_search.py: Tests CSV search functionality with various queries
  2. demo_agent_workflow.py: Demonstrates complete agent workflow with CSV priority

Example Results:

  • Query: "Finding Nemo fish species invasive" β†’ Found relevant answer about clownfish (34689)
  • Query: "AI regulation arXiv June 2022" β†’ Found answer about "egalitarian"
  • Query: "What is the capital of France?" β†’ No results, would use other tools

Usage

The agent now automatically prioritizes CSV search. No changes needed in how you interact with it - just ask questions and the agent will:

  1. Search CSV first
  2. Use results if found
  3. Fall back to other tools if needed
  4. Provide comprehensive answers

This implementation ensures the agent leverages existing knowledge efficiently while maintaining the ability to research new information when needed.