大模型支持在必要时调用一个或多个可用工具,这些工具通常由开发人员定义。 工具可以是:Web搜索工具、自定义函数实现特定逻辑的工具等。
但是大模型不能自己调用对应的工具,他们如果认为合适,只是表达调用工具的意图,在其响应中会通过特殊的响应参数指示需要调用哪个特定工具。 然后,开发人员根据响应数据,对指定的工具进行调用。
LangChain4j的Low Level API和High Level API都支持对工具的调用,本文主要使用AI Service演示函数工具的调用。
不使用函数工具的演示
我们想计算两个字符串的长度的和的平方根,代码如下:
package com.renr.langchain4jnew.app3;
import com.renr.langchain4jnew.constant.CommonConstants;
import dev.langchain4j.community.model.zhipu.ZhipuAiChatModel;
import dev.langchain4j.service.AiServices;
import java.time.Duration;
/**
* @Title: App1
* @Author 老任与码
* @Date 2025-04-16 13:27
*/
public class App1 {
public static void main(String[] args) {
ZhipuAiChatModel model = ZhipuAiChatModel.builder()
// 模型key
.apiKey(CommonConstants.API_KEY)
// 精确度
.temperature(0.9)
.model("GLM-4-Flash")
.maxRetries(3)
.callTimeout(Duration.ofSeconds(60))
.connectTimeout(Duration.ofSeconds(60))
.writeTimeout(Duration.ofSeconds(60))
.readTimeout(Duration.ofSeconds(60))
.logRequests(true)
.logResponses(true)
.build();
// 创建代理对象
Assistant assistant = AiServices.builder(Assistant.class)
.chatLanguageModel(model)
.build();
String question = "计算如下两个字符串的长度和的平方根:hello和world";
String answer = assistant.chat(question);
System.out.println(answer);
}
}
执行的输出结果如下:

我们的本意是想直接得到计算结果,但是从结果中可以看到,大模型只是给出了代码,但是没有给出最终答案。
指定函数工具
定义工具类
package com.renr.langchain4jnew.app3;
import dev.langchain4j.agent.tool.Tool;
public class Calculator {
@Tool("计算字符串的长度")
int stringLength(String s) {
System.out.println("Called stringLength with s='" + s + "'");
return s.length();
}
@Tool("计算两个整数的和")
int add(int a, int b) {
System.out.println("Called add with a=" + a + ", b=" + b);
return a + b;
}
@Tool("计算整数的平方根")
double sqrt(int x) {
System.out.println("Called sqrt with x=" + x);
return Math.sqrt(x);
}
}
@Tool用于对函数进行功能描述,这个很重要,描述的越清楚,大模型越容易考虑是否需要使用函数工具。
AI Service接口
package com.renr.langchain4jnew.app3;
import dev.langchain4j.service.UserMessage;
interface Assistant {
String chat(String userMessage);
}
代码中指定工具
package com.renr.langchain4jnew.app3;
import com.renr.langchain4jnew.constant.CommonConstants;
import dev.langchain4j.community.model.zhipu.ZhipuAiChatModel;
import dev.langchain4j.service.AiServices;
import java.time.Duration;
/**
* @Title: App1
* @Author 老任与码
* @Date 2025-04-16 13:27
*/
public class App1 {
public static void main(String[] args) {
ZhipuAiChatModel model = ZhipuAiChatModel.builder()
// 模型key
.apiKey(CommonConstants.API_KEY)
// 精确度
.temperature(0.9)
.model("GLM-4-Flash")
.maxRetries(3)
.callTimeout(Duration.ofSeconds(60))
.connectTimeout(Duration.ofSeconds(60))
.writeTimeout(Duration.ofSeconds(60))
.readTimeout(Duration.ofSeconds(60))
.logRequests(true)
.logResponses(true)
.build();
// 创建代理对象
Assistant assistant = AiServices.builder(Assistant.class)
.chatLanguageModel(model)
.tools(new Calculator())
.build();
String question = "计算如下两个字符串的长度和的平方根:hello和world";
String answer = assistant.chat(question);
System.out.println(answer);
}
}
通过tools()方法指定函数工具类对象
输出结果

