直接用JSON定义工具
在前面章节中,我们用AddTool方法增加工具,用AddParameter方法增加参数。
AddTool和AddParameter是Foxtable的二次封装,目的是降低工具定义的难度。
但是AI工具的定义其实是比较多样的,单靠这两个方法难以覆盖所有需求。
DeepSeek关于工具定义的官方文档地址:
https://api-docs.deepseek.com/zh-cn/guides/tool_calls
可以看到对于一些特殊场景的需求,AddTool和AddParameter是无法覆盖到的。
所以LLMCaller另外提供了一个方法,可以直接用原生JSON方式添加工具:
AddJsonTool(tool)
tool:String或JObject类型,用于定义工具的JSON字符串或JObject对象
示例一
下面的代码,用JSON定义两个工具,分别是add和substract,然后一次性注册到LLMCaller:
Dim
jsontools
As
String
=
<![CDATA[
[
{
"type": "function",
"function": {
"name": "add",
"description": "计算两个数字的和(加法),会在正确结果上加上100,结果为:
a + b + 100",
"parameters": {
"type": "object",
"properties": {
"a": {"type": "number", "description": "第一个数字"},
"b": {"type": "number", "description": "第二个数字"}
},
"required": ["a", "b"]
}
}
},
{
"type": "function",
"function": {
"name": "subtract",
"description": "计算两个数字的差(减法),会在正却结果上加100,结果为:
a - b + 100",
"parameters": {
"type": "object",
"properties": {
"a": {"type": "number", "description": "被减数"},
"b": {"type": "number", "description": "减数"}
},
"required": ["a", "b"]
}
}
}
]
]]>.Value
Dim
caller
As
New
LLMCaller("https://api.deepseek.com/chat/completions",
"deepseek-flash",
"sk-a734b83536da45e9aedc3e42605bfb3a")
caller.SystemPrompt =
"对于使用外部工具返回的结果,不得质疑篡改,无论结果对错,都直接使用即可,无需多余解释"
caller.Messages =
New
JArray()
'开启多轮对话
caller.AddJsonTool(jsontools)
'将工具注册到LLmCaller
caller.ToolExecutor =
Function(ttoolName
As
String,
targs
As
JObject)
As
String
Select
Case
ttoolName
Case
"add"
Dim
ta
As
Double
= targs("a").ToObject(Of
Double)()
Dim
tb
As
Double
= targs("b").ToObject(Of
Double)()
Return
ta + tb + 100
'为了验证是否是工具执行,将结果加上100
Case
"subtract"
Dim
ta
As
Double
= targs("a").ToObject(Of
Double)()
Dim
tb
As
Double
= targs("b").ToObject(Of
Double)()
Return
ta - tb + 100
'为了验证是否是工具执行,将结果加上100
Case
Else
Return
"未知工具:"
& ttoolName
End
Select
End
Function
For
Each
msg
As
String
In
{"3+5等于?",
"再减去四呢?"}
caller.UserMessage = msg
If
caller.GetResult()
Then
MessageBox.Show(caller.Result)
End
If
Next
你也可以逐个工具添加,例如:
Dim
tool1
As
String
=
<![CDATA[
{
"type": "function",
"function": {
"name": "add",
"description": "计算两个数字的和(加法),会在正确结果上加上100,结果为:
a + b + 100",
"parameters": {
"type": "object",
"properties": {
"a": {"type": "number", "description": "第一个数字"},
"b": {"type": "number", "description": "第二个数字"}
},
"required": ["a", "b"]
}
}
}
]]>.Value
caller.AddJsonTool(tool1)
Dim
tool2
As
String
=
<![CDATA[
{
"type": "function",
"function": {
"name": "subtract",
"description": "计算两个数字的差(减法),会在正却结果上加100,结果为:
a - b + 100",
"parameters": {
"type": "object",
"properties": {
"a": {"type": "number", "description": "被减数"},
"b": {"type": "number", "description": "减数"}
},
"required": ["a", "b"]
}
}
}
]]>.Value
caller.AddJsonTool(tool2)
示例二
需要的话,
你也可以通过JObject来定义工具,例如:
Dim
caller
As
New
LLMCaller("https://api.deepseek.com/chat/completions",
"deepseek-flash",
"sk-a734b83536da45e9aedc3e42605bfb3a")
caller.SystemPrompt =
"对于使用外部工具返回的结果,不得质疑篡改,无论结果对错,都直接使用即可,无需多余解释"
caller.Messages =
New
JArray()
'
开启多轮对话
'========== 工具1:add
==========
Dim
addFunc
As
New
JObject()
addFunc("name")
=
"add"
addFunc("description")
=
"计算两个数字的和(加法),会在正确结果上加上100,结果为:a
+ b + 100"
Dim
addParams
As
New
JObject()
addParams("type")
=
"object"
Dim
addProps
As
New
JObject()
Dim
addA
As
New
JObject()
addA("type")
=
"number"
addA("description")
=
"第一个数字"
addProps("a")
= addA
Dim
addB
As
New
JObject()
addB("type")
=
"number"
addB("description")
=
"第二个数字"
addProps("b")
= addB
addParams("properties")
= addProps
addParams("required")
=
New
JArray("a",
"b")
addFunc("parameters")
= addParams
Dim
addTool
As
New
JObject()
addTool("type")
=
"function"
addTool("function")
= addFunc
caller.AddJsonTool(addTool) '将工具添加到LLMCaller中
'========== 工具2:subtract
==========
Dim
subFunc
As
New
JObject()
subFunc("name")
=
"subtract"
subFunc("description")
=
"计算两个数字的差(减法),会在正确结果上加上100,结果为:a
- b + 100"
Dim
subParams
As
New
JObject()
subParams("type")
=
"object"
Dim
subProps
As
New
JObject()
Dim
subA
As
New
JObject()
subA("type")
=
"number"
subA("description")
=
"被减数"
subProps("a")
= subA
Dim
subB
As
New
JObject()
subB("type")
=
"number"
subB("description")
=
"减数"
subProps("b")
= subB
subParams("properties")
= subProps
subParams("required")
=
New
JArray("a",
"b")
subFunc("parameters")
= subParams
Dim
subTool
As
New
JObject()
subTool("type")
=
"function"
subTool("function")
= subFunc
caller.AddJsonTool(subTool)
'将工具添加到LLMCaller中
'==========
工具执行逻辑
==========
caller.ToolExecutor =
Function(ttoolName
As
String,
targs
As
JObject)
As
String
Select
Case
ttoolName
Case
"add"
Dim
ta
As
Double
= targs("a").ToObject(Of
Double)()
Dim
tb
As
Double
= targs("b").ToObject(Of
Double)()
Return
ta + tb + 100
Case
"subtract"
Dim
ta
As
Double
= targs("a").ToObject(Of
Double)()
Dim
tb
As
Double
= targs("b").ToObject(Of
Double)()
Return
ta - tb + 100
Case
Else
Return
"未知工具:"
& ttoolName
End
Select
End
Function
'==========
测试工具
==========
For
Each
msg
As
String
In
New
String()
{"3+5等于?",
"再减去四呢?"}
caller.UserMessage = msg
If
caller.GetResult()
Then
MessageBox.Show(caller.Result)
End
If
Next