ai-world-sdk 1.0.2 → 1.0.5
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 +491 -52
- 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/doubao-image-generation.d.ts +36 -0
- package/dist/{image_generation.js → doubao-image-generation.js} +7 -7
- package/dist/gemini-image-generation.d.ts +36 -0
- package/dist/gemini-image-generation.js +62 -0
- package/dist/index.d.ts +5 -2
- package/dist/index.js +22 -34
- package/package.json +4 -3
- package/dist/image_generation.d.ts +0 -36
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Doubao Image Generation Client
|
|
3
|
+
* 豆包图像生成客户端
|
|
4
|
+
*/
|
|
5
|
+
export interface DoubaoImageGenerationConfig {
|
|
6
|
+
baseUrl?: string;
|
|
7
|
+
headers?: Record<string, string>;
|
|
8
|
+
}
|
|
9
|
+
export interface DoubaoImageGenerationRequest {
|
|
10
|
+
model?: string;
|
|
11
|
+
prompt: string;
|
|
12
|
+
size?: string;
|
|
13
|
+
watermark?: boolean;
|
|
14
|
+
n?: number;
|
|
15
|
+
quality?: "standard" | "hd";
|
|
16
|
+
response_format?: "url" | "b64_json";
|
|
17
|
+
style?: "vivid" | "natural";
|
|
18
|
+
user?: string;
|
|
19
|
+
}
|
|
20
|
+
export interface DoubaoImageData {
|
|
21
|
+
url?: string;
|
|
22
|
+
b64_json?: string;
|
|
23
|
+
}
|
|
24
|
+
export interface DoubaoImageGenerationResponse {
|
|
25
|
+
created: number;
|
|
26
|
+
data: DoubaoImageData[];
|
|
27
|
+
}
|
|
28
|
+
export declare class DoubaoImageGenerationClient {
|
|
29
|
+
private headers;
|
|
30
|
+
constructor(config?: DoubaoImageGenerationConfig);
|
|
31
|
+
/**
|
|
32
|
+
* Generate images
|
|
33
|
+
* 生成图像
|
|
34
|
+
*/
|
|
35
|
+
generate(request: DoubaoImageGenerationRequest): Promise<DoubaoImageGenerationResponse>;
|
|
36
|
+
}
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
/**
|
|
3
|
-
* Image Generation Client
|
|
4
|
-
*
|
|
3
|
+
* Doubao Image Generation Client
|
|
4
|
+
* 豆包图像生成客户端
|
|
5
5
|
*/
|
|
6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
-
exports.
|
|
7
|
+
exports.DoubaoImageGenerationClient = void 0;
|
|
8
8
|
const config_1 = require("./config");
|
|
9
9
|
const log_1 = require("./log");
|
|
10
|
-
class
|
|
10
|
+
class DoubaoImageGenerationClient {
|
|
11
11
|
constructor(config) {
|
|
12
12
|
// 合并全局 headers 和配置 headers
|
|
13
13
|
const globalHeaders = config_1.sdkConfig.getHeaders();
|
|
@@ -46,7 +46,7 @@ class ImageGenerationClient {
|
|
|
46
46
|
if (request.user) {
|
|
47
47
|
requestBody.user = request.user;
|
|
48
48
|
}
|
|
49
|
-
const url = `${config_1.sdkConfig.getServerUrl()}/api/image-proxy/generate`;
|
|
49
|
+
const url = `${config_1.sdkConfig.getServerUrl()}/api/doubao-image-proxy/generate`;
|
|
50
50
|
(0, log_1.logRequest)("POST", url, this.headers, requestBody);
|
|
51
51
|
const response = await fetch(url, {
|
|
52
52
|
method: "POST",
|
|
@@ -56,11 +56,11 @@ class ImageGenerationClient {
|
|
|
56
56
|
if (!response.ok) {
|
|
57
57
|
const errorText = await response.text();
|
|
58
58
|
(0, log_1.logResponse)(response.status, response.statusText, response.headers, errorText);
|
|
59
|
-
throw new Error(`
|
|
59
|
+
throw new Error(`Doubao image generation API error: ${response.status} ${errorText}`);
|
|
60
60
|
}
|
|
61
61
|
const data = (await response.json());
|
|
62
62
|
(0, log_1.logResponse)(response.status, response.statusText, response.headers, data);
|
|
63
63
|
return data;
|
|
64
64
|
}
|
|
65
65
|
}
|
|
66
|
-
exports.
|
|
66
|
+
exports.DoubaoImageGenerationClient = DoubaoImageGenerationClient;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Gemini Image Generation Client
|
|
3
|
+
* Google Gemini 图像生成客户端
|
|
4
|
+
*/
|
|
5
|
+
export interface GeminiImageGenerationConfig {
|
|
6
|
+
baseUrl?: string;
|
|
7
|
+
headers?: Record<string, string>;
|
|
8
|
+
}
|
|
9
|
+
export interface GeminiImageGenerationRequest {
|
|
10
|
+
prompt: string;
|
|
11
|
+
model?: string;
|
|
12
|
+
number_of_images?: number;
|
|
13
|
+
aspect_ratio?: "1:1" | "9:16" | "16:9" | "4:3" | "3:4";
|
|
14
|
+
temperature?: number;
|
|
15
|
+
max_output_tokens?: number;
|
|
16
|
+
user?: string;
|
|
17
|
+
}
|
|
18
|
+
export interface GeminiImageData {
|
|
19
|
+
url?: string;
|
|
20
|
+
b64_json?: string;
|
|
21
|
+
text?: string;
|
|
22
|
+
}
|
|
23
|
+
export interface GeminiImageGenerationResponse {
|
|
24
|
+
created: number;
|
|
25
|
+
data: GeminiImageData[];
|
|
26
|
+
text?: string;
|
|
27
|
+
}
|
|
28
|
+
export declare class GeminiImageGenerationClient {
|
|
29
|
+
private headers;
|
|
30
|
+
constructor(config?: GeminiImageGenerationConfig);
|
|
31
|
+
/**
|
|
32
|
+
* Generate images
|
|
33
|
+
* 生成图像
|
|
34
|
+
*/
|
|
35
|
+
generate(request: GeminiImageGenerationRequest): Promise<GeminiImageGenerationResponse>;
|
|
36
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Gemini Image Generation Client
|
|
4
|
+
* Google Gemini 图像生成客户端
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.GeminiImageGenerationClient = void 0;
|
|
8
|
+
const config_1 = require("./config");
|
|
9
|
+
const log_1 = require("./log");
|
|
10
|
+
class GeminiImageGenerationClient {
|
|
11
|
+
constructor(config) {
|
|
12
|
+
// 合并全局 headers 和配置 headers
|
|
13
|
+
const globalHeaders = config_1.sdkConfig.getHeaders();
|
|
14
|
+
this.headers = {
|
|
15
|
+
"Content-Type": "application/json",
|
|
16
|
+
"Authorization": `Bearer ${config_1.sdkConfig.getToken()}`,
|
|
17
|
+
"X-Base-Url": config?.baseUrl || "",
|
|
18
|
+
...globalHeaders,
|
|
19
|
+
...config?.headers,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Generate images
|
|
24
|
+
* 生成图像
|
|
25
|
+
*/
|
|
26
|
+
async generate(request) {
|
|
27
|
+
const requestBody = {
|
|
28
|
+
prompt: request.prompt,
|
|
29
|
+
model: request.model || "gemini-2.0-flash-exp-image-generation",
|
|
30
|
+
number_of_images: request.number_of_images || 1,
|
|
31
|
+
};
|
|
32
|
+
// 添加可选参数
|
|
33
|
+
if (request.aspect_ratio) {
|
|
34
|
+
requestBody.aspect_ratio = request.aspect_ratio;
|
|
35
|
+
}
|
|
36
|
+
if (request.temperature !== undefined) {
|
|
37
|
+
requestBody.temperature = request.temperature;
|
|
38
|
+
}
|
|
39
|
+
if (request.max_output_tokens) {
|
|
40
|
+
requestBody.max_output_tokens = request.max_output_tokens;
|
|
41
|
+
}
|
|
42
|
+
if (request.user) {
|
|
43
|
+
requestBody.user = request.user;
|
|
44
|
+
}
|
|
45
|
+
const url = `${config_1.sdkConfig.getServerUrl()}/api/gemini-image-proxy/generate`;
|
|
46
|
+
(0, log_1.logRequest)("POST", url, this.headers, requestBody);
|
|
47
|
+
const response = await fetch(url, {
|
|
48
|
+
method: "POST",
|
|
49
|
+
headers: this.headers,
|
|
50
|
+
body: JSON.stringify(requestBody),
|
|
51
|
+
});
|
|
52
|
+
if (!response.ok) {
|
|
53
|
+
const errorText = await response.text();
|
|
54
|
+
(0, log_1.logResponse)(response.status, response.statusText, response.headers, errorText);
|
|
55
|
+
throw new Error(`Gemini image generation API error: ${response.status} ${errorText}`);
|
|
56
|
+
}
|
|
57
|
+
const data = (await response.json());
|
|
58
|
+
(0, log_1.logResponse)(response.status, response.statusText, response.headers, data);
|
|
59
|
+
return data;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
exports.GeminiImageGenerationClient = GeminiImageGenerationClient;
|
package/dist/index.d.ts
CHANGED
|
@@ -5,7 +5,8 @@
|
|
|
5
5
|
* @see https://github.com/langchain-ai/langchainjs
|
|
6
6
|
*/
|
|
7
7
|
import { BaseChatModel, BaseChatModelParams } from "./base";
|
|
8
|
-
import {
|
|
8
|
+
import { DoubaoImageGenerationClient, type DoubaoImageGenerationConfig, type DoubaoImageGenerationRequest, type DoubaoImageGenerationResponse } from "./doubao-image-generation";
|
|
9
|
+
import { GeminiImageGenerationClient, type GeminiImageGenerationConfig, type GeminiImageGenerationRequest, type GeminiImageGenerationResponse } from "./gemini-image-generation";
|
|
9
10
|
import { VideoGenerationClient, type VideoGenerationConfig, type VideoGenerationRequest, type ContentGenerationTaskID, type ContentGenerationTask } from "./video_generation";
|
|
10
11
|
import { sdkConfig } from "./config";
|
|
11
12
|
export { BaseMessage, HumanMessage, AIMessage, SystemMessage, AIMessageChunk, type MessageContent, type AIMessageChunkData, } from "./messages";
|
|
@@ -18,7 +19,9 @@ export interface LangchainClientConfig {
|
|
|
18
19
|
token?: string;
|
|
19
20
|
headers?: Record<string, string>;
|
|
20
21
|
}
|
|
21
|
-
export {
|
|
22
|
+
export { DoubaoImageGenerationClient, type DoubaoImageGenerationConfig, type DoubaoImageGenerationRequest, type DoubaoImageGenerationResponse, };
|
|
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, };
|
|
22
25
|
export { VideoGenerationClient, type VideoGenerationConfig, type VideoGenerationRequest, type ContentGenerationTaskID, type ContentGenerationTask, };
|
|
23
26
|
export { sdkConfig };
|
|
24
27
|
/**
|
package/dist/index.js
CHANGED
|
@@ -6,13 +6,15 @@
|
|
|
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.ImageGenerationClient = exports.ChatAnthropic = exports.ChatGoogleGenerativeAI = exports.ChatOpenAI = exports.BaseChatModel = exports.AIMessageChunk = exports.SystemMessage = exports.AIMessage = exports.HumanMessage = void 0;
|
|
9
|
+
exports.sdkConfig = exports.VideoGenerationClient = exports.ImageGenerationClient = 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
|
-
const
|
|
14
|
-
|
|
15
|
-
Object.defineProperty(exports, "ImageGenerationClient", { enumerable: true, get: function () { return
|
|
13
|
+
const doubao_image_generation_1 = require("./doubao-image-generation");
|
|
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
|
+
const gemini_image_generation_1 = require("./gemini-image-generation");
|
|
17
|
+
Object.defineProperty(exports, "GeminiImageGenerationClient", { enumerable: true, get: function () { return gemini_image_generation_1.GeminiImageGenerationClient; } });
|
|
16
18
|
const video_generation_1 = require("./video_generation");
|
|
17
19
|
Object.defineProperty(exports, "VideoGenerationClient", { enumerable: true, get: function () { return video_generation_1.VideoGenerationClient; } });
|
|
18
20
|
const config_1 = require("./config");
|
|
@@ -29,8 +31,8 @@ var openai_2 = require("./chat_models/openai");
|
|
|
29
31
|
Object.defineProperty(exports, "ChatOpenAI", { enumerable: true, get: function () { return openai_2.ChatOpenAI; } });
|
|
30
32
|
var google_2 = require("./chat_models/google");
|
|
31
33
|
Object.defineProperty(exports, "ChatGoogleGenerativeAI", { enumerable: true, get: function () { return google_2.ChatGoogleGenerativeAI; } });
|
|
32
|
-
var
|
|
33
|
-
Object.defineProperty(exports, "ChatAnthropic", { enumerable: true, get: function () { return
|
|
34
|
+
var anthropic_1 = require("./chat_models/anthropic");
|
|
35
|
+
Object.defineProperty(exports, "ChatAnthropic", { enumerable: true, get: function () { return anthropic_1.ChatAnthropic; } });
|
|
34
36
|
/**
|
|
35
37
|
* Create a chat model instance based on model name
|
|
36
38
|
*/
|
|
@@ -46,33 +48,19 @@ function createChatModel(model, config) {
|
|
|
46
48
|
...config.headers,
|
|
47
49
|
},
|
|
48
50
|
};
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
else if (modelLower.startsWith("gemini")) {
|
|
64
|
-
return new google_1.ChatGoogleGenerativeAI({
|
|
65
|
-
modelName: model,
|
|
66
|
-
...finalConfig,
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
else if (modelLower.startsWith("claude")) {
|
|
70
|
-
return new anthropic_1.ChatAnthropic({
|
|
71
|
-
modelName: model,
|
|
72
|
-
...finalConfig,
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
else {
|
|
76
|
-
throw new Error(`Unsupported model: ${model}. Supported models: gpt-*, o1-*, doubao-*, gemini-*, claude-*`);
|
|
51
|
+
switch (config.provider) {
|
|
52
|
+
case "doubao":
|
|
53
|
+
case "aihubmix":
|
|
54
|
+
return new openai_1.ChatOpenAI({
|
|
55
|
+
modelName: model,
|
|
56
|
+
...finalConfig,
|
|
57
|
+
});
|
|
58
|
+
case "gemini":
|
|
59
|
+
return new google_1.ChatGoogleGenerativeAI({
|
|
60
|
+
modelName: model,
|
|
61
|
+
...finalConfig,
|
|
62
|
+
});
|
|
63
|
+
default:
|
|
64
|
+
throw new Error(`Unsupported provider: ${config.provider}`);
|
|
77
65
|
}
|
|
78
66
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ai-world-sdk",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.5",
|
|
4
4
|
"description": "TypeScript SDK for AI World Platform - Chat Models, Image Generation, and Video Generation",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -9,11 +9,12 @@
|
|
|
9
9
|
"prepublishOnly": "npm run build",
|
|
10
10
|
"test": "jest",
|
|
11
11
|
"test:watch": "jest --watch",
|
|
12
|
-
"test:non-stream": "jest -t 'ChatGoogleGenerativeAI - 非流式调用'",
|
|
13
|
-
"test:stream": "jest -t 'ChatGoogleGenerativeAI - 流式调用'",
|
|
12
|
+
"test:genimi-non-stream": "jest -t 'ChatGoogleGenerativeAI - 非流式调用'",
|
|
13
|
+
"test:genimi-stream": "jest -t 'ChatGoogleGenerativeAI - 流式调用'",
|
|
14
14
|
"test:factory": "jest -t '工厂函数'",
|
|
15
15
|
"test:bind": "jest -t 'bind 方法'",
|
|
16
16
|
"test:batch": "jest -t 'batch 方法'",
|
|
17
|
+
"test:openai": "jest -t 'ChatOpenAI - 基础测试'",
|
|
17
18
|
"test:aihubmix-no-stream": "jest -t 'aihubmix.com no stream'",
|
|
18
19
|
"test:aihubmix-stream": "jest -t 'aihubmix.com stream'",
|
|
19
20
|
"test:aihubmix-image": "jest -t 'aihubmix.com 图像生成'",
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Image Generation Client
|
|
3
|
-
* 图像生成客户端
|
|
4
|
-
*/
|
|
5
|
-
export interface ImageGenerationConfig {
|
|
6
|
-
baseUrl?: string;
|
|
7
|
-
headers?: Record<string, string>;
|
|
8
|
-
}
|
|
9
|
-
export interface ImageGenerationRequest {
|
|
10
|
-
model?: string;
|
|
11
|
-
prompt: string;
|
|
12
|
-
size?: string;
|
|
13
|
-
watermark?: boolean;
|
|
14
|
-
n?: number;
|
|
15
|
-
quality?: "standard" | "hd";
|
|
16
|
-
response_format?: "url" | "b64_json";
|
|
17
|
-
style?: "vivid" | "natural";
|
|
18
|
-
user?: string;
|
|
19
|
-
}
|
|
20
|
-
export interface ImageData {
|
|
21
|
-
url?: string;
|
|
22
|
-
b64_json?: string;
|
|
23
|
-
}
|
|
24
|
-
export interface ImageGenerationResponse {
|
|
25
|
-
created: number;
|
|
26
|
-
data: ImageData[];
|
|
27
|
-
}
|
|
28
|
-
export declare class ImageGenerationClient {
|
|
29
|
-
private headers;
|
|
30
|
-
constructor(config?: ImageGenerationConfig);
|
|
31
|
-
/**
|
|
32
|
-
* Generate images
|
|
33
|
-
* 生成图像
|
|
34
|
-
*/
|
|
35
|
-
generate(request: ImageGenerationRequest): Promise<ImageGenerationResponse>;
|
|
36
|
-
}
|