Copyright | (c) 2025 Tushar Adhatrao |
---|---|
License | MIT |
Maintainer | Tushar Adhatrao <[email protected]> |
Stability | experimental |
Safe Haskell | None |
Language | Haskell2010 |
Data.Ollama.Generate
Description
This module provides functions and types for generating text using an Ollama model. It includes APIs
for sending generation requests, both in IO (generate
) and monadic (generateM
) contexts, with
support for streaming and non-streaming responses. The GenerateOps
type configures the generation
request, allowing customization of the model, prompt, images, format, and other parameters. The
defaultGenerateOps
provides a convenient starting point for configuration.
The module supports advanced features like Base64-encoded images, custom templates, and model-specific options (e.g., temperature). It also includes validation to ensure required fields are non-empty.
Example:
>>>
let ops = defaultGenerateOps { modelName = "gemma3", prompt = "Write a poem." }
>>>
generate ops Nothing
Right (GenerateResponse ...)
Synopsis
- generate :: GenerateOps -> Maybe OllamaConfig -> IO (Either OllamaError GenerateResponse)
- generateM :: MonadIO m => GenerateOps -> Maybe OllamaConfig -> m (Either OllamaError GenerateResponse)
- defaultGenerateOps :: GenerateOps
- data GenerateOps = GenerateOps {
- modelName :: !Text
- prompt :: !Text
- suffix :: Maybe Text
- images :: !(Maybe [Text])
- format :: !(Maybe Format)
- system :: !(Maybe Text)
- template :: !(Maybe Text)
- stream :: !(Maybe (GenerateResponse -> IO (), IO ()))
- raw :: !(Maybe Bool)
- keepAlive :: !(Maybe Int)
- options :: !(Maybe ModelOptions)
- think :: !(Maybe Bool)
- validateGenerateOps :: GenerateOps -> Either OllamaError GenerateOps
- data GenerateResponse = GenerateResponse {
- model :: !Text
- createdAt :: !UTCTime
- genResponse :: !Text
- done :: !Bool
- totalDuration :: !(Maybe Int64)
- loadDuration :: !(Maybe Int64)
- promptEvalCount :: !(Maybe Int64)
- promptEvalDuration :: !(Maybe Int64)
- evalCount :: !(Maybe Int64)
- evalDuration :: !(Maybe Int64)
- thinking :: !(Maybe Text)
- data Format
- data OllamaConfig = OllamaConfig {
- hostUrl :: Text
- timeout :: Int
- onModelStart :: Maybe (IO ())
- onModelError :: Maybe (IO ())
- onModelFinish :: Maybe (IO ())
- retryCount :: Maybe Int
- retryDelay :: Maybe Int
- commonManager :: Maybe Manager
- defaultOllamaConfig :: OllamaConfig
- data ModelOptions = ModelOptions {
- numKeep :: Maybe Int
- seed :: Maybe Int
- numPredict :: Maybe Int
- topK :: Maybe Int
- topP :: Maybe Double
- minP :: Maybe Double
- typicalP :: Maybe Double
- repeatLastN :: Maybe Int
- temperature :: Maybe Double
- repeatPenalty :: Maybe Double
- presencePenalty :: Maybe Double
- frequencyPenalty :: Maybe Double
- penalizeNewline :: Maybe Bool
- stop :: Maybe [Text]
- numa :: Maybe Bool
- numCtx :: Maybe Int
- numBatch :: Maybe Int
- numGpu :: Maybe Int
- mainGpu :: Maybe Int
- useMmap :: Maybe Bool
- numThread :: Maybe Int
- defaultModelOptions :: ModelOptions
- data OllamaError
Generate Texts
generate :: GenerateOps -> Maybe OllamaConfig -> IO (Either OllamaError GenerateResponse) Source #
Generates text using the specified model and configuration.
Validates the GenerateOps
configuration and sends a POST request to the "api/generate" endpoint.
Supports both streaming and non-streaming responses based on the stream
field in GenerateOps
.
Returns Right
with a GenerateResponse
on success or Left
with an OllamaError
on failure.
Example:
>>>
let ops = defaultGenerateOps { modelName = "gemma3", prompt = "Write a short poem." }
>>>
generate ops Nothing
Right (GenerateResponse ...)
generateM :: MonadIO m => GenerateOps -> Maybe OllamaConfig -> m (Either OllamaError GenerateResponse) Source #
MonadIO version of generate
for use in monadic contexts.
Lifts the generate
function into a MonadIO
context, allowing it to be used in monadic computations.
Example:
>>>
import Control.Monad.IO.Class
>>>
let ops = defaultGenerateOps { modelName = "gemma3", prompt = "Hello!" }
>>>
runReaderT (generateM ops Nothing) someContext
Right (GenerateResponse ...)
Configuration
defaultGenerateOps :: GenerateOps Source #
Default configuration for text generation.
Provides a default GenerateOps
with the "gemma3" model and an empty prompt. Other fields are set
to Nothing
or default values. Can be customized by modifying fields as needed.
Example:
>>>
let ops = defaultGenerateOps { modelName = "customModel", prompt = "Hello!" }
>>>
generate ops Nothing
data GenerateOps Source #
Configuration for a text generation request.
Constructors
GenerateOps | |
Fields
|
Instances
ToJSON GenerateOps Source # | |
Defined in Data.Ollama.Generate Methods toJSON :: GenerateOps -> Value # toEncoding :: GenerateOps -> Encoding # toJSONList :: [GenerateOps] -> Value # toEncodingList :: [GenerateOps] -> Encoding # omitField :: GenerateOps -> Bool # | |
Show GenerateOps Source # | |
Defined in Data.Ollama.Generate Methods showsPrec :: Int -> GenerateOps -> ShowS # show :: GenerateOps -> String # showList :: [GenerateOps] -> ShowS # | |
Eq GenerateOps Source # | |
Defined in Data.Ollama.Generate |
validateGenerateOps :: GenerateOps -> Either OllamaError GenerateOps Source #
Validates GenerateOps
to ensure required fields are non-empty.
Checks that the modelName
and prompt
fields are not empty. Returns Right
with the validated
GenerateOps
or Left
with an OllamaError
if validation fails.
Example:
>>>
validateGenerateOps defaultGenerateOps
Left (InvalidRequest "Prompt cannot be empty") -- -- @since 0.2.0.0
Response and Configuration Types
data GenerateResponse Source #
Result type for generate
function containing the model's response and meta-information.
Constructors
GenerateResponse | |
Fields
|
Instances
FromJSON GenerateResponse Source # | |
Defined in Data.Ollama.Common.Types Methods parseJSON :: Value -> Parser GenerateResponse # parseJSONList :: Value -> Parser [GenerateResponse] # | |
Show GenerateResponse Source # | |
Defined in Data.Ollama.Common.Types Methods showsPrec :: Int -> GenerateResponse -> ShowS # show :: GenerateResponse -> String # showList :: [GenerateResponse] -> ShowS # | |
Eq GenerateResponse Source # | |
Defined in Data.Ollama.Common.Types Methods (==) :: GenerateResponse -> GenerateResponse -> Bool # (/=) :: GenerateResponse -> GenerateResponse -> Bool # | |
HasDone GenerateResponse Source # | |
Defined in Data.Ollama.Common.Types Methods getDone :: GenerateResponse -> Bool Source # |
Format specification for the chat output.
Since: 0.1.3.0
Constructors
JsonFormat | |
SchemaFormat Schema |
data OllamaConfig Source #
Configuration for the Ollama client. Used across all requests to customize behavior such as timeouts, retries, custom HTTP manager, and lifecycle hooks. -- -- @since 0.2.0.0
Constructors
OllamaConfig | |
Fields
|
Instances
defaultOllamaConfig :: OllamaConfig Source #
A default configuration pointing to localhost:11434
with 90s timeout
and no hooks or retry logic.
data ModelOptions Source #
Optional model tuning parameters that influence generation behavior.
Since: 0.2.0.0
Constructors
ModelOptions | |
Fields
|
Instances
ToJSON ModelOptions Source # | Custom ToJSON instance for Options |
Defined in Data.Ollama.Common.Types Methods toJSON :: ModelOptions -> Value # toEncoding :: ModelOptions -> Encoding # toJSONList :: [ModelOptions] -> Value # toEncodingList :: [ModelOptions] -> Encoding # omitField :: ModelOptions -> Bool # | |
Show ModelOptions Source # | |
Defined in Data.Ollama.Common.Types Methods showsPrec :: Int -> ModelOptions -> ShowS # show :: ModelOptions -> String # showList :: [ModelOptions] -> ShowS # | |
Eq ModelOptions Source # | |
Defined in Data.Ollama.Common.Types |
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 }
Error Types
data OllamaError Source #
Represents all possible errors that may occur when using the Ollama client.
Since: 0.2.0.0
Constructors
HttpError HttpException | Low-level HTTP exception (connection failure, etc.) |
DecodeError DecodingErrorMessage DecodingFailedValue | Failure to decode a JSON response, includes message and raw value |
ApiError Text | Error returned from Ollama's HTTP API |
FileError IOException | Error during file operations (e.g., loading an image) |
JsonSchemaError String | Mismatch in expected JSON schema or structure |
TimeoutError String | Request timed out |
InvalidRequest String | Request is malformed or violates input constraints |
Instances
Exception OllamaError Source # | |||||
Defined in Data.Ollama.Common.Error Methods toException :: OllamaError -> SomeException # fromException :: SomeException -> Maybe OllamaError # displayException :: OllamaError -> String # backtraceDesired :: OllamaError -> Bool # | |||||
Generic OllamaError Source # | |||||
Defined in Data.Ollama.Common.Error Associated Types
| |||||
Show OllamaError Source # | |||||
Defined in Data.Ollama.Common.Error Methods showsPrec :: Int -> OllamaError -> ShowS # show :: OllamaError -> String # showList :: [OllamaError] -> ShowS # | |||||
Eq OllamaError Source # | |||||
Defined in Data.Ollama.Common.Error | |||||
type Rep OllamaError Source # | |||||
Defined in Data.Ollama.Common.Error type Rep OllamaError = D1 ('MetaData "OllamaError" "Data.Ollama.Common.Error" "ollama-haskell-0.2.0.0-FoTQnIMi8UeBjcOZ65CLdd" 'False) ((C1 ('MetaCons "HttpError" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 HttpException)) :+: (C1 ('MetaCons "DecodeError" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 DecodingErrorMessage) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 DecodingFailedValue)) :+: C1 ('MetaCons "ApiError" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Text)))) :+: ((C1 ('MetaCons "FileError" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 IOException)) :+: C1 ('MetaCons "JsonSchemaError" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 String))) :+: (C1 ('MetaCons "TimeoutError" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 String)) :+: C1 ('MetaCons "InvalidRequest" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 String))))) |