可以看到给出了最终数据。
函数工具调用的过程
下面我们来分析下这中间发生了些什么。
1.第一次发送请求的数据
根据日志,当我们调用chat()方法时,发送的请求参数如下:
17:55:32.778 [main] DEBUG dev.langchain4j.community.model.zhipu.RequestLoggingInterceptor - Request:
- method: POST
- url: https://open.bigmodel.cn/api/paas/v4/chat/completions
- headers: [Authorization: Bearer ey...fQ.ew0KICAiZXhwIiA6IDE3NDQ3OTkxMzI3MTcsDQogICJhcGlfa2V5IiA6ICJkMjMzNGIyZTk4NTE0YWUwOTIzMDQ4YTM4NDEzNTMwNiIsDQogICJ0aW1lc3RhbXAiIDogMTc0NDc5NzMzMjcxNw0KfQ.sj3HzVhf9QI5DzrbSexvT3h1fYETR5F9ZS98p1Or868]
- body: {
"model" : "GLM-4-Flash",
"messages" : [ {
"role" : "user",
"content" : "计算如下两个字符串的长度和的平方根:hello和world"
} ],
"stream" : false,
"temperature" : 0.9,
"max_tokens" : 512,
"tools" : [ {
"type" : "function",
"function" : {
"name" : "add",
"description" : "计算两个整数的和",
"parameters" : {
"type" : "object",
"properties" : {
"arg0" : {
"type" : "integer"
},
"arg1" : {
"type" : "integer"
}
},
"required" : [ "arg0", "arg1" ]
}
}
}, {
"type" : "function",
"function" : {
"name" : "sqrt",
"description" : "计算整数的平方根",
"parameters" : {
"type" : "object",
"properties" : {
"arg0" : {
"type" : "integer"
}
},
"required" : [ "arg0" ]
}
}
}, {
"type" : "function",
"function" : {
"name" : "stringLength",
"description" : "计算字符串的长度",
"parameters" : {
"type" : "object",
"properties" : {
"arg0" : {
"type" : "string"
}
},
"required" : [ "arg0" ]
}
}
} ],
"tool_choice" : "auto"
}
请求参数中,tools表示工具信息,type表示工具的类型是函数,function属性对函数的作用和参数等进行了描述。请求参数中将所有的函数工具都告知了大模型。
2.大模型给出第一次请求对应的响应
{
"choices": [{
"finish_reason": "tool_calls",
"index": 0,
"message": {
"role": "assistant",
"tool_calls": [{
"function": {
"arguments": "{\"arg0\": 10}",
"name": "sqrt"
},
"id": "call_-8790934249832040286",
"index": 0,
"type": "function"
}]
}
}],
"created": 1744797332,
"id": "202504161755325f0447a96f894837",
"model": "GLM-4-Flash",
"request_id": "202504161755325f0447a96f894837",
"usage": {
"completion_tokens": 10,
"prompt_tokens": 353,
"total_tokens": 363
}
}
从响应数据发现,大模型认为根据用户的提问,需要调用我们函数工具中的sqrt()函数,而且对应的参数值为10。说明大模型根据我们发送的信息,自动计算出"hello"和"world"两个单词的长度和为10,然后指示调用sqrt()方法进行运算。
3.LangChain4j根据响应结果自动调用sqrt()

