Browse Source

智能选址agent

liutao 2 months ago
parent
commit
76df9fcf39

+ 2 - 2
aiAgent_gd/qwen_agent/memory/data/sqls/sql_examples_智能选址.jsonl

@@ -2,11 +2,11 @@
   {
     "query_type": "land_site_selection",
     "query": "帮我在萧山区推荐几块50亩左右的工业用地",
-    "sql_code": "select objectid, xzqmc, xzqdm, dymc, yddm, ydxz, ydmj, rjlsx, rjlxx, jzmdsx, jzmdxx, jzgdsx, jzgdxx, ldlsx, ldlxx, pfwh, pfsj, shape, st_area(shape::geography) as pfmarea,st_astext(shape) as geom, st_astext(st_centroid(shape)) as center_wkt from dlgis.gcs330000g2007_kzxxxgh_kgdk_kgy_dsgj as a where xzqmc = '萧山区' and ydxz like '%工业%' and abs(ydmj - 50*0.0667) <= 1 and NOT EXISTS (select 1 from dlgis.gcs330000k3003_zdzy_gd as b where st_intersects(a.shape, b.shape)) order by ydmj nulls last limit 10"
+    "sql_code": "select objectid, dymc, xzqmc, ydxz, st_area(shape::geography) as pfmarea, st_astext(st_centroid(shape)) as center_wkt from dlgis.gcs330000g2007_kzxxxgh_kgdk_kgy_dsgj as a where xzqmc = '萧山区' and ydxz like '%工业%' and abs(ydmj - 50*0.0667) <= 1 and NOT EXISTS (select 1 from dlgis.gcs330000k3003_zdzy_gd as b where st_intersects(a.shape, b.shape)) order by ydmj nulls last limit 10"
   },
   {
     "query_type": "land_site_selection",
     "query": "帮我在萧山区推荐一宗1公顷左右的学校用地",
-    "sql_code": "select objectid, xzqmc, xzqdm, dymc, yddm, ydxz, ydmj, rjlsx, rjlxx, jzmdsx, jzmdxx, jzgdsx, jzgdxx, ldlsx, ldlxx, pfwh, pfsj, shape, st_astext(shape) as geom, st_astext(st_centroid(shape)) as center_wkt from dlgis.gcs330000g2007_kzxxxgh_kgdk_kgy_dsgj as a where xzqmc = '萧山区' and ydxz like '%学校%' and abs(ydmj - 1) <= 1 and NOT EXISTS (select 1 from dlgis.gcs330000k3003_zdzy_gd as b where st_intersects(a.shape, b.shape)) order by ydmj nulls last limit 10"
+    "sql_code": "select objectid,dymc, xzqmc, ydxz, st_area(shape::geography) as pfmarea, st_astext(st_centroid(shape)) as center_wkt from dlgis.gcs330000g2007_kzxxxgh_kgdk_kgy_dsgj as a where xzqmc = '萧山区' and ydxz like '%学校%' and abs(ydmj - 1) <= 1 and NOT EXISTS (select 1 from dlgis.gcs330000k3003_zdzy_gd as b where st_intersects(a.shape, b.shape)) order by ydmj nulls last limit 10"
   }
 ]

+ 15 - 15
aiAgent_gd/qwen_agent/sub_agent/sql/land_site_selection_sql_agent.py

@@ -54,21 +54,21 @@ class LandSiteSelectionSqlAgent(BaseSubAgent):
         )'
 
         有几个注意事项:
