{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
module OllamaExamples (main) where
import Control.Monad (void)
import Data.Aeson
import Data.List.NonEmpty (NonEmpty ((:|)))
import Data.Maybe (fromMaybe)
import Data.Ollama.Chat (chatJson)
import Data.Ollama.Chat qualified as Chat
import Data.Ollama.Common.Utils (encodeImage)
import Data.Ollama.Generate (generateJson)
import Data.Text.IO qualified as T
import GHC.Generics
import Ollama (GenerateOps (..), Role (..), chat, defaultChatOps, defaultGenerateOps, generate)
import Ollama qualified
data Example = Example
{ Example -> [String]
sortedList :: [String]
, Example -> Bool
wasListAlreadSorted :: Bool
}
deriving (Int -> Example -> ShowS
[Example] -> ShowS
Example -> String
(Int -> Example -> ShowS)
-> (Example -> String) -> ([Example] -> ShowS) -> Show Example
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Example -> ShowS
showsPrec :: Int -> Example -> ShowS
$cshow :: Example -> String
show :: Example -> String
$cshowList :: [Example] -> ShowS
showList :: [Example] -> ShowS
Show, Example -> Example -> Bool
(Example -> Example -> Bool)
-> (Example -> Example -> Bool) -> Eq Example
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Example -> Example -> Bool
== :: Example -> Example -> Bool
$c/= :: Example -> Example -> Bool
/= :: Example -> Example -> Bool
Eq, (forall x. Example -> Rep Example x)
-> (forall x. Rep Example x -> Example) -> Generic Example
forall x. Rep Example x -> Example
forall x. Example -> Rep Example x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Example -> Rep Example x
from :: forall x. Example -> Rep Example x
$cto :: forall x. Rep Example x -> Example
to :: forall x. Rep Example x -> Example
Generic, Maybe Example
Value -> Parser [Example]
Value -> Parser Example
(Value -> Parser Example)
-> (Value -> Parser [Example]) -> Maybe Example -> FromJSON Example
forall a.
(Value -> Parser a)
-> (Value -> Parser [a]) -> Maybe a -> FromJSON a
$cparseJSON :: Value -> Parser Example
parseJSON :: Value -> Parser Example
$cparseJSONList :: Value -> Parser [Example]
parseJSONList :: Value -> Parser [Example]
$comittedField :: Maybe Example
omittedField :: Maybe Example
FromJSON, [Example] -> Value
[Example] -> Encoding
Example -> Bool
Example -> Value
Example -> Encoding
(Example -> Value)
-> (Example -> Encoding)
-> ([Example] -> Value)
-> ([Example] -> Encoding)
-> (Example -> Bool)
-> ToJSON Example
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> (a -> Bool)
-> ToJSON a
$ctoJSON :: Example -> Value
toJSON :: Example -> Value
$ctoEncoding :: Example -> Encoding
toEncoding :: Example -> Encoding
$ctoJSONList :: [Example] -> Value
toJSONList :: [Example] -> Value
$ctoEncodingList :: [Example] -> Encoding
toEncodingList :: [Example] -> Encoding
$comitField :: Example -> Bool
omitField :: Example -> Bool
ToJSON)
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
let expectedJsonStrucutre :: Example
expectedJsonStrucutre =
Example
{ sortedList :: [String]
sortedList = [String
"sorted List here"]
, wasListAlreadSorted :: Bool
wasListAlreadSorted = Bool
False
}
Either String Example
eRes2 <-
GenerateOps -> Example -> Maybe Int -> IO (Either String Example)
forall jsonResult.
(ToJSON jsonResult, FromJSON jsonResult) =>
GenerateOps
-> jsonResult -> Maybe Int -> IO (Either String jsonResult)
generateJson
GenerateOps
defaultGenerateOps
{ modelName = "llama3.2"
, prompt = "Sort given list: [14, 12 , 13, 67]. Also tell whether list was already sorted or not."
}
Example
expectedJsonStrucutre
(Int -> Maybe Int
forall a. a -> Maybe a
Just Int
2)
case Either String Example
eRes2 of
Left String
e -> String -> IO ()
putStrLn String
e
Right Example
r -> (String, Example) -> IO ()
forall a. Show a => a -> IO ()
print (String
"JSON response: " :: String, Example
r)
let msg0 :: Message
msg0 =
Role -> Text -> Maybe [Text] -> Message
Ollama.Message
Role
User
Text
"Sort given list: [4, 2 , 3, 67]. Also tell whether list was already sorted or not."
Maybe [Text]
forall a. Maybe a
Nothing
Either String Example
eRes3 <-
ChatOps -> Example -> Maybe Int -> IO (Either String Example)
forall jsonResult.
(FromJSON jsonResult, ToJSON jsonResult) =>
ChatOps -> jsonResult -> Maybe Int -> IO (Either String jsonResult)
chatJson
ChatOps
defaultChatOps
{ Chat.chatModelName = "llama3.2"
, Chat.messages = msg0 :| []
}
Example
expectedJsonStrucutre
(Int -> Maybe Int
forall a. a -> Maybe a
Just Int
2)
Either String Example -> IO ()
forall a. Show a => a -> IO ()
print Either String Example
eRes3
Maybe Text
mImg <- String -> IO (Maybe Text)
encodeImage String
"/home/user/sample.png"
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-vision"
, prompt = "Describe the given image"
, images = (\Text
x -> [Text] -> Maybe [Text]
forall a. a -> Maybe a
Just [Text
x]) =<< mImg
, stream = Just (T.putStr . Ollama.response_, pure ())
}