my-ai-chat-framework 1.1.0 → 2.0.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/.env +15 -0
- package/README.md +69 -454
- package/dist/my-ai-chat-framework.browser.es.js +426 -2910
- package/dist/my-ai-chat-framework.browser.es.js.map +1 -1
- package/dist/my-ai-chat-framework.browser.umd.js +442 -2917
- package/dist/my-ai-chat-framework.browser.umd.js.map +1 -1
- package/dist/my-ai-chat-framework.node.cjs.js +435 -1661
- package/dist/my-ai-chat-framework.node.cjs.js.map +1 -1
- package/package.json +15 -32
- package/src/adapters/openai.js +130 -0
- package/src/core/ChatService.js +110 -0
- package/src/core/EventEmitter.js +32 -126
- package/src/core/MessageStore.js +73 -0
- package/src/index.js +14 -539
- package/src/plugins/tool-calling.js +119 -0
- package/test.js +107 -0
- package/README_ZH.md +0 -545
- package/src/config/defaults.js +0 -25
- package/src/core/ApiClient.js +0 -316
- package/src/core/ConfigManager.js +0 -173
- package/src/core/MessageFormatter.js +0 -136
- package/src/core/Messages.js +0 -589
- package/src/core/RequestBuilder.js +0 -99
- package/src/core/ToolManager.js +0 -175
- package/src/utils/index.js +0 -21
package/.env
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# AI Chat Framework 环境变量配置
|
|
2
|
+
|
|
3
|
+
# DeepSeek API 配置
|
|
4
|
+
DEEPSEEK_API_KEY=sk-3b396e7fd64b463390d4ff033c6e7e0b
|
|
5
|
+
DEEPSEEK_API_URL=https://api.deepseek.com/v1/chat/completions
|
|
6
|
+
|
|
7
|
+
# 测试配置
|
|
8
|
+
TEST_API_KEY=sk-3b396e7fd64b463390d4ff033c6e7e0b
|
|
9
|
+
TEST_MODEL=deepseek-chat
|
|
10
|
+
TEST_TEMPERATURE=0.7
|
|
11
|
+
TEST_MAX_TOKENS=2000
|
|
12
|
+
|
|
13
|
+
# 调试配置
|
|
14
|
+
DEBUG=false
|
|
15
|
+
LOG_LEVEL=info
|
package/README.md
CHANGED
|
@@ -1,492 +1,107 @@
|
|
|
1
|
-
# 🤖 My AI Chat Framework / 我的AI聊天框架
|
|
2
1
|
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
**⚠️ Note**: This project is created for **learning purposes** and is **AI‑generated**. It is not intended for production use. No backward compatibility is guaranteed. Use at your own risk.
|
|
3
|
+
|
|
4
|
+
# 🤖 My AI Chat Framework
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
A lightweight, modular AI chat framework with plugin system, unified message format, and tool calling support.
|
|
7
7
|
|
|
8
|
-
## ✨
|
|
9
|
-
- **🔧 工具调用系统** / **Tool Calling System**: 完整的OpenAI兼容工具调用支持,包括工具注册、执行和错误处理
|
|
10
|
-
- **🌊 流式传输** / **Streaming**: 实时流式传输,支持工具调用和进度回调
|
|
11
|
-
- **⚡ 事件驱动架构** / **Event-Driven Architecture**: 基于EventEmitter的丰富事件系统
|
|
12
|
-
- **📦 模块化设计** / **Modular Design**: 清晰的关注点分离,易于扩展和维护
|
|
8
|
+
## ✨ Features
|
|
13
9
|
|
|
14
|
-
|
|
15
|
-
-
|
|
16
|
-
-
|
|
17
|
-
- **
|
|
18
|
-
-
|
|
19
|
-
-
|
|
20
|
-
-
|
|
10
|
+
- **Lightweight Core** – Only ~300 lines, easy to understand and extend.
|
|
11
|
+
- **Plugin System** – Add features like tool calling, reasoning, or custom adapters without touching the core.
|
|
12
|
+
- **Unified Message Format** – Consistent data structure across all components.
|
|
13
|
+
- **Multi‑Environment** – Builds ES module, UMD, and CommonJS formats for browser and Node.js.
|
|
14
|
+
- **No External Dependencies** – Uses native `fetch` (Node 18+ & modern browsers).
|
|
15
|
+
- **Tool Calling** – Built‑in plugin to handle function calls from AI models.
|
|
16
|
+
- **Streaming** – Full support for real‑time responses.
|
|
21
17
|
|
|
22
|
-
##
|
|
23
|
-
```
|
|
24
|
-
my-ai-chat-framework/
|
|
25
|
-
├── dist/ # 打包输出目录 / Build output
|
|
26
|
-
│ ├── my-ai-chat-framework.es.js # ES模块 / ES Module
|
|
27
|
-
│ ├── my-ai-chat-framework.umd.js # UMD模块(注册到window.AIChatFramework)/ UMD Module
|
|
28
|
-
│ └── my-ai-chat-framework.cjs.js # CommonJS模块 / CommonJS Module
|
|
29
|
-
├── src/ # 源代码 / Source code
|
|
30
|
-
│ ├── core/ # 核心类 / Core classes
|
|
31
|
-
│ │ ├── EventEmitter.js # 事件系统 / Event System
|
|
32
|
-
│ │ ├── Messages.js # 消息状态管理 / Message State Management
|
|
33
|
-
│ │ ├── MessageFormatter.js # 消息格式转换 / Message Format Conversion
|
|
34
|
-
│ │ ├── RequestBuilder.js # 请求构建器 / Request Builder
|
|
35
|
-
│ │ ├── ApiClient.js # API客户端(使用axios)/ API Client (axios)
|
|
36
|
-
│ │ └── ToolManager.js # 工具管理器 / Tool Manager
|
|
37
|
-
│ ├── utils/ # 工具函数 / Utility functions
|
|
38
|
-
│ └── index.js # 主入口文件 / Main entry point
|
|
39
|
-
├── test-*.html # 各种测试页面 / Various test pages
|
|
40
|
-
├── README_ZH.md # 详细中文文档 / Detailed Chinese documentation
|
|
41
|
-
├── FIX_SUMMARY.md # 修复总结 / Fix summary
|
|
42
|
-
├── vite.config.js # Vite配置 / Vite configuration
|
|
43
|
-
└── package.json # 项目配置 / Project configuration
|
|
44
|
-
```
|
|
45
|
-
|
|
46
|
-
## 🎯 统一数据格式 / Unified Data Format
|
|
47
|
-
|
|
48
|
-
从v1.1.0开始,框架使用统一的数据格式在所有组件之间传递消息,简化了使用体验。
|
|
18
|
+
## 📦 Installation
|
|
49
19
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
所有消息都遵循以下格式:
|
|
53
|
-
|
|
54
|
-
```javascript
|
|
55
|
-
{
|
|
56
|
-
// 必需字段 / Required fields
|
|
57
|
-
role: 'user' | 'assistant' | 'system' | 'tool',
|
|
58
|
-
content: string, // 允许空字符串 / Empty string allowed
|
|
59
|
-
|
|
60
|
-
// 标准扩展字段 / Standard extension fields
|
|
61
|
-
timestamp: string, // ISO格式时间戳,自动生成 / ISO timestamp, auto-generated
|
|
62
|
-
metadata?: object, // 自定义元数据 / Custom metadata
|
|
63
|
-
|
|
64
|
-
// 角色特定字段 / Role-specific fields
|
|
65
|
-
reasoning_content?: string, // 助手消息的推理内容 / Reasoning content for assistant messages
|
|
66
|
-
tool_calls?: array, // 助手消息的工具调用 / Tool calls for assistant messages
|
|
67
|
-
tool_call_id?: string, // 工具消息的调用ID / Call ID for tool messages
|
|
68
|
-
name?: string, // 工具消息的工具名称 / Tool name for tool messages
|
|
69
|
-
|
|
70
|
-
// 自定义字段(任意)/ Custom fields (any)
|
|
71
|
-
[key: string]: any // 所有自定义字段都会被保留 / All custom fields are preserved
|
|
72
|
-
}
|
|
20
|
+
```bash
|
|
21
|
+
npm install my-ai-chat-framework
|
|
73
22
|
```
|
|
74
23
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
1. **字段保留** / **Field Preservation**: 所有自定义字段都被保留,不再丢失
|
|
78
|
-
2. **空内容处理** / **Empty Content Handling**: 允许空字符串内容,仅在API请求时过滤
|
|
79
|
-
3. **工具消息特殊处理** / **Tool Message Special Handling**: tool消息可以没有content
|
|
80
|
-
4. **事件数据统一** / **Event Data Unification**: 所有事件使用相同的数据结构
|
|
81
|
-
5. **向后兼容** / **Backward Compatibility**: 现有API保持不变
|
|
82
|
-
|
|
83
|
-
### 使用示例
|
|
24
|
+
## 🚀 Quick Start
|
|
84
25
|
|
|
85
26
|
```javascript
|
|
86
|
-
|
|
87
|
-
const msgIndex = chatService.messages.addUserMessage('Hello', {
|
|
88
|
-
metadata: { userId: '123', priority: 'high' },
|
|
89
|
-
customData: { session: 'test' }
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
// 获取完整消息 / Get complete message
|
|
93
|
-
const fullMessage = chatService.messages.getMessages()[msgIndex];
|
|
94
|
-
// 包含所有字段 / Contains all fields
|
|
27
|
+
import { ChatService, openaiAdapter, toolCallingPlugin } from 'my-ai-chat-framework';
|
|
95
28
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
29
|
+
const chat = new ChatService({
|
|
30
|
+
apiKey: 'your-api-key',
|
|
31
|
+
model: 'deepseek-chat',
|
|
32
|
+
apiUrl: 'https://api.deepseek.com/v1/chat/completions'
|
|
100
33
|
});
|
|
101
|
-
```
|
|
102
|
-
|
|
103
|
-
## 📦 安装和使用 / Installation and Usage
|
|
104
34
|
|
|
105
|
-
|
|
35
|
+
chat.use(openaiAdapter);
|
|
36
|
+
chat.use(toolCallingPlugin);
|
|
106
37
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
<script>
|
|
111
|
-
// 全局变量 AIChatFramework 可用
|
|
112
|
-
const { ChatService, Messages } = AIChatFramework;
|
|
113
|
-
|
|
114
|
-
// 创建聊天服务
|
|
115
|
-
const chat = new ChatService('your-api-key');
|
|
116
|
-
</script>
|
|
117
|
-
```
|
|
118
|
-
|
|
119
|
-
#### 方式2:使用ES模块
|
|
120
|
-
```html
|
|
121
|
-
<script type="module">
|
|
122
|
-
import { ChatService, Messages } from 'https://unpkg.com/my-ai-chat-framework@latest/dist/my-ai-chat-framework.browser.es.js';
|
|
123
|
-
|
|
124
|
-
const chat = new ChatService('your-api-key');
|
|
125
|
-
</script>
|
|
126
|
-
```
|
|
38
|
+
chat.registerTool('greet', 'Say hello', async (args) => {
|
|
39
|
+
return `Hello, ${args.name}!`;
|
|
40
|
+
});
|
|
127
41
|
|
|
128
|
-
|
|
42
|
+
chat.on('message', msg => console.log(msg.content));
|
|
129
43
|
|
|
130
|
-
|
|
131
|
-
```bash
|
|
132
|
-
npm install my-ai-chat-framework axios
|
|
133
|
-
# 或
|
|
134
|
-
yarn add my-ai-chat-framework axios
|
|
135
|
-
# 或
|
|
136
|
-
pnpm add my-ai-chat-framework axios
|
|
44
|
+
await chat.send('Please greet Alice.');
|
|
137
45
|
```
|
|
138
46
|
|
|
139
|
-
|
|
140
|
-
```javascript
|
|
141
|
-
// CommonJS
|
|
142
|
-
const { ChatService, Messages } = require('my-ai-chat-framework');
|
|
47
|
+
## 🔌 Plugins & Adapters
|
|
143
48
|
|
|
144
|
-
|
|
145
|
-
|
|
49
|
+
- **openaiAdapter** – Converts internal messages to OpenAI-compatible format.
|
|
50
|
+
- **toolCallingPlugin** – Detects `tool_calls` in responses, executes tools, and continues the conversation.
|
|
146
51
|
|
|
147
|
-
|
|
148
|
-
const chat = new ChatService('your-api-key');
|
|
149
|
-
```
|
|
52
|
+
You can easily create your own adapter for other APIs (Anthropic, Cohere, etc.) or plugins for logging, caching, etc.
|
|
150
53
|
|
|
151
|
-
|
|
54
|
+
## 📚 API
|
|
152
55
|
|
|
153
|
-
|
|
154
|
-
```bash
|
|
155
|
-
npm install
|
|
156
|
-
```
|
|
56
|
+
### ChatService
|
|
157
57
|
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
58
|
+
```typescript
|
|
59
|
+
new ChatService(config: {
|
|
60
|
+
apiKey: string;
|
|
61
|
+
model?: string;
|
|
62
|
+
apiUrl?: string;
|
|
63
|
+
temperature?: number;
|
|
64
|
+
maxTokens?: number;
|
|
65
|
+
})
|
|
161
66
|
```
|
|
162
67
|
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
- `
|
|
166
|
-
- `
|
|
167
|
-
- `my-ai-chat-framework.cjs.js` - CommonJS格式(包含axios依赖)
|
|
68
|
+
- `use(plugin)` – Load a plugin.
|
|
69
|
+
- `send(message)` – Send a message and wait for the complete response.
|
|
70
|
+
- `stream(message, onProgress, onDone)` – Stream the response.
|
|
71
|
+
- `registerTool(name, description, executor)` – Register a tool (only available after loading `toolCallingPlugin`).
|
|
168
72
|
|
|
169
|
-
|
|
73
|
+
### MessageStore
|
|
170
74
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
75
|
+
- `add(message)` – Add a message (unified format).
|
|
76
|
+
- `addUser(content, metadata)` – Convenience method.
|
|
77
|
+
- `addAssistant(content, metadata)`
|
|
78
|
+
- `addSystem(content, metadata)`
|
|
79
|
+
- `addTool(content, toolCallId, metadata)`
|
|
80
|
+
- `getAll()` – Get all messages.
|
|
81
|
+
- `undoToLastAssistant()` – Rollback to the last assistant message.
|
|
175
82
|
|
|
176
|
-
###
|
|
83
|
+
### EventEmitter
|
|
177
84
|
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
<script>
|
|
182
|
-
// 使用全局变量 / Use global variable
|
|
183
|
-
const { Messages, ApiClient, ChatRequestBuilder, ChatService } = window.AIChatFramework;
|
|
184
|
-
const chat = new ChatService('your-api-key');
|
|
185
|
-
</script>
|
|
186
|
-
```
|
|
187
|
-
|
|
188
|
-
#### 现代浏览器(ES模块)/ Modern Browser (ES Module)
|
|
189
|
-
```html
|
|
190
|
-
<script type="module">
|
|
191
|
-
import { Messages, ChatService } from './dist/my-ai-chat-framework.es.js';
|
|
192
|
-
const chat = new ChatService('your-api-key');
|
|
193
|
-
</script>
|
|
194
|
-
```
|
|
195
|
-
|
|
196
|
-
### 在Node.js中使用 / Usage in Node.js
|
|
197
|
-
```javascript
|
|
198
|
-
const { Messages, ChatService } = require('./dist/my-ai-chat-framework.cjs.js');
|
|
199
|
-
const chat = new ChatService('your-api-key');
|
|
200
|
-
```
|
|
201
|
-
|
|
202
|
-
## 核心类说明 / Core Classes Description
|
|
203
|
-
|
|
204
|
-
### Messages 类
|
|
205
|
-
管理对话消息的核心类,支持:
|
|
206
|
-
- 消息的推送、修改、撤回
|
|
207
|
-
- 系统提示词管理
|
|
208
|
-
- 工具定义管理
|
|
209
|
-
- 数据导入/导出
|
|
210
|
-
|
|
211
|
-
### ChatRequestBuilder 类
|
|
212
|
-
构建符合API规范的请求体,支持:
|
|
213
|
-
- 自动格式化消息
|
|
214
|
-
- 配置模型参数
|
|
215
|
-
- 生成JSON请求体
|
|
216
|
-
|
|
217
|
-
### ApiClient 类
|
|
218
|
-
HTTP客户端,支持:
|
|
219
|
-
- 普通请求和流式请求
|
|
220
|
-
- 请求中断
|
|
221
|
-
- 错误处理
|
|
222
|
-
- 前后端通用(使用axios)
|
|
223
|
-
|
|
224
|
-
### ChatService 类
|
|
225
|
-
高级封装类,简化使用:
|
|
226
|
-
- 集成所有核心功能
|
|
227
|
-
- 提供简单API接口
|
|
228
|
-
- 自动管理消息状态
|
|
229
|
-
|
|
230
|
-
## 示例代码 / Example Code
|
|
231
|
-
|
|
232
|
-
```javascript
|
|
233
|
-
// 创建聊天服务 / Create chat service
|
|
234
|
-
const chat = new ChatService('your-api-key', 'deepseek-chat');
|
|
235
|
-
|
|
236
|
-
// 发送消息 / Send message
|
|
237
|
-
const response = await chat.send('你好,世界!');
|
|
238
|
-
console.log('AI回复:', response);
|
|
239
|
-
|
|
240
|
-
// 流式消息 / Stream message
|
|
241
|
-
await chat.stream('告诉我一个故事',
|
|
242
|
-
(chunk) => console.log('收到数据:', chunk),
|
|
243
|
-
() => console.log('流式传输完成')
|
|
244
|
-
);
|
|
245
|
-
|
|
246
|
-
// 流式传输中的工具调用 / Tool calling in streaming
|
|
247
|
-
await chat.stream('查询北京天气',
|
|
248
|
-
(chunk) => console.log('实时数据:', chunk.content),
|
|
249
|
-
(final) => console.log('最终结果:', final.content)
|
|
250
|
-
);
|
|
251
|
-
|
|
252
|
-
// 撤回消息 / Undo message
|
|
253
|
-
chat.undo();
|
|
254
|
-
|
|
255
|
-
// 导出对话 / Export conversation
|
|
256
|
-
const exportData = chat.export();
|
|
257
|
-
|
|
258
|
-
## 🤤 npm发布 / npm Publishing
|
|
259
|
-
|
|
260
|
-
### 发布准备
|
|
261
|
-
1. 确保已登录npm账号:
|
|
262
|
-
```bash
|
|
263
|
-
npm login
|
|
264
|
-
```
|
|
265
|
-
|
|
266
|
-
2. 更新版本号(如果需要):
|
|
267
|
-
```bash
|
|
268
|
-
npm version patch # 小版本更新
|
|
269
|
-
npm version minor # 中版本更新
|
|
270
|
-
npm version major # 大版本更新
|
|
271
|
-
```
|
|
272
|
-
|
|
273
|
-
3. 构建项目:
|
|
274
|
-
```bash
|
|
275
|
-
npm run build
|
|
276
|
-
```
|
|
85
|
+
- `on(event, handler)` – Subscribe to events.
|
|
86
|
+
- `off(event, handler)` – Unsubscribe.
|
|
87
|
+
- `emit(event, data)` – Emit an event.
|
|
277
88
|
|
|
278
|
-
|
|
279
|
-
```bash
|
|
280
|
-
npm publish
|
|
281
|
-
```
|
|
89
|
+
Events: `sending`, `message`, `stream-progress`, `error`, etc.
|
|
282
90
|
|
|
283
|
-
|
|
284
|
-
1. 检查包是否发布成功:
|
|
285
|
-
```bash
|
|
286
|
-
npm view my-ai-chat-framework
|
|
287
|
-
```
|
|
91
|
+
## 🛠️ Development
|
|
288
92
|
|
|
289
|
-
2. 测试安装:
|
|
290
93
|
```bash
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
- **当前版本**: 1.0.2
|
|
297
|
-
- **许可证**: MIT
|
|
298
|
-
- **关键词**: AI, chat, framework, messages, API, deepseek, openai, tool-calling
|
|
299
|
-
|
|
300
|
-
## 📄 许可证 / License
|
|
301
|
-
MIT License - 详见 [LICENSE](LICENSE) 文件
|
|
302
|
-
|
|
303
|
-
## 🤝 贡献 / Contributing
|
|
304
|
-
欢迎提交Issue和Pull Request!
|
|
305
|
-
|
|
306
|
-
## 📞 支持 / Support
|
|
307
|
-
如有问题,请提交Issue或联系作者。
|
|
308
|
-
|
|
309
|
-
---
|
|
310
|
-
|
|
311
|
-
**✨ 感谢使用 My AI Chat Framework! ✨**
|
|
312
|
-
|
|
313
|
-
// 导入对话 / Import conversation
|
|
314
|
-
chat.import(exportData);
|
|
315
|
-
```
|
|
316
|
-
|
|
317
|
-
## 🌊 流式传输下的工具调用 / Tool Calling in Streaming
|
|
318
|
-
|
|
319
|
-
### 工作原理 / How It Works
|
|
320
|
-
框架自动处理流式传输中的工具调用:
|
|
321
|
-
1. **流式收集** / **Stream Collection**: 在流式传输过程中收集完整的工具调用信息
|
|
322
|
-
2. **工具执行** / **Tool Execution**: 执行所有请求的工具
|
|
323
|
-
3. **继续流式** / **Continue Streaming**: 将工具结果发送给AI并继续流式传输
|
|
324
|
-
|
|
325
|
-
### 代码示例 / Code Example
|
|
326
|
-
```javascript
|
|
327
|
-
// 注册工具 / Register tool
|
|
328
|
-
chat.registerTool('get_weather', toolDefinition, async (args) => {
|
|
329
|
-
return { temperature: '25°C', condition: 'Sunny' };
|
|
330
|
-
});
|
|
331
|
-
|
|
332
|
-
// 流式传输会自动处理工具调用 / Streaming automatically handles tool calls
|
|
333
|
-
await chat.stream('What is the weather in Beijing?',
|
|
334
|
-
(chunk) => {
|
|
335
|
-
// 实时接收流式数据 / Receive streaming data in real-time
|
|
336
|
-
if (chunk.content) console.log('Content:', chunk.content);
|
|
337
|
-
if (chunk.tool_calls) console.log('Tool calls:', chunk.tool_calls);
|
|
338
|
-
},
|
|
339
|
-
(final) => {
|
|
340
|
-
// 流式传输完成 / Streaming completed
|
|
341
|
-
console.log('Final result:', final.content);
|
|
342
|
-
}
|
|
343
|
-
);
|
|
344
|
-
```
|
|
345
|
-
|
|
346
|
-
## 📚 更多文档 / More Documentation
|
|
347
|
-
- **[详细中文文档](README_ZH.md)** - 完整的功能说明和使用示例
|
|
348
|
-
- **[修复总结](FIX_SUMMARY.md)** - 技术修复细节和架构改进
|
|
349
|
-
|
|
350
|
-
## 许可证 / License
|
|
351
|
-
本项目采用MIT许可证。详见LICENSE文件。
|
|
352
|
-
|
|
353
|
-
This project is licensed under the MIT License. See the LICENSE file for more details.
|
|
354
|
-
|
|
355
|
-
## 依赖库 / Dependencies
|
|
356
|
-
- [axios](https://github.com/axios/axios) - MIT License
|
|
357
|
-
- [vite](https://vitejs.dev/) - MIT License
|
|
358
|
-
|
|
359
|
-
---
|
|
360
|
-
|
|
361
|
-
*此文档由AI辅助生成,内容可能不完整或存在错误,请以实际代码为准。*
|
|
362
|
-
*This document is AI-assisted and may be incomplete or contain errors. Please refer to the actual code for accurate information.*
|
|
363
|
-
|
|
364
|
-
### 配置系统章节
|
|
365
|
-
需要在两个 README 中都添加:
|
|
366
|
-
|
|
367
|
-
```markdown
|
|
368
|
-
## 🔧 配置系统 / Configuration System
|
|
369
|
-
|
|
370
|
-
### 配置结构
|
|
371
|
-
```javascript
|
|
372
|
-
{
|
|
373
|
-
api: {
|
|
374
|
-
key: '', // API密钥
|
|
375
|
-
url: 'https://api.deepseek.com/v1/chat/completions',
|
|
376
|
-
timeout: 30000
|
|
377
|
-
},
|
|
378
|
-
model: {
|
|
379
|
-
name: 'deepseek-chat',
|
|
380
|
-
temperature: 0.7,
|
|
381
|
-
maxTokens: 2000,
|
|
382
|
-
stream: false,
|
|
383
|
-
reasoningEffort: 'medium'
|
|
384
|
-
}
|
|
385
|
-
// ...
|
|
386
|
-
}
|
|
387
|
-
```
|
|
388
|
-
```
|
|
389
|
-
````
|
|
390
|
-
This is the code block that represents the suggested code change:
|
|
391
|
-
````markdown
|
|
392
|
-
## 🔧 配置系统 / Configuration System
|
|
393
|
-
|
|
394
|
-
### 配置结构 / Configuration Structure
|
|
395
|
-
```javascript
|
|
396
|
-
// 完整配置结构 / Complete configuration structure
|
|
397
|
-
{
|
|
398
|
-
api: {
|
|
399
|
-
key: '', // API密钥(必填)/ API Key (required)
|
|
400
|
-
url: 'https://api.deepseek.com/v1/chat/completions',
|
|
401
|
-
timeout: 30000 // 请求超时(毫秒)/ Request timeout (ms)
|
|
402
|
-
},
|
|
403
|
-
model: {
|
|
404
|
-
name: 'deepseek-chat', // 模型名称 / Model name
|
|
405
|
-
temperature: 0.7, // 温度(0-2)/ Temperature (0-2)
|
|
406
|
-
maxTokens: 2000, // 最大token数 / Max tokens
|
|
407
|
-
stream: false, // 是否流式传输 / Whether to use streaming
|
|
408
|
-
reasoningEffort: 'medium' // 推理强度(仅DeepSeek)/ Reasoning effort (DeepSeek only)
|
|
409
|
-
},
|
|
410
|
-
conversation: {
|
|
411
|
-
baseRounds: 10, // 基础对话轮次 / Base conversation rounds
|
|
412
|
-
cycleRounds: 5 // 循环对话轮次 / Cycle conversation rounds
|
|
413
|
-
},
|
|
414
|
-
debug: false // 调试模式 / Debug mode
|
|
415
|
-
}
|
|
416
|
-
```
|
|
417
|
-
|
|
418
|
-
### 使用方式 / Usage
|
|
419
|
-
|
|
420
|
-
#### 方式1:基本使用 / Basic Usage
|
|
421
|
-
```javascript
|
|
422
|
-
import { ChatService } from './src/index.js';
|
|
423
|
-
|
|
424
|
-
// 最小配置 / Minimal configuration
|
|
425
|
-
const chat = new ChatService({
|
|
426
|
-
api: { key: 'your-api-key' }
|
|
427
|
-
});
|
|
428
|
-
|
|
429
|
-
// 完整配置 / Complete configuration
|
|
430
|
-
const chat = new ChatService({
|
|
431
|
-
api: {
|
|
432
|
-
key: 'your-api-key',
|
|
433
|
-
url: 'https://api.openai.com/v1/chat/completions'
|
|
434
|
-
},
|
|
435
|
-
model: {
|
|
436
|
-
name: 'gpt-4',
|
|
437
|
-
temperature: 0.8,
|
|
438
|
-
stream: true
|
|
439
|
-
}
|
|
440
|
-
});
|
|
441
|
-
```
|
|
442
|
-
|
|
443
|
-
#### 方式2:动态配置更新 / Dynamic Configuration Updates
|
|
444
|
-
```javascript
|
|
445
|
-
// 创建服务后更新配置 / Update configuration after creating service
|
|
446
|
-
const chat = new ChatService({ api: { key: 'initial-key' } });
|
|
447
|
-
|
|
448
|
-
// 方法1:直接修改配置(自动同步)/ Method 1: Direct modification (auto-sync)
|
|
449
|
-
chat.configManager.config.api.key = 'new-key';
|
|
450
|
-
chat.configManager.config.model.temperature = 0.9;
|
|
451
|
-
|
|
452
|
-
// 方法2:批量更新 / Method 2: Batch update
|
|
453
|
-
chat.configManager.update({
|
|
454
|
-
api: { url: 'https://custom-api.com/v1/chat/completions' },
|
|
455
|
-
model: { name: 'custom-model' }
|
|
456
|
-
});
|
|
457
|
-
|
|
458
|
-
// 方法3:便捷方法 / Method 3: Convenience methods
|
|
459
|
-
chat.setApi('new-key', 'https://api.openai.com/v1/chat/completions');
|
|
460
|
-
chat.setModel('gpt-4', 0.8);
|
|
94
|
+
git clone https://github.com/your-username/my-ai-chat-framework.git
|
|
95
|
+
cd my-ai-chat-framework
|
|
96
|
+
npm install
|
|
97
|
+
npm run build # build the library
|
|
98
|
+
npm test # run the test script
|
|
461
99
|
```
|
|
462
100
|
|
|
463
|
-
|
|
464
|
-
```javascript
|
|
465
|
-
// 监听配置变更 / Listen to configuration changes
|
|
466
|
-
chat.configManager.on('config-changed', (data) => {
|
|
467
|
-
console.log(`配置 ${data.path} 已更新:`, data.oldValue, '→', data.newValue);
|
|
468
|
-
console.log(`Configuration ${data.path} updated:`, data.oldValue, '→', data.newValue);
|
|
469
|
-
});
|
|
470
|
-
|
|
471
|
-
// 监听特定配置变更 / Listen to specific configuration changes
|
|
472
|
-
chat.configManager.on('config-changed:model.temperature', (data) => {
|
|
473
|
-
console.log('温度已更新:', data.newValue);
|
|
474
|
-
console.log('Temperature updated:', data.newValue);
|
|
475
|
-
});
|
|
476
|
-
```
|
|
101
|
+
## 📄 License
|
|
477
102
|
|
|
478
|
-
|
|
479
|
-
框架会自动验证配置 / The framework automatically validates configuration:
|
|
480
|
-
- ✅ API Key 格式检查 / API Key format check
|
|
481
|
-
- ✅ URL 格式验证 / URL format validation
|
|
482
|
-
- ✅ 数值范围检查(temperature: 0-2)/ Value range check (temperature: 0-2)
|
|
483
|
-
- ✅ 枚举值验证(reasoningEffort: 'low'/'medium'/'high')/ Enum value validation
|
|
103
|
+
MIT
|
|
484
104
|
|
|
485
|
-
|
|
105
|
+
## 🤝 Contributing
|
|
486
106
|
|
|
487
|
-
|
|
488
|
-
1. **环境变量** / **Environment Variables**: 在生产环境中使用环境变量存储API Key / Use environment variables to store API keys in production
|
|
489
|
-
2. **配置分离** / **Configuration Separation**: 将配置提取到单独的文件中 / Extract configuration to separate files
|
|
490
|
-
3. **类型检查** / **Type Checking**: 使用TypeScript获得更好的类型安全 / Use TypeScript for better type safety
|
|
491
|
-
4. **版本控制** / **Version Control**: 不要将敏感配置提交到版本控制系统 / Do not commit sensitive configurations to version control
|
|
492
|
-
````
|
|
107
|
+
Contributions are welcome! Please open an issue or submit a pull request.
|