-        注意1: 统计面积时候,无指定单位的情况下,请默认转换为公顷,平方米转换公顷请乘以0.0001,亩转换为公顷请乘以0.0667
-        注意2: 查询地区条件时,区县为**时请使用 xzqmc 字段。省为**时请先将行政区名称转换为行政区代码,使用xzqdm字段的前2位进行模糊查询,市为**时请先将行政区名称转换为行政区代码,使用xzqdm字段的前4位进行模糊查询
-        注意3: 使用 order by 进行排序时。必须使用 nulls last 确保 null值不会对排序产生影响,使用方法如下: order by xxxx desc nulls last 或者 order by xxxx nulls last
-        注意4: 当用户问题中的面积等于不是一个确定值的时候,如‘面积为xx亩左右’或‘面积为xx平方米左右’或‘面积为xx公顷左右’, 需要条件中添加 abs(ydmj - xx) <= 1, 将面积差控制在1公顷之内。使用 ‘order by abs(ydmj - xx)  nulls last’ 来进行排序
-        注意5: 当用户问题中的面积是一个确定值的时候,如‘面积为xx亩’或‘面积为xx平方米’或‘面积为xx公顷左右’, 需要条件中添加 abs(ydmj - xx) <= 1, 将面积差控制在1公顷之内。使用 ‘order by abs(ydmj - xx)  nulls last’ 来进行排序
-        注意6: 查询出地块。必须要对ydmj进行desc排序。查询地块有数量限制时,比如'1宗','一宗','1块',使用limit 1语句;未限定时,只查询10宗,使用limit 10语句
-        注意7: 问题中设计具体的地点时,需要使用round(st_distance(st_geometryfromtext('具体地点的wkt', 4490)::geography,shape::geography)::numeric,0)获取其distance, 如果问题未指定范围则使用 distance <= 5000 来限制在地点5公里内,并对其排序
-        注意8: 生成sql时,只对涉及表结构中的字段进行条件设置,不可生成不在表字段列表中的查询条件,不可生成任何不在表字段中的条件,比如周边5公里有什么设施
-        注意9: 生成sql时,必须使用 st_astext(st_centroid(shape)) as center_wkt 
-        注意10: 查询语句必须包含 objectid, xzqmc, xzqdm, dymc, yddm, ydxz, ydmj, rjlsx, rjlxx, jzmdsx, jzmdxx, jzgdsx, jzgdxx, ldlsx, ldlxx, pfwh, pfsj, shape,st_area(shape::geography) as pfmarea, st_astext(shape) as geom, st_astext(st_centroid(shape)) as center_wkt 这几个字段
-        注意11: 只准生成查询 的sql 语句,不可生成任何 修改数据的语句, 比如:update, delete, insert, truncate 等
-        注意12:当用户问题中的用地性质是"工业用地"时,去掉"用地",使用ydxz进行模糊查询,比如ydxz like '%工业%',工业用地没有二级分类
-        注意13:where语句中必须包含NOT EXISTS (select 1 from dlgis.gcs330000k3003_zdzy_gd as b where st_intersects(a.shape, b.shape)
-        注意14:数据表的schema是dlgis
-        注意15:from语句中给dlgis.gcs330000g2007_kzxxxgh_kgdk_kgy_dsgj设置别名a
+        注意1: 查询地区条件时,区县为**时请使用 xzqmc 字段。省为**时请先将行政区名称转换为行政区代码,使用xzqdm字段的前2位进行模糊查询,市为**时请先将行政区名称转换为行政区代码,使用xzqdm字段的前4位进行模糊查询
+        注意2: 使用 order by 进行排序时。必须使用 nulls last 确保 null值不会对排序产生影响,使用方法如下: order by xxxx desc nulls last 或者 order by xxxx nulls last
+        注意3: 当用户问题中的面积等于不是一个确定值的时候,如‘面积为xx亩左右’或‘面积为xx平方米左右’或‘面积为xx公顷左右’, 需要条件中添加 abs(ydmj - xx) <= 1, 将面积差控制在1公顷之内。使用 ‘order by abs(ydmj - xx)  nulls last’ 来进行排序
+        注意4: 当用户问题中的面积是一个确定值的时候,如‘面积为xx亩’或‘面积为xx平方米’或‘面积为xx公顷左右’, 需要条件中添加 abs(ydmj - xx) <= 1, 将面积差控制在1公顷之内。使用 ‘order by abs(ydmj - xx)  nulls last’ 来进行排序
+        注意5: 查询出地块。必须要对ydmj进行desc排序。查询地块有数量限制时,比如'1宗','一宗','1块',使用limit 1语句;未限定时,只查询10宗,使用limit 10语句
+        注意6: 问题中设计具体的地点时,需要使用round(st_distance(st_geometryfromtext('具体地点的wkt', 4490)::geography,shape::geography)::numeric,0)获取其distance, 如果问题未指定范围则使用 distance <= 5000 来限制在地点5公里内,并对其排序
+        注意7: 生成sql时,只对涉及表结构中的字段进行条件设置,不可生成不在表字段列表中的查询条件,不可生成任何不在表字段中的条件,比如周边5公里有什么设施
+        注意8: 生成sql时,必须使用 st_astext(st_centroid(shape)) as center_wkt 
+        注意9: 查询语句必须包含 objectid, dymc, xzqmc, ydxz, st_area(shape::geography) as pfmarea, st_astext(st_centroid(shape)) as center_wkt 这几个字段
+        注意10: 只准生成查询 的sql 语句,不可生成任何 修改数据的语句, 比如:update, delete, insert, truncate 等
+        注意11:当用户问题中的用地性质是"工业用地"时,去掉"用地",使用ydxz进行模糊查询,比如ydxz like '%工业%',工业用地没有二级分类
+        注意12:where语句中必须包含NOT EXISTS (select 1 from dlgis.gcs330000k3003_zdzy_gd as b where st_intersects(a.shape, b.shape)
+        注意13:数据表的schema是dlgis
+        注意14:from语句中给dlgis.gcs330000g2007_kzxxxgh_kgdk_kgy_dsgj设置别名a
+        注意15:select语句中使用st_area(shape::geography) as pfmarea字段来计算面积,单位是平方米,不要做任何的单位换算
         """
         self.retriever = SqlRetriever(query_type='land_site_selection')
 

+ 22 - 3
aiAgent_gd/run_server_async.py

@@ -2,8 +2,9 @@ import shutil
 import tempfile
 import time
 import sys
+from typing import List
 from zipfile import ZipFile
-
+import json
 import fiona
 from shapely.geometry import shape
 from sse_starlette.sse import EventSourceResponse
@@ -24,14 +25,14 @@ from qwen_agent.planning.plan_continue_executor import PlanContinueExecutor
 from qwen_agent.llm.llm_client import LLMClient, LLMAsyncClient
 from agent_config import LLMDict_Qwen_72B_1211, LLMDict_GPT4_TURBO
 from agent_messages import BaseRequest
-
+from qwen_agent.tools.tools import async_xzdb
 prompt_lan = "CN"
 llm_name = "qwen-plus"
 llm_turbo_name = "gpt-4-turbo"
 max_ref_token = 4000
 # model_server = "http://10.10.0.10:7907/v1"
 # model_server = "http://lq.lianqiai.cn:7905/v1"
-# model_server = "http://10.36.162.54:20331/v1"
+# model_server = "http://172.20.28.16:20331/v1"
 model_server = "http://ac.zjugis.com:8511/v1"
 api_key = ""
 server_host = "0.0.0.0"
@@ -172,6 +173,23 @@ async def clarificationByTurbo(request: BaseRequest):
     )
 
 
+@app.get("/queryGeometry")
+async def queryGeometry(id: int):
+    sql = f'select objectid, xzqmc, xzqdm, dymc, yddm, ydxz, ydmj, rjlsx, rjlxx, jzmdsx, jzmdxx, jzgdsx, jzgdxx, ldlsx, ldlxx, pfwh, pfsj, shape, st_area(shape::geography) as pfmarea,st_astext(shape) as geom, st_astext(st_centroid(shape)) as center_wkt from dlgis.gcs330000g2007_kzxxxgh_kgdk_kgy_dsgj where objectid = {id}'
+    res_tuples = await async_xzdb.run(sql)
+    result, success = res_tuples
+    print(success, result)
+    return json.loads(result)
+
+
+@app.get("/queryGeometryList")
+async def queryGeometryList(items):
+    sql = f'select st_asgeojson(shape) as geom from dlgis.gcs330000g2007_kzxxxgh_kgdk_kgy_dsgj where objectid in {items}'
+    res_tuples = await async_xzdb.run(sql)
+    result, success = res_tuples
+    print(success, result)
+    return json.loads(result)
+
 llm_client = LLMClient(model=llm_name, model_server=model_server)
 llm_client_async = LLMAsyncClient(model=llm_name, model_server=model_server)
 
@@ -217,4 +235,5 @@ async def call_with_stream(
 
 
 if __name__ == "__main__":
+    # uvicorn.run("run_server_async:app", host=server_host, port=server_port, workers=5)
     uvicorn.run(app, host=server_host, port=server_port, workers=1)