Class OllamaHttpClient

java.lang.Object
io.agentscope.extensions.model.ollama.OllamaHttpClient

public class OllamaHttpClient extends Object
HTTP client for Ollama API.

This client handles communication with Ollama's chat and embedding APIs. It provides both synchronous and asynchronous (streaming) request capabilities with proper JSON serialization/deserialization and error handling.

Features:

  • Synchronous and streaming request support
  • NDJSON stream parsing for streaming responses
  • Automatic JSON serialization/deserialization with snake_case naming
  • Proper error handling with detailed exception information
  • Configurable base URL and HTTP transport

Default base URL is http://localhost:11434, which is the standard Ollama server address.

  • Field Details

  • Constructor Details

    • OllamaHttpClient

      public OllamaHttpClient(io.agentscope.core.model.transport.HttpTransport transport, String baseUrl)
      Create a new OllamaHttpClient.
      Parameters:
      transport - the HTTP transport to use
      baseUrl - the base URL (null for default)
  • Method Details

    • chat

      public OllamaResponse chat(OllamaRequest request)
      Make a synchronous Chat API call.
      Parameters:
      request - the Ollama request
      Returns:
      the Ollama response
    • embed

      Make a synchronous Embed API call.
      Parameters:
      request - the Embed request
      Returns:
      the Embed response
    • call

      public OllamaResponse call(String endpoint, Object request)
      Make a synchronous API call to specific endpoint.
      Parameters:
      endpoint - the API endpoint
      request - the request object
      Returns:
      the Ollama response
    • call

      public <T> T call(String endpoint, Object request, Class<T> responseType)
      Make a synchronous API call to specific endpoint with custom response type.
      Type Parameters:
      T - the response type
      Parameters:
      endpoint - the API endpoint
      request - the request object
      responseType - the response class
      Returns:
      the response object
    • stream

      public reactor.core.publisher.Flux<OllamaResponse> stream(OllamaRequest request)
      Make a streaming Chat API call.
      Parameters:
      request - the Ollama request
      Returns:
      a Flux of Ollama responses
    • getBaseUrl

      public String getBaseUrl()
      Get the base URL.
      Returns:
      the base URL
    • close

      public void close()
      Close the transport.
    • builder

      public static OllamaHttpClient.Builder builder()
      Create a new builder for OllamaHttpClient.
      Returns:
      a new Builder instance