从输出可以看到,自动调用了sqrt()函数
4.LangChainj自动发送第二次请求
17:55:33.608 [main] DEBUG dev.langchain4j.community.model.zhipu.RequestLoggingInterceptor - Request:
- method: POST
- url: https://open.bigmodel.cn/api/paas/v4/chat/completions
- headers: [Authorization: Bearer ey...fQ.ew0KICAiZXhwIiA6IDE3NDQ3OTkxMzI3MTcsDQogICJhcGlfa2V5IiA6ICJkMjMzNGIyZTk4NTE0YWUwOTIzMDQ4YTM4NDEzNTMwNiIsDQogICJ0aW1lc3RhbXAiIDogMTc0NDc5NzMzMjcxNw0KfQ.sj3HzVhf9QI5DzrbSexvT3h1fYETR5F9ZS98p1Or868]
- body: {
"model" : "GLM-4-Flash",
"messages" : [ {
"role" : "user",
"content" : "计算如下两个字符串的长度和的平方根:hello和world"
}, {
"role" : "assistant",
"tool_calls" : [ {
"id" : "call_-8790934249832040286",
"type" : "function",
"function" : {
"name" : "sqrt",
"arguments" : "{\"arg0\": 10}"
}
} ]
}, {
"role" : "tool",
"content" : "3.1622776601683795"
} ],
"stream" : false,
"temperature" : 0.9,
"max_tokens" : 512,
"tools" : [ {
"type" : "function",
"function" : {
"name" : "add",
"description" : "计算两个整数的和",
"parameters" : {
"type" : "object",
"properties" : {
"arg0" : {
"type" : "integer"
},
"arg1" : {
"type" : "integer"
}
},
"required" : [ "arg0", "arg1" ]
}
}
}, {
"type" : "function",
"function" : {
"name" : "sqrt",
"description" : "计算整数的平方根",
"parameters" : {
"type" : "object",
"properties" : {
"arg0" : {
"type" : "integer"
}
},
"required" : [ "arg0" ]
}
}
}, {
"type" : "function",
"function" : {
"name" : "stringLength",
"description" : "计算字符串的长度",
"parameters" : {
"type" : "object",
"properties" : {
"arg0" : {
"type" : "string"
}
},
"required" : [ "arg0" ]
}
}
} ],
"tool_choice" : "auto"
}
从请求参数中看到,tool_calls显示对应调用的函数,content给出了运算结果。
5.大模型给出第二次请求的响应
{
"choices": [{
"finish_reason": "stop",
"index": 0,
"message": {
"content": "两个字符串的长度和为10,其平方根为3.1622776601683795。",
"role": "assistant"
}
}],
"created": 1744797333,
"id": "20250416175532151479cd99954a72",
"model": "GLM-4-Flash",
"request_id": "20250416175532151479cd99954a72",
"usage": {
"completion_tokens": 26,
"prompt_tokens": 376,
"total_tokens": 402
}
}
从响应中看出,大模型根据前面步骤的计算结果,结合一开始用户发送的问题,给出了符合自然语言的返回信息。
最大限度的调用函数工具
上面的过程只是调用了我们自定义的sqrt()函数,我们的本意是还想调用stringLength()函数和add()函数,于是对AI Service的接口的代码进行修改:
package com.renr.langchain4jnew.app3;
import dev.langchain4j.service.UserMessage;
interface Assistant {
@UserMessage("针对提出的问题:{{it}},请尽量调用已有的函数工具")
String chat(String userMessage);
}
这里,我们通过提示词,让大模型尽量考虑调用我们的函数工具。
再次执行后,结果如下:
![]()
结果也没有问题。
修改AI Service接口后的函数工具调用过程
1.第一次发送请求
18:18:23.014 [main] DEBUG dev.langchain4j.community.model.zhipu.RequestLoggingInterceptor - Request:
- method: POST
- url: https://open.bigmodel.cn/api/paas/v4/chat/completions
- headers: [Authorization: Bearer ey...fQ.ew0KICAiZXhwIiA6IDE3NDQ4MDA1MDI5MzUsDQogICJhcGlfa2V5IiA6ICJkMjMzNGIyZTk4NTE0YWUwOTIzMDQ4YTM4NDEzNTMwNiIsDQogICJ0aW1lc3RhbXAiIDogMTc0NDc5ODcwMjkzNQ0KfQ.4LzlCQ_sG51GDEfNV2jyjjpJQTDl-9NUqVGKxpEBqQI]
- body: {
"model" : "GLM-4-Flash",
"messages" : [ {
"role" : "user",
"content" : "针对提出的问题计算如下两个字符串的长度和的平方根:hello和world,请尽量调用已有的函数工具"
} ],
"stream" : false,
"temperature" : 0.9,
"max_tokens" : 512,
"tools" : [ {
"type" : "function",
"function" : {
"name" : "add",
"description" : "计算两个整数的和",
"parameters" : {
"type" : "object",
"properties" : {
"arg0" : {
"type" : "integer"
},
"arg1" : {
"type" : "integer"
}
},
"required" : [ "arg0", "arg1" ]
}
}
}, {
"type" : "function",
"function" : {
"name" : "sqrt",
"description" : "计算整数的平方根",
"parameters" : {
"type" : "object",
"properties" : {
"arg0" : {
"type" : "integer"
}
},
"required" : [ "arg0" ]
}
}
}, {
"type" : "function",
"function" : {
"name" : "stringLength",
"description" : "计算字符串的长度",
"parameters" : {
"type" : "object",
"properties" : {
"arg0" : {
"type" : "string"
}
},
"required" : [ "arg0" ]
}
}
} ],
"tool_choice" : "auto"
}
2.第一次请求对应的响应数据
{
"choices": [{
"finish_reason": "tool_calls",
"index": 0,
"message": {
"role": "assistant",
"tool_calls": [{
"function": {
"arguments": "{\"arg0\": \"hello\"}",
"name": "stringLength"
},
"id": "call_-8790922223921709504",
"index": 0,
"type": "function"
}]
}
}],
"created": 1744798702,
"id": "202504161818220aacbe020d834e70",
"model": "GLM-4-Flash",
"request_id": "202504161818220aacbe020d834e70",
"usage": {
"completion_tokens": 11,
"prompt_tokens": 363,
"total_tokens": 374
}
}
从响应中看到,其中提示要调用stringLength()方法,计算"hello"这个字符串的长度。
3.LangChain4j自动调用stringLength计算长度

4.第二次发送请求数据
18:18:23.689 [main] DEBUG dev.langchain4j.community.model.zhipu.RequestLoggingInterceptor - Request:
- method: POST
- url: https://open.bigmodel.cn/api/paas/v4/chat/completions
- headers: [Authorization: Bearer ey...fQ.ew0KICAiZXhwIiA6IDE3NDQ4MDA1MDI5MzUsDQogICJhcGlfa2V5IiA6ICJkMjMzNGIyZTk4NTE0YWUwOTIzMDQ4YTM4NDEzNTMwNiIsDQogICJ0aW1lc3RhbXAiIDogMTc0NDc5ODcwMjkzNQ0KfQ.4LzlCQ_sG51GDEfNV2jyjjpJQTDl-9NUqVGKxpEBqQI]
- body: {
"model" : "GLM-4-Flash",
"messages" : [ {
"role" : "user",
"content" : "针对提出的问题计算如下两个字符串的长度和的平方根:hello和world,请尽量调用已有的函数工具"
}, {
"role" : "assistant",
"tool_calls" : [ {
"id" : "call_-8790922223921709504",
"type" : "function",
"function" : {
"name" : "stringLength",
"arguments" : "{\"arg0\": \"hello\"}"
}
} ]
}, {
"role" : "tool",
"content" : "5"
} ],
"stream" : false,
"temperature" : 0.9,
"max_tokens" : 512,
"tools" : [ {
"type" : "function",
"function" : {
"name" : "add",
"description" : "计算两个整数的和",
"parameters" : {
"type" : "object",
"properties" : {
"arg0" : {
"type" : "integer"
},
"arg1" : {
"type" : "integer"
}
},
"required" : [ "arg0", "arg1" ]
}
}
}, {
"type" : "function",
"function" : {
"name" : "sqrt",
"description" : "计算整数的平方根",
"parameters" : {
"type" : "object",
"properties" : {
"arg0" : {
"type" : "integer"
}
},
"required" : [ "arg0" ]
}
}
}, {
"type" : "function",
"function" : {
"name" : "stringLength",
"description" : "计算字符串的长度",
"parameters" : {
"type" : "object",
"properties" : {
"arg0" : {
"type" : "string"
}
},
"required" : [ "arg0" ]
}
}
} ],
"tool_choice" : "auto"
}
tool_calls中显示调用了stringLength()方法
5.第二次请求对应的响应
{
"choices": [{
"finish_reason": "tool_calls",
"index": 0,
"message": {
"role": "assistant",
"tool_calls": [{
"function": {
"arguments": "{\"arg0\": \"world\"}",
"name": "stringLength"
},
"id": "call_-8790922876756788063",
"index": 0,
"type": "function"
}]
}
}],
"created": 1744798703,
"id": "20250416181822199e0e3c66b84e2c",
"model": "GLM-4-Flash",
"request_id": "20250416181822199e0e3c66b84e2c",
"usage": {
"completion_tokens": 11,
"prompt_tokens": 377,
"total_tokens": 388
}
}
通过响应,大模型提示需要调用stringLength()方法,对"world"这个单词计算长度
6.LangChain4j自动调用stringLength函数

