When going through the incoming JSON array from OpenAI API, each element can be of a different type.
Deno uses TypeScript which uses type guards to simplify type assertions.
Examples
Let's say we have an array of messages:
const messageArray: ChatCompletionMessage[]
When a FunctionCompletionMessage is being inserted to messageArray, it is important to verify that the last element is of type FunctionAwareAssistantCompletionMessage.
When inserting user input into the messageArray, it is important to check that it is well-formed (UserCompletionMessage).
Suggestion
Add the following guard functions:
isSystemCompletionMessage(x: unknown): x is SystemCompletionMessage
isUserCompletionMessage(x: unknown): x is UserCompletionMessage
isFunctionAwareAssistantCompletionMessage(x: unknown): x is FunctionAwareAssistantCompletionMessage
isFunctionCompletionMessage(x: unknown): x is FunctionCompletionMessage
isAssistantCompletionMessage(x: unknown): x is AssistantCompletionMessage
When going through the incoming JSON array from OpenAI API, each element can be of a different type.
Deno uses TypeScript which uses type guards to simplify type assertions.
Examples
Let's say we have an array of messages:
When a
FunctionCompletionMessage
is being inserted tomessageArray
, it is important to verify that the last element is of typeFunctionAwareAssistantCompletionMessage
.When inserting user input into the
messageArray
, it is important to check that it is well-formed (UserCompletionMessage
).Suggestion
Add the following guard functions:
isSystemCompletionMessage(x: unknown): x is SystemCompletionMessage
isUserCompletionMessage(x: unknown): x is UserCompletionMessage
isFunctionAwareAssistantCompletionMessage(x: unknown): x is FunctionAwareAssistantCompletionMessage
isFunctionCompletionMessage(x: unknown): x is FunctionCompletionMessage
isAssistantCompletionMessage(x: unknown): x is AssistantCompletionMessage