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.Conversation

Description

This module provides types and functions for managing conversations in the Ollama client. It defines a Conversation type to represent a chat session, a ConversationStore typeclass for storage operations, and an in-memory implementation using InMemoryStore and ConvoM. The module supports saving, loading, listing, and deleting conversations, with thread-safe operations using STM (Software Transactional Memory).

The Conversation type includes metadata such as a unique ID, messages, model name, and timestamps. The ConversationStore typeclass defines a generic interface for conversation storage, while InMemoryStore provides a concrete in-memory implementation. The ConvoM monad integrates with InMemoryStore for monadic operations.

Example:

>>> store <- initInMemoryStore
>>> let conv = Conversation "conv1" [userMessage "Hello!"] "gemma3" <$> getCurrentTime <*> getCurrentTime
>>> runInMemoryConvo store $ saveConversation conv
>>> runInMemoryConvo store $ loadConversation "conv1"
Just (Conversation ...)
Synopsis

Conversation Types

data Conversation Source #

Represents a chat session with metadata and messages.

Stores a conversation's unique identifier, list of messages, model name, creation time, and last updated time.

Constructors

Conversation 

Fields

Instances

Instances details
FromJSON Conversation Source # 
Instance details

Defined in Data.Ollama.Conversation

ToJSON Conversation Source # 
Instance details

Defined in Data.Ollama.Conversation

Generic Conversation Source # 
Instance details

Defined in Data.Ollama.Conversation

Associated Types

type Rep Conversation 
Instance details

Defined in Data.Ollama.Conversation

type Rep Conversation = D1 ('MetaData "Conversation" "Data.Ollama.Conversation" "ollama-haskell-0.2.0.0-FoTQnIMi8UeBjcOZ65CLdd" 'False) (C1 ('MetaCons "Conversation" 'PrefixI 'True) ((S1 ('MetaSel ('Just "conversationId") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Text) :*: S1 ('MetaSel ('Just "messages") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 [Message])) :*: (S1 ('MetaSel ('Just "model") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Text) :*: (S1 ('MetaSel ('Just "createdAt") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 UTCTime) :*: S1 ('MetaSel ('Just "lastUpdated") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 UTCTime)))))
Show Conversation Source # 
Instance details

Defined in Data.Ollama.Conversation

Eq Conversation Source # 
Instance details

Defined in Data.Ollama.Conversation

type Rep Conversation Source # 
Instance details

Defined in Data.Ollama.Conversation

type Rep Conversation = D1 ('MetaData "Conversation" "Data.Ollama.Conversation" "ollama-haskell-0.2.0.0-FoTQnIMi8UeBjcOZ65CLdd" 'False) (C1 ('MetaCons "Conversation" 'PrefixI 'True) ((S1 ('MetaSel ('Just "conversationId") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Text) :*: S1 ('MetaSel ('Just "messages") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 [Message])) :*: (S1 ('MetaSel ('Just "model") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Text) :*: (S1 ('MetaSel ('Just "createdAt") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 UTCTime) :*: S1 ('MetaSel ('Just "lastUpdated") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 UTCTime)))))

class Monad m => ConversationStore (m :: Type -> Type) where Source #

Typeclass defining operations for storing and managing conversations.

Provides methods for saving, loading, listing, and deleting conversations in a monadic context. -- -- @since 0.2.0.0

Methods

saveConversation :: Conversation -> m () Source #

Saves a conversation to the store.

Validates the conversation and updates its lastUpdated timestamp before saving.

loadConversation :: Text -> m (Maybe Conversation) Source #

Loads a conversation by its ID.

Returns Just the conversation if found, or Nothing if not.

listConversations :: m [Conversation] Source #

Lists all conversations in the store.

deleteConversation :: Text -> m Bool Source #

Deletes a conversation by its ID.

Returns True if the conversation was found and deleted, False otherwise.

In-Memory Store

newtype InMemoryStore Source #

In-memory conversation store using a TVar for thread-safe operations.

Stores conversations in a Map keyed by conversation IDs, wrapped in a TVar for concurrent access.

Instances

Instances details
MonadReader InMemoryStore ConvoM Source # 
Instance details

Defined in Data.Ollama.Conversation

newtype ConvoM a Source #

Monad for operations with InMemoryStore.

A wrapper around ReaderT that provides access to an InMemoryStore in a monadic context.

Constructors

ConvoM 

Instances

Instances details
MonadIO ConvoM Source # 
Instance details

Defined in Data.Ollama.Conversation

Methods

liftIO :: IO a -> ConvoM a #

Applicative ConvoM Source # 
Instance details

Defined in Data.Ollama.Conversation

Methods

pure :: a -> ConvoM a #

(<*>) :: ConvoM (a -> b) -> ConvoM a -> ConvoM b #

liftA2 :: (a -> b -> c) -> ConvoM a -> ConvoM b -> ConvoM c #

(*>) :: ConvoM a -> ConvoM b -> ConvoM b #

(<*) :: ConvoM a -> ConvoM b -> ConvoM a #

Functor ConvoM Source # 
Instance details

Defined in Data.Ollama.Conversation

Methods

fmap :: (a -> b) -> ConvoM a -> ConvoM b #

(<$) :: a -> ConvoM b -> ConvoM a #

Monad ConvoM Source # 
Instance details

Defined in Data.Ollama.Conversation

Methods

(>>=) :: ConvoM a -> (a -> ConvoM b) -> ConvoM b #

(>>) :: ConvoM a -> ConvoM b -> ConvoM b #

return :: a -> ConvoM a #

ConversationStore ConvoM Source # 
Instance details

Defined in Data.Ollama.Conversation

MonadReader InMemoryStore ConvoM Source # 
Instance details

Defined in Data.Ollama.Conversation

initInMemoryStore :: IO InMemoryStore Source #

Creates a new empty in-memory conversation store.

Initializes an InMemoryStore with an empty Map wrapped in a TVar for thread-safe operations.

Example:

>>> store <- initInMemoryStore
>>> runInMemoryConvo store $ listConversations
[]

runInMemoryConvo :: InMemoryStore -> ConvoM a -> IO a Source #

Runs a ConvoM action with the given InMemoryStore.

Executes a monadic computation in the context of an in-memory store.

Example:

>>> store <- initInMemoryStore
>>> runInMemoryConvo store $ saveConversation conv

Validation

validateConversation :: Conversation -> Either Text Conversation Source #

Validates a Conversation to ensure required fields are non-empty.

Checks that the conversationId is not empty and that the messages list contains at least one message. Returns Right with the validated conversation or Left with an error message.

Example:

>>> let conv = Conversation "" [] "gemma3" time time
>>> validateConversation conv
Left "Conversation ID cannot be empty"