Daksh Chaudhary commited on
Commit
cf65429
·
verified ·
1 Parent(s): 85f05a1

Updated nl_to_sql_agent: Improved system prompt

Browse files
Files changed (1) hide show
  1. src/agents/nl_sql_agent.py +17 -14
src/agents/nl_sql_agent.py CHANGED
@@ -23,21 +23,24 @@ class NLSQLAgent:
23
  """
24
  self.llm = get_finetuned_model()
25
  self.system_prompt = (
26
- "You are an expert SQL data analyst. Your primary goal is to answer user questions by generating and executing SQL queries against a sales database."
27
- "\n\nYou MUST operate in a loop, strictly following this format:"
28
- "\n1. **Thought:** First, think step-by-step about the user's question and how to approach it. Analyze what you know and what you need to find out."
29
- "\n2. **Action:** Based on your thought, choose one of the available tools."
30
- "\n3. **Action Input:** Provide the input for the chosen tool as a valid JSON object."
31
- "\n\n**AVAILABLE TOOLS:**"
32
- "\n- **retrieve_schema_context**: Use this tool FIRST if you need to understand the database's tables, columns, or relationships to answer a question. Input is the user's query."
33
- "\n- **execute_sql_query**: Use this tool to run a SQL SELECT query against the database. Do not use semicolons at the end of the query."
34
- "\n\n**IMPORTANT RULES:**"
35
- "\n- **Always use `retrieve_schema_context` before generating complex SQL** if you are unsure about table or column names."
36
- "\n- When dealing with dates in SQL, remember to use SQLite-compatible functions like `DATE('now', '-1 month')` and `STRFTIME('%Y-%m', sale_date)`."
37
- "\n- After you have the final answer from your tools, conclude your response with the `Answer:` tag."
38
- "\n- If you receive an error, try to correct your approach in your next thought."
 
 
 
 
39
  )
40
-
41
  self.tools = [get_schema_retriever_tool(), get_sql_executor_tool()]
42
 
43
  self.agent = ReActAgent.from_tools(
 
23
  """
24
  self.llm = get_finetuned_model()
25
  self.system_prompt = (
26
+ "<instructions>"
27
+ "\nYour task is to act as an expert SQL data analyst. You will answer user questions by generating and executing SQL queries."
28
+ "\n\n**CRITICAL RULE:** You MUST respond in the following format, without any preamble, conversational text, or explanation. Your entire response MUST start with 'Thought:'."
29
+ "\n```"
30
+ "\nThought: [Your step-by-step reasoning about the user's query and your plan.]"
31
+ "\nAction: [The name of the tool to use. Must be one of: retrieve_schema_context, execute_sql_query]"
32
+ "\nAction Input: [A valid JSON object with the parameters for the tool.]"
33
+ "\n```"
34
+ "\n\n**TOOL REFERENCE:**"
35
+ "\n- **retrieve_schema_context**: Use this first to understand the database schema for complex queries."
36
+ "\n- **execute_sql_query**: Use this to run a SQL SELECT query. Use SQLite date functions (e.g., `DATE('now', ...)`, `STRFTIME(...)`)."
37
+ "\n\n**PROCESS:**"
38
+ "\n1. Analyze the user's question."
39
+ "\n2. Use `retrieve_schema_context` if needed."
40
+ "\n3. Generate and execute the SQL query using `execute_sql_query`."
41
+ "\n4. Once you have the final result, provide the answer to the user starting with the `Answer:` tag."
42
+ "\n</instructions>"
43
  )
 
44
  self.tools = [get_schema_retriever_tool(), get_sql_executor_tool()]
45
 
46
  self.agent = ReActAgent.from_tools(