ollama-haskell-0.1.0.0: Ollama Haskell library
Safe HaskellSafe-Inferred
LanguageGHC2021

Data.Ollama.Generate

Synopsis

Generate Texts

generate :: GenerateOps -> IO (Either String GenerateResponse) Source #

Generate function that returns either a GenerateResponse type or an error message. It takes a GenerateOps configuration and performs a request to the Ollama generate API.

Examples:

Basic usage without streaming:

let ops = GenerateOps 
        { modelName = "llama3.2"
        , prompt = "Tell me a joke."
        , suffix = Nothing
        , images = Nothing
        , format = Nothing
        , system = Nothing
        , template = Nothing
        , stream = Nothing
        , raw = Nothing
        , keepAlive = Nothing
        }
result <- generate ops
case result of
  Left errorMsg -> putStrLn ("Error: " ++ errorMsg)
  Right response -> print response

Usage with streaming to print responses to the console:

void $
  generate
    defaultGenerateOps
      { modelName = "llama3.2"
      , prompt = "what is functional programming?"
      , stream = Just (T.putStr . response_, pure ())
      }

In this example, the first function in the $sel:stream:GenerateOps tuple processes each chunk of response by printing it, and the second function is a simple no-op flush.generate :: GenerateOps -> IO (Either String GenerateResponse)

defaultGenerateOps :: GenerateOps Source #

A function to create a default GenerateOps type with preset values.

Example:

let ops = defaultGenerateOps
generate ops

This will generate a response using the default configuration.

data GenerateOps Source #

Input type for generate functions. This data type represents all possible configurations that you can pass to the Ollama generate API.

Example:

let ops = GenerateOps 
        { modelName = "llama3.2"
        , prompt = "What is the meaning of life?"
        , suffix = Nothing
        , images = Nothing
        , format = Just "text"
        , system = Nothing
        , template = Nothing
        , stream = Nothing
        , raw = Just False
        , keepAlive = Just "yes"
        }

Constructors

GenerateOps 

Fields

  • modelName :: Text

    The name of the model to be used for generation.

  • prompt :: Text

    The prompt text that will be provided to the model for generating a response.

  • suffix :: Maybe Text

    An optional suffix to append to the generated text.

  • images :: Maybe [Text]

    Optional list of base64 encoded images to include with the request.

  • format :: Maybe Text

    An optional format specifier for the response.

  • system :: Maybe Text

    Optional system text that can be included in the generation context.

  • template :: Maybe Text

    An optional template to format the response.

  • stream :: Maybe (GenerateResponse -> IO (), IO ())

    An optional streaming function where the first function handles each chunk of response, and the second flushes the stream.

  • raw :: Maybe Bool

    An optional flag to return the raw response.

  • keepAlive :: Maybe Text

    Optional text to specify keep-alive behavior.

data GenerateResponse Source #

Result type for generate function containing the model's response and meta-information.

Constructors

GenerateResponse 

Fields