diff --git a/AI/Text2Sql/Model/__pycache__/biModel.cpython-310.pyc b/AI/Text2Sql/Model/__pycache__/biModel.cpython-310.pyc index a9bc3867..bbda8044 100644 Binary files a/AI/Text2Sql/Model/__pycache__/biModel.cpython-310.pyc and b/AI/Text2Sql/Model/__pycache__/biModel.cpython-310.pyc differ diff --git a/AI/Text2Sql/Model/biModel.py b/AI/Text2Sql/Model/biModel.py index e9d615b5..d5d5ed92 100644 --- a/AI/Text2Sql/Model/biModel.py +++ b/AI/Text2Sql/Model/biModel.py @@ -138,3 +138,38 @@ async def get_column_names(db: asyncpg.Connection, sql: str): return column_names else: return [] + +def generate_columns(data): + """ + 根据数据生成 category_columns_str 和 value_column_str + :param data: 数据集(列表字典格式,例如:[{"行政区划": "北京", "学校": "清华附中", "课程数量": 100}, ...]) + :return: (category_columns_str, value_column_str) + """ + # 获取所有字段名 + columns = list(data[0].keys()) if data else [] + + # 1. 有行政区划+学校两个字段的 + if "行政区划" in columns and "学校" in columns: + category_columns_str = "行政区划,学校" + # 2. 有学段+科目的 + elif "学段" in columns and "科目" in columns: + category_columns_str = "学段,科目" + # 3. 只有行政区的 + elif "行政区划" in columns: + category_columns_str = "行政区划" + # 4. 只有学段的 + elif "学段" in columns: + category_columns_str = "学段" + # 5. 上面都不是的,用第一个字段记录 + else: + category_columns_str = columns[0] if columns else "" + + # 6. Y轴一般的常见词是:课程数量,数量,如果都没有,用最后一个字段记录 + if "课程数量" in columns: + value_column_str = "课程数量" + elif "数量" in columns: + value_column_str = "数量" + else: + value_column_str = columns[-1] if columns else "" + + return category_columns_str, value_column_str \ No newline at end of file diff --git a/AI/Text2Sql/__pycache__/app.cpython-310.pyc b/AI/Text2Sql/__pycache__/app.cpython-310.pyc index fe668967..4af9c7a1 100644 Binary files a/AI/Text2Sql/__pycache__/app.cpython-310.pyc and b/AI/Text2Sql/__pycache__/app.cpython-310.pyc differ diff --git a/AI/Text2Sql/app.py b/AI/Text2Sql/app.py index ec60ef91..b43f8ddf 100644 --- a/AI/Text2Sql/app.py +++ b/AI/Text2Sql/app.py @@ -144,8 +144,17 @@ async def get_docx_stream( _data = await db.fetch(sql) # print(_data) # 将 asyncpg.Record 转换为 JSON 格式 - json_data = json.dumps([dict(record) for record in _data], ensure_ascii=False) - prompt = prompt + json.dumps(json_data, ensure_ascii=False) + from decimal import Decimal + + # Define a custom function to handle Decimal objects + def decimal_default(obj): + if isinstance(obj, Decimal): + return float(obj) # Convert Decimal to float + raise TypeError(f"Object of type {obj.__class__.__name__} is not JSON serializable") + + # In your get_docx_stream function, modify the json.dumps calls: + json_data = json.dumps([dict(record) for record in _data], ensure_ascii=False, default=decimal_default) + prompt = prompt + json_data # No need to call json.dumps again on json_data # 调用 OpenAI API 生成总结(流式输出) response = await client.chat.completions.create( @@ -333,8 +342,6 @@ async def get_data_scheme_by_id( async def get_chart( question_id: str = Form(None, description="问题ID(POST请求)"), title: str = Form(None, description="图表标题(POST请求)"), - category_columns_str: str = Form(None, description="分类数据列(X 轴或饼图标签)"), - value_column_str: str = Form(None, description="数值数据列(Y 轴或饼图值)"), db: asyncpg.Connection = Depends(get_db) ): # 根据问题ID获取查询SQL @@ -342,6 +349,11 @@ async def get_chart( # 执行SQL获取数据集 _data = await get_data_by_sql(db, sql) + # 调用函数 + category_columns_str, value_column_str = generate_columns(_data) + print(f"category_columns_str: {category_columns_str}") + print(f"value_column_str: {value_column_str}") + # 图表文件名称 uuid_str = str(uuid.uuid4()) filename = f"static/html/{uuid_str}.html" diff --git a/AI/Text2Sql/static/docx/0853596e-829f-42e7-a3a7-48bfecadf4cf.docx b/AI/Text2Sql/static/docx/0853596e-829f-42e7-a3a7-48bfecadf4cf.docx new file mode 100644 index 00000000..9ec43d82 Binary files /dev/null and b/AI/Text2Sql/static/docx/0853596e-829f-42e7-a3a7-48bfecadf4cf.docx differ diff --git a/AI/Text2Sql/static/docx/309d4b34-bda8-49df-abbb-b9d0af82c3fb.docx b/AI/Text2Sql/static/docx/309d4b34-bda8-49df-abbb-b9d0af82c3fb.docx new file mode 100644 index 00000000..242618e8 Binary files /dev/null and b/AI/Text2Sql/static/docx/309d4b34-bda8-49df-abbb-b9d0af82c3fb.docx differ diff --git a/AI/Text2Sql/static/docx/4666c309-8d2c-40bb-a7c5-f7d0c6578432.docx b/AI/Text2Sql/static/docx/4666c309-8d2c-40bb-a7c5-f7d0c6578432.docx new file mode 100644 index 00000000..16d3c29f Binary files /dev/null and b/AI/Text2Sql/static/docx/4666c309-8d2c-40bb-a7c5-f7d0c6578432.docx differ diff --git a/AI/Text2Sql/static/docx/55e97964-f632-4ee3-b16a-e16ac6f6558b.docx b/AI/Text2Sql/static/docx/55e97964-f632-4ee3-b16a-e16ac6f6558b.docx new file mode 100644 index 00000000..6f020e88 Binary files /dev/null and b/AI/Text2Sql/static/docx/55e97964-f632-4ee3-b16a-e16ac6f6558b.docx differ diff --git a/AI/Text2Sql/static/docx/7fdce75f-b40f-4398-a086-069b9396e6e0.docx b/AI/Text2Sql/static/docx/7fdce75f-b40f-4398-a086-069b9396e6e0.docx new file mode 100644 index 00000000..e3a1c264 Binary files /dev/null and b/AI/Text2Sql/static/docx/7fdce75f-b40f-4398-a086-069b9396e6e0.docx differ diff --git a/AI/Text2Sql/static/docx/8a6a2195-fb3d-4390-bfed-672beea0ac70.docx b/AI/Text2Sql/static/docx/8a6a2195-fb3d-4390-bfed-672beea0ac70.docx new file mode 100644 index 00000000..43ef7256 Binary files /dev/null and b/AI/Text2Sql/static/docx/8a6a2195-fb3d-4390-bfed-672beea0ac70.docx differ diff --git a/AI/Text2Sql/static/docx/9b0ccbe3-5a42-4322-acf1-d3adb5d1fa9e.docx b/AI/Text2Sql/static/docx/9b0ccbe3-5a42-4322-acf1-d3adb5d1fa9e.docx new file mode 100644 index 00000000..e6dc3e54 Binary files /dev/null and b/AI/Text2Sql/static/docx/9b0ccbe3-5a42-4322-acf1-d3adb5d1fa9e.docx differ diff --git a/AI/Text2Sql/static/docx/9bfc087a-82cf-4fe9-a5f9-3c5af8f3fabe.docx b/AI/Text2Sql/static/docx/9bfc087a-82cf-4fe9-a5f9-3c5af8f3fabe.docx new file mode 100644 index 00000000..4f5875d1 Binary files /dev/null and b/AI/Text2Sql/static/docx/9bfc087a-82cf-4fe9-a5f9-3c5af8f3fabe.docx differ diff --git a/AI/Text2Sql/static/docx/afca4515-20c9-4cd4-abc5-5b49ffab2feb.docx b/AI/Text2Sql/static/docx/afca4515-20c9-4cd4-abc5-5b49ffab2feb.docx new file mode 100644 index 00000000..2c917bbd Binary files /dev/null and b/AI/Text2Sql/static/docx/afca4515-20c9-4cd4-abc5-5b49ffab2feb.docx differ diff --git a/AI/Text2Sql/static/docx/b15b6f73-5171-4456-a24b-5e89efc7ada5.docx b/AI/Text2Sql/static/docx/b15b6f73-5171-4456-a24b-5e89efc7ada5.docx new file mode 100644 index 00000000..af511438 Binary files /dev/null and b/AI/Text2Sql/static/docx/b15b6f73-5171-4456-a24b-5e89efc7ada5.docx differ diff --git a/AI/Text2Sql/static/docx/b5234915-9cb7-438c-a4e1-104dc0e0cc3b.docx b/AI/Text2Sql/static/docx/b5234915-9cb7-438c-a4e1-104dc0e0cc3b.docx new file mode 100644 index 00000000..1adc002b Binary files /dev/null and b/AI/Text2Sql/static/docx/b5234915-9cb7-438c-a4e1-104dc0e0cc3b.docx differ diff --git a/AI/Text2Sql/static/docx/dae43efa-f44a-444b-bdeb-6c364e1665ce.docx b/AI/Text2Sql/static/docx/dae43efa-f44a-444b-bdeb-6c364e1665ce.docx new file mode 100644 index 00000000..835ea6ed Binary files /dev/null and b/AI/Text2Sql/static/docx/dae43efa-f44a-444b-bdeb-6c364e1665ce.docx differ diff --git a/AI/Text2Sql/static/docx/dc9ef3da-e636-4971-8c07-c9fe9e0cc46b.docx b/AI/Text2Sql/static/docx/dc9ef3da-e636-4971-8c07-c9fe9e0cc46b.docx new file mode 100644 index 00000000..df93d2ea Binary files /dev/null and b/AI/Text2Sql/static/docx/dc9ef3da-e636-4971-8c07-c9fe9e0cc46b.docx differ diff --git a/AI/Text2Sql/static/docx/f210e8d1-a305-48b8-a396-18a0b0435bec.docx b/AI/Text2Sql/static/docx/f210e8d1-a305-48b8-a396-18a0b0435bec.docx new file mode 100644 index 00000000..c606eec1 Binary files /dev/null and b/AI/Text2Sql/static/docx/f210e8d1-a305-48b8-a396-18a0b0435bec.docx differ diff --git a/AI/Text2Sql/static/docx/fd5078a2-23b1-48fe-b68c-7d512c1ca07b.docx b/AI/Text2Sql/static/docx/fd5078a2-23b1-48fe-b68c-7d512c1ca07b.docx new file mode 100644 index 00000000..f3fd5428 Binary files /dev/null and b/AI/Text2Sql/static/docx/fd5078a2-23b1-48fe-b68c-7d512c1ca07b.docx differ diff --git a/AI/Text2Sql/static/html/7c5a59eb-58ee-4aa3-b68d-6004c3e1d09d.html b/AI/Text2Sql/static/html/7c5a59eb-58ee-4aa3-b68d-6004c3e1d09d.html new file mode 100644 index 00000000..0a6bc4cf --- /dev/null +++ b/AI/Text2Sql/static/html/7c5a59eb-58ee-4aa3-b68d-6004c3e1d09d.html @@ -0,0 +1,715 @@ + + +
+ +