|
| 1 | +package com.cloud.alibaba.ai.example.controller; |
| 2 | + |
| 3 | +import com.alibaba.cloud.ai.graph.RunnableConfig; |
| 4 | +import com.alibaba.cloud.ai.graph.action.InterruptionMetadata; |
| 5 | +import com.alibaba.cloud.ai.graph.agent.ReactAgent; |
| 6 | +import org.springframework.stereotype.Controller; |
| 7 | +import org.springframework.web.bind.annotation.GetMapping; |
| 8 | +import org.springframework.web.bind.annotation.PostMapping; |
| 9 | +import org.springframework.web.bind.annotation.RequestBody; |
| 10 | +import org.springframework.web.bind.annotation.RequestParam; |
| 11 | +import org.springframework.web.bind.annotation.ResponseBody; |
| 12 | + |
| 13 | +import java.util.List; |
| 14 | +import java.util.Map; |
| 15 | +import java.util.concurrent.ConcurrentHashMap; |
| 16 | + |
| 17 | +@Controller |
| 18 | +public class AgentController { |
| 19 | + |
| 20 | + private final ReactAgent reactAgent; |
| 21 | + |
| 22 | + private final Map<String, InterruptionMetadata> map = new ConcurrentHashMap<>(); |
| 23 | + |
| 24 | + public AgentController(ReactAgent reactAgent) { |
| 25 | + this.reactAgent = reactAgent; |
| 26 | + } |
| 27 | + |
| 28 | + @GetMapping("/invoke") |
| 29 | + @ResponseBody |
| 30 | + public List<InterruptionMetadata.ToolFeedback> invoke(@RequestParam("query") String query, |
| 31 | + @RequestParam("threadId") String threadId |
| 32 | + ) throws Exception { |
| 33 | + RunnableConfig runnableConfig = RunnableConfig.builder().threadId(threadId).build(); |
| 34 | + InterruptionMetadata metadata = (InterruptionMetadata) reactAgent.invokeAndGetOutput(query, runnableConfig).orElseThrow(); |
| 35 | + map.put(threadId, metadata); |
| 36 | + return metadata.toolFeedbacks(); |
| 37 | + } |
| 38 | + |
| 39 | + @PostMapping("/feedback") |
| 40 | + @ResponseBody |
| 41 | + public String feedback(@RequestBody List<Feedback> feedbacks, |
| 42 | + @RequestParam("threadId") String threadId |
| 43 | + ) throws Exception { |
| 44 | + InterruptionMetadata metadata = map.get(threadId); |
| 45 | + if(metadata == null) { |
| 46 | + return "no metadata found"; |
| 47 | + } |
| 48 | + if(metadata.toolFeedbacks().size() != feedbacks.size()) { |
| 49 | + return "feedback size not match"; |
| 50 | + } |
| 51 | + |
| 52 | + InterruptionMetadata.Builder newBuilder = InterruptionMetadata.builder() |
| 53 | + .nodeId(metadata.node()) |
| 54 | + .state(metadata.state()); |
| 55 | + for (int i = 0; i < feedbacks.size(); i++) { |
| 56 | + var toolFeedback = metadata.toolFeedbacks().get(i); |
| 57 | + InterruptionMetadata.ToolFeedback.Builder editedFeedbackBuilder = InterruptionMetadata.ToolFeedback |
| 58 | + .builder(toolFeedback); |
| 59 | + if(feedbacks.get(i).isApproved()) { |
| 60 | + editedFeedbackBuilder.result(InterruptionMetadata.ToolFeedback.FeedbackResult.APPROVED); |
| 61 | + } else { |
| 62 | + editedFeedbackBuilder.result(InterruptionMetadata.ToolFeedback.FeedbackResult.REJECTED) |
| 63 | + .description(feedbacks.get(i).feedback()); |
| 64 | + } |
| 65 | + newBuilder.addToolFeedback(editedFeedbackBuilder.build()); |
| 66 | + } |
| 67 | + RunnableConfig resumeRunnableConfig = RunnableConfig.builder().threadId(threadId) |
| 68 | + .addMetadata(RunnableConfig.HUMAN_FEEDBACK_METADATA_KEY, newBuilder.build()) |
| 69 | + .build(); |
| 70 | + reactAgent.invokeAndGetOutput("", resumeRunnableConfig); |
| 71 | + return "success"; |
| 72 | + } |
| 73 | + |
| 74 | + @GetMapping |
| 75 | + public String index() { |
| 76 | + return "index"; |
| 77 | + } |
| 78 | +} |
0 commit comments