使用java sdk访问通义千问,在parameters中添加了tools之后,返回的message变成空的了。没添加之前是正常的,tools也是可用的。请问是有人知道是什么原因吗?
灵积的官方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"}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。