You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Initialize_Agent完成链响应中整合LangChain Agent结果?

问题描述

需要将Agent执行链的最终答案整合到LLM的响应中,当前LLM仅输出后续引导语Would you like to select one of these indices?,未包含Agent生成的最终答案。

当前输出示例

Observation: Final answer here with SQLResult: The available indices in the USA are:
1. S&P 400 Consumer Staples (Sector) - indexCode: 30
2. S&P United Arab Emirates LargeMidCap (US Dollar) - indexCode: SPCPMICAEUSD
3. S&P Qatar LargeMidCap (US Dollar) - indexCode: SPCPMICQAUSD
Thought:I now know the final answer.
Final Answer: The available indices in the USA are:
1. S&P 400 Consumer Staples (Sector) - indexCode: 30
2. S&P United Arab Emirates LargeMidCap (US Dollar) - indexCode: SPCPMICAEUSD
3. S&P Qatar LargeMidCap (US Dollar) - indexCode: SPCPMICQAUSD

> Finished chain.
'Would you like to select one of these indices?'

当前代码片段

from langchain.chains.conversation.memory import ConversationBufferWindowMemory
from langchain.agents import ZeroShotAgent, Tool, AgentExecutor
from langchain.memory import ConversationBufferMemory, ReadOnlySharedMemory
from langchain.llms import OpenAI
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
from langchain.utilities import GoogleSearchAPIWrapper
from langchain.agents.self_ask_with_search.output_parser import SelfAskOutputParser
from langchain.callbacks.streaming_stdout_final_only import (
    FinalStreamingStdOutCallbackHandler,
)

#prompt = PromptTemplate(input_variables=["input", "chat_history"], template=TEMPLATE)
memory = ConversationBufferMemory(memory_key="chat_history")
readonlymemory = ReadOnlySharedMemory(memory=memory)
tool_names = [tool.name for tool in tools]

model3 = "text-davinci-003"
deployment_id3 = 'text-davinci-003'

from langchain.chat_models import ChatOpenAI


llm = OpenAI(temperature=0, openai_api_key= api_key,verbose=False, n = 1,deployment_id=deployment_id3, model=model3, callbacks = callbacks, streaming = True)


agent = initialize_agent(
    agent=AgentType.CONVERSATIONAL_REACT_DESCRIPTION,
    tools=tools,
    llm=llm,
    #output_parser = SelfAskOutputParser(),
    verbose=False,
    max_iterations=3,
    #callbacks=callbacks,
    early_stopping_method='generate',
    memory=readonlymemory, 
    agent_kwargs={
        'prefix':TEMPLATE,
        'suffix':SUFFIX,
    }
)

解决方案建议

1. 调整Agent的Prompt模板

修改agent_kwargs中的suffix,明确指令LLM先输出Agent的最终答案,再添加后续引导语。示例:

SUFFIX = """
{chat_history}
Question: {input}
{agent_scratchpad}
请先输出上述问题的最终答案,然后询问用户:"Would you like to select one of these indices?"
"""

2. 自定义输出处理逻辑

调用Agent后,手动提取Final Answer并与LLM引导语拼接:

# 执行Agent获取结果
result = agent.run(user_query)

# 从Agent输出中提取Final Answer(可通过字符串匹配或结构化解析实现)
final_answer = result.split("Final Answer:")[-1].split("\n> Finished chain.")[0].strip()

# 拼接完整响应
full_response = f"{final_answer}\n\nWould you like to select one of these indices?"

3. 用自定义回调捕获Final Answer

通过回调函数在Agent执行完成时捕获Final Answer,再生成完整响应:

from langchain.callbacks.base import BaseCallbackHandler

class FinalAnswerCaptureCallback(BaseCallbackHandler):
    def __init__(self):
        self.final_answer = ""

    def on_agent_finish(self, finish, **kwargs):
        self.final_answer = finish.return_values["output"]

# 初始化回调与LLM
capture_callback = FinalAnswerCaptureCallback()
llm = OpenAI(temperature=0, openai_api_key=api_key, callbacks=[capture_callback])

# 执行Agent
agent.run(user_query)

# 生成完整响应
full_response = f"{capture_callback.final_answer}\n\nWould you like to select one of these indices?"

4. 优化记忆配置

将Agent的Final Answer存入对话记忆,让LLM生成响应时自动读取:

# 使用可写入的ConversationBufferMemory替代ReadOnlySharedMemory
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)

agent = initialize_agent(
    agent=AgentType.CONVERSATIONAL_REACT_DESCRIPTION,
    tools=tools,
    llm=llm,
    verbose=False,
    max_iterations=3,
    early_stopping_method='generate',
    memory=memory, 
    agent_kwargs={
        'prefix':TEMPLATE,
        'suffix':SUFFIX,
    }
)

内容的提问来源于stack exchange,提问作者James Scott

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 16:30:06