求教:agent ainvote 以及 astream异步执行时无限循环,不输出结果。

如题,我写了一个自定义工具,使用agent来调用它。我保证工具是可以使用的,在尝试异步调用的时候它会重复的执行好多遍,然后卡在某一个地方。程序既不正常结束,也不报错推出。代码如下:


from chatglm_LLM import ChatGlm_LLM

import datetime
import json
import os
import re
import traceback

import torch
import uvicorn
from fastapi import FastAPI, Request, HTTPException, APIRouter
from fastapi_utils.cbv import cbv
import asyncio
from langchain.agents import initialize_agent, AgentType,  AgentExecutor
from langchain_core.output_parsers import StrOutputParser
from langchain.tools import StructuredTool

from global_params import logger
# from untils_tools import func_map, tools
from untils_functions.base_info import base_info

# system_info = {
#     "role": "system",
#     "content": '',
#     # "content": "Answer the following questions as best as you can. You have access to the following tools:",
#     # "content": "你是一个工具调用助手,对于用户的问题,你只能调用对应的工具来回答,你应该拒绝回答不需要调用工具的任何问题,你可以调用以下工具:",
#     "tools": tools
# }
async def main():
    messages = [
                {"role": "system",
                "content": """当使用function时,要牢记其参数已经明确定义,不得随意更改或新增。比如processData(x, y, z),
                    只能接受这三个参数x、y、z,任何额外的参数都是不允许的,并且会导致错误。如果你擅自修改function的参数,就会引发严重错误。"""},
                {"role": "system",
                "content": """当使用function时,请只返回function的必要信息,不要多余。例如processData(x, y, z),请只返回'processData\n{'x'=x, 'y'=y, 'z'=z}',不要多余的信息。"""},
                {"role": "system",
                "content": "影城=影院,影城编码=影院编码,影城名称=影院名称"
                }
                ]
    messages.append({"role": "system", "content": "对所有问题,都请使用中文回答"})
    # messages.append({"role": "user", "content": "{input}"})
    messages.append({"role": "placeholder", "content": "{agent_scratchpad}"})

    model_path = os.environ.get('MODEL_PATH')
    chatglm = ChatGlm_LLM(model_path=model_path)

    output_parser = StrOutputParser()

    from langchain_core.prompts import ChatPromptTemplate
    # tool = StructuredTool.from_function(coroutine=base_info, )
    tools = [base_info
            ]
    promat = ChatPromptTemplate.from_messages(messages)
    llm = promat | chatglm | output_parser

    agent = initialize_agent(tools, llm, agent=AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION, verbose=True, handle_parsing_errors=False)
    # x = await agent.ainvoke('查询北京市海淀区魔影国际影城吉彩店在2023年的总票房')
    async for chunk in agent.astream("查询北京市海淀区魔影国际影城吉彩店在2023年的总票房"):
        # chunks.append(chunk)
        print(chunk, flush=True)
    print('*'*50)
    # print(x)

# app = FastAPI()
# router = APIRouter()
asyncio.run(main())

我看官网上handle_parsing_errors 这个参数可以在大模型输出错误结果的时候自动重新调用,我特意把它关闭,但依然是这样。。最后的输出面板是这样的:

(chatglm4) ps@ps:~$ /home/ps/chatglm4/bin/python /home/ps/ai-datahouse/langchain_test/llm_main.py
2025-03-28 15:52:42.276 WARNING streamlit.runtime.scriptrunner_utils.script_run_context: Thread 'MainThread': missing ScriptRunContext! This warning can be ignored when running in bare mode.
2025-03-28 15:52:42.276 WARNING streamlit.runtime.state.session_state_proxy: Session state does not function when running a script without `streamlit run`
2025-03-28 15:52:42.276 WARNING streamlit.runtime.scriptrunner_utils.script_run_context: Thread 'MainThread': missing ScriptRunContext! This warning can be ignored when running in bare mode.
2025-03-28 15:52:42.276 WARNING streamlit.runtime.scriptrunner_utils.script_run_context: Thread 'MainThread': missing ScriptRunContext! This warning can be ignored when running in bare mode.
2025-03-28 15:52:42.276 WARNING streamlit.runtime.scriptrunner_utils.script_run_context: Thread 'MainThread': missing ScriptRunContext! This warning can be ignored when running in bare mode.
asyncio - 2025-03-28 15:52:42,337 - DEBUG - Using selector: EpollSelector
Loading checkpoint shards: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████| 10/10 [00:03<00:00,  2.56it/s]
/home/ps/ai-datahouse/langchain_test/llm_main.py:92: LangChainDeprecationWarning: LangChain agents will continue to be supported, but it is recommended for new use cases to be built with LangGraph. LangGraph offers a more flexible and full-featured framework for building agents, including support for tool-calling, persistence of state, and human-in-the-loop workflows. For details, refer to the `LangGraph documentation <https://langchain-ai.github.io/langgraph/>`_ as well as guides for `Migrating from AgentExecutor <https://python.langchain.com/docs/how_to/migrate_agent/>`_ and LangGraph's `Pre-built ReAct agent <https://langchain-ai.github.io/langgraph/how-tos/create-react-agent/>`_.
  agent = initialize_agent(tools, llm, agent=AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION, verbose=False, handle_parsing_errors='')
