Commit 47df4bb21fcc8230bee1338b52c9dea484cb2b6b
1 parent
3445baf9
feat: mcp demo
Showing
1 changed file
with
124 additions
and
0 deletions
yudao-module-ai/yudao-spring-boot-starter-ai/src/test/java/cn/iocoder/yudao/framework/ai/mcp/DouBaoMcpTests.java
0 → 100644
| 1 | +package cn.iocoder.yudao.framework.ai.mcp; | |
| 2 | + | |
| 3 | +import cn.iocoder.yudao.framework.ai.core.model.doubao.DouBaoChatModel; | |
| 4 | +import org.junit.jupiter.api.Test; | |
| 5 | +import org.springframework.ai.chat.client.ChatClient; | |
| 6 | +import org.springframework.ai.openai.OpenAiChatModel; | |
| 7 | +import org.springframework.ai.openai.OpenAiChatOptions; | |
| 8 | +import org.springframework.ai.openai.api.OpenAiApi; | |
| 9 | +import org.springframework.ai.tool.annotation.Tool; | |
| 10 | +import org.springframework.ai.tool.method.MethodToolCallbackProvider; | |
| 11 | + | |
| 12 | + | |
| 13 | +public class DouBaoMcpTests { | |
| 14 | + | |
| 15 | + | |
| 16 | + private final OpenAiChatModel openAiChatModel = OpenAiChatModel.builder() | |
| 17 | + .openAiApi(OpenAiApi.builder() | |
| 18 | + .baseUrl(DouBaoChatModel.BASE_URL) | |
| 19 | + .apiKey("5c1b5747-26d2-4ebd-a4e0-dd0e8d8b4272") // apiKey | |
| 20 | + .build()) | |
| 21 | + .defaultOptions(OpenAiChatOptions.builder() | |
| 22 | + .model("doubao-1-5-lite-32k-250115") // 模型(doubao) | |
| 23 | + .temperature(0.7) | |
| 24 | + .build()) | |
| 25 | + .build(); | |
| 26 | + | |
| 27 | + private final DouBaoChatModel chatModel = new DouBaoChatModel(openAiChatModel); | |
| 28 | + | |
| 29 | + private final MethodToolCallbackProvider provider = MethodToolCallbackProvider.builder() | |
| 30 | + .toolObjects(new UserService()) | |
| 31 | + .build(); | |
| 32 | + | |
| 33 | + private final ChatClient chatClient = ChatClient.builder(chatModel) | |
| 34 | + .defaultTools(provider) | |
| 35 | + .build(); | |
| 36 | + | |
| 37 | + @Test | |
| 38 | + public void testMcpGetUserInfo() { | |
| 39 | + | |
| 40 | + // 打印结果 | |
| 41 | + System.out.println(chatClient.prompt() | |
| 42 | + .user("目前有哪些工具可以使用") | |
| 43 | + .call() | |
| 44 | + .content()); | |
| 45 | + System.out.println("===================================="); | |
| 46 | + // 打印结果 | |
| 47 | + System.out.println(chatClient.prompt() | |
| 48 | + .user("小新的年龄是多少") | |
| 49 | + .call() | |
| 50 | + .content()); | |
| 51 | + System.out.println("===================================="); | |
| 52 | + // 打印结果 | |
| 53 | + System.out.println(chatClient.prompt() | |
| 54 | + .user("获取小新的基本信息") | |
| 55 | + .call() | |
| 56 | + .content()); | |
| 57 | + System.out.println("===================================="); | |
| 58 | + // 打印结果 | |
| 59 | + System.out.println(chatClient.prompt() | |
| 60 | + .user("小新是什么职业的") | |
| 61 | + .call() | |
| 62 | + .content()); | |
| 63 | + System.out.println("===================================="); | |
| 64 | + // 打印结果 | |
| 65 | + System.out.println(chatClient.prompt() | |
| 66 | + .user("小新的教育背景") | |
| 67 | + .call() | |
| 68 | + .content()); | |
| 69 | + System.out.println("===================================="); | |
| 70 | + // 打印结果 | |
| 71 | + System.out.println(chatClient.prompt() | |
| 72 | + .user("小新的兴趣爱好是什么") | |
| 73 | + .call() | |
| 74 | + .content()); | |
| 75 | + System.out.println("===================================="); | |
| 76 | + | |
| 77 | + } | |
| 78 | + | |
| 79 | + | |
| 80 | + static class UserService { | |
| 81 | + | |
| 82 | + @Tool(name = "getUserAge", description = "获取用户年龄") | |
| 83 | + public String getUserAge(String userName) { | |
| 84 | + return "《" + userName + "》的年龄为:18"; | |
| 85 | + } | |
| 86 | + | |
| 87 | + @Tool(name = "getUserSex", description = "获取用户性别") | |
| 88 | + public String getUserSex(String userName) { | |
| 89 | + return "《" + userName + "》的性别为:男"; | |
| 90 | + } | |
| 91 | + | |
| 92 | + @Tool(name = "getUserBasicInfo", description = "获取用户基本信息,包括姓名、年龄、性别等") | |
| 93 | + public String getUserBasicInfo(String userName) { | |
| 94 | + return "《" + userName + "》的基本信息:\n姓名:" + userName + "\n年龄:18\n性别:男\n身高:175cm\n体重:65kg"; | |
| 95 | + } | |
| 96 | + | |
| 97 | + @Tool(name = "getUserContact", description = "获取用户联系方式,包括电话、邮箱等") | |
| 98 | + public String getUserContact(String userName) { | |
| 99 | + return "《" + userName + "》的联系方式:\n电话:138****1234\n邮箱:" + userName.toLowerCase() + "@example.com\nQQ:123456789"; | |
| 100 | + } | |
| 101 | + | |
| 102 | + @Tool(name = "getUserAddress", description = "获取用户地址信息") | |
| 103 | + public String getUserAddress(String userName) { | |
| 104 | + return "《" + userName + "》的地址信息:北京市朝阳区科技园区88号"; | |
| 105 | + } | |
| 106 | + | |
| 107 | + @Tool(name = "getUserJob", description = "获取用户职业信息") | |
| 108 | + public String getUserJob(String userName) { | |
| 109 | + return "《" + userName + "》的职业信息:软件工程师,就职于ABC科技有限公司,工作年限5年"; | |
| 110 | + } | |
| 111 | + | |
| 112 | + @Tool(name = "getUserHobbies", description = "获取用户兴趣爱好") | |
| 113 | + public String getUserHobbies(String userName) { | |
| 114 | + return "《" + userName + "》的兴趣爱好:编程、阅读、旅游、摄影、打篮球"; | |
| 115 | + } | |
| 116 | + | |
| 117 | + @Tool(name = "getUserEducation", description = "获取用户教育背景") | |
| 118 | + public String getUserEducation(String userName) { | |
| 119 | + return "《" + userName + "》的教育背景:\n本科:计算机科学与技术专业,北京大学\n硕士:软件工程专业,清华大学"; | |
| 120 | + } | |
| 121 | + | |
| 122 | + } | |
| 123 | + | |
| 124 | +} | |
| 0 | 125 | \ No newline at end of file | ... | ... |