更新时间:2024-08-29 GMT+08:00
分享

监听Agent

一次Agent的响应如果涉及到多个任务的分解,往往会执行比较长的时间,此时可以对agent的执行过程进行监听。

AgentListener的定义如下:

public interface AgentListener {
    /**
     * Session启动时调用
     *
     * @param agentSession AgentSession
     */
    default void onSessionStart(AgentSession agentSession) {
    }
    /**
     * Session迭代过程中调用
     *
     * @param agentSession AgentSession
     */
    default void onSessionIteration(AgentSession agentSession) {
    }
    /**
     * onSessionIteration调用结束后,检查Agent是否需要终止,如果需要终止,则返回true,默认不终止
     * 可以在终止前对agentSession进行修改,如:修改agent的finalAnswer
     *
     * @param agentSession AgentSession
     */
    default boolean onCheckInterruptRequirement(AgentSession agentSession) {
        return false;
    }
    /**
     * Session结束时调用
     *
     * @param agentSession AgentSession
     */
    default void onSessionEnd(AgentSession agentSession) {
    }
}

定义一个监听器

通过实现AgentListener定义一个监听器:

import com.huaweicloud.pangu.dev.sdk.api.agent.AgentListener;

public static class TestAgentListener implements AgentListener {
    @Override
    public void onSessionStart(AgentSession agentSession) {
        System.out.println(agentSession);
    }
    @Override
    public void onSessionIteration(AgentSession agentSession) {
        System.out.println(agentSession);
    }
    @Override
    public void onSessionEnd(AgentSession agentSession) {
        System.out.println(agentSession);
    }
}

上述代码分别对应了Agent的开始、中间过程、结束阶段。

为Agent添加一个监听器

通多调用Agent的addListener接口添加一个监听器:

import com.huaweicloud.pangu.dev.sdk.agent.ReactPanguAgent;
import com.huaweicloud.pangu.dev.sdk.api.llms.LLMs;

public static void initAgent() {
    ReactPanguAgent agent = new ReactPanguAgent(LLMs.of(LLMs.PANGU));
    final TestAgentListener testAgentListener = new TestAgentListener();
    agent.addListener(testAgentListener);
}

其中,listener会在Agent运行时生效。

监听的对象

监听的对象为一个AgentSession:

public class AgentSession {
    /**
     * UUID,在一个session内唯一
     */
    private String sessionId;
    /**
     * Agent返回的最终答案(最后一个AgentAction的输出)
     */
    private String finalAnswer = "";
    /**
     * 本次session的用户query
     */
    private List<ConversationMessage> messages;
    /**
     * 历史Action
     */
    private List<AgentAction> historyAction;
    /**
     * 当前Action
     */
    private AgentAction currentAction;
    /**
     * Agent状态
     */
    private AgentSessionStatus agentSessionStatus;
}

AgentAction包含Agent的工具选择、工具执行结果、思考等信息,AgentSessionStatus为一个枚举,包含Agnet的执行状态。建议直接对Agent的run接口的返回进行修改,以控制Agent的行为。如果想控制中间过程,可以对Agent的runStep的返回进行修改。

通过监听终止Agent的执行

当需要在Agent的执行过程中终止执行时,除了通过setMaxIterations设置Agent的最大迭代次数,也可以通过实现监听器的onCheckInterruptRequirement实现。

agent.addListener(new AgentListener() {
    @Override
    public boolean onCheckInterruptRequirement(AgentSession agentSession) {
        final AgentAction currentAction = agentSession.getCurrentAction();
        // 如果当前的action为capital,则返回capital的工具调用原始返回值
        if ("capital".equals(currentAction.getAction())) {
            agentSession.setFinalAnswer(currentAction.getObservation());
            return true;
        }
        return false;
    }
});

上述例子中,当满足if判断条件时,会直接终止Agent的执行,且finalAnswer被设置为工具的原始返回值。

相关文档