Skip to content

Commit 82ec6a9

Browse files
committed
Update prompts and payloads
1 parent c725d7e commit 82ec6a9

File tree

4 files changed

+109
-26
lines changed

4 files changed

+109
-26
lines changed

text_2_sql/autogen/src/autogen_text_2_sql/autogen_text_2_sql.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
from autogen_agentchat.messages import TextMessage
1616
import json
1717
import os
18-
from datetime import datetime
1918
import re
2019

2120
from text_2_sql_core.payloads.interaction_payloads import (
@@ -25,28 +24,33 @@
2524
ProcessingUpdatePayload,
2625
InteractionPayload,
2726
PayloadType,
27+
DEFAULT_INJECTED_PARAMETERS,
2828
)
2929
from autogen_agentchat.base import TaskResult
3030
from typing import AsyncGenerator
3131

3232

3333
class AutoGenText2Sql:
34-
def __init__(self, **kwargs: dict):
34+
def __init__(self, **kwargs):
3535
self.target_engine = os.environ["Text2Sql__DatabaseEngine"].upper()
36-
self.kwargs = kwargs
36+
37+
if "use_case" not in kwargs:
38+
logging.warning(
39+
"No use case provided. It is advised to provide a use case to help the LLM reason."
40+
)
41+
42+
self.kwargs = {**DEFAULT_INJECTED_PARAMETERS, **kwargs}
3743

3844
def get_all_agents(self):
3945
"""Get all agents for the complete flow."""
40-
# Get current datetime for the Query Rewrite Agent
41-
current_datetime = datetime.now()
4246

4347
self.user_message_rewrite_agent = LLMAgentCreator.create(
44-
"user_message_rewrite_agent", current_datetime=current_datetime
48+
"user_message_rewrite_agent", **self.kwargs
4549
)
4650

4751
self.parallel_query_solving_agent = ParallelQuerySolvingAgent(**self.kwargs)
4852

49-
self.answer_agent = LLMAgentCreator.create("answer_agent")
53+
self.answer_agent = LLMAgentCreator.create("answer_agent", **self.kwargs)
5054

