MCPcopy Create free account
hub / github.com/explorerhq/sql-explorer / build_prompt

Function build_prompt

explorer/assistant/utils.py:156–179  ·  view source on GitHub ↗
(db_connection, assistant_request, included_tables, query_error=None, sql=None)

Source from the content-addressed store, hash-verified

154
155
156def build_prompt(db_connection, assistant_request, included_tables, query_error=None, sql=None):
157 included_tables = [t.lower() for t in included_tables]
158
159 error_chunk = f"## Query Error ##\n{query_error}" if query_error else None
160 sql_chunk = f"## Existing User-Written SQL ##\n{sql}" if sql else None
161 request_chunk = f"## User's Request to Assistant ##\n{assistant_request}"
162 table_chunks = [
163 TablePromptData(
164 name=t,
165 schema=table_schema(db_connection, t),
166 sample=sample_rows_from_table(db_connection.as_django_connection(), t),
167 annotation=get_relevant_annotation(db_connection, t)
168 ).render()
169 for t in included_tables
170 ]
171 few_shot_chunk = get_few_shot_chunk(db_connection, included_tables)
172
173 chunks = [error_chunk, sql_chunk, *table_chunks, few_shot_chunk, request_chunk]
174
175 prompt = {
176 "system": build_system_prompt(db_connection.as_django_connection().vendor),
177 "user": "\n\n".join([c for c in chunks if c]),
178 }
179 return prompt

Calls 8

TablePromptDataClass · 0.85
table_schemaFunction · 0.85
sample_rows_from_tableFunction · 0.85
get_relevant_annotationFunction · 0.85
get_few_shot_chunkFunction · 0.85
build_system_promptFunction · 0.85
as_django_connectionMethod · 0.80
renderMethod · 0.45