|
| 1 | +import { AIMessage, HumanMessage } from '@langchain/core/messages'; |
| 2 | +import chalk from 'chalk'; |
| 3 | +import { ChatMessageHistory } from 'langchain/stores/message/in_memory'; |
| 4 | +import readline from 'node:readline'; |
| 5 | + |
| 6 | +import type { Platform } from '../../platform'; |
| 7 | + |
| 8 | +import { AIGC } from '../../core'; |
| 9 | +import BaseCommand from '../extends/command'; |
| 10 | + |
| 11 | +enum PromptRole { |
| 12 | + AI = 'ai', |
| 13 | + USER = 'user' |
| 14 | +} |
| 15 | + |
| 16 | +const promptMessageMap = { |
| 17 | + [PromptRole.AI]: AIMessage, |
| 18 | + [PromptRole.USER]: HumanMessage |
| 19 | +}; |
| 20 | +const promptRoleDisplayMap = { |
| 21 | + [PromptRole.AI]: { |
| 22 | + color: 'yellow', |
| 23 | + name: 'AI' |
| 24 | + }, |
| 25 | + [PromptRole.USER]: { |
| 26 | + color: 'green', |
| 27 | + name: 'You' |
| 28 | + } |
| 29 | +} as const; |
| 30 | + |
| 31 | +const reader = readline.createInterface({ |
| 32 | + input: process.stdin, |
| 33 | + output: process.stdout |
| 34 | +}); |
| 35 | + |
| 36 | +class ChatCommand extends BaseCommand { |
| 37 | + static args = {}; |
| 38 | + |
| 39 | + static description = 'Chat with the LLM'; |
| 40 | + |
| 41 | + static examples = []; |
| 42 | + |
| 43 | + static flags = {}; |
| 44 | + |
| 45 | + private lastMessage = 'How can I help you today?'; |
| 46 | + |
| 47 | + private messages = new ChatMessageHistory(); |
| 48 | + |
| 49 | + async run(): Promise<void> { |
| 50 | + const config = await this.configManager.getAll(); |
| 51 | + |
| 52 | + if (Object.keys(config).length > 0) { |
| 53 | + const detector = new AIGC({ |
| 54 | + apiKey: config.apiKey, |
| 55 | + platform: config.platform as unknown as Platform |
| 56 | + }); |
| 57 | + const userDisplay = this.getDisplayContent(PromptRole.USER); |
| 58 | + |
| 59 | + // eslint-disable-next-line no-constant-condition |
| 60 | + while (true) { |
| 61 | + const aiMessage = await this.addMessage(PromptRole.AI, this.lastMessage); |
| 62 | + const userMessage = await this.getUserMessage(aiMessage + `\n${userDisplay}`); |
| 63 | + const answer = await detector.chat(userMessage, await this.messages.getMessages()); |
| 64 | + |
| 65 | + await this.addMessage(PromptRole.USER, userMessage); |
| 66 | + this.lastMessage = answer; |
| 67 | + } |
| 68 | + } else { |
| 69 | + this.showHelp(); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private async addMessage(role: PromptRole, content: string): Promise<string> { |
| 74 | + const Message = promptMessageMap[role]; |
| 75 | + |
| 76 | + await this.messages.addMessage(new Message(content)); |
| 77 | + |
| 78 | + return this.getDisplayContent(role) + content; |
| 79 | + } |
| 80 | + |
| 81 | + private getDisplayContent(role: PromptRole): string { |
| 82 | + const roleDisplay = promptRoleDisplayMap[role]; |
| 83 | + |
| 84 | + return chalk[roleDisplay.color](`[${roleDisplay.name}] `); |
| 85 | + } |
| 86 | + |
| 87 | + private getUserMessage(aiMessage: string): Promise<string> { |
| 88 | + return new Promise<string>((resolve) => { |
| 89 | + reader.question(aiMessage, resolve); |
| 90 | + }); |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +export default ChatCommand; |
0 commit comments