langflow 将自定义回调集成到以Recode格式输出代理步骤中

drnojrws  于 2个月前  发布在  其他
关注(0)|答案(4)|浏览(37)

I would like to incorporate a custom callback into the ToolCallingAgentComponent to output the agent's steps in a Recode format. Below is my current source code. Could you guide me on which part to modify?

def print_callback(step_output):
    print(f"Step: {step_output.step}")
    print(f"Action: {step_output.action}")
    print(f"Observation: {step_output.observation}")
    print(f"Thought: {step_output.thought}")

if "input" not in user_prompt:
    raise ValueError("Prompt must contain 'input' key.")
messages = [
    ("system", system_message),
    (
        "placeholder",
        "{chat_history}",
    ),
    ("human", user_prompt),
    ("placeholder", "{agent_scratchpad}"),
]

# activity_diagram = diagram.activity_diagram_callback(note_max_length=2000)
# sequence_diagram = diagram.sequence_diagram_callback(note_max_length=2000)
# handler = StdOutCallbackHandler()

prompt = ChatPromptTemplate.from_messages(messages)
agent = create_tool_calling_agent(llm, tools, prompt)
result = await self.run_agent(agent, input_value, tools, message_history, handle_parsing_errors)
self.status = result
ijxebb2r

ijxebb2r1#

要将自定义回调集成到 ToolCallingAgentComponent 中以输出代理的步骤,您需要修改 build 方法以包含自定义回调逻辑。以下是您需要修改的部分代码:

  1. 定义一个继承自 AsyncCallbackHandler 的自定义回调类。
  2. 修改 run_agent 方法以接受并使用自定义回调。

以下是更新后的代码:


# 定义一个继承自 `AsyncCallbackHandler` 的自定义回调类

class CustomCallback(`AsyncCallbackHandler`):
    pass

# 修改 `run_agent` 方法以接受并使用自定义回调

def `run_agent`(self, callback=None):
    if callback is not None:
        # 使用自定义回调
        self.some_method(callback)
    else:
        # 不使用自定义回调
        self.some_method()

此外,您需要确保 run_agent 方法支持传递回调参数。以下是修改后的 run_agent 方法:

def `run_agent`(self, callback=None):
    # 支持传递回调参数
    if callback is not None:
        # 使用回调函数执行某些操作
        self.some_method(callback)
    else:
        # 不使用回调函数执行某些操作
        self.some_method()

此修改允许 run_agent 方法在代理 1(https://github.com/logspace-ai/langflow/blob/dev/src/backend/base/langflow/components/agents/ToolCallingAgent.py)2(https://github.com/logspace-ai/langflow/blob/dev/src/backend/base/langflow/base/agents/agent.py) 执行过程中接受一个自定义回调函数。

8e2ybdfx

8e2ybdfx2#

根据Dosubot的建议,我已经将Yamon工具调用代理部署到Store中,引用了ToolCallingAgentComponent。看起来Langflow团队打算在Service层实现回调,因此我暂时重写了LCAgentComponent的run_agent方法来实现它。

laawzig2

laawzig23#

你好,@YamonBot ,
需要帮助解决这个案例吗?如果不需要,请告知我们是否可以关闭此问题。

z2acfund

z2acfund4#

你好,

我在Discord上发布了一个类似的请求,关于这个主题。我目前反对在LangSmith或LangWatch中实现的日志记录方法。这些方法还使用LangChain回调进行日志记录,这使得很难响应每个解决方案的更新。由于解决方案的更新基于LangChain,每个解决方案都有其专门处理它的方式。这种配置应该在回调组件级别进行管理。component类应该默认定义一个成员来处理回调。
https://discord.com/channels/1116803230643527710/1117938353761947678/1260442118208491622

相关问题