/home/ps/chatglm4/lib/python3.11/site-packages/transformers/generation/configuration_utils.py:628: UserWarning: `do_sample` is set to `False`. However, `temperature` is set to `0.8` -- this flag is only used in sample-based generation modes. You should set `do_sample=True` or unset `temperature`.
  warnings.warn(
/home/ps/chatglm4/lib/python3.11/site-packages/transformers/generation/configuration_utils.py:633: UserWarning: `do_sample` is set to `False`. However, `top_p` is set to `0.8` -- this flag is only used in sample-based generation modes. You should set `do_sample=True` or unset `top_p`.
  warnings.warn(
/home/ps/chatglm4/lib/python3.11/site-packages/transformers/generation/configuration_utils.py:650: UserWarning: `do_sample` is set to `False`. However, `top_k` is set to `1` -- this flag is only used in sample-based generation modes. You should set `do_sample=True` or unset `top_k`.
  warnings.warn(
asyncio - 2025-03-28 15:52:54,931 - DEBUG - AI response:
Thought: 用户想要查询魔影国际影城吉彩店在2023年的总票房,需要使用base_info函数,并传入影院名称为查询条件。
Action: ```{"action": "base_info", "action_input": {"cinemaName": "魔影国际影城吉彩店", "business_date_start": "2023-01-01", "business_date_end": "2023-12-31"}}```
{'actions': [AgentAction(tool='base_info', tool_input={'cinemaName': '魔影国际影城吉彩店', 'business_date_start': '2023-01-01', 'business_date_end': '2023-12-31'}, log='\nThought: 用户想要查询魔影国际影城吉彩店在2023年的总票房,需要使用base_info函数,并传入影院名称为查询条件。\nAction: ```{"action": "base_info", "action_input": {"cinemaName": "魔影国际影城吉彩店", "business_date_start": "2023-01-01", "business_date_end": "2023-12-31"}}```')], 'messages': [AIMessage(content='\nThought: 用户想要查询魔影国际影城吉彩店在2023年的总票房,需要使用base_info函数,并传入影院名称为查询条件。\nAction: ```{"action": "base_info", "action_input": {"cinemaName": "魔影国际影城吉彩店", "business_date_start": "2023-01-01", "business_date_end": "2023-12-31"}}```', additional_kwargs={}, response_metadata={})]}
{'steps': [AgentStep(action=AgentAction(tool='base_info', tool_input={'cinemaName': '魔影国际影城吉彩店', 'business_date_start': '2023-01-01', 'business_date_end': '2023-12-31'}, log='\nThought: 用户想要查询魔影国际影城吉彩店在2023年的总票房,需要使用base_info函数,并传入影院名称为查询条件。\nAction: ```{"action": "base_info", "action_input": {"cinemaName": "魔影国际影城吉彩店", "business_date_start": "2023-01-01", "business_date_end": "2023-12-31"}}```'), observation={'type': 'dict', 'result': {'影院编码': '11062091', '影院名称': '北京市海淀区魔影国际影城吉彩店', '所属院线': '华夏联合', '院线全称': '北京华夏联合电影院线', '开业时间': '2021-02-08', '影厅数量': 7, '座位数量': 1342, '今年票房': 0.0, '今年人次': 0, '今年排座': 0, '今年排名': 0, '日票房': 0.0, '日人次': 0, '日排座': 0, '日排名': 0, '人民院线': '-', '艺连院线': '-', '营业状态': 2, '影投名称': '-', '城市级别': '一线城市', '所属大区': '华北地区', '地区': '北京市 北京市 海淀区 田村路街道', '省份': '北京市', '城市': '北京市', '区县': '海淀区', '工商名称': '北京魔影吉彩影城有限公司', '电话': '13810533602', '联系人': '孙唯昊', '详细地址': '玉海园五里22号配套商业楼玉兴园地下一层B1-03号一层1-03号二层2-03号三层3-03号四层4-03号', '经度': '116.26450689614059', '纬度': '39.92792973641576', 'cgs影厅数量': 0, 'cgs座位数量': 0, 'cinity影厅数量': 0, 'cinity座位数量': 0, 'imax影厅数量': 0, 'imax座位数量': 0, 'dobly影厅数量': 0, 'dobly座位数量': 0, '上年票房': 0.0, '票房体量': 0.04, '特殊厅': '', '座位体量': '4', '最新票房日期': '2022-03-15', '是否活跃': 0, '影院所属省份日排名': 2, '影院所属省份年排名': 7, '影院所属城市日排名': 1, '影院所属城市年排名': 2, '影院所属区县日排名': 6, '影院所属区县年排名': 4, '影院所属城市等级日排名': 2, '影院所属城市等级年排名': 3, '影院所属大区日排名': 2, '影院所属大区年排名': 3, '票房日期': '2025-03-27', '数据创建时间': '2025-03-28T07:17:24.000+00:00', '历年票房表现': [{'年度': '2022', '票房': 181079825, '上座率': 699, '平均票价': 6718, '全国排名': '5719', '省份排名': '210', '城市排名': '32'}, {'年度': '2021', '票房': 666485160, '上座率': 654, '平均票价': 5189, '全国排名': '1926', '省份排名': '133', '城市排名': '20'}], '昨日上座率': 0, '今年上座率': 0}})], 'messages': [HumanMessage(content='{"type": "dict", "result": {"影院编码": "11062091", "影院名称": "北京市海淀区魔影国际影城吉彩店", "所属院线": "华夏联合", "院线全称": "北京华夏联合电影院线", "开业时间": "2021-02-08", "影厅数量": 7, "座位数量": 1342, "今年票房": 0.0, "今年人次": 0, "今年排座": 0, "今年排名": 0, "日票房": 0.0, "日人次": 0, "日排座": 0, "日排名": 0, "人民院线": "-", "艺连院线": "-", "营业状态": 2, "影投名称": "-", "城市级别": "一线城市", "所属大区": "华北地区", "地区": "北京市 北京市 海淀区 田村路街道", "省份": "北京市", "城市": "北京市", "区县": "海淀区", "工商名称": "北京魔影吉彩影城有限公司", "电话": "13810533602", "联系人": "孙唯昊", "详细地址": "玉海园五里22号配套商业楼玉兴园地下一层B1-03号一层1-03号二层2-03号三层3-03号四层4-03号", "经度": "116.26450689614059", "纬度": "39.92792973641576", "cgs影厅数量": 0, "cgs座位数量": 0, "cinity影厅数量": 0, "cinity座位数量": 0, "imax影厅数量": 0, "imax座位数量": 0, "dobly影厅数量": 0, "dobly座位数量": 0, "上年票房": 0.0, "票房体量": 0.04, "特殊厅": "", "座位体量": "4", "最新票房日期": "2022-03-15", "是否活跃": 0, "影院所属省份日排名": 2, "影院所属省份年排名": 7, "影院所属城市日排名": 1, "影院所属城市年排名": 2, "影院所属区县日排名": 6, "影院所属区县年排名": 4, "影院所属城市等级日排名": 2, "影院所属城市等级年排名": 3, "影院所属大区日排名": 2, "影院所属大区年排名": 3, "票房日期": "2025-03-27", "数据创建时间": "2025-03-28T07:17:24.000+00:00", "历年票房表现": [{"年度": "2022", "票房": 181079825, "上座率": 699, "平均票价": 6718, "全国排名": "5719", "省份排名": "210", "城市排名": "32"}, {"年度": "2021", "票房": 666485160, "上座率": 654, "平均票价": 5189, "全国排名": "1926", "省份排名": "133", "城市排名": "20"}], "昨日上座率": 0, "今年上座率": 0}}', additional_kwargs={}, response_metadata={})]}
/home/ps/chatglm4/lib/python3.11/site-packages/transformers/generation/configuration_utils.py:628: UserWarning: `do_sample` is set to `False`. However, `temperature` is set to `0.8` -- this flag is only used in sample-based generation modes. You should set `do_sample=True` or unset `temperature`.
  warnings.warn(
/home/ps/chatglm4/lib/python3.11/site-packages/transformers/generation/configuration_utils.py:633: UserWarning: `do_sample` is set to `False`. However, `top_p` is set to `0.8` -- this flag is only used in sample-based generation modes. You should set `do_sample=True` or unset `top_p`.
  warnings.warn(
/home/ps/chatglm4/lib/python3.11/site-packages/transformers/generation/configuration_utils.py:650: UserWarning: `do_sample` is set to `False`. However, `top_k` is set to `1` -- this flag is only used in sample-based generation modes. You should set `do_sample=True` or unset `top_k`.
  warnings.warn(
asyncio - 2025-03-28 15:53:02,362 - DEBUG - AI response:
Thought: 用户想要查询魔影国际影城吉彩店在2023年的总票房,需要使用base_info函数,并传入影院名称为查询条件。
Action: ```{"action": "base_info", "action_input": {"cinemaName": "魔影国际影城吉彩店", "business_date_start": "2023-01-01", "business_date_end": "2023-12-31"}}```
{'actions': [AgentAction(tool='base_info', tool_input={'cinemaName': '魔影国际影城吉彩店', 'business_date_start': '2023-01-01', 'business_date_end': '2023-12-31'}, log='\nThought: 用户想要查询魔影国际影城吉彩店在2023年的总票房,需要使用base_info函数,并传入影院名称为查询条件。\nAction: ```{"action": "base_info", "action_input": {"cinemaName": "魔影国际影城吉彩店", "business_date_start": "2023-01-01", "business_date_end": "2023-12-31"}}```')], 'messages': [AIMessage(content='\nThought: 用户想要查询魔影国际影城吉彩店在2023年的总票房,需要使用base_info函数,并传入影院名称为查询条件。\nAction: ```{"action": "base_info", "action_input": {"cinemaName": "魔影国际影城吉彩店", "business_date_start": "2023-01-01", "business_date_end": "2023-12-31"}}```', additional_kwargs={}, response_metadata={})]}
{'steps': [AgentStep(action=AgentAction(tool='base_info', tool_input={'cinemaName': '魔影国际影城吉彩店', 'business_date_start': '2023-01-01', 'business_date_end': '2023-12-31'}, log='\nThought: 用户想要查询魔影国际影城吉彩店在2023年的总票房,需要使用base_info函数,并传入影院名称为查询条件。\nAction: ```{"action": "base_info", "action_input": {"cinemaName": "魔影国际影城吉彩店", "business_date_start": "2023-01-01", "business_date_end": "2023-12-31"}}```'), observation={'type': 'dict', 'result': {'影院编码': '11062091', '影院名称': '北京市海淀区魔影国际影城吉彩店', '所属院线': '华夏联合', '院线全称': '北京华夏联合电影院线', '开业时间': '2021-02-08', '影厅数量': 7, '座位数量': 1342, '今年票房': 0.0, '今年人次': 0, '今年排座': 0, '今年排名': 0, '日票房': 0.0, '日人次': 0, '日排座': 0, '日排名': 0, '人民院线': '-', '艺连院线': '-', '营业状态': 2, '影投名称': '-', '城市级别': '一线城市', '所属大区': '华北地区', '地区': '北京市 北京市 海淀区 田村路街道', '省份': '北京市', '城市': '北京市', '区县': '海淀区', '工商名称': '北京魔影吉彩影城有限公司', '电话': '13810533602', '联系人': '孙唯昊', '详细地址': '玉海园五里22号配套商业楼玉兴园地下一层B1-03号一层1-03号二层2-03号三层3-03号四层4-03号', '经度': '116.26450689614059', '纬度': '39.92792973641576', 'cgs影厅数量': 0, 'cgs座位数量': 0, 'cinity影厅数量': 0, 'cinity座位数量': 0, 'imax影厅数量': 0, 'imax座位数量': 0, 'dobly影厅数量': 0, 'dobly座位数量': 0, '上年票房': 0.0, '票房体量': 0.04, '特殊厅': '', '座位体量': '4', '最新票房日期': '2022-03-15', '是否活跃': 0, '影院所属省份日排名': 2, '影院所属省份年排名': 7, '影院所属城市日排名': 1, '影院所属城市年排名': 2, '影院所属区县日排名': 6, '影院所属区县年排名': 4, '影院所属城市等级日排名': 2, '影院所属城市等级年排名': 3, '影院所属大区日排名': 2, '影院所属大区年排名': 3, '票房日期': '2025-03-27', '数据创建时间': '2025-03-28T07:17:24.000+00:00', '历年票房表现': [{'年度': '2022', '票房': 181079825, '上座率': 699, '平均票价': 6718, '全国排名': '5719', '省份排名': '210', '城市排名': '32'}, {'年度': '2021', '票房': 666485160, '上座率': 654, '平均票价': 5189, '全国排名': '1926', '省份排名': '133', '城市排名': '20'}], '昨日上座率': 0, '今年上座率': 0}})], 'messages': [HumanMessage(content='{"type": "dict", "result": {"影院编码": "11062091", "影院名称": "北京市海淀区魔影国际影城吉彩店", "所属院线": "华夏联合", "院线全称": "北京华夏联合电影院线", "开业时间": "2021-02-08", "影厅数量": 7, "座位数量": 1342, "今年票房": 0.0, "今年人次": 0, "今年排座": 0, "今年排名": 0, "日票房": 0.0, "日人次": 0, "日排座": 0, "日排名": 0, "人民院线": "-", "艺连院线": "-", "营业状态": 2, "影投名称": "-", "城市级别": "一线城市", "所属大区": "华北地区", "地区": "北京市 北京市 海淀区 田村路街道", "省份": "北京市", "城市": "北京市", "区县": "海淀区", "工商名称": "北京魔影吉彩影城有限公司", "电话": "13810533602", "联系人": "孙唯昊", "详细地址": "玉海园五里22号配套商业楼玉兴园地下一层B1-03号一层1-03号二层2-03号三层3-03号四层4-03号", "经度": "116.26450689614059", "纬度": "39.92792973641576", "cgs影厅数量": 0, "cgs座位数量": 0, "cinity影厅数量": 0, "cinity座位数量": 0, "imax影厅数量": 0, "imax座位数量": 0, "dobly影厅数量": 0, "dobly座位数量": 0, "上年票房": 0.0, "票房体量": 0.04, "特殊厅": "", "座位体量": "4", "最新票房日期": "2022-03-15", "是否活跃": 0, "影院所属省份日排名": 2, "影院所属省份年排名": 7, "影院所属城市日排名": 1, "影院所属城市年排名": 2, "影院所属区县日排名": 6, "影院所属区县年排名": 4, "影院所属城市等级日排名": 2, "影院所属城市等级年排名": 3, "影院所属大区日排名": 2, "影院所属大区年排名": 3, "票房日期": "2025-03-27", "数据创建时间": "2025-03-28T07:17:24.000+00:00", "历年票房表现": [{"年度": "2022", "票房": 181079825, "上座率": 699, "平均票价": 6718, "全国排名": "5719", "省份排名": "210", "城市排名": "32"}, {"年度": "2021", "票房": 666485160, "上座率": 654, "平均票价": 5189, "全国排名": "1926", "省份排名": "133", "城市排名": "20"}], "昨日上座率": 0, "今年上座率": 0}}', additional_kwargs={}, response_metadata={})]}
/home/ps/chatglm4/lib/python3.11/site-packages/transformers/generation/configuration_utils.py:628: UserWarning: `do_sample` is set to `False`. However, `temperature` is set to `0.8` -- this flag is only used in sample-based generation modes. You should set `do_sample=True` or unset `temperature`.
  warnings.warn(
/home/ps/chatglm4/lib/python3.11/site-packages/transformers/generation/configuration_utils.py:633: UserWarning: `do_sample` is set to `False`. However, `top_p` is set to `0.8` -- this flag is only used in sample-based generation modes. You should set `do_sample=True` or unset `top_p`.
  warnings.warn(
/home/ps/chatglm4/lib/python3.11/site-packages/transformers/generation/configuration_utils.py:650: UserWarning: `do_sample` is set to `False`. However, `top_k` is set to `1` -- this flag is only used in sample-based generation modes. You should set `do_sample=True` or unset `top_k`.
  warnings.warn(

可以看到,一直在循环输出一些东西,并且在我粘贴的时候,程序依然没有结束