7.第三次发送请求数据
18:18:24.162 [main] DEBUG dev.langchain4j.community.model.zhipu.RequestLoggingInterceptor - Request:
- method: POST
- url: https://open.bigmodel.cn/api/paas/v4/chat/completions
- headers: [Authorization: Bearer ey...fQ.ew0KICAiZXhwIiA6IDE3NDQ4MDA1MDM2ODgsDQogICJhcGlfa2V5IiA6ICJkMjMzNGIyZTk4NTE0YWUwOTIzMDQ4YTM4NDEzNTMwNiIsDQogICJ0aW1lc3RhbXAiIDogMTc0NDc5ODcwMzY4OA0KfQ.I-Sgjgc7GC3n0efhNLZDjGZARiSAHo_oRLp-JVw7pgw]
- body: {
"model" : "GLM-4-Flash",
"messages" : [ {
"role" : "user",
"content" : "针对提出的问题计算如下两个字符串的长度和的平方根:hello和world,请尽量调用已有的函数工具"
}, {
"role" : "assistant",
"tool_calls" : [ {
"id" : "call_-8790922223921709504",
"type" : "function",
"function" : {
"name" : "stringLength",
"arguments" : "{\"arg0\": \"hello\"}"
}
} ]
}, {
"role" : "tool",
"content" : "5"
}, {
"role" : "assistant",
"tool_calls" : [ {
"id" : "call_-8790922876756788063",
"type" : "function",
"function" : {
"name" : "stringLength",
"arguments" : "{\"arg0\": \"world\"}"
}
} ]
}, {
"role" : "tool",
"content" : "5"
} ],
"stream" : false,
"temperature" : 0.9,
"max_tokens" : 512,
"tools" : [ {
"type" : "function",
"function" : {
"name" : "add",
"description" : "计算两个整数的和",
"parameters" : {
"type" : "object",
"properties" : {
"arg0" : {
"type" : "integer"
},
"arg1" : {
"type" : "integer"
}
},
"required" : [ "arg0", "arg1" ]
}
}
}, {
"type" : "function",
"function" : {
"name" : "sqrt",
"description" : "计算整数的平方根",
"parameters" : {
"type" : "object",
"properties" : {
"arg0" : {
"type" : "integer"
}
},
"required" : [ "arg0" ]
}
}
}, {
"type" : "function",
"function" : {
"name" : "stringLength",
"description" : "计算字符串的长度",
"parameters" : {
"type" : "object",
"properties" : {
"arg0" : {
"type" : "string"
}
},
"required" : [ "arg0" ]
}
}
} ],
"tool_choice" : "auto"
}
请求中显示调用了两次函数(两个tool_calls)
8.第三次请求对应的响应
{
"choices": [{
"finish_reason": "tool_calls",
"index": 0,
"message": {
"role": "assistant",
"tool_calls": [{
"function": {
"arguments": "{\"arg0\": 5, \"arg1\": 5}",
"name": "add"
},
"id": "call_-8790925178859487903",
"index": 0,
"type": "function"
}]
}
}],
"created": 1744798704,
"id": "202504161818238476a3d4885b4205",
"model": "GLM-4-Flash",
"request_id": "202504161818238476a3d4885b4205",
"usage": {
"completion_tokens": 17,
"prompt_tokens": 391,
"total_tokens": 408
}
}
大模型给出响应,表示需要调用add()方法计算两个长度的和,两个参数分别是5和5
9.LangChain4j自动调用add方法

