-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
86 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
{-# LANGUAGE ViewPatterns #-} | ||
|
||
-- | A bot for general interactions with OpenAI's GPT LLM. | ||
module CofreeBot.Bot.Behaviors.OpenAI | ||
( openAIBot, | ||
) | ||
where | ||
|
||
-------------------------------------------------------------------------------- | ||
|
||
import CofreeBot.Bot | ||
import CofreeBot.Utils.ListT (emptyListT) | ||
import Control.Monad.IO.Class (MonadIO (..)) | ||
import Data.Attoparsec.Text | ||
import Data.Text (Text) | ||
import Data.Text qualified as T | ||
import Data.Vector qualified as V | ||
import OpenAI.Client qualified as OpenAI | ||
|
||
-------------------------------------------------------------------------------- | ||
|
||
openAIBot :: OpenAI.OpenAIClient -> Bot IO () Text Text | ||
openAIBot client = | ||
contraMapMaybeBot (either (const Nothing) Just . parseOnly openAIBotParser) $ | ||
Bot $ \s (buildPrompt -> i) -> do | ||
liftIO (OpenAI.completeText client (OpenAI.EngineId "text-davinci-003") (i { OpenAI.tccrMaxTokens = Just 2096} )) >>= \case | ||
Left _err -> emptyListT | ||
Right OpenAI.TextCompletion {tcChoices} -> | ||
let OpenAI.TextCompletionChoice {..} = V.head tcChoices | ||
in pure (T.strip tccText, s) | ||
|
||
buildPrompt :: Text -> OpenAI.TextCompletionCreate | ||
buildPrompt input = | ||
let preamble = "You are a friendly chat bot named Cofree-Bot on a server dedicated to functional programming. Please respond to the following prompt:" | ||
in OpenAI.defaultTextCompletionCreate $ preamble <> input | ||
|
||
openAIBotParser :: Parser Text | ||
openAIBotParser = do | ||
_ <- "chat: " | ||
T.pack <$> many1 anyChar |