@vectorx/ai-sdk 0.1.3 → 0.3.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/{types → lib}/agent/index.d.ts +2 -2
- package/{types → lib}/ai.d.ts +7 -8
- package/lib/ai.js +18 -28
- package/{types → lib}/index.d.ts +3 -2
- package/lib/index.js +4 -3
- package/{types/types.d.ts → lib/model-type.d.ts} +24 -15
- package/lib/{types.js → model-type.js} +1 -4
- package/lib/models/Chat.d.ts +14 -0
- package/lib/models/Chat.js +36 -0
- package/lib/models/Default/index.d.ts +11 -0
- package/lib/models/{default.js → Default/index.js} +28 -11
- package/lib/models/QwenImage/index.d.ts +81 -0
- package/lib/models/QwenImage/index.js +208 -0
- package/lib/models/QwenSketchToImage/index.d.ts +35 -0
- package/lib/models/QwenSketchToImage/index.js +155 -0
- package/lib/models/QwenStyleRepaintV1/index.d.ts +114 -0
- package/lib/models/QwenStyleRepaintV1/index.js +213 -0
- package/lib/models/QwenVlMax/index.d.ts +85 -0
- package/lib/models/QwenVlMax/index.js +120 -0
- package/lib/models/index.d.ts +47 -0
- package/lib/models/index.js +40 -4
- package/{types → lib}/models/react.d.ts +3 -2
- package/lib/models/react.js +3 -3
- package/{types → lib}/stream.d.ts +1 -8
- package/lib/tokenManager.d.ts +36 -0
- package/lib/tokenManager.js +89 -0
- package/lib/utils.js +2 -3
- package/package.json +4 -5
- package/lib/models/model-types.js +0 -6
- package/types/models/default.d.ts +0 -13
- package/types/models/index.d.ts +0 -23
- package/types/models/model-types.d.ts +0 -131
- /package/{types → lib}/eventsource_parser/index.d.ts +0 -0
- /package/{types → lib}/eventsource_parser/parse.d.ts +0 -0
- /package/{types → lib}/eventsource_parser/stream.d.ts +0 -0
- /package/{types → lib}/eventsource_parser/types.d.ts +0 -0
- /package/{types → lib}/utils.d.ts +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { GetAgentInfoResponse, GetConversationsResponse, GetHistoryMessagesParams, GetHistoryMessagesResponse, SendMessageInput } from "@vectorx/
|
|
1
|
+
import type { GetAgentInfoResponse, GetConversationsResponse, GetHistoryMessagesParams, GetHistoryMessagesResponse, SendMessageInput } from "@vectorx/ai-types";
|
|
2
2
|
import type { IAbstractRequest } from "@vectorx/ai-types";
|
|
3
|
-
import type { IAgent, IAgentOptions } from "../
|
|
3
|
+
import type { IAgent, IAgentOptions } from "../model-type";
|
|
4
4
|
export declare enum AiSdkEnv {
|
|
5
5
|
Cloud = "cloud",
|
|
6
6
|
MiniProgram = "miniprogram"
|
package/{types → lib}/ai.d.ts
RENAMED
|
@@ -1,17 +1,16 @@
|
|
|
1
1
|
import type { IAbstractRequest } from "@vectorx/ai-types";
|
|
2
2
|
import { Agent } from "./agent";
|
|
3
3
|
import { AiSdkEnv } from "./index";
|
|
4
|
+
import type * as types from "./model-type";
|
|
4
5
|
import * as models from "./models";
|
|
5
|
-
import
|
|
6
|
-
declare class AI {
|
|
7
|
-
baseUrl: string;
|
|
8
|
-
request: IAbstractRequest;
|
|
9
|
-
modelSubUrl: string;
|
|
6
|
+
import { TokenManager } from "./tokenManager";
|
|
7
|
+
export declare class AI {
|
|
10
8
|
env: AiSdkEnv;
|
|
9
|
+
protected baseUrl: string;
|
|
10
|
+
protected request: IAbstractRequest;
|
|
11
|
+
tokenManager: TokenManager;
|
|
11
12
|
constructor(request: IAbstractRequest, baseUrl: string, env: AiSdkEnv);
|
|
12
|
-
createModel(model: models.ModelName): models.
|
|
13
|
-
modelRequest: (options: types.ModelRequestOptions) => Promise<any>;
|
|
13
|
+
createModel(model: models.ModelName | models.MultiModalModelName): models.ReActModel;
|
|
14
14
|
createAgent(options: types.IAgentOptions): Agent;
|
|
15
15
|
registerFunctionTool(functionTool: types.FunctionTool): void;
|
|
16
16
|
}
|
|
17
|
-
export { AI };
|
package/lib/ai.js
CHANGED
|
@@ -32,50 +32,40 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
32
32
|
return result;
|
|
33
33
|
};
|
|
34
34
|
})();
|
|
35
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
36
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
37
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
38
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
39
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
40
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
41
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
42
|
-
});
|
|
43
|
-
};
|
|
44
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
45
36
|
exports.AI = void 0;
|
|
46
37
|
const agent_1 = require("./agent");
|
|
47
38
|
const index_1 = require("./index");
|
|
48
39
|
const models = __importStar(require("./models"));
|
|
49
|
-
const
|
|
40
|
+
const tokenManager_1 = require("./tokenManager");
|
|
50
41
|
class AI {
|
|
51
42
|
constructor(request, baseUrl, env) {
|
|
52
|
-
this.
|
|
53
|
-
this.modelRequest = (options) => __awaiter(this, void 0, void 0, function* () {
|
|
54
|
-
const fetchHeaders = {
|
|
55
|
-
"Content-Type": "application/json",
|
|
56
|
-
};
|
|
57
|
-
options.stream && Object.assign(fetchHeaders, { Accept: "text/event-stream" });
|
|
58
|
-
const { data: responseData, header } = (yield this.request.fetch({
|
|
59
|
-
method: "post",
|
|
60
|
-
headers: Object.assign({}, fetchHeaders),
|
|
61
|
-
body: JSON.stringify(options),
|
|
62
|
-
url: `${this.baseUrl}/${this.modelSubUrl}`,
|
|
63
|
-
stream: options.stream,
|
|
64
|
-
}));
|
|
65
|
-
return (0, utils_1.handleResponseData)(responseData, header);
|
|
66
|
-
});
|
|
43
|
+
this.env = index_1.AiSdkEnv.Cloud;
|
|
67
44
|
this.baseUrl = baseUrl;
|
|
68
45
|
this.request = request;
|
|
69
46
|
this.env = env;
|
|
47
|
+
this.tokenManager = new tokenManager_1.TokenManager(this.request, this.baseUrl);
|
|
70
48
|
}
|
|
71
49
|
createModel(model) {
|
|
72
50
|
if (this.env === index_1.AiSdkEnv.MiniProgram) {
|
|
73
51
|
console.warn(`[${index_1.AiSdkEnv.MiniProgram}] is not supported for model creation`);
|
|
74
52
|
return null;
|
|
75
53
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
54
|
+
if (!models.isValidModel(model)) {
|
|
55
|
+
throw new Error(`[${model}] is not supported, please check the model name`);
|
|
56
|
+
}
|
|
57
|
+
switch (model) {
|
|
58
|
+
case models.MultiModalModelName.QwenImage:
|
|
59
|
+
return new models.ReActModel(new models.QwenImageModel(this.request, models.QwenImageModel.BASE_URL, this.tokenManager));
|
|
60
|
+
case models.MultiModalModelName.WanxSketchToImageLite:
|
|
61
|
+
return new models.ReActModel(new models.WanxSketchToImageLiteModel(this.request, models.WanxSketchToImageLiteModel.BASE_URL, this.tokenManager));
|
|
62
|
+
case models.MultiModalModelName.QwenStyleRepaintV1:
|
|
63
|
+
return new models.ReActModel(new models.QwenStyleRepaintV1Model(this.request, models.QwenStyleRepaintV1Model.BASE_URL, this.tokenManager));
|
|
64
|
+
case models.MultiModalModelName.QwenVlMax:
|
|
65
|
+
return new models.ReActModel(new models.QwenVlMax(this.request, models.QwenVlMax.BASE_URL, model, this.tokenManager));
|
|
66
|
+
default:
|
|
67
|
+
return new models.ReActModel(new models.DefaultSimpleModel(this.request, this.baseUrl, model));
|
|
68
|
+
}
|
|
79
69
|
}
|
|
80
70
|
createAgent(options) {
|
|
81
71
|
if (this.env === index_1.AiSdkEnv.Cloud) {
|
package/{types → lib}/index.d.ts
RENAMED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import type { IAbstractRequest } from "@vectorx/ai-types";
|
|
2
2
|
import { AiSdkEnv } from "./agent";
|
|
3
3
|
import { AI } from "./ai";
|
|
4
|
+
import { IAgentEnv } from "./model-type";
|
|
4
5
|
import * as models from "./models";
|
|
5
|
-
import { IAgentEnv } from "./types";
|
|
6
6
|
interface ICreateAi {
|
|
7
7
|
getBaseUrl: () => string;
|
|
8
8
|
request: IAbstractRequest;
|
|
9
9
|
env?: AiSdkEnv;
|
|
10
10
|
}
|
|
11
11
|
declare function createAi({ getBaseUrl, request, env }: ICreateAi): AI;
|
|
12
|
-
export * from "./
|
|
12
|
+
export * from "./model-type";
|
|
13
|
+
export * from "./tokenManager";
|
|
13
14
|
export { createAi, models, AI, AiSdkEnv, IAgentEnv };
|
package/lib/index.js
CHANGED
|
@@ -42,14 +42,15 @@ const agent_1 = require("./agent");
|
|
|
42
42
|
Object.defineProperty(exports, "AiSdkEnv", { enumerable: true, get: function () { return agent_1.AiSdkEnv; } });
|
|
43
43
|
const ai_1 = require("./ai");
|
|
44
44
|
Object.defineProperty(exports, "AI", { enumerable: true, get: function () { return ai_1.AI; } });
|
|
45
|
+
const model_type_1 = require("./model-type");
|
|
46
|
+
Object.defineProperty(exports, "IAgentEnv", { enumerable: true, get: function () { return model_type_1.IAgentEnv; } });
|
|
45
47
|
const models = __importStar(require("./models"));
|
|
46
48
|
exports.models = models;
|
|
47
|
-
const types_1 = require("./types");
|
|
48
|
-
Object.defineProperty(exports, "IAgentEnv", { enumerable: true, get: function () { return types_1.IAgentEnv; } });
|
|
49
49
|
function createAi({ getBaseUrl, request, env = agent_1.AiSdkEnv.Cloud }) {
|
|
50
50
|
if (getBaseUrl == null || typeof getBaseUrl !== "function") {
|
|
51
51
|
throw new Error("`getBaseUrl` is required!");
|
|
52
52
|
}
|
|
53
53
|
return new ai_1.AI(request, getBaseUrl(), env);
|
|
54
54
|
}
|
|
55
|
-
__exportStar(require("./
|
|
55
|
+
__exportStar(require("./model-type"), exports);
|
|
56
|
+
__exportStar(require("./tokenManager"), exports);
|
|
@@ -1,23 +1,16 @@
|
|
|
1
|
-
import type { GetAgentInfoResponse, GetConversationsResponse, GetHistoryMessagesParams, GetHistoryMessagesResponse, SendMessageInput } from "@vectorx/
|
|
1
|
+
import type { GetAgentInfoResponse, GetConversationsResponse, GetHistoryMessagesParams, GetHistoryMessagesResponse, QueryTasksResponse, SendMessageInput } from "@vectorx/ai-types";
|
|
2
2
|
import type { ModelName } from "./models";
|
|
3
|
-
export type { GetAgentInfoResponse, GetConversationsResponse, GetHistoryMessagesParams, GetHistoryMessagesResponse, SendMessageInput, };
|
|
4
|
-
type
|
|
5
|
-
|
|
6
|
-
};
|
|
7
|
-
export type DoGenerateOutput = BaseDoGenerateOutput & RawResponse;
|
|
8
|
-
export type DoStreamOutput = AsyncIterableReadableStream<BaseDoStreamOutputChunk & RawResponse>;
|
|
9
|
-
export type ChatModelConstructor = typeof SimpleChatModel;
|
|
3
|
+
export type { GetAgentInfoResponse, GetConversationsResponse, GetHistoryMessagesParams, GetHistoryMessagesResponse, SendMessageInput, QueryTasksResponse, };
|
|
4
|
+
export type DoGenerateOutput = BaseDoGenerateOutput;
|
|
5
|
+
export type DoStreamOutput = AsyncIterableReadableStream<BaseDoStreamOutputChunk>;
|
|
10
6
|
export type AsyncIterableReadableStream<T> = ReadableStream<T> & {
|
|
11
7
|
[Symbol.asyncIterator]: () => {
|
|
12
8
|
next(): Promise<IteratorResult<T>>;
|
|
13
9
|
};
|
|
14
10
|
};
|
|
15
|
-
export declare abstract class SimpleChatModel {
|
|
16
|
-
abstract doGenerate(data: ModelRequestOptions, options?: ReqOptions): Promise<DoGenerateOutput>;
|
|
17
|
-
abstract doStream(data: ModelRequestOptions, options?: ReqOptions): Promise<DoStreamOutput>;
|
|
18
|
-
}
|
|
19
11
|
export interface ModelRequestOptions {
|
|
20
|
-
|
|
12
|
+
async?: boolean;
|
|
13
|
+
model: ModelName | string;
|
|
21
14
|
max_tokens?: number;
|
|
22
15
|
temperature?: number;
|
|
23
16
|
top_p?: number;
|
|
@@ -32,7 +25,15 @@ export interface ModelRequestOptions {
|
|
|
32
25
|
}>;
|
|
33
26
|
messages?: Array<{
|
|
34
27
|
role: string;
|
|
35
|
-
content: string
|
|
28
|
+
content: string | Array<{
|
|
29
|
+
type: "text";
|
|
30
|
+
text: string;
|
|
31
|
+
} | {
|
|
32
|
+
type: "image_url";
|
|
33
|
+
image_url: {
|
|
34
|
+
url: string;
|
|
35
|
+
};
|
|
36
|
+
}>;
|
|
36
37
|
}>;
|
|
37
38
|
knowledge_base?: Array<{
|
|
38
39
|
knowledge_base_id: string;
|
|
@@ -55,6 +56,8 @@ export interface IAgentReqInput {
|
|
|
55
56
|
}
|
|
56
57
|
export interface ReqOptions {
|
|
57
58
|
timeout?: number;
|
|
59
|
+
headers?: Record<string, string>;
|
|
60
|
+
method?: string;
|
|
58
61
|
}
|
|
59
62
|
export type AgentReq = <T extends IAgentReqInput>(props: T) => T["stream"] extends true ? Promise<ReadableStream<Uint8Array>> : Promise<Object>;
|
|
60
63
|
export type UserMessage = {
|
|
@@ -113,13 +116,14 @@ export interface ChatCompletion {
|
|
|
113
116
|
log_id: string;
|
|
114
117
|
object: string;
|
|
115
118
|
error: string;
|
|
119
|
+
message?: string;
|
|
116
120
|
code: number;
|
|
117
121
|
created: number;
|
|
118
122
|
model: string;
|
|
119
123
|
choices: Choice[];
|
|
120
124
|
usage: Usage;
|
|
121
125
|
}
|
|
122
|
-
export type MessageType = "answer" | "function_call" | "function_call_response" | "knowledge_base_search" | "knowledge_base_search_response";
|
|
126
|
+
export type MessageType = "answer" | "image" | "video" | "async_task" | "function_call" | "function_call_response" | "knowledge_base_search" | "knowledge_base_search_response" | "error";
|
|
123
127
|
export interface Choice {
|
|
124
128
|
index: number;
|
|
125
129
|
message: {
|
|
@@ -133,6 +137,10 @@ export interface Choice {
|
|
|
133
137
|
search_results?: any[];
|
|
134
138
|
};
|
|
135
139
|
finish_reason: string;
|
|
140
|
+
delta?: {
|
|
141
|
+
content?: string;
|
|
142
|
+
reasoning_content?: string;
|
|
143
|
+
};
|
|
136
144
|
}
|
|
137
145
|
interface Usage {
|
|
138
146
|
prompt_tokens: number;
|
|
@@ -146,6 +154,7 @@ export interface IAgent {
|
|
|
146
154
|
getHistoryMessages?(input: GetHistoryMessagesParams): Promise<GetHistoryMessagesResponse>;
|
|
147
155
|
getAgentInfo?(): Promise<GetAgentInfoResponse>;
|
|
148
156
|
getConversations?(): Promise<GetConversationsResponse>;
|
|
157
|
+
queryTasks?(task_id?: string): Promise<QueryTasksResponse>;
|
|
149
158
|
}
|
|
150
159
|
export declare enum IAgentEnv {
|
|
151
160
|
Production = "production",
|
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.IAgentEnv =
|
|
4
|
-
class SimpleChatModel {
|
|
5
|
-
}
|
|
6
|
-
exports.SimpleChatModel = SimpleChatModel;
|
|
3
|
+
exports.IAgentEnv = void 0;
|
|
7
4
|
var IAgentEnv;
|
|
8
5
|
(function (IAgentEnv) {
|
|
9
6
|
IAgentEnv["Production"] = "production";
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { IAbstractRequest } from "@vectorx/ai-types";
|
|
2
|
+
import { DoGenerateOutput, DoStreamOutput, ModelRequestOptions, ReqOptions } from "../model-type";
|
|
3
|
+
import { TokenManager } from "../tokenManager";
|
|
4
|
+
export declare abstract class SimpleChatModel {
|
|
5
|
+
protected req: IAbstractRequest;
|
|
6
|
+
protected baseUrl: string;
|
|
7
|
+
protected subUrl: string;
|
|
8
|
+
protected tokenManager?: TokenManager;
|
|
9
|
+
constructor(req: IAbstractRequest, baseUrl: string, subUrl: string, tokenManager?: TokenManager);
|
|
10
|
+
abstract doGenerate(data: ModelRequestOptions, options?: ReqOptions): Promise<DoGenerateOutput>;
|
|
11
|
+
abstract doStream(data: ModelRequestOptions, options?: ReqOptions): Promise<DoStreamOutput>;
|
|
12
|
+
protected getValidToken(): Promise<string>;
|
|
13
|
+
protected createAuthHeaders(additionalHeaders?: Record<string, string>): Promise<Record<string, string>>;
|
|
14
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.SimpleChatModel = void 0;
|
|
13
|
+
class SimpleChatModel {
|
|
14
|
+
constructor(req, baseUrl, subUrl, tokenManager) {
|
|
15
|
+
this.req = req;
|
|
16
|
+
this.baseUrl = baseUrl;
|
|
17
|
+
this.subUrl = subUrl;
|
|
18
|
+
if (tokenManager)
|
|
19
|
+
this.tokenManager = tokenManager;
|
|
20
|
+
}
|
|
21
|
+
getValidToken() {
|
|
22
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
23
|
+
if (!this.tokenManager) {
|
|
24
|
+
throw new Error("TokenManager is not set");
|
|
25
|
+
}
|
|
26
|
+
return yield this.tokenManager.getValidToken();
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
createAuthHeaders() {
|
|
30
|
+
return __awaiter(this, arguments, void 0, function* (additionalHeaders = {}) {
|
|
31
|
+
const token = yield this.getValidToken();
|
|
32
|
+
return Object.assign({ "Content-Type": "application/json", Authorization: `Bearer ${token}` }, additionalHeaders);
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
exports.SimpleChatModel = SimpleChatModel;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { IAbstractRequest } from "@vectorx/ai-types";
|
|
2
|
+
import type { DoGenerateOutput, DoStreamOutput, ModelRequestOptions, ReqOptions } from "../../model-type";
|
|
3
|
+
import { SimpleChatModel } from "../Chat";
|
|
4
|
+
import type { ModelName } from "../index";
|
|
5
|
+
export declare class DefaultSimpleModel extends SimpleChatModel {
|
|
6
|
+
modelName: ModelName;
|
|
7
|
+
constructor(req: IAbstractRequest, baseUrl: string, modelName: ModelName);
|
|
8
|
+
protected modelRequest(data: ModelRequestOptions, options?: ReqOptions): Promise<ReadableStream<Uint8Array> | Promise<unknown>>;
|
|
9
|
+
doGenerate(data: ModelRequestOptions, options?: ReqOptions): Promise<DoGenerateOutput>;
|
|
10
|
+
doStream(data: ModelRequestOptions, options?: ReqOptions): Promise<DoStreamOutput>;
|
|
11
|
+
}
|
|
@@ -9,11 +9,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.DefaultSimpleModel =
|
|
13
|
-
const stream_1 = require("
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
exports.SimpleChatModel = SimpleChatModel;
|
|
12
|
+
exports.DefaultSimpleModel = void 0;
|
|
13
|
+
const stream_1 = require("../../stream");
|
|
14
|
+
const utils_1 = require("../../utils");
|
|
15
|
+
const Chat_1 = require("../Chat");
|
|
17
16
|
const defaultOptions = {
|
|
18
17
|
max_tokens: 1000,
|
|
19
18
|
temperature: 0.7,
|
|
@@ -26,22 +25,40 @@ const defaultOptions = {
|
|
|
26
25
|
enable_thinking: false,
|
|
27
26
|
enable_search: false,
|
|
28
27
|
};
|
|
29
|
-
class DefaultSimpleModel {
|
|
30
|
-
constructor(req, modelName) {
|
|
31
|
-
|
|
28
|
+
class DefaultSimpleModel extends Chat_1.SimpleChatModel {
|
|
29
|
+
constructor(req, baseUrl, modelName) {
|
|
30
|
+
super(req, baseUrl, "conversation/chat");
|
|
32
31
|
this.modelName = modelName;
|
|
33
32
|
}
|
|
33
|
+
modelRequest(data_1) {
|
|
34
|
+
return __awaiter(this, arguments, void 0, function* (data, options = {
|
|
35
|
+
timeout: 30 * 1000,
|
|
36
|
+
}) {
|
|
37
|
+
const fetchHeaders = Object.assign({ "Content-Type": "application/json" }, ((options === null || options === void 0 ? void 0 : options.headers) || {}));
|
|
38
|
+
data.stream && Object.assign(fetchHeaders, { Accept: "text/event-stream" });
|
|
39
|
+
const { data: responseData, header } = (yield this.req.fetch({
|
|
40
|
+
url: `${this.baseUrl}/${this.subUrl}`,
|
|
41
|
+
headers: Object.assign({}, fetchHeaders),
|
|
42
|
+
body: JSON.stringify(data),
|
|
43
|
+
method: "post",
|
|
44
|
+
stream: Boolean(data.stream),
|
|
45
|
+
}));
|
|
46
|
+
return (0, utils_1.handleResponseData)(responseData, header);
|
|
47
|
+
});
|
|
48
|
+
}
|
|
34
49
|
doGenerate(data, options) {
|
|
35
50
|
return __awaiter(this, void 0, void 0, function* () {
|
|
36
51
|
data.model = this.modelName;
|
|
37
|
-
const
|
|
38
|
-
|
|
52
|
+
const payload = Object.assign(Object.assign(Object.assign({}, defaultOptions), data), { stream: false });
|
|
53
|
+
const res = yield this.modelRequest(payload, options);
|
|
54
|
+
return res;
|
|
39
55
|
});
|
|
40
56
|
}
|
|
41
57
|
doStream(data, options) {
|
|
42
58
|
return __awaiter(this, void 0, void 0, function* () {
|
|
43
59
|
data.model = this.modelName;
|
|
44
|
-
const
|
|
60
|
+
const payload = Object.assign(Object.assign(Object.assign({}, defaultOptions), data), { stream: true });
|
|
61
|
+
const _stream = (yield this.modelRequest(payload, options));
|
|
45
62
|
const stream = (0, stream_1.toPolyfillReadable)(_stream);
|
|
46
63
|
const standardStream = (0, stream_1.intoStandardStream)(stream);
|
|
47
64
|
return (0, stream_1.createAsyncIterable)(standardStream);
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import type { IAbstractRequest } from "@vectorx/ai-types";
|
|
2
|
+
import type { DoGenerateOutput, DoStreamOutput, ModelRequestOptions, ReqOptions } from "../../model-type";
|
|
3
|
+
import { TokenManager } from "../../tokenManager";
|
|
4
|
+
import { SimpleChatModel } from "../Chat";
|
|
5
|
+
export interface QwenImageParameters {
|
|
6
|
+
negative_prompt?: string;
|
|
7
|
+
size?: "1664*928" | "1472*1140" | "1328*1328" | "1140*1472" | "928*1664" | string;
|
|
8
|
+
n?: 1;
|
|
9
|
+
prompt_extend?: boolean;
|
|
10
|
+
watermark?: boolean;
|
|
11
|
+
}
|
|
12
|
+
export interface QwenImageAPIInput {
|
|
13
|
+
model: string;
|
|
14
|
+
input: {
|
|
15
|
+
messages?: Array<{
|
|
16
|
+
role: "user";
|
|
17
|
+
content: Array<{
|
|
18
|
+
text: string;
|
|
19
|
+
}>;
|
|
20
|
+
}>;
|
|
21
|
+
prompt?: string;
|
|
22
|
+
};
|
|
23
|
+
parameters?: QwenImageParameters;
|
|
24
|
+
}
|
|
25
|
+
export type QwenImageRequestOptions = QwenImageAPIInput & {
|
|
26
|
+
parameters?: QwenImageParameters;
|
|
27
|
+
};
|
|
28
|
+
export type QwenImageContentItem = {
|
|
29
|
+
image?: string;
|
|
30
|
+
url?: string;
|
|
31
|
+
image_url?: string | {
|
|
32
|
+
url: string;
|
|
33
|
+
};
|
|
34
|
+
b64_json?: string;
|
|
35
|
+
[key: string]: any;
|
|
36
|
+
};
|
|
37
|
+
export interface QwenImageAPIResponse {
|
|
38
|
+
async?: boolean;
|
|
39
|
+
output: {
|
|
40
|
+
choices?: Array<{
|
|
41
|
+
finish_reason: string;
|
|
42
|
+
message: {
|
|
43
|
+
role: "assistant" | "user";
|
|
44
|
+
content: QwenImageContentItem[];
|
|
45
|
+
};
|
|
46
|
+
}>;
|
|
47
|
+
task_status?: string;
|
|
48
|
+
task_id?: string;
|
|
49
|
+
task_metric?: {
|
|
50
|
+
TOTAL: number;
|
|
51
|
+
FAILED: number;
|
|
52
|
+
SUCCEEDED: number;
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
usage?: {
|
|
56
|
+
width?: number;
|
|
57
|
+
height?: number;
|
|
58
|
+
image_count?: number;
|
|
59
|
+
};
|
|
60
|
+
request_id?: string;
|
|
61
|
+
id?: string;
|
|
62
|
+
model?: string;
|
|
63
|
+
created?: number;
|
|
64
|
+
object?: string;
|
|
65
|
+
code?: number;
|
|
66
|
+
error?: string;
|
|
67
|
+
}
|
|
68
|
+
export declare class QwenImageModel extends SimpleChatModel {
|
|
69
|
+
static BASE_URL: string;
|
|
70
|
+
static SUB_SYNTHESIS_URL: string;
|
|
71
|
+
static SUB_GENERATION_URL: string;
|
|
72
|
+
modelName: string;
|
|
73
|
+
constructor(req: IAbstractRequest, baseUrl: string, tokenManager: TokenManager);
|
|
74
|
+
protected normalizeStandardImageCompletion(res: QwenImageAPIResponse, fallbackModel: string): DoGenerateOutput;
|
|
75
|
+
protected coverModelRequestToQwenInput(data: ModelRequestOptions & {
|
|
76
|
+
parameters?: QwenImageParameters;
|
|
77
|
+
}): QwenImageRequestOptions;
|
|
78
|
+
protected modelRequest(data: QwenImageRequestOptions, options?: ReqOptions): Promise<ReadableStream<Uint8Array> | Promise<unknown>>;
|
|
79
|
+
doGenerate(data: ModelRequestOptions, options?: ReqOptions): Promise<DoGenerateOutput>;
|
|
80
|
+
doStream(data: ModelRequestOptions, options?: ReqOptions): Promise<DoStreamOutput>;
|
|
81
|
+
}
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.QwenImageModel = void 0;
|
|
13
|
+
const stream_1 = require("../../stream");
|
|
14
|
+
const utils_1 = require("../../utils");
|
|
15
|
+
const Chat_1 = require("../Chat");
|
|
16
|
+
const index_1 = require("../index");
|
|
17
|
+
class QwenImageModel extends Chat_1.SimpleChatModel {
|
|
18
|
+
constructor(req, baseUrl, tokenManager) {
|
|
19
|
+
super(req, baseUrl, QwenImageModel.SUB_GENERATION_URL, tokenManager);
|
|
20
|
+
this.modelName = index_1.modelName[index_1.MultiModalModelName.QwenImage];
|
|
21
|
+
}
|
|
22
|
+
normalizeStandardImageCompletion(res, fallbackModel) {
|
|
23
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
|
|
24
|
+
const qOutput = (res === null || res === void 0 ? void 0 : res.output) || {};
|
|
25
|
+
if ((qOutput === null || qOutput === void 0 ? void 0 : qOutput.task_status) && (qOutput === null || qOutput === void 0 ? void 0 : qOutput.task_id)) {
|
|
26
|
+
const created = (_a = res === null || res === void 0 ? void 0 : res.created) !== null && _a !== void 0 ? _a : Math.floor(Date.now() / 1000);
|
|
27
|
+
const id = (res === null || res === void 0 ? void 0 : res.id) || (res === null || res === void 0 ? void 0 : res.request_id) || "";
|
|
28
|
+
const normalized = {
|
|
29
|
+
id,
|
|
30
|
+
object: (_b = res === null || res === void 0 ? void 0 : res.object) !== null && _b !== void 0 ? _b : "chat.completion",
|
|
31
|
+
created,
|
|
32
|
+
model: (_c = res === null || res === void 0 ? void 0 : res.model) !== null && _c !== void 0 ? _c : fallbackModel,
|
|
33
|
+
log_id: id,
|
|
34
|
+
error: (_d = res === null || res === void 0 ? void 0 : res.error) !== null && _d !== void 0 ? _d : "",
|
|
35
|
+
code: (_e = res === null || res === void 0 ? void 0 : res.code) !== null && _e !== void 0 ? _e : 0,
|
|
36
|
+
choices: [
|
|
37
|
+
{
|
|
38
|
+
index: 0,
|
|
39
|
+
message: {
|
|
40
|
+
id,
|
|
41
|
+
role: "assistant",
|
|
42
|
+
type: "async_task",
|
|
43
|
+
content: JSON.stringify(Object.assign(Object.assign({}, ((res === null || res === void 0 ? void 0 : res.output) || {})), { request_id: (res === null || res === void 0 ? void 0 : res.request_id) || id })),
|
|
44
|
+
reasoning_content: "",
|
|
45
|
+
},
|
|
46
|
+
finish_reason: "stop",
|
|
47
|
+
},
|
|
48
|
+
],
|
|
49
|
+
usage: mapUsageToStandard(res === null || res === void 0 ? void 0 : res.usage),
|
|
50
|
+
};
|
|
51
|
+
return normalized;
|
|
52
|
+
}
|
|
53
|
+
const first = ((_g = (_f = qOutput === null || qOutput === void 0 ? void 0 : qOutput.choices) === null || _f === void 0 ? void 0 : _f[0]) !== null && _g !== void 0 ? _g : null);
|
|
54
|
+
const message = (_h = first === null || first === void 0 ? void 0 : first.message) !== null && _h !== void 0 ? _h : {};
|
|
55
|
+
const contentUrl = Array.isArray(message === null || message === void 0 ? void 0 : message.content) && ((_j = message.content[0]) === null || _j === void 0 ? void 0 : _j.image) ? String(message.content[0].image) : "";
|
|
56
|
+
const created = (_k = res === null || res === void 0 ? void 0 : res.created) !== null && _k !== void 0 ? _k : Math.floor(Date.now() / 1000);
|
|
57
|
+
const id = (res === null || res === void 0 ? void 0 : res.id) || (res === null || res === void 0 ? void 0 : res.request_id) || "";
|
|
58
|
+
const normalized = {
|
|
59
|
+
id,
|
|
60
|
+
object: (_l = res === null || res === void 0 ? void 0 : res.object) !== null && _l !== void 0 ? _l : "chat.completion",
|
|
61
|
+
created,
|
|
62
|
+
model: (_m = res === null || res === void 0 ? void 0 : res.model) !== null && _m !== void 0 ? _m : fallbackModel,
|
|
63
|
+
log_id: id,
|
|
64
|
+
error: (_o = res === null || res === void 0 ? void 0 : res.error) !== null && _o !== void 0 ? _o : "",
|
|
65
|
+
code: (_p = res === null || res === void 0 ? void 0 : res.code) !== null && _p !== void 0 ? _p : 0,
|
|
66
|
+
choices: [
|
|
67
|
+
{
|
|
68
|
+
index: 0,
|
|
69
|
+
message: {
|
|
70
|
+
id,
|
|
71
|
+
role: "assistant",
|
|
72
|
+
type: "image",
|
|
73
|
+
content: contentUrl || "",
|
|
74
|
+
reasoning_content: "",
|
|
75
|
+
},
|
|
76
|
+
finish_reason: (_q = first === null || first === void 0 ? void 0 : first.finish_reason) !== null && _q !== void 0 ? _q : "stop",
|
|
77
|
+
},
|
|
78
|
+
],
|
|
79
|
+
usage: mapUsageToStandard(res === null || res === void 0 ? void 0 : res.usage),
|
|
80
|
+
};
|
|
81
|
+
return normalized;
|
|
82
|
+
}
|
|
83
|
+
coverModelRequestToQwenInput(data) {
|
|
84
|
+
var _a;
|
|
85
|
+
let text = "";
|
|
86
|
+
const messages = data.messages || data.history || [];
|
|
87
|
+
if (Array.isArray(messages) && messages.length > 0) {
|
|
88
|
+
const firstUser = (_a = messages.find((m) => (m === null || m === void 0 ? void 0 : m.role) === "user")) !== null && _a !== void 0 ? _a : messages[0];
|
|
89
|
+
const c = firstUser === null || firstUser === void 0 ? void 0 : firstUser.content;
|
|
90
|
+
if (typeof c === "string" && c.trim()) {
|
|
91
|
+
text = c.trim();
|
|
92
|
+
}
|
|
93
|
+
else if (Array.isArray(c)) {
|
|
94
|
+
for (const p of c) {
|
|
95
|
+
if ((p === null || p === void 0 ? void 0 : p.type) === "text" && typeof p.text === "string" && p.text.trim()) {
|
|
96
|
+
text = p.text.trim();
|
|
97
|
+
break;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
if (!text && data.msg)
|
|
103
|
+
text = String(data.msg);
|
|
104
|
+
if (!text)
|
|
105
|
+
throw new Error("QwenImage 需要提供一个 text 提示词");
|
|
106
|
+
const isAsync = !!(data === null || data === void 0 ? void 0 : data.async);
|
|
107
|
+
return {
|
|
108
|
+
parameters: data.parameters,
|
|
109
|
+
model: this.modelName,
|
|
110
|
+
input: isAsync
|
|
111
|
+
? {
|
|
112
|
+
prompt: text,
|
|
113
|
+
}
|
|
114
|
+
: {
|
|
115
|
+
messages: [
|
|
116
|
+
{
|
|
117
|
+
role: "user",
|
|
118
|
+
content: [{ text }],
|
|
119
|
+
},
|
|
120
|
+
],
|
|
121
|
+
},
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
modelRequest(data_1) {
|
|
125
|
+
return __awaiter(this, arguments, void 0, function* (data, options = { timeout: 30 * 1000 }) {
|
|
126
|
+
const fetchHeaders = yield this.createAuthHeaders(options === null || options === void 0 ? void 0 : options.headers);
|
|
127
|
+
if (this.subUrl === QwenImageModel.SUB_SYNTHESIS_URL) {
|
|
128
|
+
fetchHeaders["X-DashScope-Async"] = "enable";
|
|
129
|
+
}
|
|
130
|
+
const joinedUrl = `${String(this.baseUrl).replace(/\/+$/, "")}/${String(this.subUrl).replace(/^\/+/, "")}`;
|
|
131
|
+
const { data: responseData, header } = (yield this.req.fetch({
|
|
132
|
+
url: joinedUrl,
|
|
133
|
+
headers: Object.assign({}, fetchHeaders),
|
|
134
|
+
body: JSON.stringify(data),
|
|
135
|
+
method: "post",
|
|
136
|
+
stream: false,
|
|
137
|
+
}));
|
|
138
|
+
return (0, utils_1.handleResponseData)(responseData, header);
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
doGenerate(data, options) {
|
|
142
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
143
|
+
data.model = this.modelName;
|
|
144
|
+
if (!Object.prototype.hasOwnProperty.call(data, "async")) {
|
|
145
|
+
data.async = true;
|
|
146
|
+
}
|
|
147
|
+
if (data.async) {
|
|
148
|
+
this.subUrl = QwenImageModel.SUB_SYNTHESIS_URL;
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
this.subUrl = QwenImageModel.SUB_GENERATION_URL;
|
|
152
|
+
}
|
|
153
|
+
const payload = this.coverModelRequestToQwenInput(data);
|
|
154
|
+
const res = (yield this.modelRequest(payload, options));
|
|
155
|
+
return this.normalizeStandardImageCompletion(res, this.modelName);
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
doStream(data, options) {
|
|
159
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
160
|
+
var _a, _b;
|
|
161
|
+
const nonStream = yield this.doGenerate(Object.assign({}, data), options);
|
|
162
|
+
const msg = ((_b = (_a = nonStream.choices) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.message) || {};
|
|
163
|
+
const singleChunk = {
|
|
164
|
+
id: nonStream.id,
|
|
165
|
+
object: "chat.completion.chunk",
|
|
166
|
+
created: nonStream.created,
|
|
167
|
+
model: nonStream.model,
|
|
168
|
+
log_id: nonStream.log_id,
|
|
169
|
+
error: nonStream.error || "",
|
|
170
|
+
code: nonStream.code || 0,
|
|
171
|
+
choices: [
|
|
172
|
+
{
|
|
173
|
+
index: 0,
|
|
174
|
+
message: {
|
|
175
|
+
id: nonStream.id,
|
|
176
|
+
role: "assistant",
|
|
177
|
+
type: msg.type || "image",
|
|
178
|
+
content: msg.content || "",
|
|
179
|
+
reasoning_content: "",
|
|
180
|
+
},
|
|
181
|
+
finish_reason: "stop",
|
|
182
|
+
},
|
|
183
|
+
],
|
|
184
|
+
usage: nonStream.usage,
|
|
185
|
+
};
|
|
186
|
+
const stream = new stream_1.ReadableStream({
|
|
187
|
+
start(controller) {
|
|
188
|
+
controller.enqueue(singleChunk);
|
|
189
|
+
controller.close();
|
|
190
|
+
},
|
|
191
|
+
});
|
|
192
|
+
return (0, stream_1.createAsyncIterable)(stream);
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
exports.QwenImageModel = QwenImageModel;
|
|
197
|
+
QwenImageModel.BASE_URL = "https://dashscope.aliyuncs.com";
|
|
198
|
+
QwenImageModel.SUB_SYNTHESIS_URL = "api/v1/services/aigc/text2image/image-synthesis";
|
|
199
|
+
QwenImageModel.SUB_GENERATION_URL = "api/v1/services/aigc/multimodal-generation/generation";
|
|
200
|
+
function mapUsageToStandard(usage) {
|
|
201
|
+
return {
|
|
202
|
+
prompt_tokens: 0,
|
|
203
|
+
completion_tokens: 0,
|
|
204
|
+
knowledge_tokens: 0,
|
|
205
|
+
reasoning_tokens: 0,
|
|
206
|
+
total_tokens: 0,
|
|
207
|
+
};
|
|
208
|
+
}
|