@lobehub/chat 0.32.0 → 0.33.0

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/CHANGELOG.md CHANGED
@@ -2,6 +2,31 @@
2
2
 
3
3
  # Changelog
4
4
 
5
+ ## [Version 0.33.0](https://github.com/lobehub/lobe-chat/compare/v0.32.0...v0.33.0)
6
+
7
+ <sup>Released on **2023-07-30**</sup>
8
+
9
+ #### ✨ Features
10
+
11
+ - **misc**: 支持输入模板预处理.
12
+
13
+ <br/>
14
+
15
+ <details>
16
+ <summary><kbd>Improvements and Fixes</kbd></summary>
17
+
18
+ #### What's improved
19
+
20
+ - **misc**: 支持输入模板预处理 ([84082c1](https://github.com/lobehub/lobe-chat/commit/84082c1))
21
+
22
+ </details>
23
+
24
+ <div align="right">
25
+
26
+ [![](https://img.shields.io/badge/-BACK_TO_TOP-151515?style=flat-square)](#readme-top)
27
+
28
+ </div>
29
+
5
30
  ## [Version 0.32.0](https://github.com/lobehub/lobe-chat/compare/v0.31.0...v0.32.0)
6
31
 
7
32
  <sup>Released on **2023-07-30**</sup>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lobehub/chat",
3
- "version": "0.32.0",
3
+ "version": "0.33.0",
4
4
  "description": "Lobe Chat is an open-source chatbot client using LangChain, Typescript and Next.js",
5
5
  "keywords": [
6
6
  "chatbot",
@@ -1,3 +1,4 @@
1
+ import { template } from 'lodash-es';
1
2
  import { StateCreator } from 'zustand/vanilla';
2
3
 
3
4
  import { LOADING_FLAT } from '@/const/message';
@@ -113,8 +114,41 @@ export const chatMessage: StateCreator<
113
114
  set({ chatLoadingId: assistantId });
114
115
  const config = agentSelectors.currentAgentConfigSafe(get());
115
116
 
117
+ const compiler = template(config.inputTemplate, { interpolate: /{{([\S\s]+?)}}/g });
118
+
119
+ // 对 message 做统一预处理
120
+
121
+ // 1. 替换 inputMessage 模板
122
+ const postMessages = !config.inputTemplate
123
+ ? messages
124
+ : messages.map((m) => {
125
+ if (m.role === 'user') {
126
+ try {
127
+ return { ...m, content: compiler({ text: m.content }) };
128
+ } catch (error) {
129
+ console.error(error);
130
+
131
+ return m;
132
+ }
133
+ }
134
+ return m;
135
+ });
136
+
137
+ // 2. TODO 按参数设定截断长度
138
+
139
+ // 3. 添加 systemRole
140
+ const { systemRole } = agentSelectors.currentAgentConfigSafe(get());
141
+ if (systemRole) {
142
+ postMessages.unshift({ content: systemRole, role: 'system' } as ChatMessage);
143
+ }
144
+
116
145
  const fetcher = () =>
117
- fetchChatModel({ messages, model: config.model, ...config.params, plugins: config.plugins });
146
+ fetchChatModel({
147
+ messages: postMessages,
148
+ model: config.model,
149
+ ...config.params,
150
+ plugins: config.plugins,
151
+ });
118
152
 
119
153
  let output = '';
120
154
  let isFunctionCall = false;
@@ -154,13 +188,9 @@ export const chatMessage: StateCreator<
154
188
  realFetchAIResponse: async (messages, userMessageId) => {
155
189
  const { dispatchMessage, generateMessage, triggerFunctionCall, activeTopicId } = get();
156
190
 
157
- // 添加 systemRole
158
- const { systemRole, model } = agentSelectors.currentAgentConfigSafe(get());
159
- if (systemRole) {
160
- messages.unshift({ content: systemRole, role: 'system' } as ChatMessage);
161
- }
191
+ const { model } = agentSelectors.currentAgentConfigSafe(get());
162
192
 
163
- // 再添加一个空的信息用于放置 ai 响应,注意顺序不能反
193
+ // 添加一个空的信息用于放置 ai 响应,注意顺序不能反
164
194
  // 因为如果顺序反了,messages 中将包含新增的 ai message
165
195
  const mid = nanoid();
166
196
 
@@ -283,10 +313,10 @@ export const chatMessage: StateCreator<
283
313
 
284
314
  const mid = nanoid();
285
315
 
286
- dispatchMessage({ id: mid, message: LOADING_FLAT, role: 'assistant', type: 'addMessage' });
287
-
288
316
  const chats = chatSelectors.currentChats(get());
289
317
 
318
+ dispatchMessage({ id: mid, message: LOADING_FLAT, role: 'assistant', type: 'addMessage' });
319
+
290
320
  await generateMessage(chats, mid);
291
321
  },
292
322
  });