chat - 工具上下文和附加参数
v3.4.0 后支持
Options:toolsContext
(工具上下文),可以在工具调用时附加参数。比如,传递鉴权信息、数据隔离标识等。
1、示例
public void case2(ChatConfig config, String user) {
ChatModel chatModel = ChatModel.of(config).build(); //使用聊天配置
chatModel.prompt("hello")
.options(o->o.toolsAdd(new WeatherTool()).toolsContext(Utils.asMap("user", user))) //使用聊天选项
.call();
}
//user 参数不加 @Param(即不要求 llm 生成),由 toolsContext 传入(附加参数)!
public class WeatherTool {
@ToolMapping(description = "获取指定城市的天气情况")
public String get_weather(@Param(description = "根据用户提到的地点推测城市") String location, String user) {
return "晴,24度"; //可使用 “数据库” 或 “网络” 接口根据 location 查询合适数据;
}
}
参考:《模型配置与请求选项》
2、具体说明
-
兼容
MCP
参数传递 -
Options:toolsContext
工具上下文
在 llm 生成的参数之外,传递用户附加的参数。如果参数名相同,toolsContext 会替换 llm 生成的参数。
@Param
注解的参数
情况 | 描述 |
---|---|
有 @Param 注解 | 会成为 tool 的输入架构,会要求 llm 生成 |
无 @Param 注解 | 一般由用户附加输入 |