92 public static Agent load(String yamlPathOrResource) {
94 ObjectMapper mapper =
new ObjectMapper(
new YAMLFactory());
97 Agent.class.getClassLoader().getResourceAsStream(yamlPathOrResource);
99 java.nio.file.Path filePath = java.nio.file.Paths.get(yamlPathOrResource);
100 if (java.nio.file.Files.exists(filePath)) {
101 input = java.nio.file.Files.newInputStream(filePath);
103 throw new RuntimeException(
104 yamlPathOrResource +
" not found in classpath or file system");
107 AgentSpec agentSpec = mapper.readValue(input, AgentSpec.class);
108 List<AgentToolSpec> tools = agentSpec.getTools();
109 for (AgentToolSpec tool : tools) {
110 String fqcn = tool.getToolFunctionFQCN();
111 if (fqcn !=
null && !fqcn.isEmpty()) {
113 Class<?> clazz = Class.forName(fqcn);
114 Object instance = clazz.getDeclaredConstructor().newInstance();
118 throw new RuntimeException(
119 "Class does not implement ToolFunction: " + fqcn);
121 }
catch (Exception e) {
122 throw new RuntimeException(
123 "Failed to instantiate tool function: " + fqcn, e);
127 List<
Tools.
Tool> agentTools =
new ArrayList<>();
128 for (AgentToolSpec a : tools) {
130 t.setToolFunction(a.getToolFunctionInstance());
132 ts.setName(a.getName());
133 ts.setDescription(a.getDescription());
134 ts.setParameters(a.getParameters());
139 ollama.setRequestTimeoutSeconds(120);
144 agentSpec.getModel(),
145 agentSpec.getCustomPrompt(),
147 }
catch (Exception e) {
148 throw new RuntimeException(
"Failed to load agent from YAML", e);
165 String availableToolsDescription =
173 t.getToolSpec().getName(),
174 t.getToolSpec().getDescription() !=
null
175 ? t.getToolSpec().getDescription()
177 .reduce((a, b) -> a +
"\n" + b)
178 .map(desc ->
"\nYou have access to the following tools:\n" + desc)
182 if (chatHistory.isEmpty()) {
183 String systemPrompt =
185 "You are a helpful AI assistant named %s. Your actions are limited to"
186 +
" using the available tools. %s%s",
188 (customPrompt !=
null ? customPrompt :
""),
189 availableToolsDescription);
206 chatHistory.addAll(response.getChatHistory());
208 return response.getChatHistory();