|
| 1 | +package io.sentrius.agent.analysis.agents.verbs; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.InputStream; |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.List; |
| 7 | +import com.fasterxml.jackson.core.JsonParser; |
| 8 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 9 | +import com.fasterxml.jackson.databind.node.ArrayNode; |
| 10 | +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; |
| 11 | +import io.sentrius.agent.analysis.agents.agents.AgentConfig; |
| 12 | +import io.sentrius.agent.analysis.agents.agents.AgentVerb; |
| 13 | +import io.sentrius.agent.analysis.agents.agents.PromptBuilder; |
| 14 | +import io.sentrius.agent.analysis.agents.agents.VerbRegistry; |
| 15 | +import io.sentrius.agent.analysis.model.TerminalResponse; |
| 16 | +import io.sentrius.agent.analysis.model.WebSocky; |
| 17 | +import io.sentrius.sso.core.dto.ztat.AgentExecution; |
| 18 | +import io.sentrius.sso.core.exceptions.ZtatException; |
| 19 | +import io.sentrius.sso.core.model.verbs.Verb; |
| 20 | +import io.sentrius.sso.core.services.agents.AgentClientService; |
| 21 | +import io.sentrius.sso.core.services.agents.LLMService; |
| 22 | +import io.sentrius.sso.core.services.agents.ZeroTrustClientService; |
| 23 | +import io.sentrius.sso.core.utils.JsonUtil; |
| 24 | +import io.sentrius.sso.genai.Message; |
| 25 | +import io.sentrius.sso.genai.Response; |
| 26 | +import io.sentrius.sso.genai.model.LLMRequest; |
| 27 | +import lombok.NonNull; |
| 28 | +import lombok.RequiredArgsConstructor; |
| 29 | +import lombok.extern.slf4j.Slf4j; |
| 30 | +import org.springframework.beans.factory.annotation.Value; |
| 31 | +import org.springframework.stereotype.Service; |
| 32 | + |
| 33 | +@Service |
| 34 | +@Slf4j |
| 35 | +@RequiredArgsConstructor |
| 36 | +public class ChatVerbs { |
| 37 | + |
| 38 | + @Value("${agent.ai.config}") |
| 39 | + private String agentConfigFile; |
| 40 | + |
| 41 | + final ZeroTrustClientService zeroTrustClientService; |
| 42 | + final LLMService llmService; |
| 43 | + final VerbRegistry verbRegistry; |
| 44 | + final AgentClientService agentClientService; |
| 45 | + |
| 46 | + /** |
| 47 | + * Prompts the agent for workload based on the provided arguments. |
| 48 | + * |
| 49 | + * @return An `ArrayNode` containing the plan generated by the agent. |
| 50 | + * @throws io.sentrius.sso.core.exceptions.ZtatException If there is an error during the operation. |
| 51 | + * @throws java.io.IOException If there is an error reading the configuration file. |
| 52 | + */ |
| 53 | + @Verb(name = "interpret_user_request", returnType = ArrayNode.class, description = "Queries the LLM using the " + |
| 54 | + "user input.", |
| 55 | + isAiCallable = false, requiresTokenManagement = true) |
| 56 | + public TerminalResponse interpretUserData( |
| 57 | + AgentExecution execution, @NonNull WebSocky socketConnection, |
| 58 | + @NonNull Message userMessage) throws ZtatException, |
| 59 | + IOException { |
| 60 | + |
| 61 | + var lastMessage = socketConnection.getMessages().stream().reduce((prev, next) -> next).orElse(null); |
| 62 | + if (socketConnection.getMessages().isEmpty()) { |
| 63 | + |
| 64 | + InputStream terminalHelperStream = getClass().getClassLoader().getResourceAsStream("terminal-helper.json"); |
| 65 | + if (terminalHelperStream == null) { |
| 66 | + throw new RuntimeException("assessor-config.yaml not found on classpath"); |
| 67 | + |
| 68 | + } |
| 69 | + String terminalResponse = new String(terminalHelperStream.readAllBytes()); |
| 70 | + InputStream is = getClass().getClassLoader().getResourceAsStream(agentConfigFile); |
| 71 | + if (is == null) { |
| 72 | + throw new RuntimeException(agentConfigFile + " not found on classpath"); |
| 73 | + } |
| 74 | + AgentConfig config = new ObjectMapper(new YAMLFactory()).readValue(is, AgentConfig.class); |
| 75 | + |
| 76 | + log.info("Agent config loaded: {}", config); |
| 77 | + PromptBuilder promptBuilder = new PromptBuilder(verbRegistry, config); |
| 78 | + var prompt = promptBuilder.buildPrompt(); |
| 79 | + List<Message> messages = new ArrayList<>(); |
| 80 | + |
| 81 | + messages.add(Message.builder().role("system").content(prompt).build()); |
| 82 | + messages.add(Message.builder().role("system").content("Please ensure your nextOperation abides by the " + |
| 83 | + "following json format and leave it empty if user's request doesn't require explicit use of system " + |
| 84 | + "operations" + |
| 85 | + ". Please summarize prior terminal " + |
| 86 | + "sessions, using " + |
| 87 | + "terminal output if needed " + |
| 88 | + "for clarity of the next LLM request and for the user: " + terminalResponse).build()); |
| 89 | + messages.add(Message.builder().role("user").content(userMessage.getContent()).build()); |
| 90 | + LLMRequest chatRequest = LLMRequest.builder().model("gpt-4o").messages(messages).build(); |
| 91 | + var resp = llmService.askQuestion(execution, chatRequest); |
| 92 | + execution.addMessages( messages ); |
| 93 | + Response response = JsonUtil.MAPPER.readValue(resp, Response.class); |
| 94 | + log.info("Response is {}", resp); |
| 95 | + for (Response.Choice choice : response.getChoices()) { |
| 96 | + var content = choice.getMessage().getContent(); |
| 97 | + if (content.startsWith("```json")) { |
| 98 | + content = content.substring(7, content.length() - 3); |
| 99 | + } else if (content.startsWith("```")) { |
| 100 | + content = content.substring(3, content.length() - 3); |
| 101 | + } |
| 102 | + log.info("content is {}", content); |
| 103 | + if (null != content && !content.isEmpty()) { |
| 104 | + var newResponse = JsonUtil.MAPPER.enable(JsonParser.Feature.ALLOW_COMMENTS).readValue(content, |
| 105 | + TerminalResponse.class); |
| 106 | + return newResponse; |
| 107 | + } |
| 108 | + } |
| 109 | + } else { |
| 110 | + InputStream terminalHelperStream = getClass().getClassLoader().getResourceAsStream("terminal-helper.json"); |
| 111 | + if (terminalHelperStream == null) { |
| 112 | + throw new RuntimeException("assessor-config.yaml not found on classpath"); |
| 113 | + |
| 114 | + } |
| 115 | + String terminalResponse = new String(terminalHelperStream.readAllBytes()); |
| 116 | + InputStream is = getClass().getClassLoader().getResourceAsStream(agentConfigFile); |
| 117 | + if (is == null) { |
| 118 | + throw new RuntimeException(agentConfigFile + " not found on classpath"); |
| 119 | + } |
| 120 | + AgentConfig config = new ObjectMapper(new YAMLFactory()).readValue(is, AgentConfig.class); |
| 121 | + |
| 122 | + log.info("Agent config loaded: {}", config); |
| 123 | + PromptBuilder promptBuilder = new PromptBuilder(verbRegistry, config); |
| 124 | + var prompt = promptBuilder.buildPrompt(); |
| 125 | + List<Message> messages = new ArrayList<>(); |
| 126 | + |
| 127 | + messages.add(Message.builder().role("system").content(prompt).build()); |
| 128 | + messages.add(Message.builder().role("system").content("Please ensure your nextOperation abide by the " + |
| 129 | + "following json format. Please summarize prior terminal sessions, using terminal output if needed " + |
| 130 | + "for clarity of the next LLM request and for the user: " + terminalResponse).build()); |
| 131 | + messages.add(Message.builder().role("assistant").content("prior response: " + lastMessage.getTerminalSummaryForLLM()).build()); |
| 132 | + messages.add(Message.builder().role("user").content(userMessage.getContent()).build()); |
| 133 | + |
| 134 | + LLMRequest chatRequest = LLMRequest.builder().model("gpt-4o").messages(messages).build(); |
| 135 | + var resp = llmService.askQuestion(execution, chatRequest); |
| 136 | + execution.addMessages( messages ); |
| 137 | + Response response = JsonUtil.MAPPER.readValue(resp, Response.class); |
| 138 | + log.info("Response is {}", resp); |
| 139 | + for (Response.Choice choice : response.getChoices()) { |
| 140 | + var content = choice.getMessage().getContent(); |
| 141 | + if (content.startsWith("```json")) { |
| 142 | + content = content.substring(7, content.length() - 3); |
| 143 | + } else if (content.startsWith("```")) { |
| 144 | + content = content.substring(3, content.length() - 3); |
| 145 | + } |
| 146 | + log.info("content is {}", content); |
| 147 | + if (null != content && !content.isEmpty()) { |
| 148 | + var newResponse = JsonUtil.MAPPER.enable(JsonParser.Feature.ALLOW_COMMENTS).readValue(content, |
| 149 | + TerminalResponse.class); |
| 150 | + return newResponse; |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + return null; |
| 156 | + } |
| 157 | + |
| 158 | + |
| 159 | + public TerminalResponse interpret_plan_response( |
| 160 | + AgentExecution execution, @NonNull WebSocky socketConnection, |
| 161 | + AgentVerb agentVerb, String planExecutionOutput) throws ZtatException, |
| 162 | + IOException { |
| 163 | + |
| 164 | + |
| 165 | + log.info("interpret_plan_response"); |
| 166 | + |
| 167 | + var lastMessage = socketConnection.getMessages().stream().reduce((prev, next) -> next).orElse(null); |
| 168 | + |
| 169 | + InputStream terminalHelperStream = getClass().getClassLoader().getResourceAsStream("terminal-helper.json"); |
| 170 | + if (terminalHelperStream == null) { |
| 171 | + throw new RuntimeException("assessor-config.yaml not found on classpath"); |
| 172 | + |
| 173 | + } |
| 174 | + String terminalResponse = new String(terminalHelperStream.readAllBytes()); |
| 175 | + InputStream is = getClass().getClassLoader().getResourceAsStream(agentConfigFile); |
| 176 | + if (is == null) { |
| 177 | + throw new RuntimeException(agentConfigFile + " not found on classpath"); |
| 178 | + } |
| 179 | + AgentConfig config = new ObjectMapper(new YAMLFactory()).readValue(is, AgentConfig.class); |
| 180 | + |
| 181 | + log.info("Agent config loaded: {}", config); |
| 182 | + PromptBuilder promptBuilder = new PromptBuilder(verbRegistry, config); |
| 183 | + var prompt = promptBuilder.buildPrompt(); |
| 184 | + List<Message> messages = new ArrayList<>(); |
| 185 | + |
| 186 | + messages.add(Message.builder().role("system").content(prompt).build()); |
| 187 | + messages.add(Message.builder().role("system").content("You have executed verbs for the previous user " + |
| 188 | + "messages. Please generate a user response that summarizes the last message.").build()); |
| 189 | + if (null != agentVerb ){ |
| 190 | + messages.add(Message.builder().role("system").content("You have executed verb: " + agentVerb.getName() + |
| 191 | + " with the following description: " + agentVerb.getDescription()).build()); |
| 192 | + } |
| 193 | + messages.add(Message.builder().role("assistant").content("prior response: " + lastMessage.getTerminalSummaryForLLM()).build()); |
| 194 | + messages.add(Message.builder().role("assistant").content(planExecutionOutput).build()); |
| 195 | + |
| 196 | + LLMRequest chatRequest = LLMRequest.builder().model("gpt-4o").messages(messages).build(); |
| 197 | + var resp = llmService.askQuestion(execution, chatRequest); |
| 198 | + execution.addMessages( messages ); |
| 199 | + Response response = JsonUtil.MAPPER.readValue(resp, Response.class); |
| 200 | + log.info("Response is {}", resp); |
| 201 | + for (Response.Choice choice : response.getChoices()) { |
| 202 | + var content = choice.getMessage().getContent(); |
| 203 | + if (content.startsWith("```json")) { |
| 204 | + content = content.substring(7, content.length() - 3); |
| 205 | + } else if (content.startsWith("```")) { |
| 206 | + content = content.substring(3, content.length() - 3); |
| 207 | + } |
| 208 | + log.info("content is {}", content); |
| 209 | + if (null != content && !content.isEmpty()) { |
| 210 | + var newResponse = JsonUtil.MAPPER.enable(JsonParser.Feature.ALLOW_COMMENTS).readValue(content, |
| 211 | + TerminalResponse.class); |
| 212 | + return newResponse; |
| 213 | + } |
| 214 | + } |
| 215 | + return null; |
| 216 | + } |
| 217 | +} |
0 commit comments