-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
143 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,8 +13,8 @@ jobs: | |
registry-url: 'https://registry.npmjs.org' | ||
- name: Install dependencies | ||
run: | | ||
npm install [email protected] -g | ||
pnpm install --frozen-lockfile | ||
corepack install | ||
corepack pnpm install --frozen-lockfile | ||
- name: Build | ||
run: npm run build | ||
- name: Publish | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { AIMessage, HumanMessage } from '@langchain/core/messages'; | ||
import chalk from 'chalk'; | ||
import { ChatMessageHistory } from 'langchain/stores/message/in_memory'; | ||
import readline from 'node:readline'; | ||
|
||
import type { Platform } from '../../platform'; | ||
|
||
import { AIGC } from '../../core'; | ||
import BaseCommand from '../extends/command'; | ||
|
||
enum PromptRole { | ||
AI = 'ai', | ||
USER = 'user' | ||
} | ||
|
||
const promptMessageMap = { | ||
[PromptRole.AI]: AIMessage, | ||
[PromptRole.USER]: HumanMessage | ||
}; | ||
const promptRoleDisplayMap = { | ||
[PromptRole.AI]: { | ||
color: 'yellow', | ||
name: 'AI' | ||
}, | ||
[PromptRole.USER]: { | ||
color: 'green', | ||
name: 'You' | ||
} | ||
} as const; | ||
|
||
const reader = readline.createInterface({ | ||
input: process.stdin, | ||
output: process.stdout | ||
}); | ||
|
||
class ChatCommand extends BaseCommand { | ||
static args = {}; | ||
|
||
static description = 'Chat with the LLM'; | ||
|
||
static examples = []; | ||
|
||
static flags = {}; | ||
|
||
private lastMessage = 'How can I help you today?'; | ||
|
||
private messages = new ChatMessageHistory(); | ||
|
||
async run(): Promise<void> { | ||
const config = await this.configManager.getAll(); | ||
|
||
if (Object.keys(config).length > 0) { | ||
const detector = new AIGC({ | ||
apiKey: config.apiKey, | ||
platform: config.platform as unknown as Platform | ||
}); | ||
const userDisplay = this.getDisplayContent(PromptRole.USER); | ||
|
||
// eslint-disable-next-line no-constant-condition | ||
while (true) { | ||
const aiMessage = await this.addMessage(PromptRole.AI, this.lastMessage); | ||
const userMessage = await this.getUserMessage(aiMessage + `\n${userDisplay}`); | ||
const answer = await detector.chat(userMessage, await this.messages.getMessages()); | ||
|
||
await this.addMessage(PromptRole.USER, userMessage); | ||
this.lastMessage = answer; | ||
} | ||
} else { | ||
this.showHelp(); | ||
} | ||
} | ||
|
||
private async addMessage(role: PromptRole, content: string): Promise<string> { | ||
const Message = promptMessageMap[role]; | ||
|
||
await this.messages.addMessage(new Message(content)); | ||
|
||
return this.getDisplayContent(role) + content; | ||
} | ||
|
||
private getDisplayContent(role: PromptRole): string { | ||
const roleDisplay = promptRoleDisplayMap[role]; | ||
|
||
return chalk[roleDisplay.color](`[${roleDisplay.name}] `); | ||
} | ||
|
||
private getUserMessage(aiMessage: string): Promise<string> { | ||
return new Promise<string>((resolve) => { | ||
reader.question(aiMessage, resolve); | ||
}); | ||
} | ||
} | ||
|
||
export default ChatCommand; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters