ollama-haskell-0.2.0.0: Haskell client for ollama.
Copyright(c) 2025 Tushar Adhatrao
LicenseMIT
MaintainerTushar Adhatrao <[email protected]>
Stabilityexperimental
Safe HaskellNone
LanguageHaskell2010

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 Nothing
Either OllamaError ChatResponse
Synopsis

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 Nothing
Either 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) someContext
Either OllamaError ChatResponse

Message Types

data Message Source #

Represents a message within a chat, including its role and content.

Constructors

Message 

Fields

Instances

Instances details
FromJSON Message Source # 
Instance details

Defined in Data.Ollama.Common.Types

ToJSON Message Source # 
Instance details

Defined in Data.Ollama.Common.Types

Generic Message Source # 
Instance details

Defined in Data.Ollama.Common.Types

Associated Types

type Rep Message 
Instance details

Defined in Data.Ollama.Common.Types

type Rep Message = D1 ('MetaData "Message" "Data.Ollama.Common.Types" "ollama-haskell-0.2.0.0-FoTQnIMi8UeBjcOZ65CLdd" 'False) (C1 ('MetaCons "Message" 'PrefixI 'True) ((S1 ('MetaSel ('Just "role") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Role) :*: S1 ('MetaSel ('Just "content") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Text)) :*: (S1 ('MetaSel ('Just "images") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 (Maybe [Text])) :*: (S1 ('MetaSel ('Just "tool_calls") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 (Maybe [ToolCall])) :*: S1 ('MetaSel ('Just "thinking") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 (Maybe Text))))))

Methods

from :: Message -> Rep Message x #

to :: Rep Message x -> Message #

Show Message Source # 
Instance details

Defined in Data.Ollama.Common.Types

Eq Message Source # 
Instance details

Defined in Data.Ollama.Common.Types

Methods

(==) :: Message -> Message -> Bool #

(/=) :: Message -> Message -> Bool #

type Rep Message Source # 
Instance details

Defined in Data.Ollama.Common.Types

type Rep Message = D1 ('MetaData "Message" "Data.Ollama.Common.Types" "ollama-haskell-0.2.0.0-FoTQnIMi8UeBjcOZ65CLdd" 'False) (C1 ('MetaCons "Message" 'PrefixI 'True) ((S1 ('MetaSel ('Just "role") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Role) :*: S1 ('MetaSel ('Just "content") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Text)) :*: (S1 ('MetaSel ('Just "images") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 (Maybe [Text])) :*: (S1 ('MetaSel ('Just "tool_calls") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 (Maybe [ToolCall])) :*: S1 ('MetaSel ('Just "thinking") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 (Maybe Text))))))

data Role Source #

Enumerated roles that can participate in a chat.

Constructors

System 
User 
Assistant 
Tool 

Instances

Instances details
FromJSON Role Source # 
Instance details

Defined in Data.Ollama.Common.Types

ToJSON Role Source # 
Instance details

Defined in Data.Ollama.Common.Types

Show Role Source # 
Instance details

Defined in Data.Ollama.Common.Types

Methods

showsPrec :: Int -> Role -> ShowS #

show :: Role -> String #

showList :: [Role] -> ShowS #

Eq Role Source # 
Instance details

Defined in Data.Ollama.Common.Types

Methods

(==) :: Role -> Role -> Bool #

(/=) :: Role -> Role -> Bool #

systemMessage :: Text -> Message Source #

Creates a Message with the System role.

Example:

>>> systemMessage "You are a helpful assistant."
Message {role = System, content = "You are a helpful assistant.", images = Nothing, tool_calls = Nothing, thinking = Nothing}

userMessage :: Text -> Message Source #

Creates a Message with the User role.

Example:

>>> userMessage "What's 2+2?"
Message {role = User, content = "What's 2+2?", images = Nothing, tool_calls = Nothing, thinking = Nothing}

assistantMessage :: Text -> Message Source #

Creates a Message with the Assistant role.

Example:

>>> assistantMessage "2+2 equals 4."
Message {role = Assistant, content = "2+2 equals 4.", images = Nothing, tool_calls = Nothing, thinking = Nothing}

toolMessage :: Text -> Message Source #

Creates a Message with the Tool role.

Example:

>>> toolMessage "Tool output: success"
Message {role = Tool, content = "Tool output: success", images = Nothing, tool_calls = Nothing, thinking = Nothing}

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 Nothing
Either OllamaError ChatResponse

data ChatOps Source #

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

  • chatModelName :: !Text

    The name of the chat model to be used (e.g., "gemma3").

  • messages :: !(NonEmpty Message)

    A non-empty list of messages forming the conversation context.

  • tools :: !(Maybe [InputTool])

    Optional tools that may be used in the chat.

  • format :: !(Maybe Format)

    Optional format for the chat response (e.g., JSON or JSON schema).

    Since: 0.1.3.0

  • stream :: !(Maybe (ChatResponse -> IO (), IO ()))

    Optional streaming functions: the first handles each response chunk, the second flushes the stream.

  • keepAlive :: !(Maybe Int)

    Optional override for the response timeout in minutes (default: 15 minutes).

  • options :: !(Maybe ModelOptions)

    Optional model parameters (e.g., temperature) as specified in the Modelfile.

    Since: 0.1.3.0

  • think :: !(Maybe Bool)

    Optional flag to enable thinking mode.

    Since: 0.2.0.0

Instances

Instances details
ToJSON ChatOps Source # 
Instance details

Defined in Data.Ollama.Chat

Show ChatOps Source # 
Instance details

Defined in Data.Ollama.Chat

Eq ChatOps Source # 
Instance details

Defined in Data.Ollama.Chat

Methods

(==) :: ChatOps -> ChatOps -> Bool #

(/=) :: ChatOps -> ChatOps -> Bool #

Response Types

data ChatResponse Source #

Constructors

ChatResponse 

Fields

data Format Source #

Format specification for the chat output.

Since: 0.1.3.0

Instances

Instances details
ToJSON Format Source # 
Instance details

Defined in Data.Ollama.Common.Types

Show Format Source # 
Instance details

Defined in Data.Ollama.Common.Types

Eq Format Source # 
Instance details

Defined in Data.Ollama.Common.Types

Methods

(==) :: Format -> Format -> Bool #

(/=) :: Format -> Format -> Bool #

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

Instances details
Generic OllamaConfig Source # 
Instance details

Defined in Data.Ollama.Common.Config

Associated Types

type Rep OllamaConfig 
Instance details

Defined in Data.Ollama.Common.Config

type Rep OllamaConfig = D1 ('MetaData "OllamaConfig" "Data.Ollama.Common.Config" "ollama-haskell-0.2.0.0-FoTQnIMi8UeBjcOZ65CLdd" '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))))))
type Rep OllamaConfig Source # 
Instance details

Defined in Data.Ollama.Common.Config

type Rep OllamaConfig = D1 ('MetaData "OllamaConfig" "Data.Ollama.Common.Config" "ollama-haskell-0.2.0.0-FoTQnIMi8UeBjcOZ65CLdd" '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

Instances details
Exception OllamaError Source # 
Instance details

Defined in Data.Ollama.Common.Error

Generic OllamaError Source # 
Instance details

Defined in Data.Ollama.Common.Error

Associated Types

type Rep OllamaError 
Instance details

Defined in Data.Ollama.Common.Error

Show OllamaError Source # 
Instance details

Defined in Data.Ollama.Common.Error

Eq OllamaError Source # 
Instance details

Defined in Data.Ollama.Common.Error

type Rep OllamaError Source # 
Instance details

Defined in Data.Ollama.Common.Error

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

data InputTool Source #

Represents a tool that can be used in the conversation.

Since: 0.2.0.0

Constructors

InputTool 

Fields

Instances

Instances details
FromJSON InputTool Source # 
Instance details

Defined in Data.Ollama.Common.Types

ToJSON InputTool Source # 
Instance details

Defined in Data.Ollama.Common.Types

Generic InputTool Source # 
Instance details

Defined in Data.Ollama.Common.Types

Associated Types

type Rep InputTool 
Instance details

Defined in Data.Ollama.Common.Types

type Rep InputTool = D1 ('MetaData "InputTool" "Data.Ollama.Common.Types" "ollama-haskell-0.2.0.0-FoTQnIMi8UeBjcOZ65CLdd" 'False) (C1 ('MetaCons "InputTool" 'PrefixI 'True) (S1 ('MetaSel ('Just "toolType") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Text) :*: S1 ('MetaSel ('Just "function") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 FunctionDef)))
Show InputTool Source # 
Instance details

Defined in Data.Ollama.Common.Types

Eq InputTool Source # 
Instance details

Defined in Data.Ollama.Common.Types

type Rep InputTool Source # 
Instance details

Defined in Data.Ollama.Common.Types

type Rep InputTool = D1 ('MetaData "InputTool" "Data.Ollama.Common.Types" "ollama-haskell-0.2.0.0-FoTQnIMi8UeBjcOZ65CLdd" '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

Instances details
FromJSON FunctionDef Source # 
Instance details

Defined in Data.Ollama.Common.Types

ToJSON FunctionDef Source # 
Instance details

Defined in Data.Ollama.Common.Types

Generic FunctionDef Source # 
Instance details

Defined in Data.Ollama.Common.Types

Associated Types

type Rep FunctionDef 
Instance details

Defined in Data.Ollama.Common.Types

type Rep FunctionDef = D1 ('MetaData "FunctionDef" "Data.Ollama.Common.Types" "ollama-haskell-0.2.0.0-FoTQnIMi8UeBjcOZ65CLdd" '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)))))
Show FunctionDef Source # 
Instance details

Defined in Data.Ollama.Common.Types

Eq FunctionDef Source # 
Instance details

Defined in Data.Ollama.Common.Types

type Rep FunctionDef Source # 
Instance details

Defined in Data.Ollama.Common.Types

type Rep FunctionDef = D1 ('MetaData "FunctionDef" "Data.Ollama.Common.Types" "ollama-haskell-0.2.0.0-FoTQnIMi8UeBjcOZ65CLdd" '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

data OutputFunction Source #

Output representation of a function to be called, including its name and arguments.

Since: 0.2.0.0

Constructors

OutputFunction 

Fields

newtype ToolCall Source #

A single tool call returned from the model, containing the function to be invoked.

Since: 0.2.0.0

Constructors

ToolCall 

Fields