DeepAgents简介
基于langchain的harness范式的智能体框架DeepAgents。一个重要特征就是自动摘要。
官网参考:https://docs.langchain.com/oss/python/deepagents/context-engineering#context-compression
自动摘要
重要参数:max_input_tokens
如果没有设置该参数,无法触发自动摘要;
长上下文时,还会因为上下文超长,导致模型报错。
参数设置
利用langchain_openai.ChatOpenAI,构建模型,来创建deepagents,是无法设置该参数的。
上代码如下:
# 基础配置 base_url = config.get("LLM").get("BASE_URL") api_key = config.get("LLM").get("APP_KEY") model_name = config.get("LLM").get("MODEL") # 参数设置 custom_profile = { "max_input_tokens": 100_000, "tool_calling": True, "structured_output": True } # 模型构造 open_ai_client = init_chat_model( model_name, model_provider="openai", base_url=base_url, api_key=api_key, profile=custom_profile ) # 创建DeepAgent实例 agent = create_deep_agent( model=open_ai_client, system_prompt=sys_prompt, middleware=[ log_tool_calls ], tools=tools, permissions=permissions, backend=backend, checkpointer=mongo_db_checkpoint_saver )
流式输出
流式输出的时候,可以打印,自动摘要相关日志。
代码如下:
for token, metadata in deep_agent.stream( {"messages": messages}, stream_mode="messages", config=deep_agent_config ): if metadata.get("lc_source") == "summarization": print(f"------------ {token} | {metadata}") continue if isinstance(token, AIMessageChunk): pass # 工具调用 if isinstance(token, ToolMessage): pass
那么触发自动摘要时,如下日志:
AI: ------------ content="Error code: 400 - {'error': {'message': 'max_tokens must be at least 1, got -3011.', 'type': 'BadRequestError', 'param': None, 'code': 400}}" additional_kwargs={} response_metadata={'finish_reason': 'error', 'model_name': 'deepseek-v32', 'model_provider': 'openai'} id='lc_run--019dd3a2-e201-77b0-b46e-76a2e161a223' tool_calls=[] invalid_tool_calls=[] tool_call_chunks=[] | {'ls_integration': 'langchain_chat_model', 'versions': {'deepagents': '0.5.3'}, 'lc_agent_name': None, 'thread_id': 'sdrowero234249jdoisjfoiet93xxxxx', 'langgraph_step': 98, 'langgraph_node': 'model', 'langgraph_triggers': ('branch:to:model',), 'langgraph_path': ('__pregel_pull', 'model'), 'langgraph_checkpoint_ns': 'model:9aa67f14-cb76-bc8c-7a9b-5565ea76f89c', 'lc_source': 'summarization', 'checkpoint_ns': 'model:9aa67f14-cb76-bc8c-7a9b-5565ea76f89c', 'ls_provider': 'openai', 'ls_model_name': 'DeepSeek-V3.2', 'ls_model_type': 'chat', 'ls_temperature': None} ------------ content='' additional_kwargs={} response_metadata={} id='lc_run--019dd3a2-e201-77b0-b46e-76a2e161a223' tool_calls=[] invalid_tool_calls=[] tool_call_chunks=[] chunk_position='last' | {'ls_integration': 'langchain_chat_model', 'versions': {'deepagents': '0.5.3'}, 'lc_agent_name': None, 'thread_id': 'sdrowero234249jdoisjfoiet93xxxxx', 'langgraph_step': 98, 'langgraph_node': 'model', 'langgraph_triggers': ('branch:to:model',), 'langgraph_path': ('__pregel_pull', 'model'), 'langgraph_checkpoint_ns': 'model:9aa67f14-cb76-bc8c-7a9b-5565ea76f89c', 'lc_source': 'summarization', 'checkpoint_ns': 'model:9aa67f14-cb76-bc8c-7a9b-5565ea76f89c', 'ls_provider': 'openai', 'ls_model_name': 'DeepSeek-V3.2', 'ls_model_type': 'chat', 'ls_temperature': None} 非常好!你已经具备了扎实的研究基础。我给你一个详细的转型和发论文策略:
可以看出:
1、自动摘要也是被动的,先用上下文调用大语言模型;
2、调用失败之后,触发自动摘要;
3、自动摘要之后,再重新调用大语言模型,生成响应。