10.第四次发送请求数据
18:18:25.316 [main] DEBUG dev.langchain4j.community.model.zhipu.RequestLoggingInterceptor - Request:
- method: POST
- url: https://open.bigmodel.cn/api/paas/v4/chat/completions
- headers: [Authorization: Bearer ey...fQ.ew0KICAiZXhwIiA6IDE3NDQ4MDA1MDQxNjIsDQogICJhcGlfa2V5IiA6ICJkMjMzNGIyZTk4NTE0YWUwOTIzMDQ4YTM4NDEzNTMwNiIsDQogICJ0aW1lc3RhbXAiIDogMTc0NDc5ODcwNDE2Mg0KfQ.Bavl1W5K2IlH_tUioVbjOIkpe6YNoefJgbayW-Mnd1Q]
- body: {
"model" : "GLM-4-Flash",
"messages" : [ {
"role" : "user",
"content" : "针对提出的问题计算如下两个字符串的长度和的平方根:hello和world,请尽量调用已有的函数工具"
}, {
"role" : "assistant",
"tool_calls" : [ {
"id" : "call_-8790922223921709504",
"type" : "function",
"function" : {
"name" : "stringLength",
"arguments" : "{\"arg0\": \"hello\"}"
}
} ]
}, {
"role" : "tool",
"content" : "5"
}, {
"role" : "assistant",
"tool_calls" : [ {
"id" : "call_-8790922876756788063",
"type" : "function",
"function" : {
"name" : "stringLength",
"arguments" : "{\"arg0\": \"world\"}"
}
} ]
}, {
"role" : "tool",
"content" : "5"
}, {
"role" : "assistant",
"tool_calls" : [ {
"id" : "call_-8790925178859487903",
"type" : "function",
"function" : {
"name" : "add",
"arguments" : "{\"arg0\": 5, \"arg1\": 5}"
}
} ]
}, {
"role" : "tool",
"content" : "10"
} ],
"stream" : false,
"temperature" : 0.9,
"max_tokens" : 512,
"tools" : [ {
"type" : "function",
"function" : {
"name" : "add",
"description" : "计算两个整数的和",
"parameters" : {
"type" : "object",
"properties" : {
"arg0" : {
"type" : "integer"
},
"arg1" : {
"type" : "integer"
}
},
"required" : [ "arg0", "arg1" ]
}
}
}, {
"type" : "function",
"function" : {
"name" : "sqrt",
"description" : "计算整数的平方根",
"parameters" : {
"type" : "object",
"properties" : {
"arg0" : {
"type" : "integer"
}
},
"required" : [ "arg0" ]
}
}
}, {
"type" : "function",
"function" : {
"name" : "stringLength",
"description" : "计算字符串的长度",
"parameters" : {
"type" : "object",
"properties" : {
"arg0" : {
"type" : "string"
}
},
"required" : [ "arg0" ]
}
}
} ],
"tool_choice" : "auto"
}
请求数据中显示,已经调用了三次函数
11.第四次请求给出的响应
{
"choices": [{
"finish_reason": "tool_calls",
"index": 0,
"message": {
"role": "assistant",
"tool_calls": [{
"function": {
"arguments": "{\"arg0\": 10}",
"name": "sqrt"
},
"id": "call_-8790941396658651614",
"index": 0,
"type": "function"
}]
}
}],
"created": 1744798704,
"id": "20250416181824ae9c0bea21804e21",
"model": "GLM-4-Flash",
"request_id": "20250416181824ae9c0bea21804e21",
"usage": {
"completion_tokens": 10,
"prompt_tokens": 411,
"total_tokens": 421
}
}
响应中提示需要调用sqrt()方法
12.LangChain4j自动调用sqrt方法得到运算结果

