packages feed

genai-lib-2.0: src/example/ex-genai-ollama.hs

import Control.Exception (Handler (..), catches)
import Data.Text.Lazy.IO qualified as TL
import GenAILib (ClientError, Request (..), boolopt, jsonToText, mkRequest,
  numopt, optional, systemmsg, usermsg)
import GenAILib.HTTP (GenAIException, ollamaChat, ollamaChatJ)
import GenAILib.Ollama (OllamaRequest, getMessage, keepalive)


main :: IO ()
main = do
  ollamaJSON
  ollamaData

-- A simple example with no error handling that expects an Aeson Value
-- (OllamaRequest is an instance of ToJSON) and displays the encoded JSON
-- response
ollamaJSON :: IO ()
ollamaJSON = do
  let req :: OllamaRequest = mkRequest "gemma3:1b" $ usermsg "Why is the sky blue?"
  TL.putStrLn . jsonToText =<< ollamaChatJ Nothing req

-- Another example with exception handling that expects an OllamaResponse
-- data structure, displaying that and also just the response text
ollamaData :: IO ()
ollamaData = do
  let mbDuration = Just "30m"
  let req =  mkRequest "gemma3:1b"
          (  systemmsg "Answer in the style of Bugs Bunny. Try to work in the phrase \"What's up Doc?\" somewhere."
          <> usermsg "Why is the sky blue?"
          <> numopt "temperature" 0.1
          <> boolopt "penalize_newline" True
          <> (keepalive `optional` mbDuration)
          )
  res <- ollamaChat Nothing req `catches`
    [ Handler (\(e :: GenAIException) -> error . show $ e)
    , Handler (\(e :: ClientError) -> error . show $ e)
    ]
  print res  -- The entire OllamaResponse
  TL.putStrLn . getMessage $ res  -- Just the response Message Content