Skip to content

Commit

Permalink
release 0.1.0 (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengxs2018 authored Dec 18, 2023
1 parent 30c54ed commit e5b65ed
Show file tree
Hide file tree
Showing 24 changed files with 281 additions and 98 deletions.
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ $ pnpm install @zhengxs/wechaty-plugin-assistant
在这里获取你的 [accessToken](https://aistudio.baidu.com/index/accessToken) 值。

```ts
import { ChatERNIEBot, createAssistant } from '@zhengxs/wechaty-plugin-assistant';
import {
ChatERNIEBot,
createAssistant,
} from '@zhengxs/wechaty-plugin-assistant';
import { WechatyBuilder } from 'wechaty';
import { QRCodeTerminal } from 'wechaty-plugin-contrib';

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

```ts
import { ChatERNIEBot, ChatQWen, createAssistant, MultiChatModelSwitch } from '@zhengxs/wechaty-plugin-assistant';
import {
ChatERNIEBot,
ChatQWen,
createAssistant,
MultiChatModelSwitch,
} from '@zhengxs/wechaty-plugin-assistant';

const assistant = createAssistant({
llm: new MultiChatModelSwitch([
Expand Down Expand Up @@ -151,7 +159,7 @@ const assistant = createAssistant({
### 指令

> [!NOTE]
> 后续将去除内置指令,改为按需手动注册。
> 不再内置指令,改为按需手动注册。
| 名称 | 描述 | 状态 |
| -------- | ----------------------------------------------------------------------------------------------------------------------------- | ----- |
Expand Down
39 changes: 39 additions & 0 deletions demo/help.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import {
kfc,
ChatType,
createAssistant,
createMockTextMessage,
MultiChatModelSwitch,
} from '../src';

const assistant = createAssistant({
llm: new MultiChatModelSwitch([
{
name: 'yiyan',
human_name: '文心一言',
input_type: [ChatType.Text],
summary: '文心一言 [百度]',
call(ctx) {
console.log('[文心一言] 收到', ctx.message.text());
},
},
{
name: 'xinghuo',
human_name: '星火认知 [讯飞]',
input_type: [ChatType.Text],
call(ctx) {
console.log('[讯飞星火] 收到', ctx.message.text());
},
},
]),
});

assistant.command.addCommand(kfc)

async function main() {
assistant.run();

await assistant.handler(createMockTextMessage('帮助'));
}

main();
34 changes: 34 additions & 0 deletions demo/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {
ChatERNIEBot,
ChatQWen,
MultiChatModelSwitch,
createAssistant,
} from '../src';
import { WechatyBuilder } from 'wechaty';
import { EventLogger, QRCodeTerminal } from 'wechaty-plugin-contrib';

const ernie = new ChatERNIEBot({
token: process.env.EB_ACCESS_TOKEN, // 飞桨平台的 token
});

const qwen = new ChatQWen({
apiKey: process.env.QWEN_API_KEY,
})

const assistant = createAssistant({
llm: new MultiChatModelSwitch([ernie, qwen])
});

const bot = WechatyBuilder.build({
name: 'demo',
puppet: 'wechaty-puppet-wechat4u',
puppetOptions: { uos: true },
});

bot.use(QRCodeTerminal({ small: true }));
bot.use(EventLogger(['login', 'logout', 'error', 'friendship', 'room-invite']));

// 作为插件使用
bot.use(assistant.callback());

bot.start();
7 changes: 7 additions & 0 deletions demo/llm/multi-llms.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { codeBlock } from 'common-tags';
import {
ChatType,
createAssistant,
Expand All @@ -19,6 +20,12 @@ const assistant = createAssistant({
name: 'xinghuo',
human_name: '讯飞星火',
input_type: [ChatType.Text],
greeting: codeBlock`
您好,我是讯飞星火认知大模型!
能够学习和理解人类的语言,进行多轮对话。
回答问题,高效便捷地帮助人们获取信息、知识和灵感。`,
call(ctx) {
console.log('[讯飞星火] 收到', ctx.message.text());
},
Expand Down
41 changes: 21 additions & 20 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zhengxs/wechaty-plugin-assistant",
"version": "0.1.0-alpha.27",
"version": "0.1.0",
"main": "./dist/index.cjs",
"module": "./dist/index.mjs",
"description": "基于 wechaty 的聊天助手插件",
Expand Down Expand Up @@ -41,10 +41,11 @@
],
"scripts": {
"prepare": "husky install",
"dev": "pnpm task --watch ./demo/multi-llms.ts",
"preinstall": "npx only-allow pnpm",
"dev": "pnpm task ./demo/index.ts",
"task": "node --no-warnings -r tsconfig-paths/register -r dotenv/config -r jiti/register",
"inspect": "node --no-warnings --inspect -r tsconfig-paths/register -r dotenv/config -r jiti/register",
"build": "trash dist dist-types && tsc --noEmit && pnpm lint && vite build",
"build": "trash dist dist-types && pnpm lint && vite build",
"lint": "eslint . --ext .ts,.cjs",
"lint:fix": "eslint . --ext .ts,.cjs --fix --fix-type [problem,suggestion]",
"fmt": "prettier --write \"**/*.{ts,json,,md}\" --log-level warn"
Expand All @@ -54,7 +55,7 @@
"wechaty": ">=1"
},
"dependencies": {
"@zhengxs/ai": "0.1.0-alpha.7",
"@zhengxs/ai": "0.5.0",
"@zhengxs/erniebot": "0.1.0-alpha.5",
"common-tags": "^1.8.2",
"eventemitter3": "^5.0.1",
Expand All @@ -64,35 +65,35 @@
"keyv": "^4.5.4",
"languagedetect": "^2.0.0",
"minimist": "^1.2.8",
"openai": "^4.20.1"
"openai": "^4.23.0"
},
"devDependencies": {
"@microsoft/api-extractor": "^7.38.3",
"@trivago/prettier-plugin-sort-imports": "^4.2.1",
"@microsoft/api-extractor": "^7.38.5",
"@trivago/prettier-plugin-sort-imports": "^4.3.0",
"@types/common-tags": "^1.8.4",
"@types/minimist": "^1.2.5",
"@types/node": "^20.9.0",
"@typescript-eslint/eslint-plugin": "^6.10.0",
"@typescript-eslint/parser": "^6.10.0",
"@types/node": "^20.10.5",
"@typescript-eslint/eslint-plugin": "^6.14.0",
"@typescript-eslint/parser": "^6.14.0",
"dotenv": "^16.3.1",
"eslint": "^8.53.0",
"eslint-config-prettier": "^9.0.0",
"eslint-define-config": "^1.24.1",
"eslint-plugin-import": "^2.29.0",
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.1.0",
"eslint-define-config": "^2.0.0",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-prettier": "^5.0.1",
"eslint-plugin-tsdoc": "^0.2.17",
"husky": "^8.0.3",
"jiti": "^1.21.0",
"lint-staged": "^15.0.2",
"lint-staged": "^15.2.0",
"mkdirp": "^3.0.1",
"prettier": "^3.0.3",
"prettier": "^3.1.1",
"trash-cli": "^5.0.0",
"tsconfig-paths": "^4.2.0",
"typescript": "~5.0.4",
"vite": "^4.5.0",
"typescript": "~5.3.3",
"vite": "^5.0.10",
"vite-plugin-checker": "^0.6.2",
"vite-plugin-dts": "^3.6.3",
"vite-plugin-externalize-deps": "^0.7.0",
"vite-plugin-dts": "^3.6.4",
"vite-plugin-externalize-deps": "^0.8.0",
"wechaty": "^1.20.2",
"wechaty-plugin-contrib": "^1.12.1"
},
Expand Down
8 changes: 1 addition & 7 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,4 @@ import hot from './hot';
import kfc from './kfc';
import moyu from './moyu';

export default {
moyu,
hot,
dict,
kfc,
deepl,
};
export { moyu, hot, dict, kfc, deepl };
16 changes: 9 additions & 7 deletions src/core/createAssistant.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { codeBlock } from 'common-tags';

import commands from '../commands';
import { Command } from '../integrations/commander';
import { deepl } from '../commands'
import { type Assistant, type AssistantConfig } from '../interfaces';
import { createAssistantHooks } from './createAssistantHooks';
import { createAssistantMonitor } from './createAssistantMonitor';
Expand All @@ -10,6 +10,8 @@ import { setupConfigAndLLM } from './setupConfigAndLLM';
import { wechatyMessageHandler } from './wechatyMessageHandler';
import { wechatyPluginCallback } from './wechatyPluginCallback';

import { keywords } from './keywords'

export function createAssistant(config: AssistantConfig) {
const options = resolveAssistantOptions(config);

Expand All @@ -27,6 +29,8 @@ export function createAssistant(config: AssistantConfig) {
chatbotUser: null,
wechaty: null,
command: program,
keywords,
llm,
handler: message => wechatyMessageHandler(assistant, message),
callback: () => {
return bot => void wechatyPluginCallback(assistant, bot);
Expand All @@ -38,7 +42,9 @@ export function createAssistant(config: AssistantConfig) {
ctx.reply(codeBlock`
⊶ 系统提示
${llm.human_name} 暂不支持处理此类消息!`);
${llm.human_name} 暂不支持处理此类消息!
-------------------
输入 "帮助" 获取更详细的使用说明。`);
}
},
run() {
Expand All @@ -50,11 +56,7 @@ export function createAssistant(config: AssistantConfig) {
},
};

program.addCommand(commands.deepl);
program.addCommand(commands.dict);
program.addCommand(commands.hot);
program.addCommand(commands.moyu);
program.addCommand(commands.kfc);
program.addCommand(deepl)

setupConfigAndLLM(options, assistant);

Expand Down
3 changes: 3 additions & 0 deletions src/core/createConversationContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export async function createConversationContext(

const {
monitor,
llm,
options: { debug, maintainers, cache },
} = assistant;

Expand Down Expand Up @@ -127,6 +128,8 @@ export async function createConversationContext(
}

const ctx: ConversationContext = {
assistant,
llm: llm,
conversationId,
conversationTitle,
talkerId,
Expand Down
8 changes: 8 additions & 0 deletions src/core/keywords.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Keywords } from '../interfaces'

export const keywords: Keywords = {
newConversation: ['新对话', '新聊天', '重新开始', '重新对话', '重新聊天', '重新对话', '重新开始对话', '重新开始聊天', '重新开始对话'],
stopConversation: ['停', '停止', '停止回复'],
help: ['帮助', '获取帮助'],
sourceCode: ['获取源码', '查看源码'],
}
29 changes: 29 additions & 0 deletions src/core/printHelp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { html } from 'common-tags';
import { MultiChatModelSwitch } from '../llms'
import { type ConversationContext } from '../interfaces';

export function printHelp(ctx: ConversationContext) {
const { llm } = ctx
const llms = llm instanceof MultiChatModelSwitch ? llm.llms : [llm];

const commands = Array.from(ctx.assistant.command.commands.values());

return ctx.reply(html`
⊶ 帮助信息
口令
☛ 重新开始
☛ 停止
☛ 查看模型
☛ 切换 <模型名称>
☛ 帮助
☛ 查看源码
指令
${commands.map(cmd => ` ☛ ${cmd.name}`)}
模型列表
${llms.map(m => ` ☛ ${m.summary || m.human_name}`)}
如恶意使用,将拉入黑名单,敬请谅解。`);
}
Loading

0 comments on commit e5b65ed

Please sign in to comment.