Copyright | (c) 2025 Tushar Adhatrao |
---|---|
License | MIT |
Maintainer | Tushar Adhatrao <[email protected]> |
Stability | experimental |
Safe Haskell | None |
Language | Haskell2010 |
Data.Ollama.Common.Utils
Description
This module provides helper functions for common tasks in the Ollama client, such as encoding images to Base64, sending HTTP requests to the Ollama API, handling streaming and non-streaming responses, and managing retries for failed requests. It also includes a default model options configuration and a function to retrieve the Ollama server version.
The functions in this module are used internally by other modules like Chat
and Generate
but can also be used directly for custom API interactions.
Synopsis
- encodeImage :: FilePath -> IO (Maybe Text)
- withOllamaRequest :: ToJSON payload => Text -> ByteString -> Maybe payload -> Maybe OllamaConfig -> (Response BodyReader -> IO (Either OllamaError response)) -> IO (Either OllamaError response)
- commonNonStreamingHandler :: FromJSON a => Response BodyReader -> IO (Either OllamaError a)
- commonStreamHandler :: (HasDone a, FromJSON a) => (a -> IO ()) -> IO () -> Response BodyReader -> IO (Either OllamaError a)
- nonJsonHandler :: Response BodyReader -> IO (Either OllamaError ByteString)
- defaultModelOptions :: ModelOptions
- withRetry :: Int -> Int -> IO (Either OllamaError a) -> IO (Either OllamaError a)
- getVersion :: IO (Either OllamaError Version)
Image Encoding
encodeImage :: FilePath -> IO (Maybe Text) Source #
Encodes an image file to Base64 format.
Takes a file path to an image (jpg, jpeg, or png) and returns its data encoded as a Base64 Text
.
Returns Nothing
if the file extension is unsupported or the file cannot be read.
This is useful for including images in API requests that expect Base64-encoded data, such as GenerateOps
images field.
HTTP Request Handling
Arguments
:: ToJSON payload | |
=> Text | API endpoint |
-> ByteString | |
-> Maybe payload | Optional request payload (must implement |
-> Maybe OllamaConfig | Optional |
-> (Response BodyReader -> IO (Either OllamaError response)) | Response handler to process the HTTP response |
-> IO (Either OllamaError response) |
Sends an HTTP request to the Ollama API.
A unified function for making API requests to the Ollama server. Supports both GET and POST methods, customizable payloads, and optional configuration. The response is processed by the provided handler.
commonNonStreamingHandler :: FromJSON a => Response BodyReader -> IO (Either OllamaError a) Source #
Handles non-streaming API responses.
Processes an HTTP response, accumulating all chunks until EOF and decoding the result as JSON.
Returns an Either
with an OllamaError
on failure or the decoded response on success.
Suitable for APIs that return a single JSON response.
Arguments
:: (HasDone a, FromJSON a) | |
=> (a -> IO ()) | Function to handle each decoded chunk |
-> IO () | Function to flush after each chunk |
-> Response BodyReader | |
-> IO (Either OllamaError a) |
Handles streaming API responses.
Processes a streaming HTTP response, decoding each chunk as JSON and passing it to the provided
sendChunk
function. The flush
function is called after each chunk. Stops when the response
indicates completion (via HasDone
). Returns the final decoded response or an error.
nonJsonHandler :: Response BodyReader -> IO (Either OllamaError ByteString) Source #
Handles non-JSON API responses.
Processes an HTTP response, accumulating all chunks into a ByteString
. Returns the accumulated
data on success (HTTP status 2xx) or an ApiError
on failure.
Model Options
defaultModelOptions :: ModelOptions Source #
Default model options for API requests.
Provides a default ModelOptions
configuration with all fields set to Nothing
,
suitable as a starting point for customizing model parameters like temperature or token limits.
Example:
>>>
let opts = defaultModelOptions { temperature = Just 0.7 }
Retry Logic
Arguments
:: Int | Number of retries |
-> Int | Delay between retries in seconds |
-> IO (Either OllamaError a) | Action to execute, returning |
-> IO (Either OllamaError a) |
Executes an action with retry logic for recoverable errors.
Retries the given action up to the specified number of times with a delay (in seconds) between attempts. Only retries on recoverable errors such as HTTP errors, timeouts, JSON schema errors, or decoding errors.
Version Retrieval
getVersion :: IO (Either OllamaError Version) Source #
Retrieves the Ollama server version.
Sends a GET request to the "api/version" endpoint and returns the server version
as a Version
wrapped in an Either
OllamaError
.
Example:
>>>
getVersion
-- -- @since 0.2.0.0