ai-utils.js
Version:
Build AI applications, chatbots, and agents with JavaScript and TypeScript.
34 lines (33 loc) • 924 B
TypeScript
/**
* A chat prompt is a sequence of messages with the following structure:
*
* - A chat prompt can optionally start with a system message.
* - After the optional system message, the first message of the chat must be a user message.
* - Then it must be alternating between an ai message and a user message.
* - The last message must always be a user message.
*
* The type checking is done at runtime, because there a no good ways to do it statically.
*
* @example
* ```ts
* [
* { system: "You are a celebrated poet." },
* { user: "Write a short story about a robot learning to love." },
* { ai: "Once upon a time, there was a robot who learned to love." },
* { user: "That's a great start!" },
* ]
* ```
*
* @see validateChatPrompt
*/
export type ChatPrompt = [...({
user: string;
} | {
ai: string;
})[]] | [{
system: string;
}, ...({
user: string;
} | {
ai: string;
})[]];