agents.rag.db_rag.sql_rag.prompts¶
Prompt templates for SQL RAG Agent.
This module contains all the prompt templates used throughout the SQL RAG workflow. Each prompt is carefully crafted to guide the LLM through specific tasks while maintaining consistency and accuracy.
The prompts follow a structured format with clear system instructions and user templates that include necessary context variables.
Example
Using prompts with LangChain:
>>> from langchain_core.prompts import ChatPromptTemplate
>>> from haive.agents.rag.db_rag.sql_rag.prompts import GENERATE_SQL_PROMPT
>>>
>>> # Format prompt with variables
>>> messages = GENERATE_SQL_PROMPT.format_messages(
... schema=db_schema,
... dialect="postgresql",
... question="Show top products",
... query_analysis=analysis_result
... )
>>>
>>> # Use with LLM
>>> llm = ChatOpenAI()
>>> response = llm.invoke(messages)
Attributes¶
Prompt for analyzing natural language queries. |
|
Prompt for grading answer relevance. |
|
Prompt for generating natural language answers. |
|
Prompt for generating SQL queries. |
|
Prompt for domain relevance checking (guardrails). |
|
Prompt for detecting hallucinations in answers. |
|
Prompt for validating SQL queries. |
Module Contents¶
- agents.rag.db_rag.sql_rag.prompts.ANALYZE_QUERY_PROMPT¶
Prompt for analyzing natural language queries.
This prompt guides the LLM to break down user questions into SQL components without generating the actual query. It helps identify: - Required tables and columns - JOIN operations needed - WHERE clause constraints - Aggregation functions - Query complexity
- Variables:
schema: Database schema information dialect: SQL dialect (postgresql, mysql, etc.) question: User’s natural language question
- Returns:
SQLAnalysisOutput with structured analysis
- agents.rag.db_rag.sql_rag.prompts.ANSWER_GRADING_PROMPT¶
Prompt for grading answer relevance.
This prompt evaluates whether the generated answer actually addresses what the user asked, ensuring quality responses.
- Variables:
question: Original question answer: Generated answer
- Returns:
GradeAnswer with binary score
- agents.rag.db_rag.sql_rag.prompts.GENERATE_FINAL_ANSWER_PROMPT¶
Prompt for generating natural language answers.
This prompt converts raw SQL query results into user-friendly answers that directly address the original question.
- Variables:
question: Original user question sql_query: Executed SQL query query_result: Raw query results
- Returns:
String with natural language answer
- agents.rag.db_rag.sql_rag.prompts.GENERATE_SQL_PROMPT¶
Prompt for generating SQL queries.
This prompt converts natural language questions into executable SQL queries based on the analysis and schema information. It ensures: - Correct syntax for the specific dialect - Safe queries (SELECT only) - Proper use of JOINs and aggregations - Result limiting for safety - Meaningful result ordering
- Variables:
schema: Database schema information dialect: SQL dialect question: User’s question query_analysis: Analysis from previous step
- Returns:
SQLQueryOutput with generated query
- agents.rag.db_rag.sql_rag.prompts.GUARDRAILS_PROMPT¶
Prompt for domain relevance checking (guardrails).
This prompt filters out irrelevant questions that aren’t about database queries, preventing the agent from attempting to answer questions outside its scope.
- Variables:
schema: Database schema tables: List of table names question: User’s question
- Returns:
GuardrailsOutput with decision and reason
- agents.rag.db_rag.sql_rag.prompts.HALLUCINATION_CHECK_PROMPT¶
Prompt for detecting hallucinations in answers.
This prompt ensures answers are grounded in actual query results and don’t include fabricated information.
- Variables:
question: Original question query_result: Actual data returned answer: Generated answer to check
- Returns:
GradeHallucinations with binary score
- agents.rag.db_rag.sql_rag.prompts.VALIDATE_SQL_PROMPT¶
Prompt for validating SQL queries.
This prompt performs comprehensive validation including: - Syntax correctness - Schema compliance - JOIN validity - Aggregation rules - Performance considerations - Security checks
- Variables:
schema: Database schema dialect: SQL dialect question: Original question sql_query: Query to validate
- Returns:
SQLValidationOutput with errors and suggestions