Initial commit

This commit is contained in:
MasseR committed 2025-03-27 13:54:44 +02:00
commit 2305885dc6
14 files changed
+648

No files matched your search

+28
View File
@@ -0,0 +1,28 @@
{-# LANGUAGE OverloadedStrings #-}
{-# OPTIONS_GHC -Wall #-}
module AI where
import Control.Lens (re, traversed, (&), (.~), (^.), (^?))
import Control.Monad.Reader (liftIO)
import Creds
import Data.Aeson (object, (.=))
import Data.Aeson.Lens (key, _Array, _String)
import Data.Text (Text)
import Data.Text.Strict.Lens (utf8)
import Network.Wreq (defaults, header, postWith, responseBody)
import UnliftIO (MonadIO)
import Control.Exception.Annotated (checkpointCallStack)
import Control.Monad.Catch (MonadCatch)
completions :: (MonadIO m, MonadCatch m) => Creds -> Text -> m (Maybe Text)
completions c prompt = checkpointCallStack $ do
let opts = defaults & header "Authorization" .~ ["Bearer " <> c ^. openrouterKey . re utf8]
let j =
object
[ "model" .= ("openai/gpt-4o-mini" :: Text),
"messages"
.= [object ["role" .= ("user" :: Text), "content" .= prompt]]
]
r <- liftIO $ postWith opts "https://openrouter.ai/api/v1/chat/completions" j
pure $ r ^? responseBody . key "choices" . _Array . traversed . key "message" . key "content" . _String