{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
module Lib where
import Control.Monad (void)
import Data.List.NonEmpty (NonEmpty((:|)))
import Data.Maybe (fromMaybe)
import Data.Ollama.Chat qualified as Chat
import Data.Text.IO qualified as T
import Ollama (GenerateOps(..), Role(..), chat, defaultChatOps, defaultGenerateOps, generate)
import Ollama qualified
main :: IO ()
main :: IO ()
main = do
IO (Either String GenerateResponse) -> IO ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (IO (Either String GenerateResponse) -> IO ())
-> IO (Either String GenerateResponse) -> IO ()
forall a b. (a -> b) -> a -> b
$
GenerateOps -> IO (Either String GenerateResponse)
generate
GenerateOps
defaultGenerateOps
{ modelName = "llama3.2"
, prompt = "what is functional programming?"
, stream = Just (T.putStr . Ollama.response_, pure ())
}
Either String GenerateResponse
eRes <-
GenerateOps -> IO (Either String GenerateResponse)
generate
GenerateOps
defaultGenerateOps
{ modelName = "llama3.2"
, prompt = "What is 2+2?"
}
case Either String GenerateResponse
eRes of
Left String
e -> String -> IO ()
putStrLn String
e
Right Ollama.GenerateResponse {Bool
Maybe Int64
Text
UTCTime
$sel:response_:GenerateResponse :: GenerateResponse -> Text
model :: Text
createdAt :: UTCTime
response_ :: Text
done :: Bool
totalDuration :: Maybe Int64
loadDuration :: Maybe Int64
promptEvalCount :: Maybe Int64
promptEvalDuration :: Maybe Int64
evalCount :: Maybe Int64
evalDuration :: Maybe Int64
$sel:model:GenerateResponse :: GenerateResponse -> Text
$sel:createdAt:GenerateResponse :: GenerateResponse -> UTCTime
$sel:done:GenerateResponse :: GenerateResponse -> Bool
$sel:totalDuration:GenerateResponse :: GenerateResponse -> Maybe Int64
$sel:loadDuration:GenerateResponse :: GenerateResponse -> Maybe Int64
$sel:promptEvalCount:GenerateResponse :: GenerateResponse -> Maybe Int64
$sel:promptEvalDuration:GenerateResponse :: GenerateResponse -> Maybe Int64
$sel:evalCount:GenerateResponse :: GenerateResponse -> Maybe Int64
$sel:evalDuration:GenerateResponse :: GenerateResponse -> Maybe Int64
..} -> Text -> IO ()
T.putStrLn Text
response_
let msg :: Message
msg = Role -> Text -> Maybe [Text] -> Message
Ollama.Message Role
User Text
"What is functional programming?" Maybe [Text]
forall a. Maybe a
Nothing
defaultMsg :: Message
defaultMsg = Role -> Text -> Maybe [Text] -> Message
Ollama.Message Role
User Text
"" Maybe [Text]
forall a. Maybe a
Nothing
IO (Either String ChatResponse) -> IO ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (IO (Either String ChatResponse) -> IO ())
-> IO (Either String ChatResponse) -> IO ()
forall a b. (a -> b) -> a -> b
$
ChatOps -> IO (Either String ChatResponse)
chat
ChatOps
defaultChatOps
{ Chat.chatModelName = "llama3.2"
, Chat.messages = msg :| []
, Chat.stream =
Just (T.putStr . Chat.content . fromMaybe defaultMsg . Chat.message, pure ())
}
Either String ChatResponse
eRes1 <-
ChatOps -> IO (Either String ChatResponse)
chat
ChatOps
defaultChatOps
{ Chat.chatModelName = "llama3.2"
, Chat.messages = msg :| []
}
case Either String ChatResponse
eRes1 of
Left String
e -> String -> IO ()
putStrLn String
e
Right ChatResponse
r -> do
let mMessage :: Maybe Message
mMessage = ChatResponse -> Maybe Message
Ollama.message ChatResponse
r
case Maybe Message
mMessage of
Maybe Message
Nothing -> String -> IO ()
putStrLn String
"Something went wrong"
Just Message
res -> Text -> IO ()
T.putStrLn (Text -> IO ()) -> Text -> IO ()
forall a b. (a -> b) -> a -> b
$ Message -> Text
Ollama.content Message
res
Maybe RunningModels
res <- IO (Maybe RunningModels)
Ollama.ps
Maybe RunningModels -> IO ()
forall a. Show a => a -> IO ()
print Maybe RunningModels
res
IO (Maybe EmbeddingResp) -> IO ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (IO (Maybe EmbeddingResp) -> IO ())
-> IO (Maybe EmbeddingResp) -> IO ()
forall a b. (a -> b) -> a -> b
$ Text -> Text -> IO (Maybe EmbeddingResp)
Ollama.embedding Text
"llama3.1" Text
"What is 5+2?"
IO (Maybe EmbeddingResp) -> IO ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (IO (Maybe EmbeddingResp) -> IO ())
-> IO (Maybe EmbeddingResp) -> IO ()
forall a b. (a -> b) -> a -> b
$ Text
-> Text -> Maybe Bool -> Maybe Text -> IO (Maybe EmbeddingResp)
Ollama.embeddingOps Text
"llama3.1" Text
"What is 5+2?" Maybe Bool
forall a. Maybe a
Nothing Maybe Text
forall a. Maybe a
Nothing