diff --git a/package.json b/package.json index 9aefd7b..7fad29d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "aigc-detector", - "version": "1.0.6", + "version": "1.0.7", "description": "Detect if content is generated by AI", "keywords": [ "aigc", diff --git a/src/cli/commands/chat.ts b/src/cli/commands/chat.ts index c3927a0..2882467 100644 --- a/src/cli/commands/chat.ts +++ b/src/cli/commands/chat.ts @@ -1,4 +1,5 @@ import { AIMessage, HumanMessage, SystemMessage } from '@langchain/core/messages'; +import ansiEscapes from 'ansi-escapes'; import chalk from 'chalk'; import { ChatMessageHistory } from 'langchain/stores/message/in_memory'; import readline from 'node:readline'; @@ -59,20 +60,14 @@ class ChatCommand extends BaseCommand { console.warn = () => {}; process.stdout.write( - this.getDisplayContent(PromptRole.SYSTEM) + `Type ${chalk.cyan('exit')} to end this conversation\n` + this.getDisplayContent(PromptRole.SYSTEM) + + `If you want to end this conversation, please tell the AI directly\n` ); process.stdout.write(aiDisplay + lastMessage + '\n'); // eslint-disable-next-line no-constant-condition while (true) { const userMessage = await this.getUserMessage(); - - if (userMessage === 'exit') { - close(); - - break; - } - const stream = detector.chat(userMessage, await this.messages.getMessages()); process.stdout.write(aiDisplay); @@ -80,6 +75,17 @@ class ChatCommand extends BaseCommand { lastMessage = await stream.getData(); + if (lastMessage === '$command:exit$') { + process.stdout.write(ansiEscapes.eraseLine + ansiEscapes.cursorLeft); + process.stdout.write(aiDisplay + 'Nice to chat with you. Bye~\n'); + + await this.wait(1000); + + close(); + + break; + } + process.stdout.write('\n'); await this.addMessage(PromptRole.USER, userMessage); diff --git a/src/cli/extends/command.ts b/src/cli/extends/command.ts index 8f5db29..64acbb0 100644 --- a/src/cli/extends/command.ts +++ b/src/cli/extends/command.ts @@ -63,6 +63,12 @@ abstract class BaseCommand extends Command { this.log(bang + ' ' + text); } + + protected wait(timeout: number): Promise { + return new Promise((resolve) => { + setTimeout(resolve, timeout); + }); + } } export default BaseCommand; diff --git a/src/core/index.ts b/src/core/index.ts index c363b1a..ad18930 100644 --- a/src/core/index.ts +++ b/src/core/index.ts @@ -35,7 +35,7 @@ export class AIGC { const platform = getPlatform(this.platform); const prompt = ChatPromptTemplate.fromMessages([ SystemMessagePromptTemplate.fromTemplate( - 'You are a helpful assistant. Answer all questions to the best of your ability.' + 'You are a helpful assistant. Answer all questions to the best of your ability.\nIf the user wants to end the conversation or exit the current chat command, directly reply to the user "$command:exit$"' ), new MessagesPlaceholder('messages'), HumanMessagePromptTemplate.fromTemplate('{question}')