13.第五次发送请求数据
18:18:25.654 [main] DEBUG dev.langchain4j.community.model.zhipu.RequestLoggingInterceptor - Request:
- method: POST
- url: https://open.bigmodel.cn/api/paas/v4/chat/completions
- headers: [Authorization: Bearer ey...fQ.ew0KICAiZXhwIiA6IDE3NDQ4MDA1MDUzMTYsDQogICJhcGlfa2V5IiA6ICJkMjMzNGIyZTk4NTE0YWUwOTIzMDQ4YTM4NDEzNTMwNiIsDQogICJ0aW1lc3RhbXAiIDogMTc0NDc5ODcwNTMxNg0KfQ.spYBN1bFpDVUPH442OesHFx9Qq0fzTlQTg1zt1l01bo]
- body: {
"model" : "GLM-4-Flash",
"messages" : [ {
"role" : "user",
"content" : "针对提出的问题计算如下两个字符串的长度和的平方根:hello和world,请尽量调用已有的函数工具"
}, {
"role" : "assistant",
"tool_calls" : [ {
"id" : "call_-8790922223921709504",
"type" : "function",
"function" : {
"name" : "stringLength",
"arguments" : "{\"arg0\": \"hello\"}"
}
} ]
}, {
"role" : "tool",
"content" : "5"
}, {
"role" : "assistant",
"tool_calls" : [ {
"id" : "call_-8790922876756788063",
"type" : "function",
"function" : {
"name" : "stringLength",
"arguments" : "{\"arg0\": \"world\"}"
}
} ]
}, {
"role" : "tool",
"content" : "5"
}, {
"role" : "assistant",
"tool_calls" : [ {
"id" : "call_-8790925178859487903",
"type" : "function",
"function" : {
"name" : "add",
"arguments" : "{\"arg0\": 5, \"arg1\": 5}"
}
} ]
}, {
"role" : "tool",
"content" : "10"
}, {
"role" : "assistant",
"tool_calls" : [ {
"id" : "call_-8790941396658651614",
"type" : "function",
"function" : {
"name" : "sqrt",
"arguments" : "{\"arg0\": 10}"
}
} ]
}, {
"role" : "tool",
"content" : "3.1622776601683795"
} ],
"stream" : false,
"temperature" : 0.9,
"max_tokens" : 512,
"tools" : [ {
"type" : "function",
"function" : {
"name" : "add",
"description" : "计算两个整数的和",
"parameters" : {
"type" : "object",
"properties" : {
"arg0" : {
"type" : "integer"
},
"arg1" : {
"type" : "integer"
}
},
"required" : [ "arg0", "arg1" ]
}
}
}, {
"type" : "function",
"function" : {
"name" : "sqrt",
"description" : "计算整数的平方根",
"parameters" : {
"type" : "object",
"properties" : {
"arg0" : {
"type" : "integer"
}
},
"required" : [ "arg0" ]
}
}
}, {
"type" : "function",
"function" : {
"name" : "stringLength",
"description" : "计算字符串的长度",
"parameters" : {
"type" : "object",
"properties" : {
"arg0" : {
"type" : "string"
}
},
"required" : [ "arg0" ]
}
}
} ],
"tool_choice" : "auto"
}
14.第五次请求后返回的响应数据
{
"choices": [{
"finish_reason": "stop",
"index": 0,
"message": {
"content": "两个字符串的长度和为10,其平方根为3.1622776601683795。",
"role": "assistant"
}
}],
"created": 1744798706,
"id": "2025041618182476bce54ece6d4e72",
"model": "GLM-4-Flash",
"request_id": "2025041618182476bce54ece6d4e72",
"usage": {
"completion_tokens": 26,
"prompt_tokens": 434,
"total_tokens": 460
}
}
整个过程结束。
通过该过程可以发现,大模型对函数工具进行了多次调用,而且发送了多次请求。
注意:LangChain4j还提供了@P注解,该注解用来修改方法的参数,本文不再演示其用法,感兴趣的读者可以注解查看LangChain4j的帮助文档工具 (函数调用) |LangChain4j
980

被折叠的 条评论
为什么被折叠?



