开发者社区 > 通义大模型 > 正文

通义千问java sdk添加function call之后不返回数据

使用java sdk访问通义千问,在parameters中添加了tools之后,返回的message变成空的了。没添加之前是正常的,tools也是可用的。请问是有人知道是什么原因吗?

展开
收起
昵称已修改 2024-03-19 15:39:41 278 0
1 条回答
写回答
取消 提交回答
  • 追求性能极限的人

    灵积的官方JavaSDK还不支持Tool调用,你可以试试我写的dashscope4j

    函数调用示例

    灵积在 2024-03-12 放出了函数调用的功能,当前支持的模型是大语言模型:qwen-turbo、qwen-plus、qwen-max、qwen-max-longcontext,下面是一个函数调用的示例:

    假设我们有一个回显函数:echo

    @ChatFn(name = "echo", description = "当用户输入echo:,回显后边的文字")
    public class EchoFunction implements ChatFunction<EchoFunction.Echo, EchoFunction.Echo> {
    
        @Override
        public CompletableFuture<Echo> call(Echo echo) {
            return CompletableFuture.completedFuture(new Echo(echo.words()));
        }
    
        public record Echo(
                @JsonPropertyDescription("需要回显的文字")
                String words
        ) {
    
        }
    
    }
    

    我们可以通过以下代码来调用这个函数:

    final var request = ChatRequest.newBuilder()
        .model(ChatModel.QWEN_MAX)
        .functions(new EchoFunction())
        .user("echo: HELLO!")
        .build();
    final var response = client.chat(request)
        .async()
        .join();
    

    输出日志

    2024-03-19 21:28:38 DEBUG dashscope://chat/qwen-max => {"model":"qwen-max","input":{"messages":[{"role":"user","content":"echo: HELLO!"}]},"parameters":{"result_format":"message","tools":[{"function":{"name":"echo","description":"当用户输入echo:,回显后边的文字","parameters":{"type":"object","properties":{"words":{"type":"string","description":"需要回显的文字"}}}},"type":"function"}]}}
    2024-03-19 21:28:40 DEBUG dashscope://chat/qwen-max <= {"output":{"choices":[{"finish_reason":"tool_calls","message":{"role":"assistant","tool_calls":[{"function":{"name":"echo","arguments":"{\"words\": \"HELLO!\"}"},"id":"","type":"function"}],"content":""}}]},"usage":{"total_tokens":28,"output_tokens":23,"input_tokens":5},"request_id":"8af40d7a-d43d-9d7f-9f12-8d52accfe8ac"}
    2024-03-19 21:28:40 DEBUG dashscope://chat/function <= {"words":"HELLO!"}
    2024-03-19 21:28:40 DEBUG dashscope://chat/function => {"words":"HELLO!"}
    2024-03-19 21:28:40 DEBUG dashscope://chat/qwen-max => {"model":"qwen-max","input":{"messages":[{"role":"user","content":"echo: HELLO!"},{"role":"assistant","tool_calls":[{"function":{"name":"echo","arguments":"{\"words\": \"HELLO!\"}"},"type":"function"}],"content":""},{"role":"tool","name":"echo","content":"{\"words\":\"HELLO!\"}"}]},"parameters":{"result_format":"message","tools":[{"function":{"name":"echo","description":"当用户输入echo:,回显后边的文字","parameters":{"type":"object","properties":{"words":{"type":"string","description":"需要回显的文字"}}}},"type":"function"}]}}
    2024-03-19 21:28:42 DEBUG dashscope://chat/qwen-max <= {"output":{"choices":[{"finish_reason":"stop","message":{"role":"assistant","content":"HELLO!"}}]},"usage":{"total_tokens":8,"output_tokens":3,"input_tokens":5},"request_id":"37ff7303-c1b2-9d7c-966d-82a7446fc52e"}
    
    2024-03-20 09:37:28
    赞同 1 展开评论 打赏

通义千问大模型家族全面升级更大参数规模模型首次面世,全新通义千问2.0版本欢迎体验。https://tongyi.aliyun.com/

相关电子书

更多
从 SDK 到编解码:视频直播架构解析 立即下载
跨平台的云服务SDK需要什么 立即下载
一个跨平台的云服务SDK需要什么 立即下载