| 340 | |
| 341 | |
| 342 | def get_sample_data( |
| 343 | cur: Cursor, |
| 344 | dbname: str, |
| 345 | prompt_field_truncate: int, |
| 346 | prompt_section_truncate: int, |
| 347 | ) -> dict[str, Any]: |
| 348 | if dbname in SAMPLE_DATA_CACHE: |
| 349 | return SAMPLE_DATA_CACHE[dbname] |
| 350 | click.echo("Preparing sample data to feed the LLM") |
| 351 | tables_query = "SHOW TABLES" |
| 352 | sample_row_query = "SELECT * FROM `{dbname}`.`{table}` LIMIT 1" |
| 353 | cur.execute(tables_query) |
| 354 | sample_data = {} |
| 355 | for (table_name,) in cur.fetchall(): |
| 356 | try: |
| 357 | cur.execute(sample_row_query.format(dbname=dbname, table=table_name)) |
| 358 | except Exception: |
| 359 | continue |
| 360 | cols = [desc[0] for desc in cur.description] |
| 361 | row = cur.fetchone() |
| 362 | if row is None: |
| 363 | continue |
| 364 | sample_data[table_name] = list( |
| 365 | zip(cols, truncate_list_elements(list(row), prompt_field_truncate, prompt_section_truncate), strict=False) |
| 366 | ) |
| 367 | SAMPLE_DATA_CACHE[dbname] = sample_data |
| 368 | return sample_data |
| 369 | |
| 370 | |
| 371 | def sql_using_llm( |