From bec634dd37f35820d1d009e7d00c2fe7a5c2feed Mon Sep 17 00:00:00 2001 From: amithkoujalgi Date: Sat, 18 Oct 2025 20:19:43 +0530 Subject: [PATCH] Refactor Agent class to include request timeout configuration and enhance interactive input display. Remove commented-out code for clarity. Update SampleAgent to utilize YAML configuration for agent instantiation. --- .../java/io/github/ollama4j/agent/Agent.java | 37 ++-- .../io/github/ollama4j/agent/SampleAgent.java | 183 ------------------ src/main/resources/agent.yaml | 1 + 3 files changed, 14 insertions(+), 207 deletions(-) diff --git a/src/main/java/io/github/ollama4j/agent/Agent.java b/src/main/java/io/github/ollama4j/agent/Agent.java index 7b3f665..f7d82be 100644 --- a/src/main/java/io/github/ollama4j/agent/Agent.java +++ b/src/main/java/io/github/ollama4j/agent/Agent.java @@ -87,9 +87,11 @@ public class Agent { t.setToolSpec(ts); agentTools.add(t); } + Ollama ollama = new Ollama(agentSpec.getHost()); + ollama.setRequestTimeoutSeconds(120); return new Agent( agentSpec.getName(), - new Ollama(agentSpec.getHost()), + ollama, agentSpec.getModel(), agentSpec.getCustomPrompt(), agentTools); @@ -114,27 +116,6 @@ public class Agent { } if (chatHistory.isEmpty()) { chatHistory.add( - // new OllamaChatMessage( - // OllamaChatMessageRole.SYSTEM, - // "You are a helpful assistant named " - // + name - // + ". You only perform tasks using tools - // available for you. You" - // + " respond very precisely and you don't - // overthink or be too" - // + " creative. Do not ever reveal the tool - // specification in" - // + " terms of code or JSON or in a way that - // a software engineer" - // + " sees it. Just be careful with your - // responses and respond" - // + " like a human. Note that you only - // execute tools provided to" - // + " you. Following are the tools that you - // have access to and" - // + " you can perform right actions using - // right tools." - // + availableToolsDescription)); new OllamaChatMessage( OllamaChatMessageRole.SYSTEM, "You are a helpful assistant named " @@ -167,7 +148,7 @@ public class Agent { public void runInteractive() throws OllamaException { Scanner sc = new Scanner(System.in); while (true) { - System.out.print("\nYou: "); + System.out.print("\n[You]: "); String input = sc.nextLine(); if ("exit".equalsIgnoreCase(input)) break; String response = this.think(input); @@ -179,10 +160,10 @@ public class Agent { private String name; private String description; private List tools; - private Tools.Parameters parameters; private String host; private String model; private String customPrompt; + private int requestTimeoutSeconds; } @Data @@ -192,4 +173,12 @@ public class Agent { private String toolFunctionFQCN = null; private ToolFunction toolFunctionInstance = null; } + + @Data + public class AgentToolParameter { + private String type; + private String description; + private boolean required; + private List _enum; // `enum` is a reserved keyword, so use _enum or similar + } } diff --git a/src/main/java/io/github/ollama4j/agent/SampleAgent.java b/src/main/java/io/github/ollama4j/agent/SampleAgent.java index 43652e4..bdce2a8 100644 --- a/src/main/java/io/github/ollama4j/agent/SampleAgent.java +++ b/src/main/java/io/github/ollama4j/agent/SampleAgent.java @@ -15,189 +15,6 @@ import java.util.Map; /** Example usage of the Agent API with some dummy tool functions. */ public class SampleAgent { public static void main(String[] args) throws OllamaException { - // Ollama ollama = new Ollama("http://192.168.29.224:11434"); - // ollama.setRequestTimeoutSeconds(120); - // String model = "mistral:7b"; - // ollama.pullModel(model); - // List tools = new ArrayList<>(); - // // Weather tool - // tools.add( - // Tools.Tool.builder() - // .toolSpec( - // Tools.ToolSpec.builder() - // .name("weather-tool") - // .description( - // "Gets the current weather for a given - // location and" - // + " day.") - // .parameters( - // Tools.Parameters.of( - // Map.of( - // "location", - // Tools.Property.builder() - // .type("string") - // .description( - // "The - // location for" - // + " - // which to" - // + " - // get the" - // + " - // weather.") - // .required(true) - // .build(), - // "day", - // Tools.Property.builder() - // .type("string") - // .description( - // "The day - // of the" - // + " - // week for" - // + " - // which to" - // + " - // get the" - // + " - // weather.") - // .required(true) - // .build()))) - // .build()) - // .toolFunction(new WeatherToolFunction()) - // .build()); - // - // // Calculator tool - // tools.add( - // Tools.Tool.builder() - // .toolSpec( - // Tools.ToolSpec.builder() - // .name("calculator-tool") - // .description( - // "Performs a simple arithmetic operation - // between two" - // + " numbers.") - // .parameters( - // Tools.Parameters.of( - // Map.of( - // "operation", - // Tools.Property.builder() - // .type("string") - // .description( - // "The - // arithmetic" - // + " - // operation" - // + " to - // perform." - // + " - // One of:" - // + " - // add," - // + " - // subtract," - // + " - // multiply," - // + " - // divide.") - // .required(true) - // .build(), - // "a", - // Tools.Property.builder() - // .type("number") - // .description( - // "The - // first" - // + " - // operand.") - // .required(true) - // .build(), - // "b", - // Tools.Property.builder() - // .type("number") - // .description( - // "The - // second" - // + " - // operand.") - // .required(true) - // .build()))) - // .build()) - // .toolFunction(new CalculatorToolFunction()) - // .build()); - // - // // Hotel Booking tool (dummy) - // tools.add( - // Tools.Tool.builder() - // .toolSpec( - // Tools.ToolSpec.builder() - // .name("hotel-booking-tool") - // .description( - // "Books a hotel room in a specified city - // for given" - // + " dates and number of guests.") - // .parameters( - // Tools.Parameters.of( - // Map.of( - // "city", - // Tools.Property.builder() - // .type("string") - // .description( - // "The city - // where the" - // + " - // hotel will" - // + " be - // booked.") - // .required(true) - // .build(), - // "checkin_date", - // Tools.Property.builder() - // .type("string") - // .description( - // "Hotel - // check-in date" - // + " - // (e.g." - // + " - // 2025-08-10).") - // .required(true) - // .build(), - // "checkout_date", - // Tools.Property.builder() - // .type("string") - // .description( - // - // "HotelCheck-out date" - // + " - // (e.g." - // + " - // 2025-08-12).") - // .required(true) - // .build(), - // "guests", - // Tools.Property.builder() - // .type("number") - // .description( - // "Number of - // guests" - // + " - // for the" - // + " - // booking.") - // .required(true) - // .build()))) - // .build()) - // .toolFunction(new HotelBookingToolFunction()) - // .build()); - // - // Map functionMap = Map.of( - // "weather-tool", new WeatherToolFunction(), - // "calculator-tool", new CalculatorToolFunction() - // ); - // List tools = - // Tools.fromYAMLFile("/Users/amithkoujalgi/Downloads/tools.yaml", functionMap); - // Agent agent = new Agent("Nimma Mirta", ollama, model, tools); Agent agent = Agent.fromYaml("agent.yaml"); agent.runInteractive(); } diff --git a/src/main/resources/agent.yaml b/src/main/resources/agent.yaml index 380e8b2..5d3bb51 100644 --- a/src/main/resources/agent.yaml +++ b/src/main/resources/agent.yaml @@ -1,6 +1,7 @@ name: Nimma Mitra host: http://192.168.29.224:11434 model: mistral:7b +requestTimeoutSeconds: 120 customPrompt: > Only use tools and do not use your creativity. Do not ever tell me to call the tool or how to use the tool or command myself.