agentscope-java icon indicating copy to clipboard operation
agentscope-java copied to clipboard

[Bug]: postActingEvent.stopAgent();后response.getGenerateReason()没有拿到ACTING_STOP_REQUESTED而是MODEL_STOP

Open chcodex opened this issue 10 months ago • 3 comments

package cn.com.qtgl.agents;

import ch.qos.logback.classic.Level; import ch.qos.logback.classic.Logger; import io.agentscope.core.ReActAgent; import io.agentscope.core.agent.Event; import io.agentscope.core.hook.; import io.agentscope.core.memory.InMemoryMemory; import io.agentscope.core.memory.Memory; import io.agentscope.core.message.; import io.agentscope.core.model.ExecutionConfig; import io.agentscope.core.model.OpenAIChatModel; import io.agentscope.core.tool.Toolkit; import org.slf4j.LoggerFactory; import reactor.core.publisher.Mono;

public class 测试V2 { static { Logger targetLogger = (Logger) LoggerFactory.getLogger("io.agentscope.core.model.OpenAIClient"); targetLogger.setLevel(Level.DEBUG); }

public static void main(String[] args) {

    // =========================
    // 1. 构建 Toolkit & ToolSchema
    // =========================
    Toolkit toolkit = new Toolkit();

    toolkit.registerTool(new Tools());
    Hook hook = new Hook() {

        @Override
        public <T extends HookEvent> Mono<T> onEvent(T event) {
            if (event instanceof PostActingEvent postActingEvent) {
                String tool = postActingEvent.getToolUse().getName();
                if (tool.equals("query_weather")) {
                    postActingEvent.stopAgent();
                }
            }
            return Mono.just(event);
        }
    };

    OpenAIChatModel model = OpenAIChatModel.builder()
            .baseUrl("https://one-api.ketanyun.com/v1")
            .apiKey("sk-xxx")
            .modelName("qwen3-next-80b-a3b-instruct")
            .stream(false)
            .build();
    Memory memory = new InMemoryMemory();

    ReActAgent agent = ReActAgent.builder()
            .name("Assistant")
            .sysPrompt("""
                       你是一个有帮助的 AI 助手。
                       可使用get_current_date工具获取当前日期
                       """)
            .model(model)
            .memory(memory)
            .toolkit(toolkit)
            .hook(hook)
            .modelExecutionConfig(ExecutionConfig.builder().maxAttempts(3).build())
            .build();

    Event event = agent.stream(
            Msg.builder()
                    .textContent("上海明天的天气怎么样?")
                    .build()
    ).blockLast();

    System.out.println(event.getMessage().getGenerateReason());
    System.out.println(event.getMessage().getTextContent());
    //模拟下一轮对话
    event = agent.stream(Msg.builder().textContent("谢谢,再见").build()).blockLast();

    System.out.println(event.getMessage().getTextContent());
}

}

package cn.com.qtgl.agents;

import io.agentscope.core.tool.Tool; import io.agentscope.core.tool.ToolParam;

import java.time.LocalDate;

