尧图建网站 尧图建网站 YAOTU WEB BUILD 免费咨询
ARTICLE DETAIL

资讯详情

深耕网站建设与建站编程的一线实战洞察。

六、工具调用

六、工具调用 Tool callingAI大模型借助工具来完成自己本身不能完成的一些事情。AI 工具调用1、web搜索天气API2、数据库数据获取3、PDF生成、文件生成4、API访问大模型工具调用流程图注意工具的调用一定是程序中自己去调用的而非直接给大模型去调用LLM本身具备不稳定性在这种意料之外的情况发生时可能造成你的数据被异常删改等情况。这里很关键的一点是安全性AI模型永远无法直接接触你的API或者系统资源所有操作都必须通过你的程序来执行这样你可以完全控制AI能做什么不能做什么。举例你有一个爆破工具用户给AI提需求拆房子AI表示我要用爆破工具但是需要经过你的同意才能执行爆破。建议使用已有的成熟框架来进行工具的开发调用因为LLM调用的成功率会高效果会好一些。工具调用成功率本应去调用工具的一个问题一些LLM判断问题不需要工具则其回答很可能是有偏差或错误的。推荐使用SpringAI langChain等框架经过专业测试的成功率会高效果会好。工具上下文作用1、安全性一些用户登录信息保存在系统的上下文中而不传给AI。2、避免给AI传递一些多余的信息。SpringAI提供了两种控制工具调用的方式SpringAI框架来调用自己手动来调用ToolCoolingManager 管理工具调用的整个生命周期。和LLM沟通对话并且将对话内容保存, 以此来提供给大模型和SpringAI框架交互的上下文信息。以便LLM语义理解给出一个恰当合适的答案。publicToolExecutionResultexecuteToolCalls(Promptprompt,ChatResponsechatResponse){Assert.notNull(prompt,prompt cannot be null);Assert.notNull(chatResponse,chatResponse cannot be null);OptionalGenerationtoolCallGenerationchatResponse.getResults().stream().filter((g)-!CollectionUtils.isEmpty(g.getOutput().getToolCalls())).findFirst();if(toolCallGeneration.isEmpty()){thrownewIllegalStateException(No tool call requested by the chat model);}else{AssistantMessageassistantMessage((Generation)toolCallGeneration.get()).getOutput();ToolContexttoolContextbuildToolContext(prompt,assistantMessage);InternalToolExecutionResultinternalToolExecutionResultthis.executeToolCall(prompt,assistantMessage,toolContext);ListMessageconversationHistorythis.buildConversationHistoryAfterToolExecution(prompt.getInstructions(),assistantMessage,internalToolExecutionResult.toolResponseMessage());returnToolExecutionResult.builder().conversationHistory(conversationHistory).returnDirect(internalToolExecutionResult.returnDirect()).build();}}Spring AI和LLM的交互根据LLM的响应结果来判断是否需要工具调用。需要调用时则LLM以固定格式来返回工具名参数SpringAI拿到工具名和参数之后去执行工具调用。工具执行结果再添加到对话上下文中再继续请求AI结合工具执行结果来回到用户问题。或者直接返回工具执行结果returnDirectly。鱼皮提供的用户自定义的手动工具执行的示例// 配置不自动执行工具ChatOptionschatOptionsToolCallingChatOptions.builder().toolCallbacks(ToolCallbacks.from(newWeatherTools())).internalToolExecutionEnabled(false)// 禁用内部工具执行.build();// 创建工具调用管理器ToolCallingManagertoolCallingManagerDefaultToolCallingManager.builder().build();// 创建初始提示PromptpromptnewPrompt(获取编程导航的热门项目教程,chatOptions);// 发送请求给模型ChatResponsechatResponsechatModel.call(prompt);// 手动处理工具调用循环while(chatResponse.hasToolCalls()){// 执行工具调用ToolExecutionResulttoolExecutionResulttoolCallingManager.executeToolCalls(prompt,chatResponse);// 创建包含工具结果的新提示promptnewPrompt(toolExecutionResult.conversationHistory(),chatOptions);// 再次发送请求给模型chatResponsechatModel.call(prompt);}// 获取最终回答System.out.println(chatResponse.getResult().getOutput().getText());异常处理当工具调用出现异常这是很常见的情况例如AI识别参数有误构造的参数格式不正确选择错了工具等等。在工具执行过程中都会出现异常。当工具调用时异常默认是不中断不抛出打日志让LLM继续执行。也可以手动将alwaysThrow改为true则出现异常时直接中断。实现工具执行异常处理器的接口直接返回默认异常处理器设置alwaysThrow为true则每次都抛出异常。BeanToolExecutionExceptionProcessortoolExecutionExceptionProcessor(){// true 表示总是抛出异常false 表示返回错误消息给模型returnnewDefaultToolExecutionExceptionProcessor(true);}自定义异常的处理BeanToolExecutionExceptionProcessorcustomExceptionProcessor(){returnexception-{if(exception.getCause()instanceofIOException){// 网络错误返回友好消息给模型returnUnable to access external resource. Please try a different approach.;}elseif(exception.getCause()instanceofSecurityException){// 安全异常直接抛出throwexception;}// 其他异常返回详细信息returnError executing tool: exception.getMessage();};}
返回列表