ai-world-sdk 1.0.3 → 1.0.6
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/README.md +313 -772
- package/dist/__tests__/example.test.js +485 -46
- package/dist/base.d.ts +4 -3
- package/dist/chat_models/google.js +1 -1
- package/dist/chat_models/openai.js +1 -1
- package/dist/index.js +16 -31
- package/package.json +4 -3
package/README.md
CHANGED
|
@@ -5,174 +5,72 @@ TypeScript SDK for AI World Platform - 一个功能完整的 AI 应用开发 SDK
|
|
|
5
5
|
[](https://www.npmjs.com/package/ai-world-sdk)
|
|
6
6
|
[](https://opensource.org/licenses/MIT)
|
|
7
7
|
|
|
8
|
-
##
|
|
8
|
+
## 特性
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
-
|
|
13
|
-
-
|
|
14
|
-
-
|
|
15
|
-
- **全局配置**: 支持全局 baseUrl 和 token 配置,简化客户端初始化
|
|
16
|
-
|
|
17
|
-
设计灵感来自 [LangChain.js](https://github.com/langchain-ai/langchainjs) 的接口风格。
|
|
10
|
+
- 🤖 **聊天模型**: 兼容 LangChain.js 接口(OpenAI、Gemini、Claude、Doubao)
|
|
11
|
+
- 🎨 **图像生成**: 支持豆包 Seedream 和 Google Gemini
|
|
12
|
+
- 🎬 **视频生成**: 支持豆包 Seedance
|
|
13
|
+
- ⚙️ **全局配置**: 自动从浏览器环境获取配置,简化初始化
|
|
14
|
+
- 🐛 **调试模式**: 支持详细的请求/响应日志
|
|
18
15
|
|
|
19
16
|
## 安装
|
|
20
17
|
|
|
21
18
|
```bash
|
|
22
19
|
npm install ai-world-sdk
|
|
23
|
-
# 或
|
|
24
|
-
|
|
25
|
-
# 或
|
|
26
|
-
pnpm add ai-world-sdk
|
|
20
|
+
# 或 yarn add ai-world-sdk
|
|
21
|
+
# 或 pnpm add ai-world-sdk
|
|
27
22
|
```
|
|
28
23
|
|
|
29
24
|
## 快速开始
|
|
30
25
|
|
|
31
|
-
### 1.
|
|
26
|
+
### 1. 配置(推荐)
|
|
32
27
|
|
|
33
28
|
```typescript
|
|
34
|
-
import { sdkConfig
|
|
29
|
+
import { sdkConfig } from 'ai-world-sdk';
|
|
35
30
|
|
|
36
|
-
//
|
|
31
|
+
// 设置全局配置(只需一次)
|
|
37
32
|
sdkConfig.setBaseUrl('http://localhost:8000');
|
|
38
33
|
sdkConfig.setToken('your-jwt-token');
|
|
34
|
+
sdkConfig.setDebug(true); // 可选:启用调试模式
|
|
39
35
|
|
|
40
|
-
//
|
|
41
|
-
const chatModel = new ChatGoogleGenerativeAI({
|
|
42
|
-
modelName: 'gemini-2.0-flash-exp-image-generation',
|
|
43
|
-
temperature: 0.7,
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
const imageClient = new ImageGenerationClient({});
|
|
47
|
-
const videoClient = new VideoGenerationClient({});
|
|
36
|
+
// 浏览器环境会自动从 window.location.origin 和 cookie 获取配置
|
|
48
37
|
```
|
|
49
38
|
|
|
50
|
-
### 2.
|
|
39
|
+
### 2. 聊天模型
|
|
51
40
|
|
|
52
41
|
```typescript
|
|
53
|
-
import {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
modelName: '
|
|
42
|
+
import { ChatGoogleGenerativeAI, ChatOpenAI, ChatAnthropic, HumanMessage, SystemMessage } from 'ai-world-sdk';
|
|
43
|
+
|
|
44
|
+
// Gemini 模型(使用 gemini provider)
|
|
45
|
+
const geminiModel = new ChatGoogleGenerativeAI({
|
|
46
|
+
modelName: 'gemini-2.5-flash-image',
|
|
47
|
+
temperature: 0.7,
|
|
48
|
+
provider: 'gemini', // 或 'aihubmix'
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
// GPT 模型(使用 aihubmix provider)
|
|
52
|
+
const gptModel = new ChatOpenAI({
|
|
53
|
+
modelName: 'gpt-4o-mini',
|
|
65
54
|
temperature: 0.7,
|
|
55
|
+
provider: 'aihubmix',
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
// Claude 模型(使用 aihubmix provider)
|
|
59
|
+
const claudeModel = new ChatAnthropic({
|
|
60
|
+
modelName: 'claude-3-sonnet-20240229',
|
|
61
|
+
temperature: 0.7,
|
|
62
|
+
provider: 'aihubmix',
|
|
66
63
|
});
|
|
67
64
|
|
|
68
65
|
// 非流式调用
|
|
69
|
-
const response = await
|
|
66
|
+
const response = await geminiModel.invoke([
|
|
70
67
|
new SystemMessage('You are a helpful assistant.'),
|
|
71
68
|
new HumanMessage('Hello! What is AI?'),
|
|
72
69
|
]);
|
|
73
|
-
|
|
74
|
-
console.log(response.content); // 输出: AI 是...
|
|
70
|
+
console.log(response.content);
|
|
75
71
|
|
|
76
72
|
// 流式调用
|
|
77
|
-
for await (const chunk of
|
|
78
|
-
new HumanMessage('Tell me a story about AI'),
|
|
79
|
-
])) {
|
|
80
|
-
// chunk 是 AIMessageChunk 对象
|
|
81
|
-
if (typeof chunk.content === 'string') {
|
|
82
|
-
process.stdout.write(chunk.content);
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
```
|
|
86
|
-
|
|
87
|
-
## 完整 API 文档
|
|
88
|
-
|
|
89
|
-
### 全局配置 API
|
|
90
|
-
|
|
91
|
-
#### `sdkConfig.setBaseUrl(baseUrl: string): void`
|
|
92
|
-
|
|
93
|
-
设置全局 baseUrl。所有客户端实例如果没有提供 baseUrl,将使用此全局值。
|
|
94
|
-
|
|
95
|
-
```typescript
|
|
96
|
-
import { sdkConfig } from 'ai-world-sdk';
|
|
97
|
-
|
|
98
|
-
sdkConfig.setBaseUrl('http://localhost:8000');
|
|
99
|
-
```
|
|
100
|
-
|
|
101
|
-
#### `sdkConfig.setToken(token: string): void`
|
|
102
|
-
|
|
103
|
-
设置全局 token。所有客户端实例如果没有提供 token,将使用此全局值。
|
|
104
|
-
|
|
105
|
-
```typescript
|
|
106
|
-
sdkConfig.setToken('your-jwt-token');
|
|
107
|
-
```
|
|
108
|
-
|
|
109
|
-
#### `sdkConfig.setHeaders(headers: Record<string, string>): void`
|
|
110
|
-
|
|
111
|
-
设置全局 headers。这些 headers 会与每个客户端实例的 headers 合并。
|
|
112
|
-
|
|
113
|
-
```typescript
|
|
114
|
-
sdkConfig.setHeaders({
|
|
115
|
-
'X-Custom-Header': 'value',
|
|
116
|
-
});
|
|
117
|
-
```
|
|
118
|
-
|
|
119
|
-
#### `sdkConfig.getBaseUrl(): string | null`
|
|
120
|
-
|
|
121
|
-
获取全局 baseUrl。
|
|
122
|
-
|
|
123
|
-
#### `sdkConfig.getToken(): string | null`
|
|
124
|
-
|
|
125
|
-
获取全局 token。
|
|
126
|
-
|
|
127
|
-
#### `sdkConfig.getHeaders(): Record<string, string>`
|
|
128
|
-
|
|
129
|
-
获取全局 headers。
|
|
130
|
-
|
|
131
|
-
#### `sdkConfig.reset(): void`
|
|
132
|
-
|
|
133
|
-
重置所有全局配置。
|
|
134
|
-
|
|
135
|
-
### 聊天模型 API
|
|
136
|
-
|
|
137
|
-
#### 支持的模型类
|
|
138
|
-
|
|
139
|
-
- `ChatOpenAI` - OpenAI 兼容模型(GPT、O1、Doubao)
|
|
140
|
-
- `ChatGoogleGenerativeAI` - Google Gemini 模型
|
|
141
|
-
- `ChatAnthropic` - Anthropic Claude 模型
|
|
142
|
-
|
|
143
|
-
#### `BaseChatModel` 接口
|
|
144
|
-
|
|
145
|
-
所有聊天模型都继承自 `BaseChatModel`,提供以下方法:
|
|
146
|
-
|
|
147
|
-
##### `invoke(messages: BaseMessage[]): Promise<AIMessage>`
|
|
148
|
-
|
|
149
|
-
发送非流式请求。
|
|
150
|
-
|
|
151
|
-
**参数:**
|
|
152
|
-
- `messages`: 消息数组,可以是 `HumanMessage`、`SystemMessage`、`AIMessage`
|
|
153
|
-
|
|
154
|
-
**返回:** `AIMessage` 对象,包含 `content` 字段
|
|
155
|
-
|
|
156
|
-
**示例:**
|
|
157
|
-
```typescript
|
|
158
|
-
const response = await model.invoke([
|
|
159
|
-
new HumanMessage('What is the capital of France?'),
|
|
160
|
-
]);
|
|
161
|
-
console.log(response.content); // "The capital of France is Paris."
|
|
162
|
-
```
|
|
163
|
-
|
|
164
|
-
##### `stream(messages: BaseMessage[]): AsyncGenerator<AIMessageChunk, void, unknown>`
|
|
165
|
-
|
|
166
|
-
发送流式请求。
|
|
167
|
-
|
|
168
|
-
**参数:**
|
|
169
|
-
- `messages`: 消息数组
|
|
170
|
-
|
|
171
|
-
**返回:** 异步生成器,每次 yield 一个 `AIMessageChunk` 对象
|
|
172
|
-
|
|
173
|
-
**示例:**
|
|
174
|
-
```typescript
|
|
175
|
-
for await (const chunk of model.stream([
|
|
73
|
+
for await (const chunk of geminiModel.stream([
|
|
176
74
|
new HumanMessage('Tell me a story'),
|
|
177
75
|
])) {
|
|
178
76
|
if (typeof chunk.content === 'string') {
|
|
@@ -181,513 +79,387 @@ for await (const chunk of model.stream([
|
|
|
181
79
|
}
|
|
182
80
|
```
|
|
183
81
|
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
批量处理多个消息集。
|
|
82
|
+
### 3. 图像生成
|
|
187
83
|
|
|
188
|
-
**参数:**
|
|
189
|
-
- `messagesList`: 消息数组的数组
|
|
190
|
-
|
|
191
|
-
**返回:** `AIMessage` 数组
|
|
192
|
-
|
|
193
|
-
**示例:**
|
|
194
84
|
```typescript
|
|
195
|
-
|
|
196
|
-
[new HumanMessage('What is AI?')],
|
|
197
|
-
[new HumanMessage('What is ML?')],
|
|
198
|
-
[new HumanMessage('What is DL?')],
|
|
199
|
-
]);
|
|
85
|
+
import { DoubaoImageGenerationClient, GeminiImageGenerationClient } from 'ai-world-sdk';
|
|
200
86
|
|
|
201
|
-
|
|
202
|
-
|
|
87
|
+
// 豆包图像生成
|
|
88
|
+
const doubaoClient = new DoubaoImageGenerationClient({});
|
|
89
|
+
const result = await doubaoClient.generate({
|
|
90
|
+
prompt: 'A beautiful sunset over the ocean',
|
|
91
|
+
size: '2K',
|
|
92
|
+
quality: 'hd',
|
|
93
|
+
n: 1,
|
|
203
94
|
});
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
##### `bind(options: BindOptions): BaseChatModel`
|
|
207
|
-
|
|
208
|
-
绑定参数到模型实例,返回新的模型实例。
|
|
209
|
-
|
|
210
|
-
**参数:**
|
|
211
|
-
- `options.temperature`: 温度参数
|
|
212
|
-
- `options.maxTokens`: 最大 token 数
|
|
213
|
-
- `options.topP`: Top-p 采样参数
|
|
214
|
-
- `options.tools`: 工具定义数组
|
|
215
|
-
- `options.toolChoice`: 工具选择策略
|
|
95
|
+
console.log('图像 URL:', result.data[0]?.url);
|
|
216
96
|
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
const
|
|
220
|
-
|
|
221
|
-
|
|
97
|
+
// Gemini 图像生成
|
|
98
|
+
const geminiClient = new GeminiImageGenerationClient({});
|
|
99
|
+
const geminiResult = await geminiClient.generate({
|
|
100
|
+
prompt: 'A futuristic city',
|
|
101
|
+
aspect_ratio: '16:9',
|
|
222
102
|
});
|
|
223
|
-
|
|
224
|
-
const response = await boundModel.invoke([
|
|
225
|
-
new HumanMessage('Hello!'),
|
|
226
|
-
]);
|
|
103
|
+
console.log('图像 URL:', geminiResult.data[0]?.url);
|
|
227
104
|
```
|
|
228
105
|
|
|
229
|
-
|
|
106
|
+
### 4. 视频生成
|
|
230
107
|
|
|
231
|
-
绑定工具到模型实例。
|
|
232
|
-
|
|
233
|
-
**参数:**
|
|
234
|
-
- `tools`: 工具定义数组
|
|
235
|
-
|
|
236
|
-
**示例:**
|
|
237
108
|
```typescript
|
|
238
|
-
import {
|
|
239
|
-
|
|
240
|
-
const tools: ToolDefinition[] = [
|
|
241
|
-
{
|
|
242
|
-
type: 'function',
|
|
243
|
-
function: {
|
|
244
|
-
name: 'get_weather',
|
|
245
|
-
description: 'Get the current weather in a location',
|
|
246
|
-
parameters: {
|
|
247
|
-
type: 'object',
|
|
248
|
-
properties: {
|
|
249
|
-
location: { type: 'string' },
|
|
250
|
-
},
|
|
251
|
-
required: ['location'],
|
|
252
|
-
},
|
|
253
|
-
},
|
|
254
|
-
},
|
|
255
|
-
];
|
|
256
|
-
|
|
257
|
-
const modelWithTools = model.bindTools(tools);
|
|
258
|
-
const response = await modelWithTools.invoke([
|
|
259
|
-
new HumanMessage('What is the weather in Paris?'),
|
|
260
|
-
]);
|
|
261
|
-
```
|
|
262
|
-
|
|
263
|
-
##### `getModelName(): string`
|
|
264
|
-
|
|
265
|
-
获取当前模型名称。
|
|
266
|
-
|
|
267
|
-
#### 消息类型
|
|
268
|
-
|
|
269
|
-
##### `HumanMessage`
|
|
270
|
-
|
|
271
|
-
用户消息。
|
|
272
|
-
|
|
273
|
-
```typescript
|
|
274
|
-
import { HumanMessage } from 'ai-world-sdk';
|
|
275
|
-
|
|
276
|
-
const message = new HumanMessage('Hello!');
|
|
277
|
-
```
|
|
109
|
+
import { VideoGenerationClient } from 'ai-world-sdk';
|
|
278
110
|
|
|
279
|
-
|
|
111
|
+
const videoClient = new VideoGenerationClient({});
|
|
280
112
|
|
|
281
|
-
|
|
113
|
+
// 创建任务
|
|
114
|
+
const task = await videoClient.create({
|
|
115
|
+
prompt: 'A beautiful sunset over the ocean',
|
|
116
|
+
duration: 5,
|
|
117
|
+
aspect_ratio: '16:9',
|
|
118
|
+
});
|
|
282
119
|
|
|
283
|
-
|
|
284
|
-
|
|
120
|
+
// 轮询直到完成
|
|
121
|
+
const result = await videoClient.poll(task.id, {
|
|
122
|
+
interval: 3000,
|
|
123
|
+
timeout: 300000,
|
|
124
|
+
});
|
|
285
125
|
|
|
286
|
-
|
|
126
|
+
if (result.status === 'succeeded') {
|
|
127
|
+
console.log('视频 URL:', result.content?.video_url);
|
|
128
|
+
}
|
|
287
129
|
```
|
|
288
130
|
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
助手消息。
|
|
131
|
+
## API 参考
|
|
292
132
|
|
|
293
|
-
|
|
294
|
-
import { AIMessage } from 'ai-world-sdk';
|
|
133
|
+
### 聊天模型
|
|
295
134
|
|
|
296
|
-
|
|
297
|
-
```
|
|
135
|
+
#### 支持的模型类
|
|
298
136
|
|
|
299
|
-
|
|
137
|
+
| 模型类 | 说明 | 示例模型 |
|
|
138
|
+
|--------|------|----------|
|
|
139
|
+
| `ChatOpenAI` | OpenAI 兼容(GPT、O1、Doubao) | `gpt-4o-mini`, `doubao-pro-4k` |
|
|
140
|
+
| `ChatGoogleGenerativeAI` | Google Gemini | `gemini-2.5-flash-image` |
|
|
141
|
+
| `ChatAnthropic` | Anthropic Claude | `claude-3-sonnet-20240229` |
|
|
300
142
|
|
|
301
|
-
|
|
143
|
+
#### 核心方法
|
|
302
144
|
|
|
303
145
|
```typescript
|
|
304
|
-
|
|
146
|
+
// 非流式调用
|
|
147
|
+
const response = await model.invoke([new HumanMessage('Hello')]);
|
|
305
148
|
|
|
306
|
-
//
|
|
307
|
-
for await (const chunk of model.stream([
|
|
308
|
-
// chunk 是 AIMessageChunk 实例
|
|
149
|
+
// 流式调用
|
|
150
|
+
for await (const chunk of model.stream([new HumanMessage('Hello')])) {
|
|
309
151
|
console.log(chunk.content);
|
|
310
|
-
console.log(chunk.id);
|
|
311
|
-
console.log(chunk.response_metadata);
|
|
312
152
|
}
|
|
313
|
-
```
|
|
314
153
|
|
|
315
|
-
|
|
154
|
+
// 批量处理
|
|
155
|
+
const responses = await model.batch([
|
|
156
|
+
[new HumanMessage('Question 1')],
|
|
157
|
+
[new HumanMessage('Question 2')],
|
|
158
|
+
]);
|
|
316
159
|
|
|
317
|
-
|
|
160
|
+
// 绑定参数
|
|
161
|
+
const boundModel = model.bind({ temperature: 0.9, maxTokens: 1000 });
|
|
318
162
|
|
|
319
|
-
|
|
320
|
-
const
|
|
321
|
-
{
|
|
322
|
-
role: 'user',
|
|
323
|
-
content: [
|
|
324
|
-
{ type: 'text', text: 'Describe this image' },
|
|
325
|
-
{
|
|
326
|
-
type: 'image_url',
|
|
327
|
-
image_url: 'data:image/jpeg;base64,/9j/4AAQSkZJRg...',
|
|
328
|
-
},
|
|
329
|
-
],
|
|
330
|
-
},
|
|
331
|
-
]);
|
|
163
|
+
// 绑定工具
|
|
164
|
+
const modelWithTools = model.bindTools([...tools]);
|
|
332
165
|
```
|
|
333
166
|
|
|
334
167
|
#### 工厂函数
|
|
335
168
|
|
|
336
|
-
使用 `createChatModel` 根据模型名称自动选择正确的模型类:
|
|
337
|
-
|
|
338
169
|
```typescript
|
|
339
170
|
import { createChatModel } from 'ai-world-sdk';
|
|
340
171
|
|
|
341
|
-
//
|
|
342
|
-
const
|
|
343
|
-
baseUrl: 'http://localhost:8000',
|
|
344
|
-
token: 'your-token',
|
|
172
|
+
// 根据模型名称自动选择正确的类
|
|
173
|
+
const model = createChatModel('gemini-2.5-flash-image', {
|
|
345
174
|
temperature: 0.7,
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
// 自动选择 ChatOpenAI
|
|
349
|
-
const gpt = createChatModel('gpt-4', {
|
|
350
|
-
baseUrl: 'http://localhost:8000',
|
|
351
|
-
token: 'your-token',
|
|
352
|
-
});
|
|
353
|
-
|
|
354
|
-
// 自动选择 ChatAnthropic
|
|
355
|
-
const claude = createChatModel('claude-3-sonnet-20240229', {
|
|
356
|
-
baseUrl: 'http://localhost:8000',
|
|
357
|
-
token: 'your-token',
|
|
175
|
+
provider: 'gemini', // 或 'aihubmix', 'doubao'
|
|
358
176
|
});
|
|
359
177
|
```
|
|
360
178
|
|
|
361
179
|
**支持的模型前缀:**
|
|
362
180
|
- `gpt-*`, `o1-*` → `ChatOpenAI`
|
|
363
|
-
- `doubao-*` → `ChatOpenAI`
|
|
181
|
+
- `doubao-*` → `ChatOpenAI`
|
|
364
182
|
- `gemini-*` → `ChatGoogleGenerativeAI`
|
|
365
183
|
- `claude-*` → `ChatAnthropic`
|
|
366
184
|
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
185
|
+
**Provider 参数说明:**
|
|
186
|
+
- `provider: 'aihubmix'` - 使用 aihubmix 聚合服务(推荐,支持所有模型)
|
|
187
|
+
- `provider: 'gemini'` - 直接使用 Google Gemini API
|
|
188
|
+
- `provider: 'doubao'` - 使用豆包服务
|
|
370
189
|
|
|
371
|
-
|
|
190
|
+
### 图像生成
|
|
372
191
|
|
|
373
|
-
|
|
192
|
+
#### DoubaoImageGenerationClient
|
|
374
193
|
|
|
375
194
|
```typescript
|
|
376
|
-
|
|
195
|
+
const client = new DoubaoImageGenerationClient({});
|
|
377
196
|
|
|
378
|
-
const
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
197
|
+
const result = await client.generate({
|
|
198
|
+
prompt: 'A beautiful landscape', // 必需
|
|
199
|
+
model: 'doubao-seedream-4-5-251128', // 可选,默认值
|
|
200
|
+
size: '2K', // 可选: 1K, 2K, 4K, 1024x1024 等
|
|
201
|
+
quality: 'hd', // 可选: standard, hd
|
|
202
|
+
n: 1, // 可选: 1-10
|
|
203
|
+
response_format: 'url', // 可选: url, b64_json
|
|
204
|
+
style: 'vivid', // 可选: vivid, natural
|
|
205
|
+
watermark: false, // 可选
|
|
384
206
|
});
|
|
385
207
|
```
|
|
386
208
|
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
生成图像(同步)。
|
|
390
|
-
|
|
391
|
-
**请求参数 (`ImageGenerationRequest`):**
|
|
392
|
-
- `prompt` (必需): 图像生成提示词
|
|
393
|
-
- `model` (可选): 模型名称,默认: `doubao-seedream-4-5-251128`
|
|
394
|
-
- `n` (可选): 生成图像数量,1-10,默认: 1
|
|
395
|
-
- `size` (可选): 图像尺寸,支持: `256x256`, `512x512`, `1024x1024`, `1792x1024`, `1024x1792`, `1K`, `2K`, `4K`,默认: `1024x1024`
|
|
396
|
-
- `quality` (可选): 图像质量,`standard` 或 `hd`,默认: `standard`
|
|
397
|
-
- `response_format` (可选): 响应格式,`url` 或 `b64_json`,默认: `url`
|
|
398
|
-
- `style` (可选): 图像风格,`vivid` 或 `natural`(仅部分模型支持)
|
|
399
|
-
- `user` (可选): 用户标识(用于追踪和审计)
|
|
209
|
+
#### GeminiImageGenerationClient
|
|
400
210
|
|
|
401
|
-
**返回 (`ImageGenerationResponse`):**
|
|
402
|
-
- `created`: 创建时间戳(Unix 时间戳)
|
|
403
|
-
- `data`: 图像数据数组,每个元素包含:
|
|
404
|
-
- `url`: 图像 URL(如果 `response_format` 为 `url`)
|
|
405
|
-
- `b64_json`: Base64 编码的图像数据(如果 `response_format` 为 `b64_json`)
|
|
406
|
-
|
|
407
|
-
**示例:**
|
|
408
211
|
```typescript
|
|
409
|
-
const
|
|
410
|
-
prompt: '充满活力的特写编辑肖像,模特眼神犀利,头戴雕塑感帽子',
|
|
411
|
-
model: 'doubao-seedream-4-5-251128',
|
|
412
|
-
size: '2K',
|
|
413
|
-
n: 1,
|
|
414
|
-
quality: 'hd',
|
|
415
|
-
response_format: 'url',
|
|
416
|
-
});
|
|
212
|
+
const client = new GeminiImageGenerationClient({});
|
|
417
213
|
|
|
418
|
-
|
|
419
|
-
|
|
214
|
+
const result = await client.generate({
|
|
215
|
+
prompt: 'A beautiful landscape', // 必需
|
|
216
|
+
model: 'gemini-2.0-flash-exp-image-generation', // 可选,默认值
|
|
217
|
+
number_of_images: 1, // 可选
|
|
218
|
+
aspect_ratio: '16:9', // 可选: 1:1, 9:16, 16:9, 4:3, 3:4
|
|
219
|
+
temperature: 0.7, // 可选
|
|
220
|
+
});
|
|
420
221
|
```
|
|
421
222
|
|
|
422
|
-
### 视频生成
|
|
223
|
+
### 视频生成
|
|
423
224
|
|
|
424
|
-
####
|
|
225
|
+
#### VideoGenerationClient
|
|
425
226
|
|
|
426
|
-
|
|
227
|
+
```typescript
|
|
228
|
+
const client = new VideoGenerationClient({});
|
|
427
229
|
|
|
428
|
-
|
|
230
|
+
// 创建任务
|
|
231
|
+
const task = await client.create({
|
|
232
|
+
prompt: 'A beautiful sunset', // 或 image_url
|
|
233
|
+
model: 'doubao-seedance-1-0-pro-fast-251015', // 可选
|
|
234
|
+
duration: 5, // 可选: 1-10 秒
|
|
235
|
+
aspect_ratio: '16:9', // 可选: 16:9, 9:16, 1:1
|
|
236
|
+
resolution: '720p', // 可选
|
|
237
|
+
return_last_frame: true, // 可选
|
|
238
|
+
});
|
|
429
239
|
|
|
430
|
-
|
|
431
|
-
|
|
240
|
+
// 查询状态
|
|
241
|
+
const status = await client.get(task.id);
|
|
432
242
|
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
'X-Custom-Header': 'value',
|
|
438
|
-
},
|
|
243
|
+
// 轮询直到完成
|
|
244
|
+
const result = await client.poll(task.id, {
|
|
245
|
+
interval: 3000, // 轮询间隔(毫秒)
|
|
246
|
+
timeout: 300000, // 超时时间(毫秒)
|
|
439
247
|
});
|
|
440
248
|
```
|
|
441
249
|
|
|
442
|
-
|
|
250
|
+
### 全局配置
|
|
443
251
|
|
|
444
|
-
|
|
252
|
+
```typescript
|
|
253
|
+
import { sdkConfig } from 'ai-world-sdk';
|
|
445
254
|
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
-
|
|
449
|
-
-
|
|
450
|
-
|
|
451
|
-
- `callback_url` (可选): 回调 URL(任务完成时通知)
|
|
452
|
-
- `return_last_frame` (可选): 是否返回最后一帧图像
|
|
453
|
-
- `service_tier` (可选): 服务层级
|
|
454
|
-
- `execution_expires_after` (可选): 执行过期时间(秒)
|
|
455
|
-
- `duration` (可选): 视频时长(秒),1-10,默认: 5(将添加到 prompt)
|
|
456
|
-
- `aspect_ratio` (可选): 视频宽高比,`16:9`, `9:16`, `1:1`,默认: `16:9`(将添加到 prompt)
|
|
457
|
-
- `resolution` (可选): 视频分辨率,例如: `720p`, `1080p`(将添加到 prompt)
|
|
458
|
-
- `user` (可选): 用户标识(用于追踪和审计)
|
|
255
|
+
// 设置配置
|
|
256
|
+
sdkConfig.setBaseUrl('http://localhost:8000');
|
|
257
|
+
sdkConfig.setToken('your-jwt-token');
|
|
258
|
+
sdkConfig.setHeaders({ 'X-Custom-Header': 'value' });
|
|
259
|
+
sdkConfig.setDebug(true);
|
|
459
260
|
|
|
460
|
-
|
|
261
|
+
// 获取配置
|
|
262
|
+
const baseUrl = sdkConfig.getServerUrl();
|
|
263
|
+
const token = sdkConfig.getToken();
|
|
264
|
+
const headers = sdkConfig.getHeaders();
|
|
265
|
+
const debug = sdkConfig.getDebug();
|
|
461
266
|
|
|
462
|
-
|
|
463
|
-
|
|
267
|
+
// 重置配置
|
|
268
|
+
sdkConfig.reset();
|
|
269
|
+
```
|
|
464
270
|
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
prompt: 'A beautiful sunset over the ocean with waves crashing on the shore',
|
|
469
|
-
model: 'doubao-seedance-1-0-pro-fast-251015',
|
|
470
|
-
duration: 5,
|
|
471
|
-
aspect_ratio: '16:9',
|
|
472
|
-
resolution: '720p',
|
|
473
|
-
});
|
|
271
|
+
**浏览器环境自动配置:**
|
|
272
|
+
- `baseUrl`: 从 `window.location.origin` 自动获取
|
|
273
|
+
- `token`: 从 `document.cookie` 中的 `auth_token` 自动获取
|
|
474
274
|
|
|
475
|
-
|
|
476
|
-
```
|
|
275
|
+
### 消息类型
|
|
477
276
|
|
|
478
|
-
**示例(图像生成视频):**
|
|
479
277
|
```typescript
|
|
480
|
-
|
|
481
|
-
image_url: 'https://example.com/image.jpg',
|
|
482
|
-
model: 'doubao-seedance-1-0-pro-fast-251015',
|
|
483
|
-
duration: 5,
|
|
484
|
-
});
|
|
278
|
+
import { HumanMessage, SystemMessage, AIMessage } from 'ai-world-sdk';
|
|
485
279
|
|
|
486
|
-
|
|
487
|
-
|
|
280
|
+
// 文本消息
|
|
281
|
+
const userMsg = new HumanMessage('Hello');
|
|
282
|
+
const systemMsg = new SystemMessage('You are a helpful assistant.');
|
|
488
283
|
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
{
|
|
495
|
-
type: 'text',
|
|
496
|
-
text: 'A beautiful sunset --ratio 16:9 --duration 5',
|
|
497
|
-
},
|
|
498
|
-
],
|
|
499
|
-
return_last_frame: true,
|
|
500
|
-
callback_url: 'https://example.com/callback',
|
|
501
|
-
});
|
|
284
|
+
// 多模态消息(文本 + 图像)
|
|
285
|
+
const multimodalMsg = new HumanMessage([
|
|
286
|
+
{ type: 'text', text: 'Describe this image' },
|
|
287
|
+
{ type: 'image_url', image_url: 'data:image/jpeg;base64,...' },
|
|
288
|
+
]);
|
|
502
289
|
```
|
|
503
290
|
|
|
504
|
-
|
|
291
|
+
## Provider 调用示例
|
|
505
292
|
|
|
506
|
-
|
|
293
|
+
SDK 支持多个 provider,每个 provider 对应不同的服务提供商。以下是各 provider 的使用示例:
|
|
507
294
|
|
|
508
|
-
|
|
509
|
-
- `taskId`: 任务 ID
|
|
295
|
+
### aihubmix Provider
|
|
510
296
|
|
|
511
|
-
|
|
512
|
-
- `id`: 任务 ID
|
|
513
|
-
- `model`: 使用的模型
|
|
514
|
-
- `status`: 任务状态,`queued` | `running` | `succeeded` | `failed` | `cancelled`
|
|
515
|
-
- `error`: 错误信息(如果失败),包含 `message` 和 `code`
|
|
516
|
-
- `content`: 生成的内容,包含:
|
|
517
|
-
- `video_url`: 视频 URL(任务完成时)
|
|
518
|
-
- `last_frame_url`: 最后一帧图像 URL(如果 `return_last_frame` 为 true)
|
|
519
|
-
- `file_url`: 文件 URL
|
|
520
|
-
- `usage`: 使用情况,包含 `completion_tokens`
|
|
521
|
-
- `created_at`: 创建时间戳(Unix 时间戳)
|
|
522
|
-
- `updated_at`: 更新时间戳
|
|
523
|
-
- 其他字段: `subdivisionlevel`, `fileformat`, `frames`, `framespersecond`, `seed`, `revised_prompt`, `service_tier`, `execution_expires_after`
|
|
297
|
+
`aihubmix` 是一个多模型聚合服务,支持多种模型(OpenAI、Gemini、Claude 等)。
|
|
524
298
|
|
|
525
|
-
**示例:**
|
|
526
299
|
```typescript
|
|
527
|
-
|
|
300
|
+
import { ChatOpenAI, ChatGoogleGenerativeAI, ChatAnthropic, HumanMessage } from 'ai-world-sdk';
|
|
528
301
|
|
|
529
|
-
|
|
530
|
-
|
|
302
|
+
// OpenAI 模型(通过 aihubmix)
|
|
303
|
+
const gptModel = new ChatOpenAI({
|
|
304
|
+
modelName: 'gpt-4o-mini',
|
|
305
|
+
temperature: 0.7,
|
|
306
|
+
provider: 'aihubmix',
|
|
307
|
+
});
|
|
531
308
|
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
309
|
+
// Gemini 模型(通过 aihubmix)
|
|
310
|
+
const geminiModel = new ChatGoogleGenerativeAI({
|
|
311
|
+
modelName: 'gemini-2.5-flash-image',
|
|
312
|
+
temperature: 0.7,
|
|
313
|
+
provider: 'aihubmix',
|
|
314
|
+
});
|
|
538
315
|
|
|
539
|
-
|
|
316
|
+
// Claude 模型(通过 aihubmix)
|
|
317
|
+
const claudeModel = new ChatAnthropic({
|
|
318
|
+
modelName: 'claude-3-sonnet-20240229',
|
|
319
|
+
temperature: 0.7,
|
|
320
|
+
provider: 'aihubmix',
|
|
321
|
+
});
|
|
540
322
|
|
|
541
|
-
|
|
323
|
+
// 使用示例
|
|
324
|
+
const response = await gptModel.invoke([
|
|
325
|
+
new HumanMessage('Hello!'),
|
|
326
|
+
]);
|
|
327
|
+
```
|
|
542
328
|
|
|
543
|
-
|
|
544
|
-
- `taskId`: 任务 ID
|
|
545
|
-
- `options.interval` (可选): 轮询间隔(毫秒),默认: 5000
|
|
546
|
-
- `options.timeout` (可选): 超时时间(毫秒),默认: 300000 (5分钟)
|
|
329
|
+
### gemini Provider
|
|
547
330
|
|
|
548
|
-
|
|
331
|
+
直接使用 Google Gemini 官方 API。
|
|
549
332
|
|
|
550
|
-
**示例:**
|
|
551
333
|
```typescript
|
|
552
|
-
|
|
553
|
-
const createTask = await videoClient.create({
|
|
554
|
-
prompt: 'A short video of a cat playing',
|
|
555
|
-
model: 'doubao-seedance-1-0-pro-fast-251015',
|
|
556
|
-
duration: 3,
|
|
557
|
-
});
|
|
334
|
+
import { ChatGoogleGenerativeAI, HumanMessage } from 'ai-world-sdk';
|
|
558
335
|
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
336
|
+
const model = new ChatGoogleGenerativeAI({
|
|
337
|
+
modelName: 'gemini-2.5-flash-image',
|
|
338
|
+
temperature: 0.7,
|
|
339
|
+
provider: 'gemini',
|
|
563
340
|
});
|
|
564
341
|
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
console.error('生成失败:', result.error.message);
|
|
569
|
-
}
|
|
342
|
+
const response = await model.invoke([
|
|
343
|
+
new HumanMessage('What is AI?'),
|
|
344
|
+
]);
|
|
570
345
|
```
|
|
571
346
|
|
|
572
|
-
|
|
347
|
+
### doubao Provider
|
|
573
348
|
|
|
574
|
-
|
|
349
|
+
使用豆包(字节跳动)的模型服务。
|
|
575
350
|
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
- `gpt-4`
|
|
579
|
-
- `gpt-4-turbo`
|
|
580
|
-
- `o1-preview`
|
|
581
|
-
- `o1-mini`
|
|
582
|
-
- `doubao-*` (豆包模型,与 OpenAI 兼容)
|
|
583
|
-
|
|
584
|
-
#### Google Gemini
|
|
585
|
-
- `gemini-pro`
|
|
586
|
-
- `gemini-1.5-pro`
|
|
587
|
-
- `gemini-1.5-flash`
|
|
588
|
-
- `gemini-2.0-flash-exp-image-generation`
|
|
589
|
-
|
|
590
|
-
#### Anthropic Claude
|
|
591
|
-
- `claude-3-opus-20240229`
|
|
592
|
-
- `claude-3-sonnet-20240229`
|
|
593
|
-
- `claude-3-haiku-20240307`
|
|
351
|
+
```typescript
|
|
352
|
+
import { ChatOpenAI, HumanMessage } from 'ai-world-sdk';
|
|
594
353
|
|
|
595
|
-
|
|
354
|
+
// Doubao 模型使用 ChatOpenAI(因为与 OpenAI 兼容)
|
|
355
|
+
const doubaoModel = new ChatOpenAI({
|
|
356
|
+
modelName: 'doubao-pro-4k',
|
|
357
|
+
temperature: 0.7,
|
|
358
|
+
provider: 'doubao',
|
|
359
|
+
});
|
|
596
360
|
|
|
597
|
-
|
|
598
|
-
|
|
361
|
+
const response = await doubaoModel.invoke([
|
|
362
|
+
new HumanMessage('你好!'),
|
|
363
|
+
]);
|
|
364
|
+
```
|
|
599
365
|
|
|
600
|
-
###
|
|
366
|
+
### Provider 选择指南
|
|
367
|
+
|
|
368
|
+
| Provider | 适用场景 | 支持的模型 |
|
|
369
|
+
|----------|----------|------------|
|
|
370
|
+
| `aihubmix` | 多模型聚合,统一接口 | GPT、Gemini、Claude、Doubao 等 |
|
|
371
|
+
| `gemini` | 直接使用 Google Gemini API | 所有 Gemini 模型 |
|
|
372
|
+
| `doubao` | 使用豆包服务 | Doubao 系列模型 |
|
|
601
373
|
|
|
602
|
-
|
|
603
|
-
- `doubao-seedance-1-0-pro-250528`
|
|
604
|
-
- `doubao-seedance-1-0-lite-t2v-250428` (文本生成视频)
|
|
605
|
-
- `doubao-seedance-1-0-lite-i2v-250428` (图像生成视频)
|
|
374
|
+
**推荐使用 `aihubmix` provider**,因为它提供了统一的接口和更好的模型选择灵活性。
|
|
606
375
|
|
|
607
376
|
## 完整示例
|
|
608
377
|
|
|
609
|
-
###
|
|
378
|
+
### 聊天对话
|
|
610
379
|
|
|
611
380
|
```typescript
|
|
612
|
-
import {
|
|
613
|
-
sdkConfig,
|
|
614
|
-
ChatGoogleGenerativeAI,
|
|
615
|
-
ImageGenerationClient,
|
|
616
|
-
VideoGenerationClient,
|
|
617
|
-
HumanMessage,
|
|
618
|
-
} from 'ai-world-sdk';
|
|
619
|
-
|
|
620
|
-
// 设置全局配置
|
|
621
|
-
sdkConfig.setBaseUrl('http://localhost:8000');
|
|
622
|
-
sdkConfig.setToken('your-jwt-token');
|
|
381
|
+
import { ChatGoogleGenerativeAI, HumanMessage, SystemMessage } from 'ai-world-sdk';
|
|
623
382
|
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
modelName: 'gemini-2.0-flash-exp-image-generation',
|
|
383
|
+
const model = new ChatGoogleGenerativeAI({
|
|
384
|
+
modelName: 'gemini-2.5-flash-image',
|
|
627
385
|
temperature: 0.7,
|
|
386
|
+
provider: 'gemini',
|
|
628
387
|
});
|
|
629
388
|
|
|
630
|
-
|
|
631
|
-
const
|
|
632
|
-
|
|
633
|
-
// 使用
|
|
634
|
-
const response = await chatModel.invoke([
|
|
635
|
-
new HumanMessage('Hello!'),
|
|
389
|
+
// 简单对话
|
|
390
|
+
const response = await model.invoke([
|
|
391
|
+
new HumanMessage('What is AI?'),
|
|
636
392
|
]);
|
|
393
|
+
console.log(response.content);
|
|
637
394
|
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
395
|
+
// 带系统提示的对话
|
|
396
|
+
const response2 = await model.invoke([
|
|
397
|
+
new SystemMessage('You are a helpful assistant.'),
|
|
398
|
+
new HumanMessage('Explain machine learning.'),
|
|
399
|
+
]);
|
|
641
400
|
```
|
|
642
401
|
|
|
643
|
-
###
|
|
402
|
+
### 流式响应
|
|
644
403
|
|
|
645
404
|
```typescript
|
|
646
|
-
import { ChatGoogleGenerativeAI, HumanMessage, AIMessageChunk } from 'ai-world-sdk';
|
|
647
|
-
|
|
648
|
-
const model = new ChatGoogleGenerativeAI({
|
|
649
|
-
baseUrl: 'http://localhost:8000',
|
|
650
|
-
token: 'your-token',
|
|
651
|
-
modelName: 'gemini-2.0-flash-exp-image-generation',
|
|
652
|
-
});
|
|
653
|
-
|
|
654
405
|
let fullText = '';
|
|
655
406
|
for await (const chunk of model.stream([
|
|
656
407
|
new HumanMessage('Tell me a story about AI'),
|
|
657
408
|
])) {
|
|
658
|
-
// chunk 是 AIMessageChunk 对象
|
|
659
409
|
if (typeof chunk.content === 'string') {
|
|
660
410
|
fullText += chunk.content;
|
|
661
411
|
process.stdout.write(chunk.content);
|
|
662
412
|
}
|
|
663
413
|
}
|
|
664
|
-
|
|
665
414
|
console.log('\n完整回复:', fullText);
|
|
666
415
|
```
|
|
667
416
|
|
|
668
|
-
###
|
|
417
|
+
### 工具调用
|
|
669
418
|
|
|
670
419
|
```typescript
|
|
671
|
-
import {
|
|
420
|
+
import { ToolDefinition } from 'ai-world-sdk';
|
|
672
421
|
|
|
673
|
-
const
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
422
|
+
const tools: ToolDefinition[] = [
|
|
423
|
+
{
|
|
424
|
+
type: 'function',
|
|
425
|
+
function: {
|
|
426
|
+
name: 'get_weather',
|
|
427
|
+
description: 'Get the current weather',
|
|
428
|
+
parameters: {
|
|
429
|
+
type: 'object',
|
|
430
|
+
properties: {
|
|
431
|
+
location: { type: 'string' },
|
|
432
|
+
},
|
|
433
|
+
required: ['location'],
|
|
434
|
+
},
|
|
435
|
+
},
|
|
436
|
+
},
|
|
437
|
+
];
|
|
438
|
+
|
|
439
|
+
const modelWithTools = model.bindTools(tools);
|
|
440
|
+
const response = await modelWithTools.invoke([
|
|
441
|
+
new HumanMessage('What is the weather in Paris?'),
|
|
442
|
+
]);
|
|
443
|
+
```
|
|
444
|
+
|
|
445
|
+
### 图像生成工作流
|
|
446
|
+
|
|
447
|
+
```typescript
|
|
448
|
+
import { DoubaoImageGenerationClient } from 'ai-world-sdk';
|
|
449
|
+
|
|
450
|
+
const client = new DoubaoImageGenerationClient({});
|
|
677
451
|
|
|
678
452
|
// 生成单张图像
|
|
679
|
-
const result = await
|
|
453
|
+
const result = await client.generate({
|
|
680
454
|
prompt: 'A futuristic city skyline at sunset',
|
|
681
|
-
model: 'doubao-seedream-4-5-251128',
|
|
682
455
|
size: '2K',
|
|
683
456
|
quality: 'hd',
|
|
684
|
-
n: 1,
|
|
685
457
|
});
|
|
686
458
|
|
|
687
459
|
console.log('图像 URL:', result.data[0]?.url);
|
|
688
460
|
|
|
689
461
|
// 生成多张图像
|
|
690
|
-
const multiResult = await
|
|
462
|
+
const multiResult = await client.generate({
|
|
691
463
|
prompt: 'A beautiful landscape',
|
|
692
464
|
n: 3,
|
|
693
465
|
size: '2K',
|
|
@@ -698,39 +470,28 @@ multiResult.data.forEach((image, index) => {
|
|
|
698
470
|
});
|
|
699
471
|
```
|
|
700
472
|
|
|
701
|
-
###
|
|
473
|
+
### 视频生成工作流
|
|
702
474
|
|
|
703
475
|
```typescript
|
|
704
476
|
import { VideoGenerationClient } from 'ai-world-sdk';
|
|
705
477
|
|
|
706
|
-
const
|
|
707
|
-
baseUrl: 'http://localhost:8000',
|
|
708
|
-
token: 'your-token',
|
|
709
|
-
});
|
|
478
|
+
const client = new VideoGenerationClient({});
|
|
710
479
|
|
|
711
|
-
// 1.
|
|
712
|
-
const
|
|
713
|
-
prompt: 'A beautiful sunset over the ocean
|
|
714
|
-
model: 'doubao-seedance-1-0-pro-fast-251015',
|
|
480
|
+
// 1. 创建任务
|
|
481
|
+
const task = await client.create({
|
|
482
|
+
prompt: 'A beautiful sunset over the ocean',
|
|
715
483
|
duration: 5,
|
|
716
484
|
aspect_ratio: '16:9',
|
|
717
485
|
resolution: '720p',
|
|
718
486
|
});
|
|
719
487
|
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
console.log('当前状态:', task.status);
|
|
725
|
-
|
|
726
|
-
// 3. 轮询直到完成
|
|
727
|
-
const result = await videoClient.poll(createTask.id, {
|
|
728
|
-
interval: 3000, // 每 3 秒查询一次
|
|
729
|
-
timeout: 300000, // 5 分钟超时
|
|
488
|
+
// 2. 轮询直到完成
|
|
489
|
+
const result = await client.poll(task.id, {
|
|
490
|
+
interval: 3000,
|
|
491
|
+
timeout: 300000,
|
|
730
492
|
});
|
|
731
493
|
|
|
732
494
|
if (result.status === 'succeeded') {
|
|
733
|
-
console.log('视频生成成功!');
|
|
734
495
|
console.log('视频 URL:', result.content?.video_url);
|
|
735
496
|
if (result.content?.last_frame_url) {
|
|
736
497
|
console.log('最后一帧:', result.content.last_frame_url);
|
|
@@ -740,147 +501,33 @@ if (result.status === 'succeeded') {
|
|
|
740
501
|
}
|
|
741
502
|
```
|
|
742
503
|
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
```typescript
|
|
746
|
-
import { ChatGoogleGenerativeAI, HumanMessage } from 'ai-world-sdk';
|
|
747
|
-
|
|
748
|
-
const model = new ChatGoogleGenerativeAI({
|
|
749
|
-
baseUrl: 'http://localhost:8000',
|
|
750
|
-
token: 'your-token',
|
|
751
|
-
modelName: 'gemini-2.0-flash-exp-image-generation',
|
|
752
|
-
});
|
|
753
|
-
|
|
754
|
-
// 批量处理多个问题
|
|
755
|
-
const questions = [
|
|
756
|
-
'What is artificial intelligence?',
|
|
757
|
-
'What is machine learning?',
|
|
758
|
-
'What is deep learning?',
|
|
759
|
-
];
|
|
760
|
-
|
|
761
|
-
const responses = await model.batch(
|
|
762
|
-
questions.map(q => [new HumanMessage(q)])
|
|
763
|
-
);
|
|
764
|
-
|
|
765
|
-
responses.forEach((response, index) => {
|
|
766
|
-
console.log(`问题 ${index + 1}:`, questions[index]);
|
|
767
|
-
console.log(`回答:`, response.content);
|
|
768
|
-
console.log('---');
|
|
769
|
-
});
|
|
770
|
-
```
|
|
771
|
-
|
|
772
|
-
### 示例 6: 工具调用
|
|
773
|
-
|
|
774
|
-
```typescript
|
|
775
|
-
import {
|
|
776
|
-
ChatGoogleGenerativeAI,
|
|
777
|
-
HumanMessage,
|
|
778
|
-
ToolDefinition,
|
|
779
|
-
} from 'ai-world-sdk';
|
|
780
|
-
|
|
781
|
-
const model = new ChatGoogleGenerativeAI({
|
|
782
|
-
baseUrl: 'http://localhost:8000',
|
|
783
|
-
token: 'your-token',
|
|
784
|
-
modelName: 'gemini-2.0-flash-exp-image-generation',
|
|
785
|
-
});
|
|
786
|
-
|
|
787
|
-
const tools: ToolDefinition[] = [
|
|
788
|
-
{
|
|
789
|
-
type: 'function',
|
|
790
|
-
function: {
|
|
791
|
-
name: 'get_weather',
|
|
792
|
-
description: 'Get the current weather in a location',
|
|
793
|
-
parameters: {
|
|
794
|
-
type: 'object',
|
|
795
|
-
properties: {
|
|
796
|
-
location: {
|
|
797
|
-
type: 'string',
|
|
798
|
-
description: 'The city and state, e.g. San Francisco, CA',
|
|
799
|
-
},
|
|
800
|
-
unit: {
|
|
801
|
-
type: 'string',
|
|
802
|
-
enum: ['celsius', 'fahrenheit'],
|
|
803
|
-
description: 'The unit of temperature',
|
|
804
|
-
},
|
|
805
|
-
},
|
|
806
|
-
required: ['location'],
|
|
807
|
-
},
|
|
808
|
-
},
|
|
809
|
-
},
|
|
810
|
-
];
|
|
811
|
-
|
|
812
|
-
const modelWithTools = model.bindTools(tools);
|
|
813
|
-
|
|
814
|
-
const response = await modelWithTools.invoke([
|
|
815
|
-
new HumanMessage('What is the weather in Paris?'),
|
|
816
|
-
]);
|
|
504
|
+
## 支持的模型
|
|
817
505
|
|
|
818
|
-
|
|
819
|
-
// 检查是否有工具调用
|
|
820
|
-
if (response.tool_calls && response.tool_calls.length > 0) {
|
|
821
|
-
console.log('工具调用:', response.tool_calls);
|
|
822
|
-
}
|
|
823
|
-
```
|
|
506
|
+
### 聊天模型
|
|
824
507
|
|
|
825
|
-
|
|
508
|
+
| 提供商 | Provider | 模型示例 | 模型类 |
|
|
509
|
+
|--------|----------|----------|--------|
|
|
510
|
+
| OpenAI | `aihubmix` | `gpt-4o-mini`, `gpt-4`, `o1-preview` | `ChatOpenAI` |
|
|
511
|
+
| Google Gemini | `gemini` 或 `aihubmix` | `gemini-2.5-flash-image`, `gemini-1.5-pro` | `ChatGoogleGenerativeAI` |
|
|
512
|
+
| Anthropic Claude | `aihubmix` | `claude-3-sonnet-20240229`, `claude-3-opus-20240229` | `ChatAnthropic` |
|
|
513
|
+
| Doubao | `doubao` 或 `aihubmix` | `doubao-pro-4k`, `doubao-seedream-4-5-251128` | `ChatOpenAI` |
|
|
826
514
|
|
|
827
|
-
|
|
515
|
+
**注意:**
|
|
516
|
+
- 使用 `aihubmix` provider 可以访问所有模型,推荐用于多模型场景
|
|
517
|
+
- 使用特定 provider(如 `gemini`、`doubao`)会直接调用对应的官方 API
|
|
828
518
|
|
|
829
|
-
|
|
830
|
-
// 消息类型
|
|
831
|
-
export class HumanMessage extends BaseMessage { ... }
|
|
832
|
-
export class SystemMessage extends BaseMessage { ... }
|
|
833
|
-
export class AIMessage extends BaseMessage { ... }
|
|
834
|
-
export class AIMessageChunk extends BaseMessage { ... }
|
|
835
|
-
|
|
836
|
-
// 配置类型
|
|
837
|
-
export interface BaseChatModelParams {
|
|
838
|
-
temperature?: number;
|
|
839
|
-
maxTokens?: number;
|
|
840
|
-
topP?: number;
|
|
841
|
-
modelName?: string;
|
|
842
|
-
provider?: string;
|
|
843
|
-
apiKey?: string;
|
|
844
|
-
}
|
|
519
|
+
### 图像生成模型
|
|
845
520
|
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
token?: string;
|
|
849
|
-
headers?: Record<string, string>;
|
|
850
|
-
}
|
|
521
|
+
- **豆包 Seedream**: `doubao-seedream-4-5-251128` (默认)
|
|
522
|
+
- **Google Gemini**: `gemini-2.0-flash-exp-image-generation` (默认)
|
|
851
523
|
|
|
852
|
-
|
|
853
|
-
export interface ToolDefinition {
|
|
854
|
-
type: 'function';
|
|
855
|
-
function: {
|
|
856
|
-
name: string;
|
|
857
|
-
description: string;
|
|
858
|
-
parameters?: Record<string, any>;
|
|
859
|
-
};
|
|
860
|
-
}
|
|
524
|
+
### 视频生成模型
|
|
861
525
|
|
|
862
|
-
|
|
863
|
-
temperature?: number;
|
|
864
|
-
maxTokens?: number;
|
|
865
|
-
topP?: number;
|
|
866
|
-
tools?: ToolDefinition[];
|
|
867
|
-
toolChoice?: 'auto' | 'none' | 'required' | { type: 'function'; function: { name: string } };
|
|
868
|
-
}
|
|
869
|
-
```
|
|
526
|
+
- **豆包 Seedance**: `doubao-seedance-1-0-pro-fast-251015` (默认,推荐)
|
|
870
527
|
|
|
871
528
|
## 错误处理
|
|
872
529
|
|
|
873
|
-
所有 API 调用都可能抛出错误。建议使用 try-catch 进行错误处理:
|
|
874
|
-
|
|
875
530
|
```typescript
|
|
876
|
-
import { ChatGoogleGenerativeAI, HumanMessage } from 'ai-world-sdk';
|
|
877
|
-
|
|
878
|
-
const model = new ChatGoogleGenerativeAI({
|
|
879
|
-
baseUrl: 'http://localhost:8000',
|
|
880
|
-
token: 'your-token',
|
|
881
|
-
modelName: 'gemini-2.0-flash-exp-image-generation',
|
|
882
|
-
});
|
|
883
|
-
|
|
884
531
|
try {
|
|
885
532
|
const response = await model.invoke([
|
|
886
533
|
new HumanMessage('Hello!'),
|
|
@@ -894,25 +541,6 @@ try {
|
|
|
894
541
|
}
|
|
895
542
|
```
|
|
896
543
|
|
|
897
|
-
## 参考地址
|
|
898
|
-
|
|
899
|
-
### 官方文档
|
|
900
|
-
|
|
901
|
-
- **AI World 平台**: [项目仓库](https://github.com/your-org/ai-world)
|
|
902
|
-
- **LangChain.js**: [https://github.com/langchain-ai/langchainjs](https://github.com/langchain-ai/langchainjs)
|
|
903
|
-
- **火山引擎方舟平台**: [https://www.volcengine.com/docs/82379](https://www.volcengine.com/docs/82379)
|
|
904
|
-
|
|
905
|
-
### API 文档
|
|
906
|
-
|
|
907
|
-
- **图像生成 API**: [https://www.volcengine.com/docs/82379/1824121](https://www.volcengine.com/docs/82379/1824121)
|
|
908
|
-
- **视频生成 API**: [https://www.volcengine.com/docs/82379/1366799](https://www.volcengine.com/docs/82379/1366799)
|
|
909
|
-
- **对话 API**: [https://www.volcengine.com/docs/82379/1541595](https://www.volcengine.com/docs/82379/1541595)
|
|
910
|
-
|
|
911
|
-
### 模型文档
|
|
912
|
-
|
|
913
|
-
- **Seedream 4.0-4.5 教程**: [https://www.volcengine.com/docs/82379/1824121?lang=zh](https://www.volcengine.com/docs/82379/1824121?lang=zh)
|
|
914
|
-
- **Seedance 提示词指南**: [https://www.volcengine.com/docs/82379/1901652](https://www.volcengine.com/docs/82379/1901652)
|
|
915
|
-
|
|
916
544
|
## 开发
|
|
917
545
|
|
|
918
546
|
### 构建
|
|
@@ -921,121 +549,34 @@ try {
|
|
|
921
549
|
npm run build
|
|
922
550
|
```
|
|
923
551
|
|
|
924
|
-
这会执行以下操作:
|
|
925
|
-
- 清理 `dist/` 目录
|
|
926
|
-
- 编译 TypeScript 源代码到 `dist/` 目录
|
|
927
|
-
- **自动生成 `.d.ts` 类型声明文件**
|
|
928
|
-
|
|
929
|
-
#### 生成 TypeScript 声明文件 (.d.ts)
|
|
930
|
-
|
|
931
|
-
项目已配置为自动生成类型声明文件。配置说明:
|
|
932
|
-
|
|
933
|
-
1. **tsconfig.json 配置**:
|
|
934
|
-
```json
|
|
935
|
-
{
|
|
936
|
-
"compilerOptions": {
|
|
937
|
-
"declaration": true, // 启用声明文件生成
|
|
938
|
-
"outDir": "./dist", // 输出目录
|
|
939
|
-
"rootDir": "./src" // 源代码目录
|
|
940
|
-
}
|
|
941
|
-
}
|
|
942
|
-
```
|
|
943
|
-
|
|
944
|
-
2. **构建命令**:
|
|
945
|
-
```bash
|
|
946
|
-
npm run build
|
|
947
|
-
# 等同于: rm -rf dist && tsc
|
|
948
|
-
```
|
|
949
|
-
|
|
950
|
-
3. **生成的文件**:
|
|
951
|
-
运行构建后,`dist/` 目录会包含:
|
|
952
|
-
- `.js` 文件(编译后的 JavaScript)
|
|
953
|
-
- `.d.ts` 文件(TypeScript 类型声明)
|
|
954
|
-
|
|
955
|
-
例如:
|
|
956
|
-
```
|
|
957
|
-
dist/
|
|
958
|
-
├── index.js # 编译后的代码
|
|
959
|
-
├── index.d.ts # 类型声明文件
|
|
960
|
-
├── base.js
|
|
961
|
-
├── base.d.ts
|
|
962
|
-
└── ...
|
|
963
|
-
```
|
|
964
|
-
|
|
965
|
-
4. **package.json 配置**:
|
|
966
|
-
```json
|
|
967
|
-
{
|
|
968
|
-
"main": "dist/index.js",
|
|
969
|
-
"types": "dist/index.d.ts" // 指定类型声明文件入口
|
|
970
|
-
}
|
|
971
|
-
```
|
|
972
|
-
|
|
973
|
-
5. **验证声明文件**:
|
|
974
|
-
```bash
|
|
975
|
-
# 检查 dist 目录中的 .d.ts 文件
|
|
976
|
-
ls -la dist/*.d.ts
|
|
977
|
-
|
|
978
|
-
# 或者查看特定文件的声明
|
|
979
|
-
cat dist/index.d.ts
|
|
980
|
-
```
|
|
981
|
-
|
|
982
552
|
### 测试
|
|
983
553
|
|
|
984
554
|
```bash
|
|
985
|
-
# 运行所有测试
|
|
986
555
|
npm test
|
|
987
|
-
|
|
988
|
-
# 运行特定测试
|
|
556
|
+
# 或运行特定测试
|
|
989
557
|
npm run test:stream
|
|
990
558
|
npm run test:image-generation
|
|
991
559
|
npm run test:video-generation
|
|
992
560
|
```
|
|
993
561
|
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
```
|
|
997
|
-
ai-world-sdk/
|
|
998
|
-
├── src/
|
|
999
|
-
│ ├── base.ts # 基础聊天模型类
|
|
1000
|
-
│ ├── config.ts # 全局配置管理
|
|
1001
|
-
│ ├── messages.ts # 消息类型定义
|
|
1002
|
-
│ ├── image_generation.ts # 图像生成客户端
|
|
1003
|
-
│ ├── video_generation.ts # 视频生成客户端
|
|
1004
|
-
│ ├── chat_models/ # 具体模型实现
|
|
1005
|
-
│ │ ├── openai.ts
|
|
1006
|
-
│ │ ├── google.ts
|
|
1007
|
-
│ │ └── anthropic.ts
|
|
1008
|
-
│ └── index.ts # 主入口文件
|
|
1009
|
-
├── __tests__/ # 测试文件
|
|
1010
|
-
├── package.json
|
|
1011
|
-
├── tsconfig.json
|
|
1012
|
-
└── README.md
|
|
1013
|
-
```
|
|
1014
|
-
|
|
1015
|
-
## 发布
|
|
562
|
+
## 参考链接
|
|
1016
563
|
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
```bash
|
|
1020
|
-
npm install ai-world-sdk
|
|
1021
|
-
```
|
|
564
|
+
- [LangChain.js](https://github.com/langchain-ai/langchainjs) - 设计灵感来源
|
|
565
|
+
- [火山引擎方舟平台](https://www.volcengine.com/docs/82379) - 图像/视频生成 API 文档
|
|
1022
566
|
|
|
1023
567
|
## 许可证
|
|
1024
568
|
|
|
1025
569
|
MIT
|
|
1026
570
|
|
|
1027
|
-
## 贡献
|
|
1028
|
-
|
|
1029
|
-
欢迎提交 Issue 和 Pull Request!
|
|
1030
|
-
|
|
1031
571
|
## 更新日志
|
|
1032
572
|
|
|
1033
|
-
### 1.0.
|
|
573
|
+
### 1.0.3
|
|
574
|
+
- ✨ 新增 Google Gemini 图像生成客户端
|
|
575
|
+
- ✨ 全局配置支持自动从浏览器环境获取
|
|
576
|
+
- ✨ 新增调试模式
|
|
577
|
+
- 🔧 改进日志输出
|
|
1034
578
|
|
|
579
|
+
### 1.0.0
|
|
1035
580
|
- ✨ 初始版本发布
|
|
1036
|
-
- ✨
|
|
1037
|
-
- ✨ 支持图像生成(Seedream)
|
|
1038
|
-
- ✨ 支持视频生成(Seedance)
|
|
1039
|
-
- ✨ 支持全局配置(baseUrl、token)
|
|
581
|
+
- ✨ 支持聊天模型、图像生成、视频生成
|
|
1040
582
|
- ✨ 兼容 LangChain.js 接口风格
|
|
1041
|
-
- ✨ 完整的 TypeScript 类型定义
|