Skip to content

Tool add Mono #2761

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
poo0054 opened this issue Apr 16, 2025 · 23 comments
Closed

Tool add Mono #2761

poo0054 opened this issue Apr 16, 2025 · 23 comments

Comments

@poo0054
Copy link

poo0054 commented Apr 16, 2025

I have a requirement. I am using webflux. I need to request my local interface in @tool, but I can't get the header. For example, add the Authorization header in webflux in webClient or restTemplate.

The code is as follows. Can you support it or is there another method?

 @Tool(name = "品质事件查询", description = "根据用户输入内容查询品质事件")
    public Mono<String> searchQualityEvent(@ToolParam(description = "查询参数") QualityEventSearch qualityEventSearch) {
        QualityEventSearch finalQualityEventSearch = qualityEventSearch;
        Mono<R> rMono = getRequest().flatMap(serverHttpRequest -> {
            String first = serverHttpRequest.getHeaders().get(HttpHeaders.AUTHORIZATION).getFirst();
            return webClient.post().uri("https://xxx")
                    .header(HttpHeaders.AUTHORIZATION, first)
                    .accept(MediaType.APPLICATION_JSON)
                    .body(Mono.just(JackSon2Utils.writeString(finalQualityEventSearch)), String.class)
                    .retrieve()
                    .bodyToMono(R.class);
        });
        return rMono.flatMap(r -> {
            if (r.isSuccess()) {
                return Mono.just(JackSon2Utils.writeString(r.getData()));
            }
            return Mono.just("权限不足");
        });
@poo0054
Copy link
Author

poo0054 commented Apr 16, 2025

Although using CallAroundAdvisor can solve this problem, it is more troublesome

@18801151992
Copy link

webflux确实有这样的问题,无法获取到header,之前也webflux项目,实现了WebFilter,chain.filter(exchange).contextWrite(ctx -> ctx.put(ContextHolder.KEY_EXCHANGE, exchange))是可以的,不知道为啥,新建的mcp的项目里面就不行了,你先换成mvc呗,mvc写个拦截器,把header里面的内容取出来,添加到全局上下文,是可以实现的,没必要非要用webflux。

@poo0054
Copy link
Author

poo0054 commented Apr 17, 2025

Thanks for the answer。Webmvc is too slow, and some models do not support Webmvc, only Webflux. Can Webflux be supported in the tool?

@18801151992
Copy link

建议你client端使用python,大模型都使用python那层来做,server端使用java,mvc的模型也都是支持的,你说不支持指的是什么呢?

@poo0054
Copy link
Author

poo0054 commented Apr 17, 2025

谢谢回复,

  public static Mono<ServerHttpRequest> getRequest() {
        return Mono.subscriberContext()
                .map(ctx -> ctx.get(ServerHttpRequest.class));
    }
    
@Tool(name = "品质事件查询", description = "根据用户输入内容查询品质事件")
    public Mono<String> searchQualityEvent(@ToolParam(description = "查询参数") QualityEventSearch qualityEventSearch) {
        QualityEventSearch finalQualityEventSearch = qualityEventSearch;
        Mono<R> rMono = getRequest().flatMap(serverHttpRequest -> {
            String first = serverHttpRequest.getHeaders().get(HttpHeaders.AUTHORIZATION).getFirst();
            return webClient.post().uri("https://xxx")
                    .header(HttpHeaders.AUTHORIZATION, first)
                    .accept(MediaType.APPLICATION_JSON)
                    .body(Mono.just(JackSon2Utils.writeString(finalQualityEventSearch)), String.class)
                    .retrieve()
                    .bodyToMono(R.class);
        });
        return rMono.flatMap(r -> {
            if (r.isSuccess()) {
                return Mono.just(JackSon2Utils.writeString(r.getData()));
            }
            return Mono.just("权限不足");
        });    

当前Tool的返回不能使用Mono<String>,如果返回string,那么其中的getRequest()就不能获取不到header

@poo0054
Copy link
Author

poo0054 commented Apr 17, 2025

CallAroundAdvisor 中存在一个AdvisedRequest#adviseContext可以在advisors中添加需要的值。代码如下:

    @GetMapping("/advisor")
    public String advisor(@RequestParam(value = "message", defaultValue = "你会做些什么") String message, HttpServletRequest request) {
        return chatClient.prompt()
                .advisors(advisorSpec ->
                        advisorSpec.param(AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY,
                                getIpAddress(request))
                )
                .messages(new UserMessage(message))
                .call()
                .content();
    }

advisorSpec.param(AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY, getIpAddress(request))这个代码可以放入值 然后后面需要的时候取出来

@18801151992
Copy link

因为你不是从controller里面进的,如果是从controller里面就可以获取到,本来mvc能做的,你非要倔~

@poo0054
Copy link
Author

poo0054 commented Apr 17, 2025

但是在@Tool中没法存储值,特别是在webflux中。而且ThreadLocal不能使用, Mono.deferContextual().block()使用了block就无法获取

@poo0054
Copy link
Author

poo0054 commented Apr 17, 2025

因为你不是从controller里面进的,如果是从controller里面就可以获取到,本来mvc能做的,你非要倔~

我的需要在tool中获取我当前权限,然后在api调用的时候放入其中。webmvc很慢,而且tongyi有个模型也用不了,那个模型只支持webflux。所以需要tool支持reactor

@18801151992
Copy link

通义哪个模型必须要webflux,我咋不知道?

@18801151992
Copy link

reactor可以在mvc引入webflux包,使用webclient

@poo0054
Copy link
Author

poo0054 commented Apr 17, 2025

reactor可以在mvc引入webflux包,使用webclient

no,兄弟,你试试在@tool中使用webflux,试试有没有办法获取当前http的header

@18801151992
Copy link

没办法的,webflux这个坑我已经踩过了

@18801151992
Copy link

通义的,都会支持mvc,你说通义的只支持webflux是不可能的

@poo0054
Copy link
Author

poo0054 commented Apr 17, 2025

所以我来这里提,能不能让@tool支持mono

@18801151992
Copy link

这个问题已经提过了,没人搭理

@poo0054
Copy link
Author

poo0054 commented Apr 17, 2025

多提几次,需要的人多了,自然会有人开发的。没人愿意的话就只能自己提pr了

@18801151992
Copy link

嗯,所以没人解决,就不必纠结了,用mvc解决吧或者先不用权限了

@poo0054
Copy link
Author

poo0054 commented Apr 17, 2025

嗯,所以没人解决,就不必纠结了,用mvc解决吧或者先不用权限了

谢谢

@xiaolegehaha
Copy link

大佬们能不能指导一下如何在tool中刷新resource list,我现在需要在tool执行完后刷新发布的resource,但是找不到相关资料

@poo0054
Copy link
Author

poo0054 commented Apr 17, 2025

repeat #2757

@poo0054 poo0054 closed this as completed Apr 17, 2025
@poo0054
Copy link
Author

poo0054 commented Apr 17, 2025

大佬们能不能指导一下如何在tool中刷新resource list,我现在需要在tool执行完后刷新发布的resource,但是找不到相关资料

@xiaolegehaha 你可以重新打开一个新的issues,并附上代码。我不太懂你说的resource list是指什么,你可以看看Advisor。我觉得这应该是你需要的。

@18801151992
Copy link

大佬们能不能指导一下如何在tool中刷新resource list,我现在需要在tool执行完后刷新发布的resource,但是找不到相关资料

resource为啥要用到这个?我觉得不管是什么,都使用tool来解决就可以了呀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants