| Copyright | (c) 2025 Tushar Adhatrao |
|---|---|
| License | MIT |
| Maintainer | Tushar Adhatrao <tusharadhatrao@gmail.com> |
| Stability | experimental |
| Safe Haskell | None |
| Language | Haskell2010 |
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
- data Conversation = Conversation {
- conversationId :: !Text
- messages :: ![Message]
- model :: !Text
- createdAt :: !UTCTime
- lastUpdated :: !UTCTime
- class Monad m => ConversationStore (m :: Type -> Type) where
- saveConversation :: Conversation -> m ()
- loadConversation :: Text -> m (Maybe Conversation)
- listConversations :: m [Conversation]
- deleteConversation :: Text -> m Bool
- newtype InMemoryStore = InMemoryStore (TVar (Map Text Conversation))
- newtype ConvoM a = ConvoM {
- runConvoM :: ReaderT InMemoryStore IO a
- initInMemoryStore :: IO InMemoryStore
- runInMemoryConvo :: InMemoryStore -> ConvoM a -> IO a
- validateConversation :: Conversation -> Either Text Conversation
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
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 #
listConversations :: m [Conversation] Source #
Lists all conversations in the store.
deleteConversation :: Text -> m Bool Source #
Instances
| ConversationStore ConvoM Source # | |
Defined in Data.Ollama.Conversation Methods saveConversation :: Conversation -> ConvoM () Source # loadConversation :: Text -> ConvoM (Maybe Conversation) Source # | |
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.
Constructors
| InMemoryStore (TVar (Map Text Conversation)) |
Instances
| MonadReader InMemoryStore ConvoM Source # | |
Defined in Data.Ollama.Conversation Methods ask :: ConvoM InMemoryStore # local :: (InMemoryStore -> InMemoryStore) -> ConvoM a -> ConvoM a # reader :: (InMemoryStore -> a) -> ConvoM a # | |
Monad for operations with InMemoryStore.
A wrapper around ReaderT that provides access to an InMemoryStore in a monadic context.
Constructors
| ConvoM | |
Fields
| |
Instances
| MonadIO ConvoM Source # | |
Defined in Data.Ollama.Conversation | |
| Applicative ConvoM Source # | |
| Functor ConvoM Source # | |
| Monad ConvoM Source # | |
| ConversationStore ConvoM Source # | |
Defined in Data.Ollama.Conversation Methods saveConversation :: Conversation -> ConvoM () Source # loadConversation :: Text -> ConvoM (Maybe Conversation) Source # | |
| MonadReader InMemoryStore ConvoM Source # | |
Defined in Data.Ollama.Conversation Methods ask :: ConvoM InMemoryStore # local :: (InMemoryStore -> InMemoryStore) -> ConvoM a -> ConvoM a # reader :: (InMemoryStore -> a) -> ConvoM a # | |
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 convLeft "Conversation ID cannot be empty"