| Copyright | (c) 2025 Tushar Adhatrao |
|---|---|
| License | MIT |
| Maintainer | Tushar Adhatrao <tusharadhatrao@gmail.com> |
| Stability | experimental |
| Safe Haskell | None |
| Language | Haskell2010 |
Data.Ollama.Chat
Description
This module provides functions and types for initiating and managing chat interactions with an Ollama model. It includes APIs for sending chat requests, constructing messages with different roles, and configuring chat operations. The module supports both streaming and non-streaming responses, as well as optional tools and structured output formats.
The primary functions are chat and chatM for sending chat requests, and helper functions like
systemMessage, userMessage, assistantMessage, and toolMessage for constructing messages.
The ChatOps type allows customization of chat parameters, and defaultChatOps provides a convenient
starting point for configuration.
Example:
>>>let ops = defaultChatOps { chatModelName = "customModel", messages = userMessage "Hello!" :| [] }>>>chat ops NothingEither OllamaError ChatResponse
Synopsis
- chat :: ChatOps -> Maybe OllamaConfig -> IO (Either OllamaError ChatResponse)
- chatM :: MonadIO m => ChatOps -> Maybe OllamaConfig -> m (Either OllamaError ChatResponse)
- data Message = Message {}
- data Role
- systemMessage :: Text -> Message
- userMessage :: Text -> Message
- assistantMessage :: Text -> Message
- toolMessage :: Text -> Message
- genMessage :: Role -> Text -> Message
- defaultChatOps :: ChatOps
- data ChatOps = ChatOps {}
- data ChatResponse = ChatResponse {}
- 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 OllamaError
- 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 InputTool = InputTool {
- toolType :: Text
- function :: FunctionDef
- data FunctionDef = FunctionDef {}
- data FunctionParameters = FunctionParameters {}
- data OutputFunction = OutputFunction {}
- newtype ToolCall = ToolCall {}
Chat APIs
chat :: ChatOps -> Maybe OllamaConfig -> IO (Either OllamaError ChatResponse) Source #
Sends a chat request to the Ollama API.
Validates the ChatOps configuration and sends a POST request to the "api/chat" endpoint.
Supports both streaming and non-streaming responses based on the stream field in ChatOps.
Returns an Either containing an OllamaError on failure or a ChatResponse on success.
Example:
>>>let ops = defaultChatOps { chatModelName = "gemma3", messages = userMessage "What's the capital of France?" :| [] }>>>chat ops NothingEither OllamaError ChatResponse
chatM :: MonadIO m => ChatOps -> Maybe OllamaConfig -> m (Either OllamaError ChatResponse) Source #
MonadIO version of chat for use in monadic contexts.
Lifts the chat function into a MonadIO context, allowing it to be used in monadic computations.
Example:
>>>import Control.Monad.IO.Class>>>let ops = defaultChatOps { chatModelName = "gemma3", messages = userMessage "Hello!" :| [] }>>>runReaderT (chatM ops Nothing) someContextEither OllamaError ChatResponse
Message Types
Represents a message within a chat, including its role and content.
Constructors
| Message | |
Fields | |
Instances
Enumerated roles that can participate in a chat.
systemMessage :: Text -> Message Source #
userMessage :: Text -> Message Source #
assistantMessage :: Text -> Message Source #
toolMessage :: Text -> Message Source #
genMessage :: Role -> Text -> Message Source #
Constructs a Message with the specified role and content.
Creates a Message with the given Role and textual content, setting optional fields
(images, tool_calls, thinking) to Nothing.
Example:
>>>genMessage User "What's the weather like?"Message {role = User, content = "What's the weather like?", images = Nothing, tool_calls = Nothing, thinking = Nothing}
Chat Configuration
defaultChatOps :: ChatOps Source #
Default configuration for initiating a chat.
Provides a default ChatOps with the "gemma3" model and a sample user message ("What is 2+2?").
Can be customized by modifying fields as needed.
Example:
>>>let ops = defaultChatOps { chatModelName = "customModel", messages = userMessage "Hello!" :| [] }>>>chat ops NothingEither OllamaError ChatResponse
Configuration for initiating a chat with an Ollama model.
Defines the parameters for a chat request, including the model name, messages, and optional settings for tools, response format, streaming, timeout, and model options.
Constructors
| ChatOps | |
Fields
| |
Response Types
data ChatResponse Source #
Constructors
| ChatResponse | |
Fields
| |
Instances
| FromJSON ChatResponse Source # | |
Defined in Data.Ollama.Common.Types | |
| Show ChatResponse Source # | |
Defined in Data.Ollama.Common.Types Methods showsPrec :: Int -> ChatResponse -> ShowS # show :: ChatResponse -> String # showList :: [ChatResponse] -> ShowS # | |
| Eq ChatResponse Source # | |
Defined in Data.Ollama.Common.Types | |
| HasDone ChatResponse Source # | |
Defined in Data.Ollama.Common.Types Methods getDone :: ChatResponse -> Bool Source # | |
Format specification for the chat output.
Since: 0.1.3.0
Constructors
| JsonFormat | |
| SchemaFormat Schema |
Configuration and Error Types
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
| Generic OllamaConfig Source # | |||||
Defined in Data.Ollama.Common.Config Associated Types
| |||||
| type Rep OllamaConfig Source # | |||||
Defined in Data.Ollama.Common.Config type Rep OllamaConfig = D1 ('MetaData "OllamaConfig" "Data.Ollama.Common.Config" "ollama-haskell-0.2.0.0-J0UKzHAyP8eLXolo5NNMUa" 'False) (C1 ('MetaCons "OllamaConfig" 'PrefixI 'True) (((S1 ('MetaSel ('Just "hostUrl") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Text) :*: S1 ('MetaSel ('Just "timeout") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Int)) :*: (S1 ('MetaSel ('Just "onModelStart") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (Maybe (IO ()))) :*: S1 ('MetaSel ('Just "onModelError") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (Maybe (IO ()))))) :*: ((S1 ('MetaSel ('Just "onModelFinish") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (Maybe (IO ()))) :*: S1 ('MetaSel ('Just "retryCount") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (Maybe Int))) :*: (S1 ('MetaSel ('Just "retryDelay") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (Maybe Int)) :*: S1 ('MetaSel ('Just "commonManager") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (Maybe Manager)))))) | |||||
defaultOllamaConfig :: OllamaConfig Source #
A default configuration pointing to localhost:11434 with 90s timeout
and no hooks or retry logic.
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 # | |||||
| 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-J0UKzHAyP8eLXolo5NNMUa" '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))))) | |||||
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 }
Tool and Function Types
Represents a tool that can be used in the conversation.
Since: 0.2.0.0
Constructors
| InputTool | |
Fields
| |
Instances
| FromJSON InputTool Source # | |||||
Defined in Data.Ollama.Common.Types | |||||
| ToJSON InputTool Source # | |||||
| Generic InputTool Source # | |||||
Defined in Data.Ollama.Common.Types Associated Types
| |||||
| Show InputTool Source # | |||||
| Eq InputTool Source # | |||||
| type Rep InputTool Source # | |||||
Defined in Data.Ollama.Common.Types type Rep InputTool = D1 ('MetaData "InputTool" "Data.Ollama.Common.Types" "ollama-haskell-0.2.0.0-J0UKzHAyP8eLXolo5NNMUa" 'False) (C1 ('MetaCons "InputTool" 'PrefixI 'True) (S1 ('MetaSel ('Just "toolType") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Text) :*: S1 ('MetaSel ('Just "function") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 FunctionDef))) | |||||
data FunctionDef Source #
Represents a function that can be called by the model.
Since: 0.2.0.0
Constructors
| FunctionDef | |
Fields
| |
Instances
| FromJSON FunctionDef Source # | |||||
Defined in Data.Ollama.Common.Types | |||||
| ToJSON FunctionDef Source # | |||||
Defined in Data.Ollama.Common.Types Methods toJSON :: FunctionDef -> Value # toEncoding :: FunctionDef -> Encoding # toJSONList :: [FunctionDef] -> Value # toEncodingList :: [FunctionDef] -> Encoding # omitField :: FunctionDef -> Bool # | |||||
| Generic FunctionDef Source # | |||||
Defined in Data.Ollama.Common.Types Associated Types
| |||||
| Show FunctionDef Source # | |||||
Defined in Data.Ollama.Common.Types Methods showsPrec :: Int -> FunctionDef -> ShowS # show :: FunctionDef -> String # showList :: [FunctionDef] -> ShowS # | |||||
| Eq FunctionDef Source # | |||||
Defined in Data.Ollama.Common.Types | |||||
| type Rep FunctionDef Source # | |||||
Defined in Data.Ollama.Common.Types type Rep FunctionDef = D1 ('MetaData "FunctionDef" "Data.Ollama.Common.Types" "ollama-haskell-0.2.0.0-J0UKzHAyP8eLXolo5NNMUa" 'False) (C1 ('MetaCons "FunctionDef" 'PrefixI 'True) ((S1 ('MetaSel ('Just "functionName") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Text) :*: S1 ('MetaSel ('Just "functionDescription") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (Maybe Text))) :*: (S1 ('MetaSel ('Just "functionParameters") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (Maybe FunctionParameters)) :*: S1 ('MetaSel ('Just "functionStrict") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (Maybe Bool))))) | |||||
data FunctionParameters Source #
Parameters definition for a function call used in structured output or tool calls.
Since: 0.2.0.0
Constructors
| FunctionParameters | |
Fields
| |
Instances
| FromJSON FunctionParameters Source # | |
Defined in Data.Ollama.Common.Types Methods parseJSON :: Value -> Parser FunctionParameters # parseJSONList :: Value -> Parser [FunctionParameters] # | |
| ToJSON FunctionParameters Source # | |
Defined in Data.Ollama.Common.Types Methods toJSON :: FunctionParameters -> Value # toEncoding :: FunctionParameters -> Encoding # toJSONList :: [FunctionParameters] -> Value # toEncodingList :: [FunctionParameters] -> Encoding # omitField :: FunctionParameters -> Bool # | |
| Show FunctionParameters Source # | |
Defined in Data.Ollama.Common.Types Methods showsPrec :: Int -> FunctionParameters -> ShowS # show :: FunctionParameters -> String # showList :: [FunctionParameters] -> ShowS # | |
| Eq FunctionParameters Source # | |
Defined in Data.Ollama.Common.Types Methods (==) :: FunctionParameters -> FunctionParameters -> Bool # (/=) :: FunctionParameters -> FunctionParameters -> Bool # | |
data OutputFunction Source #
Output representation of a function to be called, including its name and arguments.
Since: 0.2.0.0
Constructors
| OutputFunction | |
Instances
| FromJSON OutputFunction Source # | |
Defined in Data.Ollama.Common.Types Methods parseJSON :: Value -> Parser OutputFunction # parseJSONList :: Value -> Parser [OutputFunction] # | |
| ToJSON OutputFunction Source # | |
Defined in Data.Ollama.Common.Types Methods toJSON :: OutputFunction -> Value # toEncoding :: OutputFunction -> Encoding # toJSONList :: [OutputFunction] -> Value # toEncodingList :: [OutputFunction] -> Encoding # omitField :: OutputFunction -> Bool # | |
| Show OutputFunction Source # | |
Defined in Data.Ollama.Common.Types Methods showsPrec :: Int -> OutputFunction -> ShowS # show :: OutputFunction -> String # showList :: [OutputFunction] -> ShowS # | |
| Eq OutputFunction Source # | |
Defined in Data.Ollama.Common.Types Methods (==) :: OutputFunction -> OutputFunction -> Bool # (/=) :: OutputFunction -> OutputFunction -> Bool # | |
A single tool call returned from the model, containing the function to be invoked.
Since: 0.2.0.0
Constructors
| ToolCall | |
Fields
| |