| 436 | } |
| 437 | |
| 438 | export function GPTChatFormatToClaudeInstant( |
| 439 | chatMessages: ChatGPTMessage[], |
| 440 | ): string { |
| 441 | const roleToName = { |
| 442 | system: "", |
| 443 | user: "Human:\n", |
| 444 | assistant: "Assistant:\n", |
| 445 | function: "Function: ", // Should never be used in Claude Instant |
| 446 | }; |
| 447 | if (chatMessages.filter((m) => m.role === "function").length > 0) { |
| 448 | throw new Error( |
| 449 | "Function messages are not supported in Claude Instant. Please remove them.", |
| 450 | ); |
| 451 | } |
| 452 | const out = `${chatMessages |
| 453 | .map((message) => `${roleToName[message.role]}${message.content}`) |
| 454 | .join("\n\n")}`; |
| 455 | if (!out.includes("\n\nHuman:")) { |
| 456 | throw new Error( |
| 457 | "Claude Instant requires a user message. Please add a user message to the prompt.", |
| 458 | ); |
| 459 | } |
| 460 | return out; |
| 461 | } |