| 154 | |
| 155 | |
| 156 | def 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 |