@shun-js/aibaiban-server 0.5.0 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shun-js/aibaiban-server",
3
- "version": "0.5.0",
3
+ "version": "0.5.1",
4
4
  "description": "aibaiban.com server",
5
5
  "keywords": [
6
6
  "ai aibaiban"
@@ -44,5 +44,5 @@
44
44
  "access": "public",
45
45
  "registry": "https://registry.npmjs.org/"
46
46
  },
47
- "gitHead": "5d962a5890ad35e4399adbf9359ae36fb096046f"
47
+ "gitHead": "2284cb85d3534a7c0e78e717db78bafeb1f67703"
48
48
  }
@@ -9,12 +9,4 @@ module.exports = (app) => {
9
9
  app.get('/', (req, res) => {
10
10
  service.index(req, res);
11
11
  });
12
-
13
- // chat
14
- app.post('/chat', (req, res) => {
15
- service.chat(req, res);
16
- });
17
- app.post('/chat-streaming', (req, res) => {
18
- service.chatWithStreaming(req, res);
19
- });
20
12
  };
@@ -0,0 +1,17 @@
1
+ // service
2
+ const service = require('../service/LLMService.js');
3
+
4
+ /**
5
+ * controller
6
+ */
7
+ module.exports = (app) => {
8
+ // intent
9
+ app.post('/intent', (req, res) => {
10
+ service.intent(req, res);
11
+ });
12
+
13
+ //
14
+ app.post('/chat-streaming', (req, res) => {
15
+ service.chatWithStreaming(req, res);
16
+ });
17
+ };
@@ -1,9 +1,3 @@
1
- // llm
2
- const { llmParseIntent } = require('../util/llm');
3
-
4
- // util
5
- const { chatFeishuMsg, chatResFeishuMsg, errorFeishuMsg } = require('../util/feishu.js');
6
-
7
1
  /**
8
2
  * index
9
3
  * @param {*} req
@@ -20,88 +14,3 @@ exports.index = async (req, res) => {
20
14
  // render
21
15
  res.render(pagePath, {}, true);
22
16
  };
23
-
24
- /**
25
- * chat
26
- * @param {*} req
27
- * @param {*} res
28
- * @returns
29
- */
30
- exports.chat = async (req, res) => {
31
- const methodName = 'chat';
32
-
33
- // check
34
- if (!req.body.userPrompt) {
35
- const msg = 'need userPrompt';
36
- req.logger.error(methodName, msg);
37
- res.jsonFail(msg);
38
- return;
39
- }
40
-
41
- // const
42
- const userPrompt = decodeURIComponent(req.body.userPrompt);
43
- req.logger.info(methodName, 'userPrompt', userPrompt);
44
- chatFeishuMsg(req);
45
-
46
- // go
47
- try {
48
- const llmParseIntentRes = await llmParseIntent(userPrompt);
49
- const llmParseIntentObj = JSON.parse(llmParseIntentRes);
50
- req.logger.info(methodName, 'llmParseIntentObj', llmParseIntentObj);
51
-
52
- // r
53
- chatResFeishuMsg(req, JSON.stringify(llmParseIntentObj));
54
- res.jsonSuccess('success', llmParseIntentObj);
55
- } catch (error) {
56
- const msg = 'chat error';
57
- errorFeishuMsg(req, msg);
58
- req.logger.error(methodName, msg, error);
59
- res.jsonFail(msg);
60
- }
61
- };
62
-
63
- /**
64
- * chatWithStreaming
65
- * @param {*} req
66
- * @param {*} res
67
- * @returns
68
- */
69
- exports.chatWithStreaming = async () => {
70
- // const methodName = 'chatWithStreaming';
71
- // // check
72
- // if (!req.body.userPrompt) {
73
- // const msg = 'need userPrompt';
74
- // req.logger.error(methodName, msg);
75
- // res.jsonFail(msg);
76
- // return;
77
- // }
78
- // // const
79
- // const userPrompt = decodeURIComponent(req.body.userPrompt);
80
- // req.logger.info(methodName, 'userPrompt', userPrompt);
81
- // chatFeishuMsg(req);
82
- // // go
83
- // const userPrompts = [{ text: userPrompt }];
84
- // chatWithStreaming(userPrompts, {
85
- // beginCallback: () => {
86
- // req.logger.info(methodName, 'beginCallback');
87
- // res.streamingStart();
88
- // },
89
- // firstContentCallback: () => {
90
- // req.logger.info(methodName, 'firstContentCallback');
91
- // // res.streaming('First chunk received!');
92
- // },
93
- // contentCallback: (content) => {
94
- // req.logger.info(methodName, 'contentCallback', content);
95
- // res.streaming(content);
96
- // },
97
- // endCallback: () => {
98
- // req.logger.info(methodName, 'endCallback');
99
- // res.streamingEnd();
100
- // },
101
- // errorCallback: (error) => {
102
- // errorFeishuMsg(req, 'errorCallback');
103
- // req.logger.info(methodName, 'errorCallback', error);
104
- // res.streamingEnd();
105
- // },
106
- // });
107
- };
@@ -0,0 +1,90 @@
1
+ // llm
2
+ const { llmParseIntent } = require('../util/llm.js');
3
+
4
+ // util
5
+ const { chatFeishuMsg, chatResFeishuMsg, errorFeishuMsg } = require('../util/feishu.js');
6
+
7
+ /**
8
+ * intent
9
+ * @param {*} req
10
+ * @param {*} res
11
+ * @returns
12
+ */
13
+ exports.intent = async (req, res) => {
14
+ const methodName = 'intent';
15
+
16
+ // check
17
+ if (!req.body.userPrompt) {
18
+ const msg = 'need userPrompt';
19
+ req.logger.error(methodName, msg);
20
+ res.jsonFail(msg);
21
+ return;
22
+ }
23
+
24
+ // const
25
+ const userPrompt = decodeURIComponent(req.body.userPrompt);
26
+ req.logger.info(methodName, 'userPrompt', userPrompt);
27
+ chatFeishuMsg(req);
28
+
29
+ // go
30
+ try {
31
+ const llmParseIntentRes = await llmParseIntent(userPrompt);
32
+ const llmParseIntentObj = JSON.parse(llmParseIntentRes);
33
+ req.logger.info(methodName, 'llmParseIntentObj', llmParseIntentObj);
34
+
35
+ // r
36
+ chatResFeishuMsg(req, JSON.stringify(llmParseIntentObj));
37
+ res.jsonSuccess('success', llmParseIntentObj);
38
+ } catch (error) {
39
+ const msg = 'parse intent error';
40
+ errorFeishuMsg(req, msg);
41
+ req.logger.error(methodName, msg, error);
42
+ res.jsonFail(msg);
43
+ }
44
+ };
45
+
46
+ /**
47
+ * chatWithStreaming
48
+ * @param {*} req
49
+ * @param {*} res
50
+ * @returns
51
+ */
52
+ exports.chatWithStreaming = async () => {
53
+ // const methodName = 'chatWithStreaming';
54
+ // // check
55
+ // if (!req.body.userPrompt) {
56
+ // const msg = 'need userPrompt';
57
+ // req.logger.error(methodName, msg);
58
+ // res.jsonFail(msg);
59
+ // return;
60
+ // }
61
+ // // const
62
+ // const userPrompt = decodeURIComponent(req.body.userPrompt);
63
+ // req.logger.info(methodName, 'userPrompt', userPrompt);
64
+ // chatFeishuMsg(req);
65
+ // // go
66
+ // const userPrompts = [{ text: userPrompt }];
67
+ // chatWithStreaming(userPrompts, {
68
+ // beginCallback: () => {
69
+ // req.logger.info(methodName, 'beginCallback');
70
+ // res.streamingStart();
71
+ // },
72
+ // firstContentCallback: () => {
73
+ // req.logger.info(methodName, 'firstContentCallback');
74
+ // // res.streaming('First chunk received!');
75
+ // },
76
+ // contentCallback: (content) => {
77
+ // req.logger.info(methodName, 'contentCallback', content);
78
+ // res.streaming(content);
79
+ // },
80
+ // endCallback: () => {
81
+ // req.logger.info(methodName, 'endCallback');
82
+ // res.streamingEnd();
83
+ // },
84
+ // errorCallback: (error) => {
85
+ // errorFeishuMsg(req, 'errorCallback');
86
+ // req.logger.info(methodName, 'errorCallback', error);
87
+ // res.streamingEnd();
88
+ // },
89
+ // });
90
+ };
@@ -15,58 +15,62 @@ const gemini = GeminiVertex({
15
15
  });
