Ollama4j
A Java library (wrapper/binding) for Ollama server.
Loading...
Searching...
No Matches
io.github.ollama4j.agent.Agent Class Reference

Classes

class  AgentSpec
 
class  AgentToolParameter
 

Public Member Functions

 Agent (String name, Ollama ollamaClient, String model, String customPrompt, List< Tools.Tool > tools)
 
List< OllamaChatMessageinteract (String userInput, OllamaChatStreamObserver chatTokenHandler) throws OllamaException
 
void runInteractive () throws OllamaException
 

Static Public Member Functions

static Agent load (String yamlPathOrResource)
 

Detailed Description

The Agent class represents an AI assistant capable of interacting with the Ollama API server.

It supports the use of tools (interchangeable code components), persistent chat history, and interactive as well as pre-scripted chat sessions.

Usage

Definition at line 40 of file Agent.java.

Constructor & Destructor Documentation

◆ Agent()

io.github.ollama4j.agent.Agent.Agent ( String name,
Ollama ollamaClient,
String model,
String customPrompt,
List< Tools.Tool > tools )

Constructs a new Agent.

Parameters
nameThe agent's given name.
ollamaClientThe Ollama API client instance to use.
modelThe model name to use for chat completion.
customPromptA custom prompt to prepend to all conversations (may be null).
toolsList of available tools for function calling.

Definition at line 68 of file Agent.java.

Member Function Documentation

◆ interact()

List< OllamaChatMessage > io.github.ollama4j.agent.Agent.interact ( String userInput,
OllamaChatStreamObserver chatTokenHandler ) throws OllamaException

Conducts a conversational interaction with the agent.

Parameters
userInputthe user's question, instruction, or message for the agent.
chatTokenHandleran optional handler for receiving streaming token updates from the model as it generates a reply. Can be null if streaming output is not needed.
Returns
Updated chat history, as a list of OllamaChatMessage objects representing the complete conversation so far. This includes system, user, assistant, and any tool/function calls/results.
Exceptions
OllamaExceptionif an error occurs communicating with the Ollama API or running tools.

Definition at line 162 of file Agent.java.

◆ load()

static Agent io.github.ollama4j.agent.Agent.load ( String yamlPathOrResource)
static

Loads and constructs an Agent from a YAML configuration file (classpath or filesystem).

The YAML should define the agent, the model, and the desired tool functions (using their fully qualified class names for auto-discovery).

Parameters
yamlPathOrResourcePath or classpath resource name of the YAML file.
Returns
New Agent instance loaded according to the YAML definition.
Exceptions
RuntimeExceptionif the YAML cannot be read or agent cannot be constructed.

Definition at line 92 of file Agent.java.

◆ runInteractive()

void io.github.ollama4j.agent.Agent.runInteractive ( ) throws OllamaException

Launches an endless interactive console session with the agent, echoing user input and the agent's response using the provided chat model and tools.

Type exit to break the loop and terminate the session.

Exceptions
OllamaExceptionif any errors occur talking to the Ollama API.

Definition at line 219 of file Agent.java.


The documentation for this class was generated from the following file: