mirror of
https://github.com/wasp-lang/wasp.git
synced 2024-11-23 19:29:17 +03:00
Upped number of retries for JSON from 1 to 2. Added pretty debugging of chatGPT prompts.
This commit is contained in:
parent
69129df3ce
commit
349875b242
@ -31,30 +31,34 @@ type File = (FilePath, Text)
|
|||||||
data AuthProvider = UsernameAndPassword
|
data AuthProvider = UsernameAndPassword
|
||||||
|
|
||||||
queryChatGPTForJSON :: FromJSON a => ChatGPTParams -> [ChatMessage] -> CodeAgent a
|
queryChatGPTForJSON :: FromJSON a => ChatGPTParams -> [ChatMessage] -> CodeAgent a
|
||||||
queryChatGPTForJSON chatGPTParams = doQueryForJSON True
|
queryChatGPTForJSON chatGPTParams = doQueryForJSON 0
|
||||||
where
|
where
|
||||||
doQueryForJSON isFirstTry chatMsgs = do
|
doQueryForJSON :: (FromJSON a) => Int -> [ChatMessage] -> CodeAgent a
|
||||||
|
doQueryForJSON numPrevFailures chatMsgs = do
|
||||||
response <- queryChatGPT chatGPTParams chatMsgs
|
response <- queryChatGPT chatGPTParams chatMsgs
|
||||||
case Aeson.eitherDecode . textToLazyBS . naiveTrimJSON $ response of
|
case Aeson.eitherDecode . textToLazyBS . naiveTrimJSON $ response of
|
||||||
Right result -> return result
|
Right result -> return result
|
||||||
Left errMsg ->
|
Left errMsg ->
|
||||||
if isFirstTry
|
let numFailures = numPrevFailures + 1
|
||||||
then
|
in if numFailures <= maxNumFailuresBeforeGivingUp
|
||||||
doQueryForJSON False $
|
then
|
||||||
chatMsgs
|
doQueryForJSON (numPrevFailures + 1) $
|
||||||
++ [ GPT.ChatMessage {GPT.role = GPT.Assistant, GPT.content = response},
|
chatMsgs
|
||||||
GPT.ChatMessage
|
++ [ GPT.ChatMessage {GPT.role = GPT.Assistant, GPT.content = response},
|
||||||
{ GPT.role = GPT.User,
|
GPT.ChatMessage
|
||||||
GPT.content =
|
{ GPT.role = GPT.User,
|
||||||
"You did not respond with valid JSON. Please fix it and respond with only"
|
GPT.content =
|
||||||
<> " valid JSON, no other text or explanations. Error I got parsing JSON"
|
"You did not respond with valid JSON. Please fix it and respond with only"
|
||||||
<> " from your last message: "
|
<> " valid JSON, no other text or explanations. Error I got parsing JSON"
|
||||||
<> T.pack errMsg
|
<> " from your last message: "
|
||||||
}
|
<> T.pack errMsg
|
||||||
]
|
}
|
||||||
else do
|
]
|
||||||
writeToLog "Failed to parse ChatGPT response as JSON."
|
else do
|
||||||
error $ "Failed to parse ChatGPT response as JSON: " <> errMsg
|
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).
|
-- TODO: Test more for the optimal temperature (possibly higher).
|
||||||
defaultChatGPTParams :: ChatGPTParams
|
defaultChatGPTParams :: ChatGPTParams
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
{-# LANGUAGE DeriveGeneric #-}
|
{-# LANGUAGE DeriveGeneric #-}
|
||||||
{-# LANGUAGE ViewPatterns #-}
|
|
||||||
|
|
||||||
module Wasp.AI.OpenAI.ChatGPT
|
module Wasp.AI.OpenAI.ChatGPT
|
||||||
( queryChatGPT,
|
( queryChatGPT,
|
||||||
@ -14,10 +13,12 @@ module Wasp.AI.OpenAI.ChatGPT
|
|||||||
where
|
where
|
||||||
|
|
||||||
import Control.Arrow ()
|
import Control.Arrow ()
|
||||||
|
import Control.Monad (when)
|
||||||
import Data.Aeson ((.=))
|
import Data.Aeson ((.=))
|
||||||
import qualified Data.Aeson as Aeson
|
import qualified Data.Aeson as Aeson
|
||||||
import Data.ByteString.UTF8 as BSU
|
import Data.ByteString.UTF8 as BSU
|
||||||
import Data.Text (Text)
|
import Data.Text (Text)
|
||||||
|
import Debug.Pretty.Simple (pTrace)
|
||||||
import GHC.Generics (Generic)
|
import GHC.Generics (Generic)
|
||||||
import qualified Network.HTTP.Conduit as HTTP.C
|
import qualified Network.HTTP.Conduit as HTTP.C
|
||||||
import qualified Network.HTTP.Simple as HTTP
|
import qualified Network.HTTP.Simple as HTTP
|
||||||
@ -48,6 +49,16 @@ queryChatGPT apiKey params requestMessages = do
|
|||||||
|
|
||||||
let (chatResponse :: ChatResponse) = HTTP.getResponseBody response
|
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
|
return $ content $ message $ head $ choices chatResponse
|
||||||
where
|
where
|
||||||
secondsToMicroSeconds :: Int -> Int
|
secondsToMicroSeconds :: Int -> Int
|
||||||
|
@ -119,6 +119,7 @@ library
|
|||||||
, parsec ^>= 3.1.14
|
, parsec ^>= 3.1.14
|
||||||
, path ^>= 0.9.2
|
, path ^>= 0.9.2
|
||||||
, path-io ^>= 1.6.3
|
, path-io ^>= 1.6.3
|
||||||
|
, pretty-simple ^>= 4.1.2
|
||||||
, regex-tdfa ^>= 1.3.1
|
, regex-tdfa ^>= 1.3.1
|
||||||
, strong-path ^>= 1.1.4
|
, strong-path ^>= 1.1.4
|
||||||
, unliftio ^>= 0.2.20
|
, unliftio ^>= 0.2.20
|
||||||
|
Loading…
Reference in New Issue
Block a user