16
16
 
17
17
  // const
18
- let cachedSystemPrompt = null;
19
- const jsonSchema = (() => {
20
- return zodToJsonSchema(
21
- z.object({
22
- intent: z.enum(['DRAW', 'REJECT']).describe('用户意图:DRAW(绘图)或 REJECT(非绘图)'),
23
- reason: z.string().describe('判断理由(一句话说明)'),
24
- }),
25
- );
26
- })();
18
+ const chatConfig = {
19
+ responseMimeType: 'application/json',
20
+ temperature: 0.2,
21
+ topP: 0.9,
22
+ topK: 40,
23
+ maxOutputTokens: 8192,
24
+ thinkingConfig: {
25
+ thinkingBudget: 0,
26
+ includeThoughts: false,
27
+ },
28
+ };
29
+ const safetySettings = [
30
+ {
31
+ category: 'HARM_CATEGORY_HATE_SPEECH',
32
+ threshold: 'BLOCK_ONLY_HIGH',
33
+ },
34
+ {
35
+ category: 'HARM_CATEGORY_DANGEROUS_CONTENT',
36
+ threshold: 'BLOCK_ONLY_HIGH',
37
+ },
38
+ {
39
+ category: 'HARM_CATEGORY_SEXUALLY_EXPLICIT',
40
+ threshold: 'BLOCK_ONLY_HIGH',
41
+ },
42
+ {
43
+ category: 'HARM_CATEGORY_HARASSMENT',
44
+ threshold: 'BLOCK_ONLY_HIGH',
45
+ },
46
+ ];
27
47
 