/**

  • @author 杜文康

  • @date 2026/1/19 22:23 */ public class Tools {

    @Tool(description = "获取指定城市的天气") public String query_weather( @ToolParam(name = "city", description = "城市名称") String city, @ToolParam(name = "date", description = "日期") String date) { return city + " 的天气:晴天,25°C,日期:" + date; }

    @Tool(description = "获取当前日期") public String get_current_date() { return LocalDate.now().toString(); }

} 运行输出: 2026-01-19 22:49:39.047 [main] INFO i.a.c.tool.Toolkit: - Registered tool 'query_weather' in group 'ungrouped' 2026-01-19 22:49:39.051 [main] INFO i.a.c.tool.Toolkit: - Registered tool 'get_current_date' in group 'ungrouped' 2026-01-19 22:49:43.818 [main] DEBUG i.a.c.m.OpenAIClient: - OpenAI request to https://one-api.ketanyun.com/v1/chat/completions: {"model":"qwen3-next-80b-a3b-instruct","messages":[{"role":"system","content":"你是一个有帮助的 AI 助手。\n可使用get_current_date工具获取当前日期\n"},{"role":"user","content":"上海明天的天气怎么样?"}],"stream":false,"tools":[{"type":"function","function":{"name":"query_weather","description":"获取指定城市的天气","parameters":{"type":"object","properties":{"date":{"type":"string","description":"日期"},"city":{"type":"string","description":"城市名称"}},"required":["city","date"]}}},{"type":"function","function":{"name":"get_current_date","description":"获取当前日期","parameters":{"type":"object","properties":{}}}}]} 2026-01-19 22:49:45.110 [main] DEBUG i.a.c.m.OpenAIClient: - OpenAI response: {"choices":[{"message":{"content":"","role":"assistant","tool_calls":[{"function":{"arguments":"{"city": "上海", "date": "明天"}","name":"query_weather"},"id":"call_b8a1a535d398476297de04","index":0,"type":"function"}]},"finish_reason":"tool_calls","index":0,"logprobs":null}],"object":"chat.completion","usage":{"prompt_tokens":234,"completion_tokens":25,"total_tokens":259},"created":1768834185,"system_fingerprint":null,"model":"qwen3-next-80b-a3b-instruct","id":"chatcmpl-363e06ce-4497-90ec-8263-e447f06a7205"} MODEL_STOP

2026-01-19 22:49:56.787 [main] DEBUG i.a.c.m.OpenAIClient: - OpenAI request to https://one-api.ketanyun.com/v1/chat/completions: {"model":"qwen3-next-80b-a3b-instruct","messages":[{"role":"system","content":"你是一个有帮助的 AI 助手。\n可使用get_current_date工具获取当前日期\n"},{"role":"user","content":"上海明天的天气怎么样?"},{"role":"assistant","name":"Assistant","tool_calls":[{"id":"call_b8a1a535d398476297de04","type":"function","function":{"name":"query_weather","arguments":"{"city": "上海", "date": "明天"}"}}]},{"role":"tool","content":""上海 的天气:晴天,25°C,日期:明天"","tool_call_id":"call_b8a1a535d398476297de04"},{"role":"user","content":"谢谢,再见"}],"stream":false,"tools":[{"type":"function","function":{"name":"query_weather","description":"获取指定城市的天气","parameters":{"type":"object","properties":{"date":{"type":"string","description":"日期"},"city":{"type":"string","description":"城市名称"}},"required":["city","date"]}}},{"type":"function","function":{"name":"get_current_date","description":"获取当前日期","parameters":{"type":"object","properties":{}}}}]} 2026-01-19 22:49:57.791 [main] DEBUG i.a.c.m.OpenAIClient: - OpenAI response: {"choices":[{"message":{"content":"不客气!祝你明天有个愉快的一天!再见!","role":"assistant"},"finish_reason":"stop","index":0,"logprobs":null}],"object":"chat.completion","usage":{"prompt_tokens":296,"completion_tokens":13,"total_tokens":309},"created":1768834198,"system_fingerprint":null,"model":"qwen3-next-80b-a3b-instruct","id":"chatcmpl-c3777ac9-fe13-9ddb-970a-0f65a0c58960"} 不客气!祝你明天有个愉快的一天!再见! 2026-01-19 22:49:57.798 [HttpTransportFactory-ShutdownHook] INFO i.a.c.m.t.HttpTransportFactory: - Shutting down 1 managed HttpTransport(s)

chcodex avatar Jan 19 '26 14:01 chcodex

按照文档说明应该是

Image

chcodex avatar Jan 19 '26 14:01 chcodex

Image

agent.stream(msgs).map(event->{ 这里拿event.getMessage().getGenerateReason(),这里代码执行时机有很大概率在上面withGenerateReason设置GenerateReason之前,导致拿到的值还是没有正确的GenerateReason }) @feelshana 所有这个bug很可能没有完全解决掉

chcodex avatar Mar 18 '26 05:03 chcodex

Closing as fixed by #606 (merged), which supplies the correct stop reason for postActingEvent.stopAgent() so response.getGenerateReason() returns ACTING_STOP_REQUESTED instead of MODEL_STOP. Reopen if the reason is still wrong in your scenario.

juruoO-o avatar Nov 12 '26 11:11 juruoO-o