Skip to content

Commit e5b65ed

Browse files
authored
release 0.1.0 (#5)
1 parent 30c54ed commit e5b65ed

24 files changed

+281
-98
lines changed

README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,10 @@ $ pnpm install @zhengxs/wechaty-plugin-assistant
8686
在这里获取你的 [accessToken](https://aistudio.baidu.com/index/accessToken) 值。
8787

8888
```ts
89-
import { ChatERNIEBot, createAssistant } from '@zhengxs/wechaty-plugin-assistant';
89+
import {
90+
ChatERNIEBot,
91+
createAssistant,
92+
} from '@zhengxs/wechaty-plugin-assistant';
9093
import { WechatyBuilder } from 'wechaty';
9194
import { QRCodeTerminal } from 'wechaty-plugin-contrib';
9295

@@ -121,7 +124,12 @@ bot.start();
121124
同时接入多个大模型。
122125

123126
```ts
124-
import { ChatERNIEBot, ChatQWen, createAssistant, MultiChatModelSwitch } from '@zhengxs/wechaty-plugin-assistant';
127+
import {
128+
ChatERNIEBot,
129+
ChatQWen,
130+
createAssistant,
131+
MultiChatModelSwitch,
132+
} from '@zhengxs/wechaty-plugin-assistant';
125133

126134
const assistant = createAssistant({
127135
llm: new MultiChatModelSwitch([
@@ -151,7 +159,7 @@ const assistant = createAssistant({
151159
### 指令
152160

153161
> [!NOTE]
154-
> 后续将去除内置指令,改为按需手动注册。
162+
> 不再内置指令,改为按需手动注册。
155163
156164
| 名称 | 描述 | 状态 |
157165
| -------- | ----------------------------------------------------------------------------------------------------------------------------- | ----- |

demo/help.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import {
2+
kfc,
3+
ChatType,
4+
createAssistant,
5+
createMockTextMessage,
6+
MultiChatModelSwitch,
7+
} from '../src';
8+
9+
const assistant = createAssistant({
10+
llm: new MultiChatModelSwitch([
11+
{
12+
name: 'yiyan',
13+
human_name: '文心一言',
14+
input_type: [ChatType.Text],
15+
summary: '文心一言 [百度]',
16+
call(ctx) {
17+
console.log('[文心一言] 收到', ctx.message.text());
18+
},
19+
},
20+
{
21+
name: 'xinghuo',
22+
human_name: '星火认知 [讯飞]',
23+
input_type: [ChatType.Text],
24+
call(ctx) {
25+
console.log('[讯飞星火] 收到', ctx.message.text());
26+
},
27+
},
28+
]),
29+
});
30+
31+
assistant.command.addCommand(kfc)
32+
33+
async function main() {
34+
assistant.run();
35+
36+
await assistant.handler(createMockTextMessage('帮助'));
37+
}
38+
39+
main();

demo/index.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import {
2+
ChatERNIEBot,
3+
ChatQWen,
4+
MultiChatModelSwitch,
5+
createAssistant,
6+
} from '../src';
7+
import { WechatyBuilder } from 'wechaty';
8+
import { EventLogger, QRCodeTerminal } from 'wechaty-plugin-contrib';
9+
10+
const ernie = new ChatERNIEBot({
11+
token: process.env.EB_ACCESS_TOKEN, // 飞桨平台的 token
12+
});
13+
14+
const qwen = new ChatQWen({
15+
apiKey: process.env.QWEN_API_KEY,
16+
})
17+
18+
const assistant = createAssistant({
19+
llm: new MultiChatModelSwitch([ernie, qwen])
20+
});
21+
22+
const bot = WechatyBuilder.build({
23+
name: 'demo',
24+
puppet: 'wechaty-puppet-wechat4u',
25+
puppetOptions: { uos: true },
26+
});
27+
28+
bot.use(QRCodeTerminal({ small: true }));
29+
bot.use(EventLogger(['login', 'logout', 'error', 'friendship', 'room-invite']));
30+
31+
// 作为插件使用
32+
bot.use(assistant.callback());
33+
34+
bot.start();

demo/llm/multi-llms.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { codeBlock } from 'common-tags';
12
import {
23
ChatType,
34
createAssistant,
@@ -19,6 +20,12 @@ const assistant = createAssistant({
1920
name: 'xinghuo',
2021
human_name: '讯飞星火',
2122
input_type: [ChatType.Text],
23+
greeting: codeBlock`
24+
您好,我是讯飞星火认知大模型!
25+
26+
能够学习和理解人类的语言,进行多轮对话。
27+
28+
回答问题,高效便捷地帮助人们获取信息、知识和灵感。`,
2229
call(ctx) {
2330
console.log('[讯飞星火] 收到', ctx.message.text());
2431
},

package.json

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@zhengxs/wechaty-plugin-assistant",
3-
"version": "0.1.0-alpha.27",
3+
"version": "0.1.0",
44
"main": "./dist/index.cjs",
55
"module": "./dist/index.mjs",
66
"description": "基于 wechaty 的聊天助手插件",
@@ -41,10 +41,11 @@
4141
],
4242
"scripts": {
4343
"prepare": "husky install",
44-
"dev": "pnpm task --watch ./demo/multi-llms.ts",
44+
"preinstall": "npx only-allow pnpm",
45+
"dev": "pnpm task ./demo/index.ts",
4546
"task": "node --no-warnings -r tsconfig-paths/register -r dotenv/config -r jiti/register",
4647
"inspect": "node --no-warnings --inspect -r tsconfig-paths/register -r dotenv/config -r jiti/register",
47-
"build": "trash dist dist-types && tsc --noEmit && pnpm lint && vite build",
48+
"build": "trash dist dist-types && pnpm lint && vite build",
4849
"lint": "eslint . --ext .ts,.cjs",
4950
"lint:fix": "eslint . --ext .ts,.cjs --fix --fix-type [problem,suggestion]",
5051
"fmt": "prettier --write \"**/*.{ts,json,,md}\" --log-level warn"
@@ -54,7 +55,7 @@
5455
"wechaty": ">=1"
5556
},
5657
"dependencies": {
57-
"@zhengxs/ai": "0.1.0-alpha.7",
58+
"@zhengxs/ai": "0.5.0",
5859
"@zhengxs/erniebot": "0.1.0-alpha.5",
5960
"common-tags": "^1.8.2",
6061
"eventemitter3": "^5.0.1",
@@ -64,35 +65,35 @@
6465
"keyv": "^4.5.4",
6566
"languagedetect": "^2.0.0",
6667
"minimist": "^1.2.8",
67-
"openai": "^4.20.1"
68+
"openai": "^4.23.0"
6869
},
6970
"devDependencies": {
70-
"@microsoft/api-extractor": "^7.38.3",
71-
"@trivago/prettier-plugin-sort-imports": "^4.2.1",
71+
"@microsoft/api-extractor": "^7.38.5",
72+
"@trivago/prettier-plugin-sort-imports": "^4.3.0",
7273
"@types/common-tags": "^1.8.4",
7374
"@types/minimist": "^1.2.5",
74-
"@types/node": "^20.9.0",
75-
"@typescript-eslint/eslint-plugin": "^6.10.0",
76-
"@typescript-eslint/parser": "^6.10.0",
75+
"@types/node": "^20.10.5",
76+
"@typescript-eslint/eslint-plugin": "^6.14.0",
77+
"@typescript-eslint/parser": "^6.14.0",
7778
"dotenv": "^16.3.1",
78-
"eslint": "^8.53.0",
79-
"eslint-config-prettier": "^9.0.0",
80-
"eslint-define-config": "^1.24.1",
81-
"eslint-plugin-import": "^2.29.0",
79+
"eslint": "^8.56.0",
80+
"eslint-config-prettier": "^9.1.0",
81+
"eslint-define-config": "^2.0.0",
82+
"eslint-plugin-import": "^2.29.1",
8283
"eslint-plugin-prettier": "^5.0.1",
8384
"eslint-plugin-tsdoc": "^0.2.17",
8485
"husky": "^8.0.3",
8586
"jiti": "^1.21.0",
86-
"lint-staged": "^15.0.2",
87+
"lint-staged": "^15.2.0",
8788
"mkdirp": "^3.0.1",
88-
"prettier": "^3.0.3",
89+
"prettier": "^3.1.1",
8990
"trash-cli": "^5.0.0",
9091
"tsconfig-paths": "^4.2.0",
91-
"typescript": "~5.0.4",
92-
"vite": "^4.5.0",
92+
"typescript": "~5.3.3",
93+
"vite": "^5.0.10",
9394
"vite-plugin-checker": "^0.6.2",
94-
"vite-plugin-dts": "^3.6.3",
95-
"vite-plugin-externalize-deps": "^0.7.0",
95+
"vite-plugin-dts": "^3.6.4",
96+
"vite-plugin-externalize-deps": "^0.8.0",
9697
"wechaty": "^1.20.2",
9798
"wechaty-plugin-contrib": "^1.12.1"
9899
},

src/commands/index.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,4 @@ import hot from './hot';
44
import kfc from './kfc';
55
import moyu from './moyu';
66

7-
export default {
8-
moyu,
9-
hot,
10-
dict,
11-
kfc,
12-
deepl,
13-
};
7+
export { moyu, hot, dict, kfc, deepl };

src/core/createAssistant.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { codeBlock } from 'common-tags';
22

3-
import commands from '../commands';
43
import { Command } from '../integrations/commander';
4+
import { deepl } from '../commands'
55
import { type Assistant, type AssistantConfig } from '../interfaces';
66
import { createAssistantHooks } from './createAssistantHooks';
77
import { createAssistantMonitor } from './createAssistantMonitor';
@@ -10,6 +10,8 @@ import { setupConfigAndLLM } from './setupConfigAndLLM';
1010
import { wechatyMessageHandler } from './wechatyMessageHandler';
1111
import { wechatyPluginCallback } from './wechatyPluginCallback';
1212

13+
import { keywords } from './keywords'
14+
1315
export function createAssistant(config: AssistantConfig) {
1416
const options = resolveAssistantOptions(config);
1517

@@ -27,6 +29,8 @@ export function createAssistant(config: AssistantConfig) {
2729
chatbotUser: null,
2830
wechaty: null,
2931
command: program,
32+
keywords,
33+
llm,
3034
handler: message => wechatyMessageHandler(assistant, message),
3135
callback: () => {
3236
return bot => void wechatyPluginCallback(assistant, bot);
@@ -38,7 +42,9 @@ export function createAssistant(config: AssistantConfig) {
3842
ctx.reply(codeBlock`
3943
⊶ 系统提示
4044
41-
${llm.human_name} 暂不支持处理此类消息!`);
45+
${llm.human_name} 暂不支持处理此类消息!
46+
-------------------
47+
输入 "帮助" 获取更详细的使用说明。`);
4248
}
4349
},
4450
run() {
@@ -50,11 +56,7 @@ export function createAssistant(config: AssistantConfig) {
5056
},
5157
};
5258

53-
program.addCommand(commands.deepl);
54-
program.addCommand(commands.dict);
55-
program.addCommand(commands.hot);
56-
program.addCommand(commands.moyu);
57-
program.addCommand(commands.kfc);
59+
program.addCommand(deepl)
5860

5961
setupConfigAndLLM(options, assistant);
6062

src/core/createConversationContext.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export async function createConversationContext(
2727

2828
const {
2929
monitor,
30+
llm,
3031
options: { debug, maintainers, cache },
3132
} = assistant;
3233

@@ -127,6 +128,8 @@ export async function createConversationContext(
127128
}
128129

129130
const ctx: ConversationContext = {
131+
assistant,
132+
llm: llm,
130133
conversationId,
131134
conversationTitle,
132135
talkerId,

src/core/keywords.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { Keywords } from '../interfaces'
2+
3+
export const keywords: Keywords = {
4+
newConversation: ['新对话', '新聊天', '重新开始', '重新对话', '重新聊天', '重新对话', '重新开始对话', '重新开始聊天', '重新开始对话'],
5+
stopConversation: ['停', '停止', '停止回复'],
6+
help: ['帮助', '获取帮助'],
7+
sourceCode: ['获取源码', '查看源码'],
8+
}

src/core/printHelp.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { html } from 'common-tags';
2+
import { MultiChatModelSwitch } from '../llms'
3+
import { type ConversationContext } from '../interfaces';
4+
5+
export function printHelp(ctx: ConversationContext) {
6+
const { llm } = ctx
7+
const llms = llm instanceof MultiChatModelSwitch ? llm.llms : [llm];
8+
9+
const commands = Array.from(ctx.assistant.command.commands.values());
10+
11+
return ctx.reply(html`
12+
⊶ 帮助信息
13+
14+
口令
15+
☛ 重新开始
16+
☛ 停止
17+
☛ 查看模型
18+
☛ 切换 <模型名称>
19+
☛ 帮助
20+
☛ 查看源码
21+
22+
指令
23+
${commands.map(cmd => ` ☛ ${cmd.name}`)}
24+
25+
模型列表
26+
${llms.map(m => ` ☛ ${m.summary || m.human_name}`)}
27+
28+
如恶意使用,将拉入黑名单,敬请谅解。`);
29+
}

0 commit comments

Comments
 (0)