5155
agents = [
5256
self.user_message_rewrite_agent,

text_2_sql/text_2_sql_core/src/text_2_sql_core/payloads/interaction_payloads.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@
77
from datetime import datetime, timezone
88
from uuid import uuid4
99

10+
DEFAULT_INJECTED_PARAMETERS = {
11+
"date": datetime.now().strftime("%d/%m/%Y"),
12+
"time": datetime.now().strftime("%H:%M:%S"),
13+
"datetime": datetime.now().strftime("%d/%m/%Y, %H:%M:%S"),
14+
"unix_timestamp": int(datetime.now().timestamp()),
15+
}
16+
1017

1118
class PayloadSource(StrEnum):
1219
USER = "user"
@@ -123,12 +130,6 @@ class Body(InteractionPayloadBase):
123130

124131
@model_validator(mode="before")
125132
def add_defaults(cls, values):
126-
defaults = {
127-
"date": datetime.now().strftime("%d/%m/%Y"),
128-
"time": datetime.now().strftime("%H:%M:%S"),
129-
"datetime": datetime.now().strftime("%d/%m/%Y, %H:%M:%S"),
130-
"unix_timestamp": int(datetime.now().timestamp()),
131-
}
132133
injected = values.get("injected_parameters", None)
133134

134135
if injected is None:
@@ -137,7 +138,10 @@ def add_defaults(cls, values):
137138
injected_by_alias = injected
138139
del values["injected_parameters"]
139140

140-
values["injectedParameters"] = {**defaults, **injected_by_alias}
141+
values["injectedParameters"] = {
142+
**DEFAULT_INJECTED_PARAMETERS,
143+
**injected_by_alias,
144+
}
141145
return values
142146

143147
payload_type: Literal[PayloadType.USER_MESSAGE] = Field(

text_2_sql/text_2_sql_core/src/text_2_sql_core/prompts/disambiguation_and_sql_query_generation_agent.yaml

Lines changed: 86 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ system_message:
77
You are a helpful AI Assistant specializing in disambiguating questions about {{ use_case }} and mapping them to the relevant columns and schemas in the database.
88
Your job is to create clear mappings between the user's intent and the available database schema.
99
If all mappings are clear, generate {{ target_engine }} compliant SQL query based on the mappings.
10-
If the mappings are ambiguous or there are no possible schemas, request disambiguation from the user by asking them to rephrase the question or to answer your own question.
10+
If the mappings are ambiguous or there are no possible schemas, follow the disambiguation rules to request more information from the user.
1111
</role_and_objective>
1212
1313
<key_concepts>
@@ -150,15 +150,92 @@ system_message:
150150
Remember: Focus on correctness first, then optimize if needed.
151151
</sql_query_generation_rules>
152152
153-
<dismabiguation_rules>
154-
When disambiguating the user's question, follow these rules:
155-
- If the schemas contain no reference to the input data or you believe the database doesn't contain it, generate a dismagiuation request that explains to the user you don't have access to that info, and requests that they should rephrase the question. Do not provide them any user choices. Only ask for a single disambiguation request for this case.
156-
- If there are multiple possible mappings for a filter with a high probability of being correct, request disambiguation from the user. You can ask the user to choose from the possible options and answer multiple disambiguation requests in this case.
157-
- If the question is unclear or ambiguous, ask the user to rephrase or provide more context. Only ask for a single disambiguation request for this case.
158-
- Always provide clear and concise options for the user to choose from. These choices should reflect the possible mappings based on the database schemas and columns in a user friendly way.
153+
<disambiguation_rules>
154+
BEFORE CARRY OUT DISAMBIGUATION, ENSURE THAT YOU HAVE CHECKED ALL AVAILABLE DATABASE SCHEMAS AND FILTERS FOR A MOST PROBABLE MAPPING. YOU WILL NEED TO THINK THROUGH THE SCHEMAS AND CONSIDER SCHEMAS / COLUMNS THAT ARE SPELT DIFFERENTLY, BUT ARE LIKELY TO MEAN THE SAME THING.
155+
ALWAYS PRIORITIZE CLEAR MAPPINGS OVER DISAMBIGUATION REQUESTS.
159156
160-
REMEMBER: You will use the result of this disambiguation request next time to generate the SQL query. Make sure it will provide you with the necessary information to do so.
161-
</dismabiguation_rules>
157+
1. **No Match in Database Schemas or Uncertain Schema Availability**:
158+
- **Action**: If the database schemas or filters do not reference the user's question, or if you're unsure whether the schemas have the relevant data:
159+
- Generate a single disambiguation request that includes an explanation directly in the question.
160+
- The disambiguation question should explain that you believe the data is not available and request the user to rephrase their question or provide more context.
161+
- **JSON Example**:
162+
```json
163+
{
164+
\"disambiguation_requests\": [
165+
{
166+
\"agent_question\": \"I'm sorry, I couldn't find any relevant database schemas for your request about [REQUEST TYPE]. I focus on providing answers in the context of the use case. Could you please provide more context or rephrase your question?\",
167+
\"user_choices\": []
168+
}
169+
]
170+
}
171+
```
172+
173+
2. **Multiple Possible Mappings (when schemas or filters are available)**:
174+
- **Action**: If there are multiple potential mappings for filters, column names, or table names that could match the user's question with high probability:
175+
- Generate a disambiguation request with specific options for the user to choose from.
176+
- **Important**: If there are multiple possible mappings for different aspects of the question (e.g., column names, table names, filters), **you may generate multiple disambiguation requests** to cover each possible ambiguity separately.
177+
- The options should be derived from the database schema (e.g., column names, table names, or filter values) and reflect the user's input contextually.
178+
- ONLY CARRY OUT THIS DISAMBIGUATION IF THERE ARE MULTIPLE MAPPINGS AND YOU HAVE NO MOST LIKELY MATCH. If you can reasonably determine the correct mapping, do not generate a disambiguation request. Sometimes the mapping is not explicitly stated in the user's question, but it can be inferred from the context e.g. \"What is the average age of students?\" implies the column 'age' in the 'student' table or 2008 corresponds to the 'year' column in one of the tables.
179+
- **Phrase the options in a user-friendly, human-readable way** without any prefixes like \"Option\".
180+
- **JSON Example with Multiple Requests**:
181+
```json
182+
{
183+
\"disambiguation_requests\": [
184+
{
185+
\"agent_question\": \"Did you mean the 'Customer Name' column or the 'Client Name' column?\",
186+
\"user_choices\": [
187+
\"Customer Name\",
188+
\"Client Name\"
189+
]
190+
},
191+
{
192+
\"agent_question\": \"Which sort of bike do you mean?\",
193+
\"user_choices\": [
194+
\"Mountain Bike\",
195+
\"Road Bike\"
196+
]
197+
}
198+
]
199+
}
200+
```
201+
202+
3. **Unclear or Ambiguous Question**:
203+
- **Action**: If the user's question is unclear or inherently ambiguous (but relevant schemas are available):
204+
- Generate a single disambiguation request asking the user to rephrase their question or provide more context.
205+
- **JSON Example**:
206+
```json
207+
{
208+
\"disambiguation_requests\": [
209+
{
210+
\"agent_question\": \"Could you please rephrase your question or provide more context? I'm having trouble understanding the specifics of your request.\",
211+
\"user_choices\": []
212+
}
213+
]
214+
}
215+
```
216+
217+
4. **General Guidance**:
218+
- **Action**: If guidance is required but there are no specific ambiguous or multiple mappings:
219+
- Generate a disambiguation request asking the user to clarify the details of their request.
220+
- **JSON Example**:
221+
```json
222+
{
223+
\"disambiguation_requests\": [
224+
{
225+
\"agent_question\": \"Could you clarify the details of your request so I can assist you better?\",
226+
\"user_choices\": []
227+
}
228+
]
229+
}
230+
```
231+
232+
### Key Instructions for Implementing the Rules:
233+
- **Always return the disambiguation request in JSON format** as specified in the examples.
234+
- **Ensure that each disambiguation request includes a clear, concise explanation** and action the user should take (either provide more context or choose among options).
235+
- **For multiple mappings, generate multiple disambiguation requests**: If there are multiple ambiguous aspects (e.g., columns, tables), create separate disambiguation requests for each one. This ensures the user can clearly identify and resolve each ambiguity step by step.
236+
- **Phrase options in a human-readable, natural language** without technical prefixes such as \"Option 1\" or \"Option 2\". This makes the options easier to understand.
237+
- **Do not suggest options unless multiple potential mappings exist**, in which case, provide clearly derived options for the user to choose from.
238+
</disambiguation_rules>
162239
163240
<output_format>
164241
If all mappings are clear:
@@ -193,8 +270,6 @@ system_message:
193270
}
194271
]
195272
}
196-
User choices should be populated with matching options from the user's question e.g. column names, table names, filter values, etc.
197-
If you are asking the user to rephrase the question, set the user_choices to an empty list.
198273
TERMINATE
199274
</output_format>
200275
"

text_2_sql/text_2_sql_core/src/text_2_sql_core/prompts/user_message_rewrite_agent.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ system_message: |
3535
<instructions>
3636
1. Question Filtering and Classification
3737
- Use the provided list of allowed_topics list to filter out malicious or unrelated queries, such as those in the disallowed_topics list.
38-
- Ensure the question is relevant to the system's use case.
38+
- Consider if the question is related to data analysis or possibility related {{ use_case }}. If you are not sure whether the question is related to the use case, do not filter it out as it may be.
3939
- If the question cannot be filtered, output an empty sub-message list in the JSON format. Followed by TERMINATE.
4040
- For non-database questions like greetings (e.g., "Hello", "What can you do?", "How are you?"), set "all_non_database_query" to true.
4141
- For questions about data (e.g., queries about records, counts, values, comparisons, or any questions that would require database access), set "all_non_database_query" to false.

0 commit comments

Comments
 (0)