Class AbstractHttpLlmClient

java.lang.Object
com.agentforge4j.llm.AbstractHttpLlmClient
All Implemented Interfaces:
LlmClient

public abstract class AbstractHttpLlmClient extends Object implements LlmClient
Base LlmClient for HTTP JSON APIs: shared validation, transport, logging, and error wrapping.

Subclasses implement:

execute(LlmExecutionRequest) orchestrates build, send, status check, and extraction.

  • Constructor Details

    • AbstractHttpLlmClient

      public AbstractHttpLlmClient(LlmClientConfiguration config)
      Constructs an HTTP-backed client from configuration (provider id, default model, connect timeout).
      Parameters:
      config - non-null provider configuration
      Throws:
      IllegalArgumentException - if provider id or default model is blank
  • Method Details

    • getRetryPolicy

      public LlmRetryPolicy getRetryPolicy()
      Description copied from interface: LlmClient
      The retry policy this client requests, or null if it has none. Callers that need a policy either way should fall back to LlmRetryPolicy.defaults() on a null return.
      Specified by:
      getRetryPolicy in interface LlmClient
      Returns:
      the requested retry policy, or null when this client has none
    • warnIfApiKeyOverPlainHttp

      protected final void warnIfApiKeyOverPlainHttp(String url, String apiKey)
      Logs a warning when an API key is configured together with a plain http:// endpoint, because the key would be sent unencrypted. Local key-less deployments (Ollama, vLLM) never trigger this. Call from provider constructors after URL and key are resolved.
      Parameters:
      url - the configured endpoint URL; no warning when null or blank
      apiKey - the configured API key; no warning when null or blank
    • buildHttpRequest

      protected abstract HttpRequest buildHttpRequest(LlmExecutionRequest request)
      Builds the outbound HTTP request for one execution.
      Parameters:
      request - validated execution parameters
      Returns:
      request ready to send
    • validateAndExtractResponse

      protected abstract LlmExecutionResponse validateAndExtractResponse(String json) throws IOException
      Parses a successful HTTP body into model output and optional provider token usage.
      Parameters:
      json - raw HTTP response body
      Returns:
      execution response with extracted text; LlmExecutionResponse.tokenUsage() is null when the provider returned no usage block
      Throws:
      IOException - if the body is malformed or indicates failure
    • execute

      public final LlmExecutionResponse execute(LlmExecutionRequest request)
      Sends one LLM request for this client's provider.

      Steps:

      1. Validates request parameters
      2. Resolves model (uses request model or falls back to default)
      3. Builds the HTTP request via buildHttpRequest(LlmExecutionRequest)
      4. Sends the request and handles the response
      5. Validates HTTP status code is 2xx
      6. Extracts and returns the response via validateAndExtractResponse(String)
      Specified by:
      execute in interface LlmClient
      Parameters:
      request - the LLM execution request
      Returns:
      execution response with extracted model output and provider token usage when reported
      Throws:
      LlmInvocationException - if the request fails due to network issues, HTTP errors, or invalid responses
      IllegalArgumentException - if the request is invalid