28
48
  /**
29
49
  * llmParseIntent
30
50
  * @param {*} userPrompts
31
51
  */
52
+ let intentSystemPrompt = null;
32
53
  exports.llmParseIntent = async (userPrompts) => {
33
- // cache
34
- if (!cachedSystemPrompt) cachedSystemPrompt = await readFile(path.resolve(__dirname, './prompt-intent.md'));
54
+ // intent system prompt
55
+ if (!intentSystemPrompt) {
56
+ intentSystemPrompt = await readFile(path.resolve(__dirname, './prompt-intent.md'));
57
+ console.log('read md');
58
+ }
59
+
60
+ // chat config
61
+ chatConfig.responseJsonSchema = zodToJsonSchema(
62
+ z.object({
63
+ intent: z.enum(['DRAW', 'REJECT']).describe('用户意图:DRAW(绘图)或 REJECT(非绘图)'),
64
+ reason: z.string().describe('判断理由(一句话说明)'),
65
+ }),
66
+ );
35
67
 
36
68
  // chat options
37
69
  const chatOptions = {
38
70
  contents: userPrompts,
39
- systemInstruction: cachedSystemPrompt,
40
- config: {
41
- responseMimeType: 'application/json',
42
- responseJsonSchema: jsonSchema,
43
- temperature: 0.2,
44
- topP: 0.9,
45
- topK: 40,
46
- maxOutputTokens: 8192,
47
- thinkingConfig: {
48
- thinkingBudget: 0,
49
- includeThoughts: false,
50
- },
51
- safetySettings: [
52
- {
53
- category: 'HARM_CATEGORY_HATE_SPEECH',
54
- threshold: 'BLOCK_ONLY_HIGH',
55
- },
56
- {
57
- category: 'HARM_CATEGORY_DANGEROUS_CONTENT',
58
- threshold: 'BLOCK_ONLY_HIGH',
59
- },
60
- {
61
- category: 'HARM_CATEGORY_SEXUALLY_EXPLICIT',
62
- threshold: 'BLOCK_ONLY_HIGH',
63
- },
64
- {
65
- category: 'HARM_CATEGORY_HARASSMENT',
66
- threshold: 'BLOCK_ONLY_HIGH',
67
- },
68
- ],
69
- },
71
+ systemInstruction: intentSystemPrompt,
72
+ config: chatConfig,
73
+ safetySettings: safetySettings,
70
74
  };
71
75
 
72
76
  // go
package/views/index.html CHANGED
@@ -56,7 +56,7 @@
56
56
  <script
57
57
  type="module"
58
58
  crossorigin
59
- src="https://static-small.vincentqiao.com/aibaiban/static/index-DLP1j0ul.js"
59
+ src="https://static-small.vincentqiao.com/aibaiban/static/index-DX_c8Rs5.js"
60
60
  ></script>
61
61
  <link rel="stylesheet" crossorigin href="https://static-small.vincentqiao.com/aibaiban/static/index-BN_GcTDx.css" />
62
62
  </head>