load1n9 / openai

Unofficial Deno wrapper for the Open Ai api
MIT License
73 stars 23 forks source link

Type guards for messages #32

Open alexewerlof opened 1 year ago

alexewerlof commented 1 year ago

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[]
  1. When a FunctionCompletionMessage is being inserted to messageArray, it is important to verify that the last element is of type FunctionAwareAssistantCompletionMessage.

  2. When inserting user input into the messageArray, it is important to check that it is well-formed (UserCompletionMessage).

Suggestion

Add the following guard functions:

lino-levan commented 1 year ago

@load1n9 what do you think?

load1n9 commented 1 year ago

sounds good tbh we should try it out