Refactor Javadoc comments in Agent class for consistency and clarity, consolidating multi-line comments into single-line format. Update interact method to return chat history instead of a string response, enhancing functionality and documentation.

This commit is contained in:
amithkoujalgi
2025-10-21 10:35:06 +05:30
parent ad03c784e5
commit 57adaafb42

View File

@@ -38,34 +38,22 @@ import lombok.*;
* </ul> * </ul>
*/ */
public class Agent { public class Agent {
/** /** The agent's display name */
* The agent's display name
*/
private final String name; private final String name;
/** /** List of supported tools for this agent */
* List of supported tools for this agent
*/
private final List<Tools.Tool> tools; private final List<Tools.Tool> tools;
/** /** Ollama client instance for communication with the API */
* Ollama client instance for communication with the API
*/
private final Ollama ollamaClient; private final Ollama ollamaClient;
/** /** The model name used for chat completions */
* The model name used for chat completions
*/
private final String model; private final String model;
/** /** Persists chat message history across rounds */
* Persists chat message history across rounds
*/
private final List<OllamaChatMessage> chatHistory; private final List<OllamaChatMessage> chatHistory;
/** /** Optional custom system prompt for the agent */
* Optional custom system prompt for the agent
*/
private final String customPrompt; private final String customPrompt;
/** /**
@@ -161,21 +149,17 @@ public class Agent {
} }
/** /**
* Facilitates a single round of chat for the agent: * Conducts a conversational interaction with the agent.
* *
* <ul> * @param userInput the user's question, instruction, or message for the agent.
* <li>Builds/promotes the system prompt on the first turn if necessary * @param chatTokenHandler an optional handler for receiving streaming token updates from the model as it generates a reply.
* <li>Adds the user's input to chat history * Can be {@code null} if streaming output is not needed.
* <li>Submits the chat turn to the Ollama model (with tool/function support) * @return Updated chat history, as a list of {@link OllamaChatMessage} objects representing the complete conversation so far.
* <li>Updates internal chat history in accordance with the Ollama chat result * This includes system, user, assistant, and any tool/function calls/results.
* </ul> * @throws OllamaException if an error occurs communicating with the Ollama API or running tools.
*
* @param userInput The user's message or question for the agent.
* @return The model's response as a string.
* @throws OllamaException If there is a problem with the Ollama API.
*/ */
public String interact(String userInput, OllamaChatStreamObserver chatTokenHandler) public List<OllamaChatMessage> interact(
throws OllamaException { String userInput, OllamaChatStreamObserver chatTokenHandler) throws OllamaException {
// Build a concise and readable description of available tools // Build a concise and readable description of available tools
String availableToolsDescription = String availableToolsDescription =
tools.isEmpty() tools.isEmpty()
@@ -217,11 +201,10 @@ public class Agent {
.build(); .build();
OllamaChatResult response = ollamaClient.chat(request, chatTokenHandler); OllamaChatResult response = ollamaClient.chat(request, chatTokenHandler);
// Update chat history for continuity
chatHistory.clear(); chatHistory.clear();
chatHistory.addAll(response.getChatHistory()); chatHistory.addAll(response.getChatHistory());
return response.getResponseModel().getMessage().getResponse(); return response.getChatHistory();
} }
/** /**
@@ -279,35 +262,23 @@ public class Agent {
@Getter @Getter
@EqualsAndHashCode(callSuper = false) @EqualsAndHashCode(callSuper = false)
private static class AgentToolSpec extends Tools.ToolSpec { private static class AgentToolSpec extends Tools.ToolSpec {
/** /** Fully qualified class name of the tool's {@link ToolFunction} implementation */
* Fully qualified class name of the tool's {@link ToolFunction} implementation
*/
private String toolFunctionFQCN = null; private String toolFunctionFQCN = null;
/** /** Instance of the {@link ToolFunction} to invoke */
* Instance of the {@link ToolFunction} to invoke
*/
private ToolFunction toolFunctionInstance = null; private ToolFunction toolFunctionInstance = null;
} }
/** /** Bean for describing a tool function parameter for use in agent YAML definitions. */
* Bean for describing a tool function parameter for use in agent YAML definitions.
*/
@Data @Data
public class AgentToolParameter { public class AgentToolParameter {
/** /** The parameter's type (e.g., string, number, etc.) */
* The parameter's type (e.g., string, number, etc.)
*/
private String type; private String type;
/** /** Description of the parameter */
* Description of the parameter
*/
private String description; private String description;
/** /** Whether this parameter is required */
* Whether this parameter is required
*/
private boolean required; private boolean required;
/** /**