Upped number of retries for JSON from 1 to 2. Added pretty debugging of chatGPT prompts.

This commit is contained in:
Martin Sosic 2023-06-27 18:24:12 +02:00
parent 69129df3ce
commit 349875b242
3 changed files with 36 additions and 20 deletions

View File

@ -31,16 +31,18 @@ type File = (FilePath, Text)
data AuthProvider = UsernameAndPassword
queryChatGPTForJSON :: FromJSON a => ChatGPTParams -> [ChatMessage] -> CodeAgent a
queryChatGPTForJSON chatGPTParams = doQueryForJSON True
queryChatGPTForJSON chatGPTParams = doQueryForJSON 0
where
doQueryForJSON isFirstTry chatMsgs = do
doQueryForJSON :: (FromJSON a) => Int -> [ChatMessage] -> CodeAgent a
doQueryForJSON numPrevFailures chatMsgs = do
response <- queryChatGPT chatGPTParams chatMsgs
case Aeson.eitherDecode . textToLazyBS . naiveTrimJSON $ response of
Right result -> return result
Left errMsg ->
if isFirstTry
let numFailures = numPrevFailures + 1
in if numFailures <= maxNumFailuresBeforeGivingUp
then
doQueryForJSON False $
doQueryForJSON (numPrevFailures + 1) $
chatMsgs
++ [ GPT.ChatMessage {GPT.role = GPT.Assistant, GPT.content = response},
GPT.ChatMessage
@ -56,6 +58,8 @@ queryChatGPTForJSON chatGPTParams = doQueryForJSON True
writeToLog "Failed to parse ChatGPT response as JSON."
error $ "Failed to parse ChatGPT response as JSON: " <> errMsg
maxNumFailuresBeforeGivingUp = 2
-- TODO: Test more for the optimal temperature (possibly higher).
defaultChatGPTParams :: ChatGPTParams
defaultChatGPTParams = GPT.ChatGPTParams {_model = GPT.GPT_3_5_turbo_16k, _temperature = Just 1.0}

View File

@ -1,5 +1,4 @@
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE ViewPatterns #-}
module Wasp.AI.OpenAI.ChatGPT
( queryChatGPT,
@ -14,10 +13,12 @@ module Wasp.AI.OpenAI.ChatGPT
where
import Control.Arrow ()
import Control.Monad (when)
import Data.Aeson ((.=))
import qualified Data.Aeson as Aeson
import Data.ByteString.UTF8 as BSU
import Data.Text (Text)
import Debug.Pretty.Simple (pTrace)
import GHC.Generics (Generic)
import qualified Network.HTTP.Conduit as HTTP.C
import qualified Network.HTTP.Simple as HTTP
@ -48,6 +49,16 @@ queryChatGPT apiKey params requestMessages = do
let (chatResponse :: ChatResponse) = HTTP.getResponseBody response
when False $
pTrace
( "\n\n\n\n==================================\n\n"
<> show requestMessages
<> "\n\n============\n\n"
<> show (usage chatResponse)
<> "\n\n==================================\n\n\n\n"
)
$ return ()
return $ content $ message $ head $ choices chatResponse
where
secondsToMicroSeconds :: Int -> Int

View File

@ -119,6 +119,7 @@ library
, parsec ^>= 3.1.14
, path ^>= 0.9.2
, path-io ^>= 1.6.3
, pretty-simple ^>= 4.1.2
, regex-tdfa ^>= 1.3.1
, strong-path ^>= 1.1.4
, unliftio ^>= 0.2.20