ai-world-sdk 1.0.8 → 1.0.10
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 +123 -7
- package/dist/__tests__/example.test.js +91 -2
- package/dist/doubao-image-generation.d.ts +11 -3
- package/dist/doubao-image-generation.js +28 -8
- package/dist/gemini-image-generation.d.ts +67 -0
- package/dist/gemini-image-generation.js +117 -0
- package/dist/index.d.ts +2 -3
- package/dist/index.js +1 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -88,7 +88,7 @@ import { DoubaoImageGenerationClient, GeminiImageGenerationClient } from 'ai-wor
|
|
|
88
88
|
const doubaoClient = new DoubaoImageGenerationClient({});
|
|
89
89
|
const result = await doubaoClient.generate({
|
|
90
90
|
prompt: 'A beautiful sunset over the ocean',
|
|
91
|
-
size: '2K',
|
|
91
|
+
size: '2K', // 或使用 '2K', '4K' 等
|
|
92
92
|
quality: 'hd',
|
|
93
93
|
n: 1,
|
|
94
94
|
});
|
|
@@ -200,7 +200,7 @@ const client = new DoubaoImageGenerationClient({});
|
|
|
200
200
|
const result = await client.generate({
|
|
201
201
|
prompt: 'A beautiful landscape', // 必需
|
|
202
202
|
model: 'doubao-seedream-4-5-251128', // 可选,默认值
|
|
203
|
-
size: '2K',
|
|
203
|
+
size: '2K', // 可选: 像素值(2048x2048, 2560x1440等)或K值(1K, 2K, 4K)
|
|
204
204
|
quality: 'hd', // 可选: standard, hd
|
|
205
205
|
n: 1, // 可选: 1-10
|
|
206
206
|
response_format: 'url', // 可选: url, b64_json
|
|
@@ -247,6 +247,34 @@ const result3 = await aihubmixClient.generate({
|
|
|
247
247
|
number_of_images: 1,
|
|
248
248
|
response_modalities: ['IMAGE'],
|
|
249
249
|
});
|
|
250
|
+
|
|
251
|
+
// 图像编辑(使用文本提示编辑图片)
|
|
252
|
+
const editResult = await client.edit({
|
|
253
|
+
prompt: 'Add a small wizard hat on the cat\'s head',
|
|
254
|
+
image: 'data:image/png;base64,iVBORw0KGgo...', // base64 编码的图片数据或 data URL
|
|
255
|
+
model: 'gemini-2.5-flash-image',
|
|
256
|
+
aspect_ratio: '1:1',
|
|
257
|
+
response_modalities: ['IMAGE'],
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
// 多轮图片修改(迭代式优化图片)
|
|
261
|
+
// 第一轮:创建初始图片
|
|
262
|
+
const firstResponse = await client.chat({
|
|
263
|
+
message: 'Create a vibrant infographic about photosynthesis',
|
|
264
|
+
model: 'gemini-3-pro-image-preview',
|
|
265
|
+
aspect_ratio: '16:9',
|
|
266
|
+
response_modalities: ['TEXT', 'IMAGE'],
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
// 后续轮次:修改图片(使用返回的 chat_id)
|
|
270
|
+
const secondResponse = await client.chat({
|
|
271
|
+
chat_id: firstResponse.chat_id,
|
|
272
|
+
message: 'Update this infographic to be in Spanish',
|
|
273
|
+
model: 'gemini-3-pro-image-preview',
|
|
274
|
+
aspect_ratio: '16:9',
|
|
275
|
+
image_size: '2K',
|
|
276
|
+
response_modalities: ['TEXT', 'IMAGE'],
|
|
277
|
+
});
|
|
250
278
|
```
|
|
251
279
|
|
|
252
280
|
**参数说明:**
|
|
@@ -276,6 +304,17 @@ const result3 = await aihubmixClient.generate({
|
|
|
276
304
|
| `gemini-2.5-flash-image` | 1024px | 快速、高效、成本低 | 日常使用、批量生成 |
|
|
277
305
|
| `gemini-3-pro-image-preview` | 1K/2K/4K | 专业级、高分辨率、高级功能 | 专业设计、高分辨率需求 |
|
|
278
306
|
|
|
307
|
+
**图像编辑和多轮修改:**
|
|
308
|
+
|
|
309
|
+
- **`edit()`** - 图像编辑:使用文本提示编辑图片,支持添加、移除或修改元素
|
|
310
|
+
- 需要提供输入图片(base64 编码或 data URL)
|
|
311
|
+
- 适用于单次编辑操作
|
|
312
|
+
|
|
313
|
+
- **`chat()`** - 多轮图片修改:通过对话迭代式优化图片
|
|
314
|
+
- 首次调用创建新的聊天会话,返回 `chat_id`
|
|
315
|
+
- 后续调用使用 `chat_id` 继续对话
|
|
316
|
+
- 推荐使用 `gemini-3-pro-image-preview` 模型进行多轮编辑
|
|
317
|
+
|
|
279
318
|
### 视频生成
|
|
280
319
|
|
|
281
320
|
#### VideoGenerationClient
|
|
@@ -517,6 +556,27 @@ const response = await modelWithTools.invoke([
|
|
|
517
556
|
|
|
518
557
|
#### 豆包图像生成
|
|
519
558
|
|
|
559
|
+
豆包 Seedream 支持多种图像生成模式:
|
|
560
|
+
|
|
561
|
+
**支持的尺寸选项:**
|
|
562
|
+
|
|
563
|
+
- **像素值(宽x高)**:
|
|
564
|
+
- `2048x2048` (1:1 正方形)
|
|
565
|
+
- `2304x1728` (4:3 横屏)
|
|
566
|
+
- `1728x2304` (3:4 竖屏)
|
|
567
|
+
- `2560x1440` (16:9 横屏)
|
|
568
|
+
- `1440x2560` (9:16 竖屏)
|
|
569
|
+
- `2496x1664` (3:2 横屏)
|
|
570
|
+
- `1664x2496` (2:3 竖屏)
|
|
571
|
+
- `3024x1296` (21:9 超宽屏)
|
|
572
|
+
|
|
573
|
+
- **K 值(根据模型版本)**:
|
|
574
|
+
- `1K` - 仅 4.0 版本支持
|
|
575
|
+
- `2K` - 4.0 和 4.5 版本支持
|
|
576
|
+
- `4K` - 4.0 和 4.5 版本支持
|
|
577
|
+
|
|
578
|
+
**1. 文生图(文本生成图像)**
|
|
579
|
+
|
|
520
580
|
```typescript
|
|
521
581
|
import { DoubaoImageGenerationClient } from 'ai-world-sdk';
|
|
522
582
|
|
|
@@ -525,17 +585,58 @@ const client = new DoubaoImageGenerationClient({});
|
|
|
525
585
|
// 生成单张图像
|
|
526
586
|
const result = await client.generate({
|
|
527
587
|
prompt: 'A futuristic city skyline at sunset',
|
|
528
|
-
size: '2K',
|
|
588
|
+
size: '2048x2048', // 1:1 正方形,或使用 '2K', '4K' 等
|
|
529
589
|
quality: 'hd',
|
|
530
590
|
});
|
|
531
591
|
|
|
532
592
|
console.log('图像 URL:', result.data[0]?.url);
|
|
593
|
+
```
|
|
594
|
+
|
|
595
|
+
**2. 图文生图(单图输入单图输出)**
|
|
533
596
|
|
|
534
|
-
|
|
597
|
+
基于一张参考图片生成新图像:
|
|
598
|
+
|
|
599
|
+
```typescript
|
|
600
|
+
// 使用单张图片作为输入
|
|
601
|
+
const result = await client.generate({
|
|
602
|
+
prompt: '将这张图片转换为水彩画风格',
|
|
603
|
+
image: 'data:image/png;base64,iVBORw0KGgo...', // base64 编码的图片或 data URL
|
|
604
|
+
size: '2560x1440', // 16:9 横屏
|
|
605
|
+
quality: 'hd',
|
|
606
|
+
});
|
|
607
|
+
|
|
608
|
+
console.log('生成的图像 URL:', result.data[0]?.url);
|
|
609
|
+
```
|
|
610
|
+
|
|
611
|
+
**3. 多图融合(多图输入单图输出)**
|
|
612
|
+
|
|
613
|
+
融合多张参考图片生成新图像:
|
|
614
|
+
|
|
615
|
+
```typescript
|
|
616
|
+
// 使用多张图片作为输入
|
|
617
|
+
const result = await client.generate({
|
|
618
|
+
prompt: '将图1的服装换为图2的服装风格',
|
|
619
|
+
image: [
|
|
620
|
+
'data:image/png;base64,iVBORw0KGgo...', // 第一张图片
|
|
621
|
+
'data:image/png;base64,iVBORw0KGgo...', // 第二张图片
|
|
622
|
+
],
|
|
623
|
+
size: '2048x2048', // 1:1 正方形
|
|
624
|
+
quality: 'hd',
|
|
625
|
+
});
|
|
626
|
+
|
|
627
|
+
console.log('融合后的图像 URL:', result.data[0]?.url);
|
|
628
|
+
```
|
|
629
|
+
|
|
630
|
+
**4. 组图输出(多图输出)**
|
|
631
|
+
|
|
632
|
+
一次生成多张不同风格的图像:
|
|
633
|
+
|
|
634
|
+
```typescript
|
|
635
|
+
// 生成多张图像(组图输出)
|
|
535
636
|
const multiResult = await client.generate({
|
|
536
637
|
prompt: 'A beautiful landscape',
|
|
537
|
-
n: 3,
|
|
538
|
-
size: '
|
|
638
|
+
n: 3, // 生成 3 张图像
|
|
639
|
+
size: '2560x1440', // 16:9 横屏
|
|
539
640
|
});
|
|
540
641
|
|
|
541
642
|
multiResult.data.forEach((image, index) => {
|
|
@@ -543,6 +644,19 @@ multiResult.data.forEach((image, index) => {
|
|
|
543
644
|
});
|
|
544
645
|
```
|
|
545
646
|
|
|
647
|
+
**组合使用示例**
|
|
648
|
+
|
|
649
|
+
```typescript
|
|
650
|
+
// 图文生图 + 组图输出:基于一张图片生成多张不同风格的图像
|
|
651
|
+
const result = await client.generate({
|
|
652
|
+
prompt: '生成不同风格的艺术作品',
|
|
653
|
+
image: 'data:image/png;base64,iVBORw0KGgo...',
|
|
654
|
+
n: 4, // 生成 4 张不同风格的图像
|
|
655
|
+
size: '2048x2048', // 1:1 正方形
|
|
656
|
+
quality: 'hd',
|
|
657
|
+
});
|
|
658
|
+
```
|
|
659
|
+
|
|
546
660
|
#### Gemini 图像生成
|
|
547
661
|
|
|
548
662
|
```typescript
|
|
@@ -623,7 +737,9 @@ if (result.status === 'succeeded') {
|
|
|
623
737
|
|
|
624
738
|
### 图像生成模型
|
|
625
739
|
|
|
626
|
-
- **豆包 Seedream**:
|
|
740
|
+
- **豆包 Seedream**:
|
|
741
|
+
- `doubao-seedream-4-5-251128` (4.5版本,默认) - 支持 2K、4K 和像素值尺寸
|
|
742
|
+
- `doubao-seedream-4-0` (4.0版本) - 支持 1K、2K、4K 和像素值尺寸
|
|
627
743
|
- **Google Gemini**:
|
|
628
744
|
- `gemini-2.5-flash-image` (Nano Banana) - **推荐**,快速、高效,1024px 分辨率,支持所有宽高比
|
|
629
745
|
- `gemini-3-pro-image-preview` (Nano Banana Pro) - 专业级,支持 1K/2K/4K 分辨率,支持 Google 搜索、思考模式,最多 14 张参考图片
|
|
@@ -267,7 +267,7 @@ describe("Langchain SDK Tests", () => {
|
|
|
267
267
|
expect(result.data).toBeDefined();
|
|
268
268
|
expect(Array.isArray(result.data)).toBe(true);
|
|
269
269
|
expect(result.data.length).toBe(2);
|
|
270
|
-
console.log("✅
|
|
270
|
+
console.log("✅ 多图像生成测试成功(组图输出)");
|
|
271
271
|
console.log(`生成图像数量: ${result.data.length}`);
|
|
272
272
|
result.data.forEach((item, index) => {
|
|
273
273
|
console.log(`图像 ${index + 1}:`, item.url || "Base64 编码");
|
|
@@ -275,7 +275,10 @@ describe("Langchain SDK Tests", () => {
|
|
|
275
275
|
}, 120000);
|
|
276
276
|
test("DoubaoImageGenerationClient - 不同尺寸测试", async () => {
|
|
277
277
|
const imageClient = new index_1.DoubaoImageGenerationClient({});
|
|
278
|
-
const sizes = [
|
|
278
|
+
const sizes = [
|
|
279
|
+
"2K",
|
|
280
|
+
"4K",
|
|
281
|
+
];
|
|
279
282
|
for (const size of sizes) {
|
|
280
283
|
const result = await imageClient.generate({
|
|
281
284
|
prompt: "A futuristic city skyline at sunset",
|
|
@@ -610,6 +613,92 @@ describe("Langchain SDK Tests", () => {
|
|
|
610
613
|
console.log("图像描述:", result.text);
|
|
611
614
|
}
|
|
612
615
|
}, 120000);
|
|
616
|
+
test("GeminiImageGenerationClient - 图像编辑", async () => {
|
|
617
|
+
const imageClient = new index_1.GeminiImageGenerationClient({});
|
|
618
|
+
// 首先生成一张基础图片
|
|
619
|
+
const generateResult = await imageClient.generate({
|
|
620
|
+
prompt: 'A simple red apple on a white background',
|
|
621
|
+
model: 'gemini-2.5-flash-image',
|
|
622
|
+
aspect_ratio: '1:1',
|
|
623
|
+
response_modalities: ['IMAGE'],
|
|
624
|
+
});
|
|
625
|
+
expect(generateResult).toBeDefined();
|
|
626
|
+
expect(generateResult.data).toBeDefined();
|
|
627
|
+
expect(generateResult.data.length).toBeGreaterThan(0);
|
|
628
|
+
// 获取第一张图片的 base64 数据
|
|
629
|
+
const firstImage = generateResult.data[0];
|
|
630
|
+
expect(firstImage).toBeDefined();
|
|
631
|
+
// 提取 base64 数据(优先使用 b64_json,否则从 text 中提取)
|
|
632
|
+
let base64Image;
|
|
633
|
+
if (firstImage.b64_json) {
|
|
634
|
+
base64Image = firstImage.b64_json;
|
|
635
|
+
}
|
|
636
|
+
else if (firstImage.text && firstImage.text.startsWith('data:')) {
|
|
637
|
+
// 从 data URL 中提取 base64 部分
|
|
638
|
+
base64Image = firstImage.text.split(',')[1];
|
|
639
|
+
}
|
|
640
|
+
else {
|
|
641
|
+
throw new Error('无法获取图片的 base64 数据');
|
|
642
|
+
}
|
|
643
|
+
expect(base64Image).toBeDefined();
|
|
644
|
+
// 使用编辑功能添加元素
|
|
645
|
+
const editResult = await imageClient.edit({
|
|
646
|
+
prompt: 'Add a small green leaf on top of the apple',
|
|
647
|
+
image: base64Image, // 直接使用 base64 字符串
|
|
648
|
+
model: 'gemini-2.5-flash-image',
|
|
649
|
+
aspect_ratio: '1:1',
|
|
650
|
+
response_modalities: ['IMAGE'],
|
|
651
|
+
});
|
|
652
|
+
expect(editResult).toBeDefined();
|
|
653
|
+
expect(editResult.created).toBeDefined();
|
|
654
|
+
expect(editResult.data).toBeDefined();
|
|
655
|
+
expect(Array.isArray(editResult.data)).toBe(true);
|
|
656
|
+
expect(editResult.data.length).toBeGreaterThan(0);
|
|
657
|
+
editResult.data.forEach((item) => {
|
|
658
|
+
expect(item).toBeDefined();
|
|
659
|
+
expect(item.url || item.b64_json).toBeDefined();
|
|
660
|
+
});
|
|
661
|
+
console.log("✅ GeminiImageGenerationClient 图像编辑测试成功");
|
|
662
|
+
console.log(`编辑后图像数量: ${editResult.data.length}`);
|
|
663
|
+
}, 180000);
|
|
664
|
+
test("GeminiImageGenerationClient - 多轮图片修改", async () => {
|
|
665
|
+
const imageClient = new index_1.GeminiImageGenerationClient({});
|
|
666
|
+
// 第一轮:创建初始图片
|
|
667
|
+
const firstResponse = await imageClient.chat({
|
|
668
|
+
message: 'Create a vibrant infographic that explains photosynthesis as if it were a recipe for a plant\'s favorite food. Show the "ingredients" (sunlight, water, CO2) and the "finished dish" (sugar/energy). The style should be like a page from a colorful kids\' cookbook, suitable for a 4th grader.',
|
|
669
|
+
model: 'gemini-3-pro-image-preview',
|
|
670
|
+
aspect_ratio: '16:9',
|
|
671
|
+
response_modalities: ['TEXT', 'IMAGE'],
|
|
672
|
+
});
|
|
673
|
+
expect(firstResponse).toBeDefined();
|
|
674
|
+
expect(firstResponse.chat_id).toBeDefined();
|
|
675
|
+
expect(typeof firstResponse.chat_id).toBe("string");
|
|
676
|
+
expect(firstResponse.data).toBeDefined();
|
|
677
|
+
expect(Array.isArray(firstResponse.data)).toBe(true);
|
|
678
|
+
expect(firstResponse.data.length).toBeGreaterThan(0);
|
|
679
|
+
console.log("✅ 第一轮图片生成成功");
|
|
680
|
+
console.log(`Chat ID: ${firstResponse.chat_id}`);
|
|
681
|
+
console.log(`生成图像数量: ${firstResponse.data.length}`);
|
|
682
|
+
// 第二轮:修改图片(将语言改为西班牙语)
|
|
683
|
+
const secondResponse = await imageClient.chat({
|
|
684
|
+
chat_id: firstResponse.chat_id,
|
|
685
|
+
message: 'Update this infographic to be in Spanish. Do not change any other elements of the image.',
|
|
686
|
+
model: 'gemini-3-pro-image-preview',
|
|
687
|
+
aspect_ratio: '16:9',
|
|
688
|
+
image_size: '2K',
|
|
689
|
+
response_modalities: ['TEXT', 'IMAGE'],
|
|
690
|
+
});
|
|
691
|
+
expect(secondResponse).toBeDefined();
|
|
692
|
+
expect(secondResponse.chat_id).toBe(firstResponse.chat_id); // chat_id 应该保持一致
|
|
693
|
+
expect(secondResponse.data).toBeDefined();
|
|
694
|
+
expect(Array.isArray(secondResponse.data)).toBe(true);
|
|
695
|
+
expect(secondResponse.data.length).toBeGreaterThan(0);
|
|
696
|
+
console.log("✅ 第二轮图片修改成功");
|
|
697
|
+
console.log(`修改后图像数量: ${secondResponse.data.length}`);
|
|
698
|
+
if (secondResponse.text) {
|
|
699
|
+
console.log("文本响应:", secondResponse.text);
|
|
700
|
+
}
|
|
701
|
+
}, 240000);
|
|
613
702
|
test("DoubaoImageGenerationClient - quality 参数测试", async () => {
|
|
614
703
|
const imageClient = new index_1.DoubaoImageGenerationClient({});
|
|
615
704
|
const qualities = ["standard", "hd"];
|
|
@@ -6,16 +6,24 @@ export interface DoubaoImageGenerationConfig {
|
|
|
6
6
|
baseUrl?: string;
|
|
7
7
|
headers?: Record<string, string>;
|
|
8
8
|
}
|
|
9
|
+
export type DoubaoImageSize = "2048x2048" | "2304x1728" | "1728x2304" | "2560x1440" | "1440x2560" | "2496x1664" | "1664x2496" | "3024x1296" | "1K" | "2K" | "4K";
|
|
9
10
|
export interface DoubaoImageGenerationRequest {
|
|
10
11
|
model?: string;
|
|
11
12
|
prompt: string;
|
|
12
|
-
|
|
13
|
-
|
|
13
|
+
background?: "transparent" | "opaque" | "auto";
|
|
14
|
+
moderation?: "low" | "auto";
|
|
14
15
|
n?: number;
|
|
15
|
-
|
|
16
|
+
output_compression?: number;
|
|
17
|
+
output_format?: "png" | "jpeg" | "webp";
|
|
18
|
+
partial_images?: number;
|
|
19
|
+
quality?: "standard" | "hd" | "low" | "medium" | "high" | "auto";
|
|
16
20
|
response_format?: "url" | "b64_json";
|
|
21
|
+
size?: DoubaoImageSize;
|
|
22
|
+
stream?: false;
|
|
17
23
|
style?: "vivid" | "natural";
|
|
24
|
+
image?: string | string[];
|
|
18
25
|
user?: string;
|
|
26
|
+
watermark?: boolean;
|
|
19
27
|
}
|
|
20
28
|
export interface DoubaoImageData {
|
|
21
29
|
url?: string;
|
|
@@ -27,25 +27,45 @@ class DoubaoImageGenerationClient {
|
|
|
27
27
|
const requestBody = {
|
|
28
28
|
model: request.model || "doubao-seedream-4-5-251128",
|
|
29
29
|
prompt: request.prompt,
|
|
30
|
-
size: request.size || "
|
|
30
|
+
size: request.size || "2048x2048",
|
|
31
31
|
n: request.n || 1,
|
|
32
32
|
};
|
|
33
|
-
//
|
|
34
|
-
if (request.
|
|
35
|
-
requestBody.
|
|
33
|
+
// 添加可选参数(按照 OpenAI SDK 参数顺序)
|
|
34
|
+
if (request.background !== undefined) {
|
|
35
|
+
requestBody.background = request.background;
|
|
36
36
|
}
|
|
37
|
-
if (request.
|
|
37
|
+
if (request.moderation !== undefined) {
|
|
38
|
+
requestBody.moderation = request.moderation;
|
|
39
|
+
}
|
|
40
|
+
if (request.output_compression !== undefined) {
|
|
41
|
+
requestBody.output_compression = request.output_compression;
|
|
42
|
+
}
|
|
43
|
+
if (request.output_format !== undefined) {
|
|
44
|
+
requestBody.output_format = request.output_format;
|
|
45
|
+
}
|
|
46
|
+
if (request.partial_images !== undefined) {
|
|
47
|
+
requestBody.partial_images = request.partial_images;
|
|
48
|
+
}
|
|
49
|
+
if (request.quality !== undefined) {
|
|
38
50
|
requestBody.quality = request.quality;
|
|
39
51
|
}
|
|
40
|
-
if (request.response_format) {
|
|
52
|
+
if (request.response_format !== undefined) {
|
|
41
53
|
requestBody.response_format = request.response_format;
|
|
42
54
|
}
|
|
43
|
-
if (request.
|
|
55
|
+
if (request.stream !== undefined) {
|
|
56
|
+
requestBody.stream = request.stream;
|
|
57
|
+
}
|
|
58
|
+
if (request.style !== undefined) {
|
|
44
59
|
requestBody.style = request.style;
|
|
45
60
|
}
|
|
46
|
-
if (request.user) {
|
|
61
|
+
if (request.user !== undefined) {
|
|
47
62
|
requestBody.user = request.user;
|
|
48
63
|
}
|
|
64
|
+
// 添加图片输入参数(图文生图或多图融合)
|
|
65
|
+
if (request.image !== undefined) {
|
|
66
|
+
requestBody.image = request.image;
|
|
67
|
+
}
|
|
68
|
+
requestBody.watermark = request.watermark || false;
|
|
49
69
|
const url = `${config_1.sdkConfig.getServerUrl()}/api/doubao-image-proxy/generate`;
|
|
50
70
|
(0, log_1.logRequest)("POST", url, this.headers, requestBody);
|
|
51
71
|
const response = await fetch(url, {
|
|
@@ -28,6 +28,32 @@ export interface GeminiImageGenerationResponse {
|
|
|
28
28
|
data: GeminiImageData[];
|
|
29
29
|
text?: string;
|
|
30
30
|
}
|
|
31
|
+
export interface GeminiImageEditRequest {
|
|
32
|
+
prompt: string;
|
|
33
|
+
image: string;
|
|
34
|
+
model?: string;
|
|
35
|
+
provider?: "aihubmix" | "gemini";
|
|
36
|
+
aspect_ratio?: "1:1" | "2:3" | "3:2" | "3:4" | "4:3" | "4:5" | "5:4" | "9:16" | "16:9" | "21:9";
|
|
37
|
+
image_size?: "1K" | "2K" | "4K";
|
|
38
|
+
response_modalities?: ("TEXT" | "IMAGE")[];
|
|
39
|
+
user?: string;
|
|
40
|
+
}
|
|
41
|
+
export interface GeminiImageChatRequest {
|
|
42
|
+
message: string;
|
|
43
|
+
chat_id?: string;
|
|
44
|
+
model?: string;
|
|
45
|
+
provider?: "aihubmix" | "gemini";
|
|
46
|
+
aspect_ratio?: "1:1" | "2:3" | "3:2" | "3:4" | "4:3" | "4:5" | "5:4" | "9:16" | "16:9" | "21:9";
|
|
47
|
+
image_size?: "1K" | "2K" | "4K";
|
|
48
|
+
response_modalities?: ("TEXT" | "IMAGE")[];
|
|
49
|
+
user?: string;
|
|
50
|
+
}
|
|
51
|
+
export interface GeminiImageChatResponse {
|
|
52
|
+
chat_id: string;
|
|
53
|
+
created: number;
|
|
54
|
+
data: GeminiImageData[];
|
|
55
|
+
text?: string;
|
|
56
|
+
}
|
|
31
57
|
export declare class GeminiImageGenerationClient {
|
|
32
58
|
private headers;
|
|
33
59
|
private provider;
|
|
@@ -43,4 +69,45 @@ export declare class GeminiImageGenerationClient {
|
|
|
43
69
|
* - number_of_images: 生成图片数量(1-4)
|
|
44
70
|
*/
|
|
45
71
|
generate(request: GeminiImageGenerationRequest): Promise<GeminiImageGenerationResponse>;
|
|
72
|
+
/**
|
|
73
|
+
* Edit images
|
|
74
|
+
* 编辑图像
|
|
75
|
+
*
|
|
76
|
+
* 使用文本提示编辑图片,支持添加、移除或修改元素
|
|
77
|
+
*
|
|
78
|
+
* 支持的参数:
|
|
79
|
+
* - image: base64 编码的图片数据,或 data URL(如 data:image/png;base64,...)
|
|
80
|
+
* - aspect_ratio: 宽高比
|
|
81
|
+
* - image_size: 图片大小(仅 gemini-3-pro-image-preview)
|
|
82
|
+
* - response_modalities: 响应模态
|
|
83
|
+
*/
|
|
84
|
+
edit(request: GeminiImageEditRequest): Promise<GeminiImageGenerationResponse>;
|
|
85
|
+
/**
|
|
86
|
+
* Chat with images (multi-turn image editing)
|
|
87
|
+
* 图像多轮对话(用于多轮图片修改)
|
|
88
|
+
*
|
|
89
|
+
* 支持通过多轮对话迭代式优化图片
|
|
90
|
+
*
|
|
91
|
+
* 使用示例:
|
|
92
|
+
* ```typescript
|
|
93
|
+
* // 第一轮:创建初始图片
|
|
94
|
+
* const firstResponse = await client.chat({
|
|
95
|
+
* message: "Create a vibrant infographic about photosynthesis"
|
|
96
|
+
* });
|
|
97
|
+
*
|
|
98
|
+
* // 后续轮次:修改图片(使用返回的 chat_id)
|
|
99
|
+
* const secondResponse = await client.chat({
|
|
100
|
+
* chat_id: firstResponse.chat_id,
|
|
101
|
+
* message: "Update this infographic to be in Spanish"
|
|
102
|
+
* });
|
|
103
|
+
* ```
|
|
104
|
+
*
|
|
105
|
+
* 支持的参数:
|
|
106
|
+
* - message: 当前轮次的提示词
|
|
107
|
+
* - chat_id: 聊天 ID(用于多轮对话,首次请求时不需要)
|
|
108
|
+
* - aspect_ratio: 宽高比
|
|
109
|
+
* - image_size: 图片大小(仅 gemini-3-pro-image-preview)
|
|
110
|
+
* - response_modalities: 响应模态
|
|
111
|
+
*/
|
|
112
|
+
chat(request: GeminiImageChatRequest): Promise<GeminiImageChatResponse>;
|
|
46
113
|
}
|
|
@@ -76,5 +76,122 @@ class GeminiImageGenerationClient {
|
|
|
76
76
|
(0, log_1.logResponse)(response.status, response.statusText, response.headers, data);
|
|
77
77
|
return data;
|
|
78
78
|
}
|
|
79
|
+
/**
|
|
80
|
+
* Edit images
|
|
81
|
+
* 编辑图像
|
|
82
|
+
*
|
|
83
|
+
* 使用文本提示编辑图片,支持添加、移除或修改元素
|
|
84
|
+
*
|
|
85
|
+
* 支持的参数:
|
|
86
|
+
* - image: base64 编码的图片数据,或 data URL(如 data:image/png;base64,...)
|
|
87
|
+
* - aspect_ratio: 宽高比
|
|
88
|
+
* - image_size: 图片大小(仅 gemini-3-pro-image-preview)
|
|
89
|
+
* - response_modalities: 响应模态
|
|
90
|
+
*/
|
|
91
|
+
async edit(request) {
|
|
92
|
+
const requestBody = {
|
|
93
|
+
prompt: request.prompt,
|
|
94
|
+
image: request.image,
|
|
95
|
+
model: request.model || "gemini-2.5-flash-image",
|
|
96
|
+
};
|
|
97
|
+
// 添加可选参数
|
|
98
|
+
if (request.aspect_ratio) {
|
|
99
|
+
requestBody.aspect_ratio = request.aspect_ratio;
|
|
100
|
+
}
|
|
101
|
+
if (request.image_size) {
|
|
102
|
+
requestBody.image_size = request.image_size;
|
|
103
|
+
}
|
|
104
|
+
if (request.response_modalities) {
|
|
105
|
+
requestBody.response_modalities = request.response_modalities;
|
|
106
|
+
}
|
|
107
|
+
if (request.user) {
|
|
108
|
+
requestBody.user = request.user;
|
|
109
|
+
}
|
|
110
|
+
if (request.provider) {
|
|
111
|
+
requestBody.provider = request.provider;
|
|
112
|
+
}
|
|
113
|
+
const url = `${config_1.sdkConfig.getServerUrl()}/api/gemini-image-proxy/edit`;
|
|
114
|
+
(0, log_1.logRequest)("POST", url, this.headers, { ...requestBody, image: "[base64 data]" });
|
|
115
|
+
const response = await fetch(url, {
|
|
116
|
+
method: "POST",
|
|
117
|
+
headers: this.headers,
|
|
118
|
+
body: JSON.stringify(requestBody),
|
|
119
|
+
});
|
|
120
|
+
if (!response.ok) {
|
|
121
|
+
const errorText = await response.text();
|
|
122
|
+
(0, log_1.logResponse)(response.status, response.statusText, response.headers, errorText);
|
|
123
|
+
throw new Error(`Gemini image edit API error: ${response.status} ${errorText}`);
|
|
124
|
+
}
|
|
125
|
+
const data = (await response.json());
|
|
126
|
+
(0, log_1.logResponse)(response.status, response.statusText, response.headers, data);
|
|
127
|
+
return data;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Chat with images (multi-turn image editing)
|
|
131
|
+
* 图像多轮对话(用于多轮图片修改)
|
|
132
|
+
*
|
|
133
|
+
* 支持通过多轮对话迭代式优化图片
|
|
134
|
+
*
|
|
135
|
+
* 使用示例:
|
|
136
|
+
* ```typescript
|
|
137
|
+
* // 第一轮:创建初始图片
|
|
138
|
+
* const firstResponse = await client.chat({
|
|
139
|
+
* message: "Create a vibrant infographic about photosynthesis"
|
|
140
|
+
* });
|
|
141
|
+
*
|
|
142
|
+
* // 后续轮次:修改图片(使用返回的 chat_id)
|
|
143
|
+
* const secondResponse = await client.chat({
|
|
144
|
+
* chat_id: firstResponse.chat_id,
|
|
145
|
+
* message: "Update this infographic to be in Spanish"
|
|
146
|
+
* });
|
|
147
|
+
* ```
|
|
148
|
+
*
|
|
149
|
+
* 支持的参数:
|
|
150
|
+
* - message: 当前轮次的提示词
|
|
151
|
+
* - chat_id: 聊天 ID(用于多轮对话,首次请求时不需要)
|
|
152
|
+
* - aspect_ratio: 宽高比
|
|
153
|
+
* - image_size: 图片大小(仅 gemini-3-pro-image-preview)
|
|
154
|
+
* - response_modalities: 响应模态
|
|
155
|
+
*/
|
|
156
|
+
async chat(request) {
|
|
157
|
+
const requestBody = {
|
|
158
|
+
message: request.message,
|
|
159
|
+
model: request.model || "gemini-3-pro-image-preview",
|
|
160
|
+
};
|
|
161
|
+
// 添加可选参数
|
|
162
|
+
if (request.chat_id) {
|
|
163
|
+
requestBody.chat_id = request.chat_id;
|
|
164
|
+
}
|
|
165
|
+
if (request.aspect_ratio) {
|
|
166
|
+
requestBody.aspect_ratio = request.aspect_ratio;
|
|
167
|
+
}
|
|
168
|
+
if (request.image_size) {
|
|
169
|
+
requestBody.image_size = request.image_size;
|
|
170
|
+
}
|
|
171
|
+
if (request.response_modalities) {
|
|
172
|
+
requestBody.response_modalities = request.response_modalities;
|
|
173
|
+
}
|
|
174
|
+
if (request.user) {
|
|
175
|
+
requestBody.user = request.user;
|
|
176
|
+
}
|
|
177
|
+
if (request.provider) {
|
|
178
|
+
requestBody.provider = request.provider;
|
|
179
|
+
}
|
|
180
|
+
const url = `${config_1.sdkConfig.getServerUrl()}/api/gemini-image-proxy/chat`;
|
|
181
|
+
(0, log_1.logRequest)("POST", url, this.headers, requestBody);
|
|
182
|
+
const response = await fetch(url, {
|
|
183
|
+
method: "POST",
|
|
184
|
+
headers: this.headers,
|
|
185
|
+
body: JSON.stringify(requestBody),
|
|
186
|
+
});
|
|
187
|
+
if (!response.ok) {
|
|
188
|
+
const errorText = await response.text();
|
|
189
|
+
(0, log_1.logResponse)(response.status, response.statusText, response.headers, errorText);
|
|
190
|
+
throw new Error(`Gemini image chat API error: ${response.status} ${errorText}`);
|
|
191
|
+
}
|
|
192
|
+
const data = (await response.json());
|
|
193
|
+
(0, log_1.logResponse)(response.status, response.statusText, response.headers, data);
|
|
194
|
+
return data;
|
|
195
|
+
}
|
|
79
196
|
}
|
|
80
197
|
exports.GeminiImageGenerationClient = GeminiImageGenerationClient;
|
package/dist/index.d.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @see https://github.com/langchain-ai/langchainjs
|
|
6
6
|
*/
|
|
7
7
|
import { BaseChatModel, BaseChatModelParams } from "./base";
|
|
8
|
-
import { DoubaoImageGenerationClient, type DoubaoImageGenerationConfig, type DoubaoImageGenerationRequest, type DoubaoImageGenerationResponse } from "./doubao-image-generation";
|
|
8
|
+
import { DoubaoImageGenerationClient, DoubaoImageSize, type DoubaoImageGenerationConfig, type DoubaoImageGenerationRequest, type DoubaoImageGenerationResponse } from "./doubao-image-generation";
|
|
9
9
|
import { GeminiImageGenerationClient, type GeminiImageGenerationConfig, type GeminiImageGenerationRequest, type GeminiImageGenerationResponse } from "./gemini-image-generation";
|
|
10
10
|
import { VideoGenerationClient, type VideoGenerationConfig, type VideoGenerationRequest, type ContentGenerationTaskID, type ContentGenerationTask } from "./video_generation";
|
|
11
11
|
import { sdkConfig } from "./config";
|
|
@@ -19,9 +19,8 @@ export interface LangchainClientConfig {
|
|
|
19
19
|
token?: string;
|
|
20
20
|
headers?: Record<string, string>;
|
|
21
21
|
}
|
|
22
|
-
export { DoubaoImageGenerationClient, type DoubaoImageGenerationConfig, type DoubaoImageGenerationRequest, type DoubaoImageGenerationResponse, };
|
|
22
|
+
export { DoubaoImageGenerationClient, type DoubaoImageGenerationConfig, type DoubaoImageGenerationRequest, type DoubaoImageGenerationResponse, type DoubaoImageSize, };
|
|
23
23
|
export { GeminiImageGenerationClient, type GeminiImageGenerationConfig, type GeminiImageGenerationRequest, type GeminiImageGenerationResponse, };
|
|
24
|
-
export { DoubaoImageGenerationClient as ImageGenerationClient, type DoubaoImageGenerationConfig as ImageGenerationConfig, type DoubaoImageGenerationRequest as ImageGenerationRequest, type DoubaoImageGenerationResponse as ImageGenerationResponse, };
|
|
25
24
|
export { VideoGenerationClient, type VideoGenerationConfig, type VideoGenerationRequest, type ContentGenerationTaskID, type ContentGenerationTask, };
|
|
26
25
|
export { sdkConfig };
|
|
27
26
|
/**
|
package/dist/index.js
CHANGED
|
@@ -6,13 +6,12 @@
|
|
|
6
6
|
* @see https://github.com/langchain-ai/langchainjs
|
|
7
7
|
*/
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
-
exports.sdkConfig = exports.VideoGenerationClient = exports.
|
|
9
|
+
exports.sdkConfig = exports.VideoGenerationClient = exports.GeminiImageGenerationClient = exports.DoubaoImageGenerationClient = exports.ChatAnthropic = exports.ChatGoogleGenerativeAI = exports.ChatOpenAI = exports.BaseChatModel = exports.AIMessageChunk = exports.SystemMessage = exports.AIMessage = exports.HumanMessage = void 0;
|
|
10
10
|
exports.createChatModel = createChatModel;
|
|
11
11
|
const openai_1 = require("./chat_models/openai");
|
|
12
12
|
const google_1 = require("./chat_models/google");
|
|
13
13
|
const doubao_image_generation_1 = require("./doubao-image-generation");
|
|
14
14
|
Object.defineProperty(exports, "DoubaoImageGenerationClient", { enumerable: true, get: function () { return doubao_image_generation_1.DoubaoImageGenerationClient; } });
|
|
15
|
-
Object.defineProperty(exports, "ImageGenerationClient", { enumerable: true, get: function () { return doubao_image_generation_1.DoubaoImageGenerationClient; } });
|
|
16
15
|
const gemini_image_generation_1 = require("./gemini-image-generation");
|
|
17
16
|
Object.defineProperty(exports, "GeminiImageGenerationClient", { enumerable: true, get: function () { return gemini_image_generation_1.GeminiImageGenerationClient; } });
|
|
18
17
|
const video_generation_1 = require("./video_generation");
|
package/package.json
CHANGED