Skip to content

Instantly share code, notes, and snippets.

@golergka
Last active July 3, 2023 12:59
Show Gist options
  • Save golergka/3fdc6b9cf9717bd1c0b315925a85e328 to your computer and use it in GitHub Desktop.
Save golergka/3fdc6b9cf9717bd1c0b315925a85e328 to your computer and use it in GitHub Desktop.
// pipe is a utility function that passes the result of previous step to the next
// pipe(a, b, c) === c(b(a))
pipe(
// This function returns an array of messages for chat completion
getTurnPromptMessages(state, action),
// This turns an array into the CC request object
mkCCRequestFromMessages,
pipe(
// flow returns a function that combines passed functions into it
// flow(a, b, c) = (x) => c(b(a(x)))
flow(
// this adds the schema that GPT should answer in to the request
addSchemaToRequest<Omit<CreateChatCompletionRequest, 'model'>>(turnS),
// this is the OpenAI's handler for chat completion
// it is an async operation (Task) that can fail (Either),
// so next steps deal with TaskEither (TE)
handleChatCompletion,
// this extracts first completion choice
// (or fails if there are no choices)
TE.flatMapEither(extractFirstCompletionChoice),
// this extracts completion message
// (or fails if there's no message)
TE.flatMapEither(extractChatCompletionMessage),
// this adds an empty message to errors from previous steps
// now any resulting error has an optional message
TE.mapLeft(mkMessageError(undefined)),
// this handles the retrieved completion message
TE.flatMap((message) =>
pipe(
// this decodes the message according to schema mentioned earlier
// (or fails if LLM doesn't build valid json)
decodeSchema(message),
// this uses another LLM request to detect a location in a story
TE.flatMap(detectLocation(state)),
// this adds the retrieved message to errors from previous steps
TE.mapLeft(mkMessageError(message))
)
)
),
// and this function accepts the handler above and makes it retriable, up to 3 times
// on every retry it adds GPT messages from above and errors to chat history
mkResilient(3)
)
)
@golergka
Copy link
Author

golergka commented Jul 3, 2023

Hey, if you've read this far, I'm actually looking for a job now. Ping me at golergka@gmail.com

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment