欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 培训 > spring ai如何使用function call调用第三方模型

spring ai如何使用function call调用第三方模型

2025/5/21 2:41:01 来源:https://blog.csdn.net/u013036688/article/details/144266724  浏览:    关键词:spring ai如何使用function call调用第三方模型

这里写自定义目录标题

      • 背景
      • 什么是function call
      • 怎么用function call?
      • 总结

背景

一直困惑于ai是如何使用插件或者其他一些功能的,后来发现,很多大模型都支持function call功能,如何让大模型能够联网查询呢,function call就可以做到。

什么是function call

Function calling enables developers to connect language models to external data and systems. You can define a set of functions as tools that the model has access to, and it can use them when appropriate based on the conversation history. You can then execute those functions on the application side, and provide results back to the model.
函数调用使开发人员能够将语言模型连接到外部数据和系统。您可以将一组函数定义为模型有权访问的工具,并且它可以根据对话历史记录在适当的时候使用它们。然后,您可以在应用程序端执行这些函数,并将结果返回给模型。

你可以理解为,大模型允许你按照一定的格式,告诉大模型你可以提供的函数和函数功能,然后当你调用大模型的时候,它可以参考是否有可用的函数来完成本次工作,如果有的话,返回值里就会指示要调用函数,并且给出入参。

怎么用function call?

官方文档写的很清楚,可以参考:Function Calling 函数调用
我主要是照着这个文档整的,好的,接下来直接给例子:
涉及到以下几个类:
你的函数类:

@Service
@Slf4j
public class WeatherService implements Function<WeatherService.Request, WeatherService.Response> {public enum Unit { C, F }@JsonClassDescription("Get the weather in location") // // function descriptionpublic record Request(String location, Unit unit) {}public record Response(double temp, Unit unit) {}@Overridepublic Response apply(Request request) {log.info("WeatherService apply request: {}", request);return new Response(30.0, Unit.C);}@Configurationstatic class Config {@Beanpublic Function<Request, Response> currentWeather() { // bean name as function namereturn new WeatherService();}@Beanpublic FunctionCallback weatherFunctionInfo() {return FunctionCallback.builder().description("Get the weather in location") // (2) function description.function("CurrentWeather", new WeatherService()) // (1) function name and instance.inputType(WeatherService.Request.class) // (3) function input type.build();}}}

调用方法:

    
@RestController
@Slf4j
public class TestController {@Autowiredprivate OpenAiChatModel openAiChatModel;@GetMapping("testFunctionCall")public String testFunctionCall(){UserMessage userMessage = new UserMessage("What's the weather like in San Francisco, Tokyo, and Paris?");OpenAiChatOptions options = OpenAiChatOptions.builder().withModel("qwen2").withFunctionCallbacks(List.of(FunctionCallback.builder().description("Get the weather in location") // (2) function description.function("CurrentWeather", new WeatherService()) // (1) function name and instance.inputType(WeatherService.Request.class) // (3) function input type.build())) // function code.withToolContext(Map.of("sessionId", "123", "userId", "user456")).build();ChatResponse response = openAiChatModel.call(new Prompt(userMessage, options));log.info("ai call response:{}",response);return response.getResult().getOutput().getContent();}
}

总结

用spring ai来使用function call还是很简单的,如果用python的话,还要给函数写定义,spring ai很多事情都已经帮我们包装好了,直接用就行。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词