Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type guards for messages #32

Open
alexewerlof opened this issue Oct 20, 2023 · 2 comments
Open

Type guards for messages #32

alexewerlof opened this issue Oct 20, 2023 · 2 comments

Comments

@alexewerlof
Copy link
Contributor

alexewerlof commented Oct 20, 2023

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:

  • 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
@lino-levan
Copy link
Collaborator

@load1n9 what do you think?

@load1n9
Copy link
Owner

load1n9 commented Oct 20, 2023

sounds good tbh we should try it out

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants