Class AbstractAIProvider

java.lang.Object
org.machanism.machai.ai.provider.AbstractAIProvider
All Implemented Interfaces:
Genai
Direct Known Subclasses:
ClaudeProvider, OpenAIProvider

public abstract class AbstractAIProvider extends Object implements Genai
  • Field Details

    • logger

      static org.slf4j.Logger logger
      Logger instance for this provider.
    • MCP_PROP_NAME_PREFIX

      protected static final String MCP_PROP_NAME_PREFIX
      See Also:
    • MAX_OUTPUT_TOKENS

      public static final long MAX_OUTPUT_TOKENS
      Default maximum number of tokens the model may generate.
      See Also:
    • LOG_SECTION_SEPARATOR

      public static final String LOG_SECTION_SEPARATOR
      See Also:
    • chatModel

      protected String chatModel
      Active model identifier used in Genai.perform().
    • workingDir

      protected File workingDir
      Working directory passed to tool handlers as contextual information.
    • timeoutSec

      protected Long timeoutSec
      Request timeout in seconds; 0 means SDK defaults are used.
    • lastUsage

      protected Usage lastUsage
      Latest usage metrics captured from the most recent Genai.perform() call.
    • instructions

      protected String instructions
      Optional instructions applied to the request.
    • maxOutputTokens

      protected Long maxOutputTokens
      Maximum number of output tokens for responses.
    • maxToolCalls

      protected Long maxToolCalls
      Maximum number of tool calls permitted per response.
    • config

      protected Configurator config
      Configuration source used to initialize clients and provider features.
    • embeddingModel

      protected String embeddingModel
      Embedding model identifier used by Genai.embedding(String, long).
  • Constructor Details

    • AbstractAIProvider

      public AbstractAIProvider()
  • Method Details

    • init

      public void init(Configurator config)
      Initializes the provider from the given configuration.
      Specified by:
      init in interface Genai
      Parameters:
      config - provider configuration source
    • addMcpServer

      protected abstract void addMcpServer()
    • addWebSearch

      protected abstract void addWebSearch()
    • normalize

      protected String normalize(String value)
      Normalizes a string for case-insensitive comparisons.
      Parameters:
      value - source value
      Returns:
      lower-cased value, or an empty string when the input is null
    • safelyInvokeTool

      protected Object safelyInvokeTool(String name, ToolFunction tool, com.fasterxml.jackson.databind.JsonNode params, File workingDir)
      Safely invokes a tool function and converts IOExceptions into a textual error payload suitable for the model conversation.
      Parameters:
      name - tool name
      tool - tool handler
      params - parsed tool parameters
      workingDir - working directory passed to the tool
      Returns:
      tool output or a formatted error message
    • logInputs

      protected void logInputs()
      Writes the current request inputs to inputsLog when logging is enabled.
    • logInputsSpec

      protected abstract void logInputsSpec(Writer streamWriter) throws IOException
      Throws:
      IOException
    • isRequiredParameter

      protected boolean isRequiredParameter(String parameterFlag)
      Determines whether the supplied parameter flag marks a required parameter.
      Parameters:
      parameterFlag - descriptor flag value
      Returns:
      true when the flag equals required, ignoring case
    • instructions

      public void instructions(String instructions)
      Sets system-level instructions applied to subsequent requests.
      Specified by:
      instructions in interface Genai
      Parameters:
      instructions - instruction text, or null to clear
    • inputsLog

      public void inputsLog(File inputsLog)
      Enables request input logging to the given file.
      Specified by:
      inputsLog in interface Genai
      Parameters:
      inputsLog - file for input logging, or null to disable
    • setWorkingDir

      public void setWorkingDir(File workingDir)
      Sets the working directory passed to tool handlers.
      Specified by:
      setWorkingDir in interface Genai
      Parameters:
      workingDir - working directory, or null
    • usage

      public Usage usage()
      Returns token usage metrics captured from the most recent Genai.perform() call.
      Specified by:
      usage in interface Genai
      Returns:
      usage metrics; never null
    • getTimeout

      public long getTimeout()
      Returns the configured request timeout.
      Returns:
      timeout in seconds; 0 indicates the SDK default
    • setTimeout

      public void setTimeout(long timeout, OpenAIProvider openAIProvider)
      Sets a request timeout in seconds for new clients created by this provider.
      Parameters:
      timeout - timeout in seconds; use 0 to use SDK defaults