Class OllamaClient

java.lang.Object
org.springframework.ai.ollama.client.OllamaClient
All Implemented Interfaces:
AiClient

public class OllamaClient extends Object implements AiClient
A client implementation for interacting with Ollama Service. This class acts as an interface between the application and the Ollama AI Service, handling request creation, communication, and response processing.
Author:
nullptr
  • Constructor Details

    • OllamaClient

      public OllamaClient(String baseUrl, String model)
      Constructs an OllamaClient with the specified base URL and model.
      Parameters:
      baseUrl - Base URL of the Ollama Service.
      model - Model specification for the AI service.
    • OllamaClient

      public OllamaClient(String baseUrl, String model, Consumer<OllamaGenerateResult> simpleCallback)
      Constructs an OllamaClient with the specified base URL, model, and a callback.
      Parameters:
      baseUrl - Base URL of the Ollama Service.
      model - Model specification for the AI service.
      simpleCallback - Callback to handle individual generation results.
  • Method Details

    • generate

      public AiResponse generate(Prompt prompt)
      Specified by:
      generate in interface AiClient
    • validatePrompt

      protected void validatePrompt(Prompt prompt)
      Validates the provided prompt.
      Parameters:
      prompt - The prompt to validate.
    • buildHttpRequest

      protected HttpRequest buildHttpRequest(Prompt prompt)
      Constructs an HTTP request for the provided prompt.
      Parameters:
      prompt - The prompt for which the request needs to be built.
      Returns:
      The constructed HttpRequest.
    • sendRequest

      protected HttpResponse<InputStream> sendRequest(HttpRequest request)
      Sends the constructed HttpRequest and retrieves the HttpResponse.
      Parameters:
      request - The HttpRequest to be sent.
      Returns:
      HttpResponse containing the response data.
    • readGenerateResults

      protected List<OllamaGenerateResult> readGenerateResults(InputStream inputStream)
      Reads and processes the results from the InputStream provided by the Ollama Service.
      Parameters:
      inputStream - InputStream containing the results from the Ollama Service.
      Returns:
      List of OllamaGenerateResult.
    • processResponseLine

      protected void processResponseLine(String line, List<OllamaGenerateResult> results)
      Processes a single line from the Ollama response.
      Parameters:
      line - The line to be processed.
      results - List to which parsed results will be added.
    • getAiResponse

      protected AiResponse getAiResponse(List<OllamaGenerateResult> results)
      Converts the list of OllamaGenerateResult into a structured AiResponse.
      Parameters:
      results - List of OllamaGenerateResult.
      Returns:
      Formulated AiResponse.
    • getModel

      public String getModel()
      Returns:
      Model name for the AI service.
    • getBaseUrl

      public String getBaseUrl()
      Returns:
      Base URL of the Ollama Service.
    • getSimpleCallback

      public Consumer<OllamaGenerateResult> getSimpleCallback()
      Returns:
      Callback that handles individual generation results.
    • setSimpleCallback

      public void setSimpleCallback(Consumer<OllamaGenerateResult> simpleCallback)
      Sets the callback that handles individual generation results.
      Parameters:
      simpleCallback - The callback to be set.