MCPcopy Create free account
hub / github.com/FlyingFeather/DEA-SQL / generate_db_prompt_bird_v2

Function generate_db_prompt_bird_v2

data_preprocess.py:461–506  ·  view source on GitHub ↗

:param root_place: :param db_name: :return:

(root_dir, dataset, db_id, limit_value=3)

Source from the content-addressed store, hash-verified

459
460
461def generate_db_prompt_bird_v2(root_dir, dataset, db_id, limit_value=3):
462 # extract create ddls
463 '''
464 :param root_place:
465 :param db_name:
466 :return:
467 '''
468 DATA_PATH = root_dir
469 dev_db_path = f"{DATA_PATH}/{dataset}/dev_databases"
470 db_uri = dev_db_path + "/" + db_id + "/" + db_id + ".sqlite"
471 full_schema_prompt_list = []
472 table_names = []
473 conn = sqlite3.connect(db_uri)
474 # Create a cursor object
475 cursor = conn.cursor()
476 cursor.execute("SELECT name FROM sqlite_master WHERE type='table'")
477 tables = cursor.fetchall()
478 schemas = {}
479 for table in tables:
480 if table == 'sqlite_sequence':
481 continue
482 if isinstance(table, tuple):
483 table_name = table[0]
484 table_names.append(table_name)
485 cursor.execute("SELECT sql FROM sqlite_master WHERE type='table' AND name='{}';".format(table[0]))
486 create_prompt = cursor.fetchone()[0]
487 schemas[table[0]] = create_prompt
488 if limit_value:
489 cur_table = table[0]
490 if cur_table in ['order', 'by', 'group']:
491 cur_table = "`{}`".format(cur_table)
492
493 cursor.execute("SELECT * FROM {} LIMIT {}".format(cur_table, limit_value))
494 column_names = [description[0] for description in cursor.description]
495 values = cursor.fetchall()
496 rows_prompt = nice_look_table(column_names=column_names, values=values)
497 verbose_prompt = "/* \n {} example rows: \n SELECT * FROM {} LIMIT {}; \n {} \n */".format(
498 limit_value, cur_table, limit_value, rows_prompt)
499 schemas[table[0]] = "{} \n {}".format(create_prompt, verbose_prompt)
500
501 for k, v in schemas.items():
502 full_schema_prompt_list.append(v)
503
504 schema_prompt = "\n\n".join(full_schema_prompt_list)
505
506 return schema_prompt, table_names
507
508
509if __name__ == "__main__":

Callers

nothing calls this directly

Calls 2

nice_look_tableFunction · 0.85
executeMethod · 0.45

Tested by

no test coverage detected