Class OllamaClient
java.lang.Object
org.springframework.ai.ollama.client.OllamaClient
- All Implemented Interfaces:
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 Summary
ConstructorsConstructorDescriptionOllamaClient(String baseUrl, String model) Constructs an OllamaClient with the specified base URL and model.OllamaClient(String baseUrl, String model, Consumer<OllamaGenerateResult> simpleCallback) Constructs an OllamaClient with the specified base URL, model, and a callback. -
Method Summary
Modifier and TypeMethodDescriptionprotected HttpRequestbuildHttpRequest(Prompt prompt) Constructs an HTTP request for the provided prompt.protected AiResponsegetAiResponse(List<OllamaGenerateResult> results) Converts the list of OllamaGenerateResult into a structured AiResponse.getModel()protected voidprocessResponseLine(String line, List<OllamaGenerateResult> results) Processes a single line from the Ollama response.protected List<OllamaGenerateResult>readGenerateResults(InputStream inputStream) Reads and processes the results from the InputStream provided by the Ollama Service.protected HttpResponse<InputStream>sendRequest(HttpRequest request) Sends the constructed HttpRequest and retrieves the HttpResponse.voidsetSimpleCallback(Consumer<OllamaGenerateResult> simpleCallback) Sets the callback that handles individual generation results.protected voidvalidatePrompt(Prompt prompt) Validates the provided prompt.
-
Constructor Details
-
OllamaClient
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
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
-
validatePrompt
Validates the provided prompt.- Parameters:
prompt- The prompt to validate.
-
buildHttpRequest
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
Sends the constructed HttpRequest and retrieves the HttpResponse.- Parameters:
request- The HttpRequest to be sent.- Returns:
- HttpResponse containing the response data.
-
readGenerateResults
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
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
Converts the list of OllamaGenerateResult into a structured AiResponse.- Parameters:
results- List of OllamaGenerateResult.- Returns:
- Formulated AiResponse.
-
getModel
- Returns:
- Model name for the AI service.
-
getBaseUrl
- Returns:
- Base URL of the Ollama Service.
-
getSimpleCallback
- Returns:
- Callback that handles individual generation results.
-
setSimpleCallback
Sets the callback that handles individual generation results.- Parameters:
simpleCallback- The callback to be set.
-