Kaynağa Gözat

springai添加可访问数据库智能体,支持外部传参,但会被多次解读,慎重使用

Taohongrun 5 ay önce
ebeveyn
işleme
af287d7d3b

+ 16 - 3
src/main/java/io/github/qifan777/knowledge/ai/agent/userAgent/UserInfo.java

@@ -4,7 +4,9 @@ import com.fasterxml.jackson.annotation.JsonProperty;
 import com.fasterxml.jackson.annotation.JsonPropertyDescription;
 import io.github.qifan777.knowledge.ai.agent.AbstractAgent;
 import io.github.qifan777.knowledge.ai.agent.Agent;
+import io.github.qifan777.knowledge.context.UserContext;
 import lombok.AllArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.ai.chat.client.ChatClient;
 import org.springframework.ai.chat.model.ChatModel;
 import org.springframework.context.annotation.Description;
@@ -20,21 +22,32 @@ import java.util.function.Function;
  * @since 2025/3/14
  */
 @Agent
-@Description("获取当前系统用户相关提问于回答")
+@Description("回答用户关于我的成绩,查询成绩,获取成绩相关问题")
 @AllArgsConstructor
+@Slf4j
 public class UserInfo extends AbstractAgent implements Function<UserInfo.Request, String> {
     private final ChatModel chatModel;
+
     @Override
     public String apply(Request request) {
+        System.out.println(request.userName);
         return ChatClient.create(chatModel)
                 .prompt()
+                .system(s -> s.text("用户名为{userName},需要查询成绩信息。问题:{query}")
+                        .param("userName", request.userName)
+                        .param("query", request.query()))
                 .functions(getFunctions(UserNameFunction.class))
-                .user(request.query())
+                .user(userSpec -> userSpec
+                        .param("userName", request.userName) // 传递参数
+                        .text(request.query()))
                 .call()
                 .content();
     }
 
     public record Request(
-            @JsonProperty(required = true) @JsonPropertyDescription(value = "用户原始的提问") String query) {
+            @JsonProperty(required = true) @JsonPropertyDescription(value = "用户原始的提问") String query,
+            @JsonProperty(required = true)
+            @JsonPropertyDescription("this param is user's name")
+            String userName) {
     }
 }

+ 14 - 8
src/main/java/io/github/qifan777/knowledge/ai/agent/userAgent/UserNameFunction.java

@@ -3,6 +3,8 @@ package io.github.qifan777.knowledge.ai.agent.userAgent;
 import cn.dev33.satoken.stp.StpUtil;
 import com.baomidou.mybatisplus.extension.toolkit.Db;
 
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.annotation.JsonPropertyDescription;
 import io.github.qifan777.knowledge.context.UserContext;
 import io.github.qifan777.knowledge.domain.po.User;
 import lombok.extern.slf4j.Slf4j;
@@ -24,21 +26,25 @@ import java.util.stream.Collectors;
  * @since 2025/3/13
  */
 @Component
-@Description("获取系统用户名称")
+@Description("查询获取成绩返回给用户")
 @Slf4j
-public  class UserNameFunction implements Function<UserNameFunction.Request, String> {
+public  class UserNameFunction implements Function<UserNameFunction.Request, Integer> {
 
     @Autowired
     RedisTemplate redisTemplate;
 
     @Override
-    public String apply(Request request) {
-        System.out.println(UserContext.getThreadUserId());
-        List<String> userNameList = Db.lambdaQuery(User.class).list().stream().map(User::getNickname).collect(Collectors.toList());
-        System.out.println(".................xxxxxxxxxxxxxx:{}xxxxxxx"+userNameList);
-        return userNameList.toString();
+    public Integer apply(Request request) {
+
+        System.out.println(request.userName);
+        return Db.lambdaQuery(User.class).select().eq(User::getNickname, request.userName).one().getScore();
+
+
+
     }
 
-    public record Request() {
+    public record Request( @JsonProperty(required = true)
+                           @JsonPropertyDescription("this param is user's name")
+                           String userName) {
     }
 }

+ 8 - 2
src/main/java/io/github/qifan777/knowledge/ai/message/AiMessageController.java

@@ -1,13 +1,16 @@
 package io.github.qifan777.knowledge.ai.message;
 
+import com.baomidou.mybatisplus.extension.toolkit.Db;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import io.github.qifan777.knowledge.ai.agent.Agent;
 import io.github.qifan777.knowledge.ai.message.dto.AiMessageInput;
 import io.github.qifan777.knowledge.ai.message.dto.AiMessageWrapper;
 import io.github.qifan777.knowledge.context.UserContext;
+import io.github.qifan777.knowledge.domain.po.User;
 import lombok.AllArgsConstructor;
 import lombok.SneakyThrows;
 import lombok.extern.slf4j.Slf4j;
+import org.babyfish.jimmer.sql.ast.PropExpression;
 import org.springframework.ai.chat.client.ChatClient;
 import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
 import org.springframework.ai.chat.client.advisor.QuestionAnswerAdvisor;
@@ -79,7 +82,6 @@ public class AiMessageController {
     @SneakyThrows
     @PostMapping(value = "chat", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
     public Flux<ServerSentEvent<String>> chat(@RequestPart String input, @RequestPart(required = false) MultipartFile file) {
-        System.out.println(UserContext.getThreadUserId());
         AiMessageWrapper aiMessageWrapper = objectMapper.readValue(input, AiMessageWrapper.class);
         String[] functionBeanNames = new String[0];
         // 如果启用Agent则获取Agent的bean
@@ -89,10 +91,14 @@ public class AiMessageController {
             functionBeanNames = new String[beansWithAnnotation.size()];
             functionBeanNames = beansWithAnnotation.keySet().toArray(functionBeanNames);
         }
+        String userId = UserContext.getThreadUserId();
         return ChatClient.create(chatModel).prompt()
                 // 启用文件问答
                 .system(promptSystemSpec -> useFile(promptSystemSpec, file))
-                .user(promptUserSpec -> toPrompt(promptUserSpec, aiMessageWrapper.getMessage()))
+                .user(promptUserSpec -> {
+                    toPrompt(promptUserSpec, aiMessageWrapper.getMessage());
+                    promptUserSpec.param("userName", Db.lambdaQuery(User.class).select().eq(User::getId,userId).one().getNickname());
+                })
                 // agent列表
                 .functions(functionBeanNames)
                 .advisors(advisorSpec -> {

+ 1 - 0
src/main/java/io/github/qifan777/knowledge/domain/po/User.java

@@ -22,4 +22,5 @@ public class User {
     private String id;
     private String phone;
     private String nickname;
+    private Integer score;
 }