mirror of
https://github.com/amithkoujalgi/ollama4j.git
synced 2025-10-23 13:49:29 +02:00
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.
This commit is contained in:
parent
cbf65fef48
commit
bec634dd37
@ -87,9 +87,11 @@ public class Agent {
|
|||||||
t.setToolSpec(ts);
|
t.setToolSpec(ts);
|
||||||
agentTools.add(t);
|
agentTools.add(t);
|
||||||
}
|
}
|
||||||
|
Ollama ollama = new Ollama(agentSpec.getHost());
|
||||||
|
ollama.setRequestTimeoutSeconds(120);
|
||||||
return new Agent(
|
return new Agent(
|
||||||
agentSpec.getName(),
|
agentSpec.getName(),
|
||||||
new Ollama(agentSpec.getHost()),
|
ollama,
|
||||||
agentSpec.getModel(),
|
agentSpec.getModel(),
|
||||||
agentSpec.getCustomPrompt(),
|
agentSpec.getCustomPrompt(),
|
||||||
agentTools);
|
agentTools);
|
||||||
@ -114,27 +116,6 @@ public class Agent {
|
|||||||
}
|
}
|
||||||
if (chatHistory.isEmpty()) {
|
if (chatHistory.isEmpty()) {
|
||||||
chatHistory.add(
|
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(
|
new OllamaChatMessage(
|
||||||
OllamaChatMessageRole.SYSTEM,
|
OllamaChatMessageRole.SYSTEM,
|
||||||
"You are a helpful assistant named "
|
"You are a helpful assistant named "
|
||||||
@ -167,7 +148,7 @@ public class Agent {
|
|||||||
public void runInteractive() throws OllamaException {
|
public void runInteractive() throws OllamaException {
|
||||||
Scanner sc = new Scanner(System.in);
|
Scanner sc = new Scanner(System.in);
|
||||||
while (true) {
|
while (true) {
|
||||||
System.out.print("\nYou: ");
|
System.out.print("\n[You]: ");
|
||||||
String input = sc.nextLine();
|
String input = sc.nextLine();
|
||||||
if ("exit".equalsIgnoreCase(input)) break;
|
if ("exit".equalsIgnoreCase(input)) break;
|
||||||
String response = this.think(input);
|
String response = this.think(input);
|
||||||
@ -179,10 +160,10 @@ public class Agent {
|
|||||||
private String name;
|
private String name;
|
||||||
private String description;
|
private String description;
|
||||||
private List<AgentToolSpec> tools;
|
private List<AgentToolSpec> tools;
|
||||||
private Tools.Parameters parameters;
|
|
||||||
private String host;
|
private String host;
|
||||||
private String model;
|
private String model;
|
||||||
private String customPrompt;
|
private String customPrompt;
|
||||||
|
private int requestTimeoutSeconds;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@ -192,4 +173,12 @@ public class Agent {
|
|||||||
private String toolFunctionFQCN = null;
|
private String toolFunctionFQCN = null;
|
||||||
private ToolFunction toolFunctionInstance = null;
|
private ToolFunction toolFunctionInstance = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class AgentToolParameter {
|
||||||
|
private String type;
|
||||||
|
private String description;
|
||||||
|
private boolean required;
|
||||||
|
private List<String> _enum; // `enum` is a reserved keyword, so use _enum or similar
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,189 +15,6 @@ import java.util.Map;
|
|||||||
/** Example usage of the Agent API with some dummy tool functions. */
|
/** Example usage of the Agent API with some dummy tool functions. */
|
||||||
public class SampleAgent {
|
public class SampleAgent {
|
||||||
public static void main(String[] args) throws OllamaException {
|
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.Tool> 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<String, ToolFunction> functionMap = Map.of(
|
|
||||||
// "weather-tool", new WeatherToolFunction(),
|
|
||||||
// "calculator-tool", new CalculatorToolFunction()
|
|
||||||
// );
|
|
||||||
// List<Tools.Tool> 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 agent = Agent.fromYaml("agent.yaml");
|
||||||
agent.runInteractive();
|
agent.runInteractive();
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
name: Nimma Mitra
|
name: Nimma Mitra
|
||||||
host: http://192.168.29.224:11434
|
host: http://192.168.29.224:11434
|
||||||
model: mistral:7b
|
model: mistral:7b
|
||||||
|
requestTimeoutSeconds: 120
|
||||||
customPrompt: >
|
customPrompt: >
|
||||||
Only use tools and do not use your creativity.
|
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.
|
Do not ever tell me to call the tool or how to use the tool or command myself.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user