niieani/gpt-tokenizer

ChatMessage[] TypeScript warning

Closed this issue ยท 3 comments

I tried to follow sample usage, but the line encodeChat(messages) is returning this warning in VS Code:

[{
        ...
	"message": "Argument of type '{ role: string; content: string; }[]' is not assignable to parameter of type 'readonly ChatMessage[]'.\n  Type '{ role: string; content: string; }' is not assignable to type 'ChatMessage'.\n    Types of property 'role' are incompatible.\n      Type 'string' is not assignable to type '\"system\" | \"user\" | \"assistant\" | undefined'.",
        ...
}]

How do I make this work without TypeScript warnings?

I ended up defining my own type, but I still think this should be included in instructions:

export type ChatMessage = {
  role: 'assistant' | 'system' | 'user' | undefined;
  content: string;
};

Owner please consider updating the documentation to either one of these:

const chat = [
  { role: 'system' as const , content: 'You are a helpful assistant.' },
  { role: 'assistant' as const, content: 'gpt-tokenizer is awesome.' },
]
const chat = [
  { role: 'system' , content: 'You are a helpful assistant.' },
  { role: 'assistant', content: 'gpt-tokenizer is awesome.' },
] as const

๐ŸŽ‰ This issue has been resolved in version 2.2.1 ๐ŸŽ‰

The release is available on:

Your semantic-release bot ๐Ÿ“ฆ๐Ÿš€