{"repo": "alex1561479/nl2sql-agent", "files": {"da_agents/agents/question_intent_agent.py": {"aeccddda8ec8cf8e868383e7df71351693e04ec6": {"t": 1785747510.6297963, "n": 2317, "h": "6ac7c7ddb675ae11", "c": "from typing import override\n\nfrom langchain_core.messages import AIMessage, HumanMessage\n\nfrom da_agents.state.sql_intent_state import IntentAgentState\nfrom da_agents.agents.base_agent import BaseAgent\nfrom langchain_openai import ChatOpenAI\nfrom ..prompts.question_intent_prompt import QUESTION_INTENT_PROMPT\nclass IntentAgent(BaseAgent):\n\n def __init__(self, agent_id: str):\n deepseek_api_key = \"sk-14090e8029614d0bbfe8c3c9a39f2cac\"\n model = ChatOpenAI(\n api_key=deepseek_api_key,\n model=\"deepseek-chat\",\n base_url=\"https://api.deepseek.com\",\n temperature=1.0\n )\n\n super().__init__(\n model=model,\n name=\"intent_agent\" + agent_id,\n saver=None,\n system_prompt=QUESTION_INTENT_PROMPT \n )\n\n @override\n def __call__(self, state: IntentAgentState) -> IntentAgentState:\n \"\"\"\n node: 智能体节点\n \"\"\"\n # print(state)\n input_messages = state.user_query\n\n resp = self.agent.invoke(\n input={\"messages\": input_messages}\n )\n intent_str = self.IntentProcess(resp[\"messages\"][-1].content)\n\n update_state: IntentAgentState = {\n \"query_intents\": [intent_str]\n }\n # print(self.name, \"\\n\", intent_str, \"\\n\\n\")\n\n return update_state\n\n def IntentProcess(self, unprocessed_intent: str) -> str:\n # 打印原始响应以供调试\n print(\"Raw Intent Response:\\n\", unprocessed_intent)\n \n # 1. 情况一:标准的 ```json 包裹\n if \"```json\" in unprocessed_intent:\n return unprocessed_intent.split(\"```json\")[1].split(\"```\")[0].strip()\n \n # 2. 情况二:没有 json 标识,只有 ``` 包裹\n elif \"```\" in unprocessed_intent:\n # 这种情况下,内容通常在第一个 ``` 和最后一个 ``` 之间\n # 使用切片跳过第一个 ``` (长度3)\n content = unprocessed_intent.split(\"```\", 1)[1]\n # 再去掉结尾的 ``` (如果有的话)\n return content.split(\"```\")[0].strip()\n \n # 3. 情况三:没有任何代码块标记,直接就是纯文本\n else:\n return unprocessed_intent.strip()\n\n\nif __name__ == \"__main__\":\n agent = IntentAgent(\"1\")\n test_message = HumanMessage(\"请找出成绩最高的8名学生\")\n test_state: IntentAgentState\n\n resp_state = agent(test_state)\n print(resp_state.query_intents[0])"}, "74263e3129f1716930200ecbc9d007e2592ba487": {"t": 1785773680.3908658, "n": 2317, "h": "6ac7c7ddb675ae11", "c": "from typing import override\n\nfrom langchain_core.messages import AIMessage, HumanMessage\n\nfrom da_agents.state.sql_intent_state import IntentAgentState\nfrom da_agents.agents.base_agent import BaseAgent\nfrom langchain_openai import ChatOpenAI\nfrom ..prompts.question_intent_prompt import QUESTION_INTENT_PROMPT\nclass IntentAgent(BaseAgent):\n\n def __init__(self, agent_id: str):\n deepseek_api_key = \"sk-14090e8029614d0bbfe8c3c9a39f2cac\"\n model = ChatOpenAI(\n api_key=deepseek_api_key,\n model=\"deepseek-chat\",\n base_url=\"https://api.deepseek.com\",\n temperature=1.0\n )\n\n super().__init__(\n model=model,\n name=\"intent_agent\" + agent_id,\n saver=None,\n system_prompt=QUESTION_INTENT_PROMPT \n )\n\n @override\n def __call__(self, state: IntentAgentState) -> IntentAgentState:\n \"\"\"\n node: 智能体节点\n \"\"\"\n # print(state)\n input_messages = state.user_query\n\n resp = self.agent.invoke(\n input={\"messages\": input_messages}\n )\n intent_str = self.IntentProcess(resp[\"messages\"][-1].content)\n\n update_state: IntentAgentState = {\n \"query_intents\": [intent_str]\n }\n # print(self.name, \"\\n\", intent_str, \"\\n\\n\")\n\n return update_state\n\n def IntentProcess(self, unprocessed_intent: str) -> str:\n # 打印原始响应以供调试\n print(\"Raw Intent Response:\\n\", unprocessed_intent)\n \n # 1. 情况一:标准的 ```json 包裹\n if \"```json\" in unprocessed_intent:\n return unprocessed_intent.split(\"```json\")[1].split(\"```\")[0].strip()\n \n # 2. 情况二:没有 json 标识,只有 ``` 包裹\n elif \"```\" in unprocessed_intent:\n # 这种情况下,内容通常在第一个 ``` 和最后一个 ``` 之间\n # 使用切片跳过第一个 ``` (长度3)\n content = unprocessed_intent.split(\"```\", 1)[1]\n # 再去掉结尾的 ``` (如果有的话)\n return content.split(\"```\")[0].strip()\n \n # 3. 情况三:没有任何代码块标记,直接就是纯文本\n else:\n return unprocessed_intent.strip()\n\n\nif __name__ == \"__main__\":\n agent = IntentAgent(\"1\")\n test_message = HumanMessage(\"请找出成绩最高的8名学生\")\n test_state: IntentAgentState\n\n resp_state = agent(test_state)\n print(resp_state.query_intents[0])"}, "e5470050b50f666526d345213a218d943b48d3c4": {"t": 1785773683.6574552, "n": 2280, "h": "e9d1c195aa9c1d1f", "c": "from typing import override\n\nfrom langchain_core.messages import AIMessage, HumanMessage\n\nfrom da_agents.state.state import AgentState\nfrom da_agents.agents.base_agent import BaseAgent\nfrom langchain_openai import ChatOpenAI\nfrom ..prompts.question_intent_prompt import QUESTION_INTENT_PROMPT\nclass IntentAgent(BaseAgent):\n\n def __init__(self, agent_id: str):\n deepseek_api_key = \"sk-14090e8029614d0bbfe8c3c9a39f2cac\"\n model = ChatOpenAI(\n api_key=deepseek_api_key,\n model=\"deepseek-chat\",\n base_url=\"https://api.deepseek.com\",\n temperature=1.0\n )\n\n super().__init__(\n model=model,\n name=\"intent_agent\" + agent_id,\n saver=None,\n system_prompt=QUESTION_INTENT_PROMPT \n )\n\n @override\n def __call__(self, state: AgentState) -> AgentState:\n \"\"\"\n node: 智能体节点\n \"\"\"\n # print(state)\n input_messages = list(state.messages)\n\n resp = self.agent.invoke(\n input={\"messages\": input_messages}\n )\n intent_str = self.IntentProcess(resp[\"messages\"][-1].content)\n\n update_state: AgentState = {\n \"query_intents\": [intent_str]\n }\n # print(self.name, \"\\n\", intent_str, \"\\n\\n\")\n\n return update_state\n\n def IntentProcess(self, unprocessed_intent: str) -> str:\n # 打印原始响应以供调试\n print(\"Raw Intent Response:\\n\", unprocessed_intent)\n \n # 1. 情况一:标准的 ```json 包裹\n if \"```json\" in unprocessed_intent:\n return unprocessed_intent.split(\"```json\")[1].split(\"```\")[0].strip()\n \n # 2. 情况二:没有 json 标识,只有 ``` 包裹\n elif \"```\" in unprocessed_intent:\n # 这种情况下,内容通常在第一个 ``` 和最后一个 ``` 之间\n # 使用切片跳过第一个 ``` (长度3)\n content = unprocessed_intent.split(\"```\", 1)[1]\n # 再去掉结尾的 ``` (如果有的话)\n return content.split(\"```\")[0].strip()\n \n # 3. 情况三:没有任何代码块标记,直接就是纯文本\n else:\n return unprocessed_intent.strip()\n\n\nif __name__ == \"__main__\":\n agent = IntentAgent(\"1\")\n test_message = HumanMessage(\"请找出成绩最高的8名学生\")\n test_state: AgentState\n\n resp_state = agent(test_state)\n print(resp_state.query_intents[0])"}, "4447b8bbb7612aa322b59004f811ba386c018a9f": {"t": 1785773684.8327694, "n": 1717, "h": "5ca5cf45bc7fde22", "c": "from typing import override\n\nfrom langchain_core.messages import AIMessage, HumanMessage\n\nfrom da_agents.state.state import AgentState\nfrom da_agents.agents.base_agent import BaseAgent\nfrom langchain_openai import ChatOpenAI\nfrom ..prompts.question_intent_prompt import QUESTION_INTENT_PROMPT\nclass IntentAgent(BaseAgent):\n\n def __init__(self, agent_id: str):\n deepseek_api_key = \"sk-14090e8029614d0bbfe8c3c9a39f2cac\"\n model = ChatOpenAI(\n api_key=deepseek_api_key,\n model=\"deepseek-chat\",\n base_url=\"https://api.deepseek.com\",\n temperature=1.0\n )\n\n super().__init__(\n model=model,\n name=\"intent_agent\" + agent_id,\n saver=None,\n system_prompt=QUESTION_INTENT_PROMPT \n )\n\n @override\n def __call__(self, state: AgentState) -> AgentState:\n \"\"\"\n node: 智能体节点\n \"\"\"\n # print(state)\n input_messages = list(state.messages)\n\n resp = self.agent.invoke(\n input={\"messages\": input_messages}\n )\n intent_str = self.IntentProcess(resp[\"messages\"][-1].content)\n\n update_state: AgentState = {\n \"query_intents\": [intent_str]\n }\n # print(self.name, \"\\n\", intent_str, \"\\n\\n\")\n\n return update_state\n\n def IntentProcess(self, unprocessed_intent: str) -> str:\n # 提取从大括号之间的内容,包括大括号\n return unprocessed_intent.split(\"```json\")[1].split(\"```\")[0].strip()\n\n\nif __name__ == \"__main__\":\n agent = IntentAgent(\"1\")\n test_message = HumanMessage(\"请找出成绩最高的8名学生\")\n test_state: AgentState\n\n resp_state = agent(test_state)\n print(resp_state.query_intents[0])"}, "20abdee45d7756b7d0474c0cfa1b9c98dede0971": {"t": 1785773686.3709817, "n": 6936, "h": "1298e29f37677ee8", "c": "from typing import override\n\nfrom langchain_core.messages import AIMessage, HumanMessage\n\nfrom da_agents.State.state import AgentState\nfrom da_agents.agents.base_agent import BaseAgent\nfrom langchain_openai import ChatOpenAI\nclass IntentAgent(BaseAgent):\n\n def __init__(self, agent_id: str):\n gemini_api_key = \"AIzaSyCUdQ9vzxNYP_qMJZ8gT2HDkgJb0CkKQhk\"\n deepseek_api_key = \"sk-14090e8029614d0bbfe8c3c9a39f2cac\"\n model = ChatOpenAI(\n api_key=deepseek_api_key,\n model=\"deepseek-chat\",\n # base_url=\"https://generativelanguage.googleapis.com/v1beta/openai/\",\n base_url=\"https://api.deepseek.com\",\n temperature=1.0\n )\n\n super().__init__(\n model=model,\n name=\"intent_agent\" + agent_id,\n saver=None,\n system_prompt=\"\"\"\n You are an expert in identifying user query intent, helping users to clearly analyze the purpose of the query so that they can write SQL later.\n \n Please analyze the query intent in detail based on the natural language questions raised by the user, and conduct specific and detailed decomposition from the following directions:\n 1. Query type\n What type of information does the user want to get? (such as: summary, sum, count, average, find details, get list, analyze trends, find maximum/minimum values, etc.)\n \n 2. Core query indicators\n What is the subject of user attention? (such as: orders, users, products, sales, visits, etc.)\n What specific information needs to be displayed? (such as: date, name, amount, status, etc.)\n \n 3. Restriction or filtering conditions\n Does the user limit the time range, region, status, type and other conditions?\n Are there specific filtering requirements? (such as: only look at the last week, only look at completed orders, only look at a certain region, etc.)\n Restriction or filtering conditions are very important, please describe the filtering conditions clearly.\n \n 4. Aggregation/statistical requirements\n What are the dimensions of statistics? (e.g. by day, month, time period, user, product, etc.)\n Does the user need to group the results? (e.g. by month, department, category, etc.)\n \n 5. Sorting requirements\n Does the user require the results to be sorted by a certain field? (e.g. by time in reverse order, by amount in ascending order, by quantity in descending order, etc.)\n \n 6. Output requirements\n What is the expected result display format for the user? (e.g. detailed list, summary table, single value, trend chart data, etc.)\n Please output the analysis content of each item in detail according to the above direction.\n There can be multiple filter conditions.\n If sorting is not required, the sorting fields and order can be empty.\n \n Output format\n ```json\n {{\n \"query_type\": \"The business action of the query, such as: summary, sum, count, average, find details, get list, analyze trends, find maximum/minimum values, etc.\",\n \"query_indicator\": [\"Fields that need to be returned or calculated, such as the number of registered people, order amount\"],\n \"filter_conditions\": {{\n \"Field 1\": \"Condition 1\",\n \"Field 2\": \"Condition 2\"\n }},\n \"aggregation_dimension\": [\"Which fields to group by, such as month, user ID\"],\n \"sort_method\": [\n {{\n \"sort_field\": \"Fields to be sorted\",\n \"sort_order\": \"asc/desc\"\n }}\n ],\n \"brief_description\": \"Briefly describe the analysis process\"\n }}\n ```\n Here are a few examples of intent extraction, where \"User Query\" is the user input and \"Output\" is the sample output:\n \n Example 1:\n User Query: “帮我查一下过去30天内,北京地区状态为‘已支付’的订单详情,按支付时间倒序排列。”\n Output:\n ```json\n {{\n \"query_type\": \"get list/find details\",\n \"query_indicator\": [\"order_id\", \"user_id\", \"order_amount\", \"payment_time\", \"status\"],\n \"filter_conditions\": {\n \"payment_time\": \"last 30 days\",\n \"region\": \"Beijing\",\n \"status\": \"paid\"\n },\n \"aggregation_dimension\": [],\n \"sort_method\": [\n {\n \"sort_field\": \"payment_time\",\n \"sort_order\": \"desc\"\n }\n ],\n \"brief_description\": \"The user wants to retrieve detailed records of paid orders in Beijing from the past month, requiring a list output sorted by time.\"\n }}\n ```\n Example 2:\n User Query: \"统计今年每个月各产品分类的总销售额,只看总额超过10万元的分类,结果按金额从高到低显示。\"\n Output:\n ```json\n {{\n \"query_type\": \"sum/summary\",\n \"query_indicator\": [\"product_category\", \"month\", \"SUM(sales_amount)\"],\n \"filter_conditions\": {\n \"year\": \"current year\",\n \"total_sales_amount\": \"> 100,000\"\n },\n \"aggregation_dimension\": [\"month\", \"product_category\"],\n \"sort_method\": [\n {\n \"sort_field\": \"total_sales_amount\",\n \"sort_order\": \"desc\"\n }\n ],\n \"brief_description\": \"The user intends to perform a multi-dimensional analysis (time and category) on sales data, with a post-aggregation filter on the total amount.\"\n }}\n ```\n \"\"\"\n )\n\n @override\n def __call__(self, state: AgentState) -> AgentState:\n \"\"\"\n node: 智能体节点\n \"\"\"\n # print(state)\n input_messages = list(state.messages)\n\n resp = self.agent.invoke(\n input={\"messages\": input_messages}\n )\n intent_str = self.IntentProcess(resp[\"messages\"][-1].content)\n\n update_state: AgentState = {\n \"messages\": AIMessage(resp[\"messages\"][-1].content),\n \"query_intents\": [intent_str]\n }\n # print(self.name, \"\\n\", intent_str, \"\\n\\n\")\n\n return update_state\n\n def IntentProcess(self, unprocessed_intent: str) -> str:\n # 提取从大括号之间的内容,包括大括号\n return unprocessed_intent.split(\"```json\")[1].split(\"```\")[0].strip()\n\n\nif __name__ == \"__main__\":\n agent = IntentAgent(\"1\")\n test_message = HumanMessage(\"请找出成绩最高的8名学生\")\n test_state: AgentState\n\n resp_state = agent(test_state)\n print(resp_state.query_intents[0])"}, "c5f666189aaddc5c6ff9480ff32db30b7091aa71": {"t": 1785773687.5799203, "n": 6930, "h": "2218ad6bdd39d484", "c": "from typing import override\n\nfrom langchain_core.messages import AIMessage, HumanMessage\n\nfrom da_agents.State.state import AgentState\nfrom da_agents.agents.base_agent import BaseAgent\nfrom langchain_openai import ChatOpenAI\nclass IntentAgent(BaseAgent):\n\n def __init__(self, agent_id: str):\n gemini_api_key = \"AIzaSyCUdQ9vzxNYP_qMJZ8gT2HDkgJb0CkKQhk\"\n deepseek_api_key = \"sk-14090e8029614d0bbfe8c3c9a39f2cac\"\n model = ChatOpenAI(\n api_key=deepseek_api_key,\n model=\"deepseek-chat\",\n # base_url=\"https://generativelanguage.googleapis.com/v1beta/openai/\",\n base_url=\"https://api.deepseek.com\",\n temperature=1.0\n )\n\n super().__init__(\n model=model,\n name=\"intent_agent\" + agent_id,\n saver=None,\n system_prompt=\"\"\"\n You are an expert in identifying user query intent, helping users to clearly analyze the purpose of the query so that they can write SQL later.\n \n Please analyze the query intent in detail based on the natural language questions raised by the user, and conduct specific and detailed decomposition from the following directions:\n 1. Query type\n What type of information does the user want to get? (such as: summary, sum, count, average, find details, get list, analyze trends, find maximum/minimum values, etc.)\n \n 2. Core query indicators\n What is the subject of user attention? (such as: orders, users, products, sales, visits, etc.)\n What specific information needs to be displayed? (such as: date, name, amount, status, etc.)\n \n 3. Restriction or filtering conditions\n Does the user limit the time range, region, status, type and other conditions?\n Are there specific filtering requirements? (such as: only look at the last week, only look at completed orders, only look at a certain region, etc.)\n Restriction or filtering conditions are very important, please describe the filtering conditions clearly.\n \n 4. Aggregation/statistical requirements\n What are the dimensions of statistics? (e.g. by day, month, time period, user, product, etc.)\n Does the user need to group the results? (e.g. by month, department, category, etc.)\n \n 5. Sorting requirements\n Does the user require the results to be sorted by a certain field? (e.g. by time in reverse order, by amount in ascending order, by quantity in descending order, etc.)\n \n 6. Output requirements\n What is the expected result display format for the user? (e.g. detailed list, summary table, single value, trend chart data, etc.)\n Please output the analysis content of each item in detail according to the above direction.\n There can be multiple filter conditions.\n If sorting is not required, the sorting fields and order can be empty.\n \n Output format\n ```json\n {{\n \"query_type\": \"The business action of the query, such as: summary, sum, count, average, find details, get list, analyze trends, find maximum/minimum values, etc.\",\n \"query_indicator\": [\"Fields that need to be returned or calculated, such as the number of registered people, order amount\"],\n \"filter_conditions\": {{\n \"Field 1\": \"Condition 1\",\n \"Field 2\": \"Condition 2\"\n }},\n \"aggregation_dimension\": [\"Which fields to group by, such as month, user ID\"],\n \"sort_method\": [\n {{\n \"sort_field\": \"Fields to be sorted\",\n \"sort_order\": \"asc/desc\"\n }}\n ],\n \"brief_description\": \"Briefly describe the analysis process\"\n }}\n ```\n Here are a few examples of intent extraction, where \"User Query\" is the user input and \"Output\" is the sample output:\n \n Example 1:\n User Query: “帮我查一下过去30天内,北京地区状态为‘已支付’的订单详情,按支付时间倒序排列。”\n Output:\n ```json\n {\n \"query_type\": \"get list/find details\",\n \"query_indicator\": [\"order_id\", \"user_id\", \"order_amount\", \"payment_time\", \"status\"],\n \"filter_conditions\": {\n \"payment_time\": \"last 30 days\",\n \"region\": \"Beijing\",\n \"status\": \"paid\"\n },\n \"aggregation_dimension\": [],\n \"sort_method\": [\n {\n \"sort_field\": \"payment_time\",\n \"sort_order\": \"desc\"\n }\n ],\n \"brief_description\": \"The user wants to retrieve detailed records of paid orders in Beijing from the past month, requiring a list output sorted by time.\"\n }\n ```\n Example 2:\n User Query: \"统计今年每个月各产品分类的总销售额,只看总额超过10万元的分类,结果按金额从高到低显示。\"\n Output:\n ```json\n {\n \"query_type\": \"sum/summary\",\n \"query_indicator\": [\"product_category\", \"month\", \"SUM(sales_amount)\"],\n \"filter_conditions\": {\n \"year\": \"current year\",\n \"total_sales_amount\": \"> 100,000\"\n },\n \"aggregation_dimension\": [\"month\", \"product_category\"],\n \"sort_method\": [\n {\n \"sort_field\": \"total_sales_amount\",\n \"sort_order\": \"desc\"\n }\n ],\n \"brief_description\": \"The user intends to perform a multi-dimensional analysis (time and category) on sales data, with a post-aggregation filter on the total amount.\"\n }\n ```\n \"\"\"\n )\n\n @override\n def __call__(self, state: AgentState) -> AgentState:\n \"\"\"\n node: 智能体节点\n \"\"\"\n print(state)\n input_messages = list(state.messages)\n\n resp = self.agent.invoke(\n input={\"messages\": input_messages}\n )\n intent_str = self.IntenTProcess(resp[\"messages\"][-1].content)\n\n update_state: AgentState = {\n \"messages\": AIMessage(resp[\"messages\"][-1].content),\n \"query_intents\": [intent_str]\n }\n # print(self.name, \"\\n\", intent_str, \"\\n\\n\")\n\n return update_state\n\n def IntenTProcess(self, unprocessed_intent: str) -> str:\n # 提取从大括号之间的内容,包括大括号\n return unprocessed_intent.split(\"```json\")[1].split(\"```\")[0].strip()\n\n\nif __name__ == \"__main__\":\n agent = IntentAgent(\"1\")\n test_message = HumanMessage(\"请找出成绩最高的8名学生\")\n test_state: AgentState\n\n resp_state = agent(test_state)\n print(resp_state.query_intents[0])"}}}}