Skip to content

Commit

Permalink
feat: 支持通过对话退出
Browse files Browse the repository at this point in the history
  • Loading branch information
crazyurus committed May 24, 2024
1 parent 17c1c0f commit df19c5c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
22 changes: 14 additions & 8 deletions src/cli/commands/chat.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -59,27 +60,32 @@ 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);
stream.pipe(process.stdout);

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);
Expand Down
6 changes: 6 additions & 0 deletions src/cli/extends/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ abstract class BaseCommand extends Command {

this.log(bang + ' ' + text);
}

protected wait(timeout: number): Promise<void> {
return new Promise((resolve) => {
setTimeout(resolve, timeout);
});
}
}

export default BaseCommand;
2 changes: 1 addition & 1 deletion src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}')
Expand Down

0 comments on commit df19c5c

Please sign in to comment.