Spring AI Alibaba——支持Agent Skill
文章目录前言版本准备1、新建skills2、自定义tools3、启动类4、测试类总结前言Spring AI Alibaba是阿里团队针对Spring AI框架在国内应用风格的一种包装、扩展与延伸。对Agent Skills的支持比Langchain4j更早但对springboot 版本要求更高点。之前写了demo忘了做记录。今天补上。版本准备jdk 17maven 3.6.3springboot 3.5.4spring-ai 1.1.2spring-ai-alibaba 1.1.2.0完整pom信息?xml version1.0 encodingUTF-8?projectxmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion3.5.4/versionrelativePath//parentartifactIdspring-ai-alibaba/artifactIdpropertiesmaven.compiler.source17/maven.compiler.sourcemaven.compiler.target17/maven.compiler.targetproject.build.sourceEncodingUTF-8/project.build.sourceEncoding/propertiesdependencyManagementdependenciesdependencygroupIdcom.alibaba.cloud.ai/groupIdartifactIdspring-ai-alibaba-bom/artifactIdversion1.1.2.0/versiontypepom/typescopeimport/scope/dependencydependencygroupIdorg.springframework.ai/groupIdartifactIdspring-ai-bom/artifactIdversion1.1.2/versiontypepom/typescopeimport/scope/dependencydependencygroupIdcom.alibaba.cloud.ai/groupIdartifactIdspring-ai-alibaba-extensions-bom/artifactIdversion1.1.2.1/versiontypepom/typescopeimport/scope/dependency/dependencies/dependencyManagementdependenciesdependencygroupIdcom.alibaba.cloud.ai/groupIdartifactIdspring-ai-alibaba-agent-framework/artifactId/dependency!-- 百炼大模型 --dependencygroupIdcom.alibaba.cloud.ai/groupIdartifactIdspring-ai-alibaba-starter-dashscope/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactId/dependency/dependencies/project#测试代码1、新建skills在src/main/resources下新建目录skills并在其中新增weather目录。SKILL.md是在https://skills.sh/随便找的也可以自己写。--- name: weather description: Get current weather conditions and forecasts. --- # Weather Two free services, no API keys needed. ## wttr.in (primary) Quick one-liner: bash curl -s wttr.in/London?format3 # Output: London: ⛅️ 8°C Compact format: bash curl -s wttr.in/London?format%l:%c%t%h%w # Output: London: ⛅️ 8°C 71% ↙5km/h Full forecast: bash curl -s wttr.in/London?T Format codes: %c condition · %t temp · %h humidity · %w wind · %l location · %m moon Tips: - URL-encode spaces: wttr.in/NewYork - Airport codes: wttr.in/JFK - Units: ?m (metric) ?u (USCS) - Today only: ?1 · Current only: ?0 - PNG: curl -s wttr.in/Berlin.png -o /tmp/weather.png ## Open-Meteo (fallback, JSON) Free, no key, good for programmatic use: bash curl -s https://api.open-meteo.com/v1/forecast?latitude51.5longitude-0.12current_weathertrue Find coordinates for a city, then query. Returns JSON with temp, windspeed, weathercode. Docs: https://open-meteo.com/en/docs2、自定义tools编写一个自定义的tools类。importorg.springframework.ai.chat.model.ToolContext;importorg.springframework.ai.tool.annotation.ToolParam;importjava.util.function.BiFunction;/** * 工具 */publicclassMyCityToolsimplementsBiFunctionString,ToolContext,String{OverridepublicStringapply(ToolParam(descriptionThe city name)Stringcity,ToolContexttoolContext){returnThe Citys name is city!;}}3、启动类importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.boot.context.properties.EnableConfigurationProperties;SpringBootApplicationEnableConfigurationPropertiespublicclassApplication{publicstaticvoidmain(String[]args){SpringApplication.run(Application.class,args);}}4、测试类importcn.xj.Application;importcn.xj.toos.MyCityTools;importcom.alibaba.cloud.ai.dashscope.api.DashScopeApi;importcom.alibaba.cloud.ai.dashscope.chat.DashScopeChatModel;importcom.alibaba.cloud.ai.dashscope.chat.DashScopeChatOptions;importcom.alibaba.cloud.ai.graph.RunnableConfig;importcom.alibaba.cloud.ai.graph.agent.ReactAgent;importcom.alibaba.cloud.ai.graph.agent.hook.skills.SkillsAgentHook;importcom.alibaba.cloud.ai.graph.checkpoint.savers.MemorySaver;importcom.alibaba.cloud.ai.graph.exception.GraphRunnerException;importcom.alibaba.cloud.ai.graph.skills.registry.filesystem.FileSystemSkillRegistry;importorg.junit.jupiter.api.Test;importorg.springframework.ai.chat.messages.AssistantMessage;importorg.springframework.ai.tool.ToolCallback;importorg.springframework.ai.tool.function.FunctionToolCallback;importorg.springframework.boot.test.context.SpringBootTest;importjava.util.List;SpringBootTest(classesApplication.class)publicclassTestDemo{Testpublicvoidtest()throwsGraphRunnerException{// 加载百炼大模型 配置keyDashScopeApidashScopeApiDashScopeApi.builder().apiKey(System.getenv(ALI_AI_KEY))//.baseUrl().build();// 构建chatmodelDashScopeChatModelchatModelDashScopeChatModel.builder().dashScopeApi(dashScopeApi).defaultOptions(DashScopeChatOptions.builder()// qwen-plus.model(DashScopeChatModel.DEFAULT_MODEL_NAME).temperature(0.5).maxToken(1000).build()).build();// 加载 toolsToolCallbackgetCityNameTollFunctionToolCallback.builder(getCityNameForLocation,newMyCityTools()).description(Get name for a given city).inputType(String.class).build();// 加载agent skillsFileSystemSkillRegistryskillRegistryFileSystemSkillRegistry.builder().projectSkillsDirectory(System.getProperty(user.dir)/src/main/resources/skills/).build();SkillsAgentHookskillsAgentHookSkillsAgentHook.builder().skillRegistry(skillRegistry).build();// 构建agentReactAgentagentReactAgent.builder().name(test_agent).model(chatModel).tools(List.of(getCityNameToll))// tools.hooks(List.of(skillsAgentHook))// skills.systemPrompt(You are a helpful assistant).saver(newMemorySaver())// RedisSaver MysqlSaver.build();// threadId 是给定对话的唯一标识符StringthreadId001;RunnableConfigrunnableConfigRunnableConfig.builder().threadId(threadId).addMetadata(user_id,1).build();// 第一次调用AssistantMessageresponseagent.call(What is the name of the city where China Optics Valley is located ?,runnableConfig);System.out.println(response.getText());// 问题命中 skillsresponseagent.call(Whats the weather like in Wuhan now?,runnableConfig);System.out.println(response.getText());// 注意我们可以使用相同的 threadId 继续对话responseagent.call(thank you!,runnableConfig);System.out.println(response.getText());}}总结AI Alibaba框架的配置Agent Skills和tools相对Langchain4j而言使用的是独立的配置。但是在Langchain4j中却是共用一个。AI AlibabaLangchain4j
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2546077.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!