-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathfollowup_sql_generation.py
More file actions
235 lines (203 loc) · 6.93 KB
/
Copy pathfollowup_sql_generation.py
File metadata and controls
235 lines (203 loc) · 6.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import logging
import sys
from typing import Any
from hamilton import base
from hamilton.async_driver import AsyncDriver
from haystack.components.builders.prompt_builder import PromptBuilder
from langfuse.decorators import observe
from src.core.engine import Engine
from src.core.pipeline import BasicPipeline
from src.core.provider import DocumentStoreProvider, LLMProvider
from src.pipelines.common import clean_up_new_lines, retrieve_metadata
from src.pipelines.generation.utils.sql import (
SQL_GENERATION_MODEL_KWARGS,
SQLGenPostProcessor,
construct_ask_history_messages,
construct_instructions,
get_calculated_field_instructions,
get_json_field_instructions,
get_metric_instructions,
get_sql_generation_system_prompt,
)
from src.pipelines.retrieval.sql_functions import SqlFunction
from src.pipelines.retrieval.sql_knowledge import SqlKnowledge
from src.utils import trace_cost
from src.web.v1.services.ask import AskHistory
logger = logging.getLogger("wren-ai-service")
text_to_sql_with_followup_user_prompt_template = """
### TASK ###
Given the following user's follow-up question and previous SQL query and summary,
generate one SQL query to best answer user's question.
### DATABASE SCHEMA ###
{% for document in documents %}
{{ document }}
{% endfor %}
{% if calculated_field_instructions %}
{{ calculated_field_instructions }}
{% endif %}
{% if metric_instructions %}
{{ metric_instructions }}
{% endif %}
{% if json_field_instructions %}
{{ json_field_instructions }}
{% endif %}
{% if sql_functions %}
### SQL FUNCTIONS ###
{% for function in sql_functions %}
{{ function }}
{% endfor %}
{% endif %}
{% if sql_samples %}
### SQL SAMPLES ###
{% for sample in sql_samples %}
Summary:
{{sample.summary}}
SQL:
{{sample.sql}}
{% endfor %}
{% endif %}
{% if instructions %}
### USER INSTRUCTIONS ###
{% for instruction in instructions %}
{{ loop.index }}. {{ instruction }}
{% endfor %}
{% endif %}
### QUESTION ###
User's Follow-up Question: {{ query }}
### REASONING PLAN ###
{{ sql_generation_reasoning }}
Let's think step by step.
"""
## Start of Pipeline
@observe(capture_input=False)
def prompt(
query: str,
documents: list[str],
sql_generation_reasoning: str,
prompt_builder: PromptBuilder,
sql_samples: list[dict] | None = None,
instructions: list[dict] | None = None,
has_calculated_field: bool = False,
has_metric: bool = False,
has_json_field: bool = False,
sql_functions: list[SqlFunction] | None = None,
sql_knowledge: SqlKnowledge | None = None,
) -> dict:
_prompt = prompt_builder.run(
query=query,
documents=documents,
sql_generation_reasoning=sql_generation_reasoning,
instructions=construct_instructions(
instructions=instructions,
),
calculated_field_instructions=(
get_calculated_field_instructions(sql_knowledge)
if has_calculated_field
else ""
),
metric_instructions=(
get_metric_instructions(sql_knowledge) if has_metric else ""
),
json_field_instructions=(
get_json_field_instructions(sql_knowledge) if has_json_field else ""
),
sql_samples=sql_samples,
sql_functions=sql_functions,
)
return {"prompt": clean_up_new_lines(_prompt.get("prompt"))}
@observe(as_type="generation", capture_input=False)
@trace_cost
async def generate_sql_in_followup(
prompt: dict, generator: Any, histories: list[AskHistory], generator_name: str
) -> dict:
history_messages = construct_ask_history_messages(histories)
return await generator(
prompt=prompt.get("prompt"), history_messages=history_messages
), generator_name
@observe(capture_input=False)
async def post_process(
generate_sql_in_followup: dict,
post_processor: SQLGenPostProcessor,
data_source: str,
project_id: str | None = None,
use_dry_plan: bool = False,
allow_dry_plan_fallback: bool = True,
) -> dict:
return await post_processor.run(
generate_sql_in_followup.get("replies"),
project_id=project_id,
use_dry_plan=use_dry_plan,
data_source=data_source,
allow_dry_plan_fallback=allow_dry_plan_fallback,
)
## End of Pipeline
class FollowUpSQLGeneration(BasicPipeline):
def __init__(
self,
llm_provider: LLMProvider,
document_store_provider: DocumentStoreProvider,
engine: Engine,
**kwargs,
):
self._retriever = document_store_provider.get_retriever(
document_store_provider.get_store("project_meta")
)
self._llm_provider = llm_provider
self._components = {
"generator_name": llm_provider.get_model(),
"prompt_builder": PromptBuilder(
template=text_to_sql_with_followup_user_prompt_template
),
"post_processor": SQLGenPostProcessor(engine=engine),
}
super().__init__(
AsyncDriver({}, sys.modules[__name__], result_builder=base.DictResult())
)
@observe(name="Follow-Up SQL Generation")
async def run(
self,
query: str,
contexts: list[str],
sql_generation_reasoning: str,
histories: list[AskHistory],
sql_samples: list[dict] | None = None,
instructions: list[dict] | None = None,
project_id: str | None = None,
has_calculated_field: bool = False,
has_metric: bool = False,
has_json_field: bool = False,
sql_functions: list[SqlFunction] | None = None,
use_dry_plan: bool = False,
allow_dry_plan_fallback: bool = True,
sql_knowledge: SqlKnowledge | None = None,
):
logger.info("Follow-Up SQL Generation pipeline is running...")
if use_dry_plan:
metadata = await retrieve_metadata(project_id or "", self._retriever)
else:
metadata = {}
self._components["generator"] = self._llm_provider.get_generator(
system_prompt=get_sql_generation_system_prompt(sql_knowledge),
generation_kwargs=SQL_GENERATION_MODEL_KWARGS,
)
return await self._pipe.execute(
["post_process"],
inputs={
"query": query,
"documents": contexts,
"sql_generation_reasoning": sql_generation_reasoning,
"histories": histories,
"project_id": project_id,
"sql_samples": sql_samples,
"instructions": instructions,
"has_calculated_field": has_calculated_field,
"has_metric": has_metric,
"has_json_field": has_json_field,
"sql_functions": sql_functions,
"use_dry_plan": use_dry_plan,
"allow_dry_plan_fallback": allow_dry_plan_fallback,
"data_source": metadata.get("data_source", "local_file"),
"sql_knowledge": sql_knowledge,
**self._components,
},
)