Skip to content

Commit 51342aa

Browse files
committed
Update prompts and answer generation logix
1 parent 72b2c80 commit 51342aa

File tree

4 files changed

+49
-14
lines changed

4 files changed

+49
-14
lines changed

text_2_sql/autogen/src/autogen_text_2_sql/autogen_text_2_sql.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,16 +143,22 @@ def extract_answer_payload(self, messages: list) -> AnswerWithSourcesPayload:
143143
sql_query_results = self.parse_message_content(messages[-2].content)
144144
logging.info("SQL Query Results: %s", sql_query_results)
145145

146-
sub_question_results = self.parse_message_content(messages[1].content)
147-
logging.info("Sub-Question Results: %s", sub_question_results)
148-
149146
try:
150147
if isinstance(sql_query_results, str):
151148
sql_query_results = json.loads(sql_query_results)
149+
except json.JSONDecodeError:
150+
logging.warning("Unable to read SQL query results: %s", sql_query_results)
151+
sql_query_results = {}
152+
sub_question_results = {}
153+
else:
154+
# Only load sub-question results if we have a database result
155+
sub_question_results = self.parse_message_content(messages[1].content)
156+
logging.info("Sub-Question Results: %s", sub_question_results)
152157

158+
try:
153159
sub_questions = [
154160
sub_question
155-
for sub_question_group in sub_question_results["sub_questions"]
161+
for sub_question_group in sub_question_results.get("sub_questions", [])
156162
for sub_question in sub_question_group
157163
]
158164

text_2_sql/autogen/src/autogen_text_2_sql/custom_agents/parallel_query_solving_agent.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ async def on_messages_stream(
8484
injected_parameters = {}
8585

8686
# Load the json of the last message to populate the final output object
87-
query_rewrites = json.loads(last_response)
87+
question_rewrites = json.loads(last_response)
8888

89-
logging.info(f"Query Rewrites: {query_rewrites}")
89+
logging.info(f"Query Rewrites: {question_rewrites}")
9090

9191
async def consume_inner_messages_from_agentic_flow(
9292
agentic_flow, identifier, database_results
@@ -162,21 +162,33 @@ async def consume_inner_messages_from_agentic_flow(
162162
inner_solving_generators = []
163163
database_results = {}
164164

165+
all_non_database_query = question_rewrites.get("all_non_database_query", False)
166+
167+
if all_non_database_query:
168+
yield Response(
169+
chat_message=TextMessage(
170+
content="All queries are non-database queries. Nothing to process.",
171+
source=self.name,
172+
),
173+
)
174+
return
175+
165176
# Start processing sub-queries
166-
for query_rewrite in query_rewrites["sub_questions"]:
167-
logging.info(f"Processing sub-query: {query_rewrite}")
177+
for question_rewrite in question_rewrites["sub_questions"]:
178+
logging.info(f"Processing sub-query: {question_rewrite}")
168179
# Create an instance of the InnerAutoGenText2Sql class
169180
inner_autogen_text_2_sql = InnerAutoGenText2Sql(
170181
self.engine_specific_rules, **self.kwargs
171182
)
172183

173-
identifier = ", ".join(query_rewrite)
184+
identifier = ", ".join(question_rewrite)
174185

175186
# Launch tasks for each sub-query
176187
inner_solving_generators.append(
177188
consume_inner_messages_from_agentic_flow(
178189
inner_autogen_text_2_sql.process_question(
179-
question=query_rewrite, injected_parameters=injected_parameters
190+
question=question_rewrite,
191+
injected_parameters=injected_parameters,
180192
),
181193
identifier,
182194
database_results,

text_2_sql/text_2_sql_core/src/text_2_sql_core/prompts/answer_agent.yaml

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,26 @@ model: "4o-mini"
22
description: "An agent that generates a response to a user's question."
33
system_message: |
44
<role_and_objective>
5-
You are a helpful AI Assistant specializing in answering a user's question about {{ use_case }}.
5+
You are a helpful AI Assistant specializing in answering a user's question about {{ use_case }}.
66
</role_and_objective>
77
8-
Use the information obtained to generate a response to the user's question. The question has been broken down into a series of SQL queries and you need to generate a response based on the results of these queries.
8+
<system_information>
9+
You are part of an overall system that provides Text2SQL functionality only. You will be passed a result from multiple SQL queries, you must formulate a response to the user's question using this information.
10+
You can assume that the SQL queries are correct and that the results are accurate.
11+
You and the wider system can only generate SQL queries and process the results of these queries. You cannot access any external resources.
12+
The main ability of the system is to perform natural language understanding and generate SQL queries from the user's question. These queries are then automatically run against the database and the results are passed to you.
13+
</system_information>
914
10-
Do not use any external resources to generate the response. The response should be based solely on the information provided in the SQL queries and their results.
15+
<instructions>
1116
12-
You can use Markdown and Markdown tables to format the response.
17+
Use the information obtained to generate a response to the user's question. The question has been broken down into a series of SQL queries and you need to generate a response based on the results of these queries.
18+
19+
Do not use any external resources to generate the response. The response should be based solely on the information provided in the SQL queries and their results.
20+
21+
You have no access to the internet or any other external resources. You can only use the information provided in the SQL queries and their results, to generate the response.
22+
23+
You can use Markdown and Markdown tables to format the response.
24+
25+
If the user is asking about your capabilities, use the <system_information> to explain what you do.
26+
27+
</instructions>

text_2_sql/text_2_sql_core/src/text_2_sql_core/prompts/question_rewrite_agent.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ system_message: |
3737
- Use the provided list of topics to filter out malicious or unrelated queries.
3838
- Ensure the question is relevant to the system's use case.
3939
- If the question cannot be filtered, output an empty sub-query list in the JSON format. Followed by TERMINATE.
40+
- Retain and decompose general questions, such as Hello, What can you do?, etc. Set "all_non_database_query" to true.
4041
4142
2. Understanding:
4243
- Use the chat history (that is available in reverse order) to understand the context of the current question.
@@ -97,6 +98,7 @@ system_message: |
9798
],
9899
"combination_logic": "<instructions for combining results>",
99100
"query_type": "<simple|complex>",
101+
"all_non_database_query": "<true|false>"
100102
}
101103
</output_format>
102104
</instructions>

0 commit comments

Comments
 (0)