DashScope JAVA SDK支持function calling吗?
你可以试试这个DashScope4j
DashScope4j:灵积 Java SDK
DashScope4j 是一个开源的灵积非官方 Java SDK,基于 JDK17 构建。它旨在提供一个功能丰富、易于集成和使用灵积API(通义千问模型)的Java库,以便开发者能够通灵积API轻松实现多模态对话、向量嵌入和图像处理等功能。
请注意:在使用 DashScope4j 时,你需要遵守灵积API的使用条款和条件。
重要更新:1.2.0;支持函数调用
灵积在 2024-03-12 放出了 函数调用 功能,当前支持的模型是大语言模型 qwen-turbo、qwen-plus、qwen-max、qwen-max-longcontext,DashScope4j 从 1.2.0 版本开始作为 Java SDK 首发支持函数调用
函数调用是我实际开发中最喜欢的一个功能,它扩展了大模型的能力边界,让AI具备了操纵现实的能力。而之前要做到这些事情我得通过 LangChain 来实现。
依赖使用
dependency>
groupId>io.github.oldmanpushcartgroupId>
artifactId>dashscope4jartifactId>
version>1.2.1version>
dependency>
函数调用示例
灵积在 2024-03-12 放出了函数调用的功能,当前支持的模型是大语言模型:qwen-turbo、qwen-plus、qwen-max、qwen-max-longcontext,下面是一个函数调用的示例:
单个函数调用示例:回显
假设我们有一个回显函数:echo
@ChatFn(name = 'echo', description = '当用户输入echo:,回显后边的文字')
public class EchoFunction implements ChatFunctionEchoFunction.Echo, EchoFunction.Echo> {
@Override
public CompletableFutureEcho> 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 {'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
级联函数调用示例:成绩查询
我们有两个函数
查询成绩函数:query_score计算平均分函数:compute_avg_score
现在需要查询某个同学的所有成绩,并计算其平均分。LLM需要先调用 query_score 函数查询成绩,然后再调用 compute_avg_score 函数计算平均分。
final var request = ChatRequest.newBuilder()
.model(ChatModel.QWEN_PLUS)
.functions(new QueryScoreFunction(), new ComputeAvgScoreFunction())
.user('张三的所有成绩,并计算平均分')
.build();
final var response = client.chat(request)
.async()
.join();
输出日志
2024-03-20 23:50:17 DEBUG dashscope://chat/qwen-plus => {'model':'qwen-plus','input':{'messages':[{'role':'user','content':'张三的所有成绩,并计算平均分'}]},'parameters':{'result_format':'message','tools':[{'function':{'name':'query_score','description':'query student's scores','parameters':{'type':'object','properties':{'name':{'type':'string','description':'the student name to query'},'subjects':{'type':'array','description':'the subjects to query','items':{'type':'string','enum':['CHINESE','MATH','ENGLISH']}}},'required':['name','subjects']}},'type':'function'},{'function':{'name':'compute_avg_score','description':'计算平均成绩','parameters':{'type':'object','properties':{'scores':{'type':'array','description':'分数集合','items':{'type':'number'}}}}},'type':'function'}]}}
2024-03-20 23:50:20 DEBUG dashscope://chat/qwen-plus {'message':'查询成功','data':[{'name':'张三','subject':'CHINESE','value':90.0},{'name':'张三','subject':'MATH','value':80.0},{'name':'张三','subject':'ENGLISH','value':70.0}],'success':true}
2024-03-20 23:50:20 DEBUG dashscope://chat/qwen-plus => {'model':'qwen-plus','input':{'messages':[{'role':'user','content':'张三的所有成绩,并计算平均分'},{'role':'assistant','tool_calls':[{'function':{'arguments':'{\'name\': \'张三\', \'subjects\': [\'CHINESE\', \'MATH\', \'ENGLISH\']}','name':'query_score'},'type':'function'}],'content':''},{'role':'tool','name':'query_score','content':'{\'message\':\'查询成功\',\'data\':[{\'name\':\'张三\',\'subject\':\'CHINESE\',\'value\':90.0},{\'name\':\'张三\',\'subject\':\'MATH\',\'value\':80.0},{\'name\':\'张三\',\'subject\':\'ENGLISH\',\'value\':70.0}],\'success\':true}'}]},'parameters':{'result_format':'message','tools':[{'function':{'name':'query_score','description':'query student's scores','parameters':{'type':'object','properties':{'name':{'type':'string','description':'the student name to query'},'subjects':{'type':'array','description':'the subjects to query','items':{'type':'string','enum':['CHINESE','MATH','ENGLISH']}}},'required':['name','subjects']}},'type':'function'},{'function':{'name':'compute_avg_score','description':'计算平均成绩','parameters':{'type':'object','properties':{'scores':{'type':'array','description':'分数集合','items':{'type':'number'}}}}},'type':'function'}]}}
2024-03-20 23:50:24 DEBUG dashscope://chat/qwen-plus {'avg_score':80.0}
2024-03-20 23:50:24 DEBUG dashscope://chat/qwen-plus => {'model':'qwen-plus','input':{'messages':[{'role':'user','content':'张三的所有成绩,并计算平均分'},{'role':'assistant','tool_calls':[{'function':{'arguments':'{\'name\': \'张三\', \'subjects\': [\'CHINESE\', \'MATH\', \'ENGLISH\']}','name':'query_score'},'type':'function'}],'content':''},{'role':'tool','name':'query_score','content':'{\'message\':\'查询成功\',\'data\':[{\'name\':\'张三\',\'subject\':\'CHINESE\',\'value\':90.0},{\'name\':\'张三\',\'subject\':\'MATH\',\'value\':80.0},{\'name\':\'张三\',\'subject\':\'ENGLISH\',\'value\':70.0}],\'success\':true}'},{'role':'assistant','tool_calls':[{'function':{'arguments':'{\'scores\': [90.0, 80.0, 70.0]}','name':'compute_avg_score'},'type':'function'}],'content':'张三的成绩如下:\n\n- 中文: 90.0分\n- 数学: 80.0分\n- 英语: 70.0分\n\n现在我们来计算他的平均分。'},{'role':'tool','name':'compute_avg_score','content':'{\'avg_score\':80.0}'}]},'parameters':{'result_format':'message','tools':[{'function':{'name':'query_score','description':'query student's scores','parameters':{'type':'object','properties':{'name':{'type':'string','description':'the student name to query'},'subjects':{'type':'array','description':'the subjects to query','items':{'type':'string','enum':['CHINESE','MATH','ENGLISH']}}},'required':['name','subjects']}},'type':'function'},{'function':{'name':'compute_avg_score','description':'计算平均成绩','parameters':{'type':'object','properties':{'scores':{'type':'array','description':'分数集合','items':{'type':'number'}}}}},'type':'function'}]}}
2024-03-20 23:50:25 DEBUG dashscope://chat/qwen-plus
赞4
踩1