@agentor/dashscope 0.0.0 → 0.0.1
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 +77 -4
- package/dist/index.d.mts +48 -9
- package/dist/index.mjs +140 -111
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -10,6 +10,8 @@
|
|
|
10
10
|
|
|
11
11
|
- **Chat Completions API** - Standard `/chat/completions` with function calling, streaming, and reasoning
|
|
12
12
|
- **Responses API** - `/responses` endpoint with built-in tools support
|
|
13
|
+
- **Embedding** - Text vectorization via OpenAI-compatible `/embeddings` endpoint
|
|
14
|
+
- **Reranking** - Document reranking via `/reranks` endpoint
|
|
13
15
|
- **Built-in Tools** - Web search, code interpreter, web extractor, file search, image search, MCP integration
|
|
14
16
|
- **Thinking Mode** - Enable reasoning/thinking with configurable budget
|
|
15
17
|
- **Multi-region** - Beijing, Singapore, US, Germany regions
|
|
@@ -245,16 +247,87 @@ const first = await generateText({
|
|
|
245
247
|
});
|
|
246
248
|
```
|
|
247
249
|
|
|
250
|
+
## Embedding
|
|
251
|
+
|
|
252
|
+
```typescript
|
|
253
|
+
import { embed, embedMany } from "ai";
|
|
254
|
+
|
|
255
|
+
// Single text embedding
|
|
256
|
+
const { embedding, usage } = await embed({
|
|
257
|
+
model: dashscope.embeddingModel("text-embedding-v4"),
|
|
258
|
+
value: "The clothes quality is excellent",
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
console.log(embedding.length); // 1024 (default dimensions)
|
|
262
|
+
|
|
263
|
+
// Batch embedding
|
|
264
|
+
const { embeddings } = await embedMany({
|
|
265
|
+
model: dashscope.embeddingModel("text-embedding-v4"),
|
|
266
|
+
values: ["Hello world", "Machine learning is fascinating"],
|
|
267
|
+
});
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
### Custom Dimensions
|
|
271
|
+
|
|
272
|
+
```typescript
|
|
273
|
+
const { embedding } = await embed({
|
|
274
|
+
model: dashscope.embeddingModel("text-embedding-v4"),
|
|
275
|
+
value: "Custom dimension embedding",
|
|
276
|
+
providerOptions: {
|
|
277
|
+
openaiCompatible: {
|
|
278
|
+
dimensions: 256,
|
|
279
|
+
},
|
|
280
|
+
},
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
console.log(embedding.length); // 256
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
## Reranking
|
|
287
|
+
|
|
288
|
+
```typescript
|
|
289
|
+
import { rerank } from "ai";
|
|
290
|
+
|
|
291
|
+
const { ranking } = await rerank({
|
|
292
|
+
model: dashscope.rerankingModel("qwen3-rerank"),
|
|
293
|
+
query: "What is a reranking model?",
|
|
294
|
+
documents: [
|
|
295
|
+
"Reranking models sort candidate texts by relevance",
|
|
296
|
+
"Quantum computing is a frontier field",
|
|
297
|
+
"Pre-trained models brought advances to reranking",
|
|
298
|
+
],
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
for (const item of ranking) {
|
|
302
|
+
console.log(`Index: ${item.originalIndex}, Score: ${item.score}`);
|
|
303
|
+
}
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
### Top N Results
|
|
307
|
+
|
|
308
|
+
```typescript
|
|
309
|
+
const { ranking } = await rerank({
|
|
310
|
+
model: dashscope.rerankingModel("qwen3-rerank"),
|
|
311
|
+
query: "How to reset password?",
|
|
312
|
+
documents: [
|
|
313
|
+
"Go to Settings > Security > Change Password",
|
|
314
|
+
"Forgot your password?",
|
|
315
|
+
"Two-factor authentication is supported",
|
|
316
|
+
],
|
|
317
|
+
topN: 2,
|
|
318
|
+
});
|
|
319
|
+
```
|
|
320
|
+
|
|
248
321
|
## Provider Configuration
|
|
249
322
|
|
|
250
323
|
```typescript
|
|
251
324
|
import { createDashScope } from "@agentor/dashscope";
|
|
252
325
|
|
|
253
326
|
const dashscope = createDashScope({
|
|
254
|
-
apiKey: "sk-xxx",
|
|
255
|
-
region: "beijing",
|
|
256
|
-
workspaceId: "ws-xxx",
|
|
257
|
-
baseURL: "https://custom-endpoint.com",
|
|
327
|
+
apiKey: "sk-xxx", // or set DASHSCOPE_API_KEY env var
|
|
328
|
+
region: "beijing", // beijing | singapore | us | germany
|
|
329
|
+
workspaceId: "ws-xxx", // required for germany region
|
|
330
|
+
baseURL: "https://custom-endpoint.com", // override default base URL
|
|
258
331
|
headers: { "X-Custom-Header": "value" }, // custom headers
|
|
259
332
|
});
|
|
260
333
|
```
|
package/dist/index.d.mts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import { OpenAICompatibleEmbeddingModel } from "@ai-sdk/openai-compatible";
|
|
1
2
|
import * as _$_ai_sdk_provider_utils0 from "@ai-sdk/provider-utils";
|
|
2
3
|
import { FetchFunction } from "@ai-sdk/provider-utils";
|
|
3
|
-
import { LanguageModelV3 } from "@ai-sdk/provider";
|
|
4
|
+
import { EmbeddingModelV3, LanguageModelV3, RerankingModelV3, RerankingModelV3CallOptions, SharedV3Warning } from "@ai-sdk/provider";
|
|
4
5
|
|
|
5
6
|
//#region src/tools.d.ts
|
|
6
7
|
declare const webSearchToolFactory: _$_ai_sdk_provider_utils0.ProviderToolFactoryWithOutputSchema<Record<string, never>, {
|
|
@@ -164,13 +165,6 @@ interface DashScopeChatOptions {
|
|
|
164
165
|
/** Enable code interpreter (requires enableThinking). */
|
|
165
166
|
enableCodeInterpreter?: boolean;
|
|
166
167
|
}
|
|
167
|
-
interface DashScopeChatConfig {
|
|
168
|
-
provider: string;
|
|
169
|
-
baseURL: string;
|
|
170
|
-
headers: () => Record<string, string>;
|
|
171
|
-
fetch?: FetchFunction;
|
|
172
|
-
includeUsage?: boolean;
|
|
173
|
-
}
|
|
174
168
|
interface DashScopeResponsesOptions {
|
|
175
169
|
enableThinking?: boolean;
|
|
176
170
|
reasoning?: {
|
|
@@ -188,6 +182,8 @@ interface DashScopeResponsesNamespace {
|
|
|
188
182
|
interface DashScopeProvider {
|
|
189
183
|
(modelId: string): LanguageModelV3;
|
|
190
184
|
languageModel(modelId: string): LanguageModelV3;
|
|
185
|
+
embeddingModel(modelId: string): EmbeddingModelV3;
|
|
186
|
+
rerankingModel(modelId: string): RerankingModelV3;
|
|
191
187
|
chatOptions: (options: DashScopeChatOptions) => {
|
|
192
188
|
providerOptions: {
|
|
193
189
|
dashscope: DashScopeChatOptions;
|
|
@@ -201,10 +197,53 @@ interface DashScopeProvider {
|
|
|
201
197
|
responses: DashScopeResponsesNamespace;
|
|
202
198
|
}
|
|
203
199
|
//#endregion
|
|
200
|
+
//#region src/utils.d.ts
|
|
201
|
+
interface DashScopeConfig {
|
|
202
|
+
provider: string;
|
|
203
|
+
baseURL: string;
|
|
204
|
+
headers: () => Record<string, string>;
|
|
205
|
+
fetch?: FetchFunction;
|
|
206
|
+
includeUsage?: boolean;
|
|
207
|
+
}
|
|
208
|
+
//#endregion
|
|
209
|
+
//#region src/embedding.d.ts
|
|
210
|
+
interface DashScopeEmbeddingOptions {
|
|
211
|
+
/** Output embedding dimensions. Supported by text-embedding-v4, text-embedding-v3, etc. */
|
|
212
|
+
dimensions?: number;
|
|
213
|
+
}
|
|
214
|
+
declare class DashScopeEmbeddingModel extends OpenAICompatibleEmbeddingModel {
|
|
215
|
+
constructor(modelId: string, config: DashScopeConfig);
|
|
216
|
+
}
|
|
217
|
+
//#endregion
|
|
218
|
+
//#region src/rerank.d.ts
|
|
219
|
+
interface DashScopeRerankOptions {
|
|
220
|
+
/** English instruction to guide the reranking strategy. */
|
|
221
|
+
instruct?: string;
|
|
222
|
+
}
|
|
223
|
+
declare class DashScopeRerankingModel implements RerankingModelV3 {
|
|
224
|
+
readonly specificationVersion: "v3";
|
|
225
|
+
readonly modelId: string;
|
|
226
|
+
private readonly config;
|
|
227
|
+
constructor(modelId: string, config: DashScopeConfig);
|
|
228
|
+
get provider(): string;
|
|
229
|
+
doRerank(options: RerankingModelV3CallOptions): Promise<{
|
|
230
|
+
ranking: {
|
|
231
|
+
index: number;
|
|
232
|
+
relevanceScore: number;
|
|
233
|
+
}[];
|
|
234
|
+
warnings: SharedV3Warning[];
|
|
235
|
+
response: {
|
|
236
|
+
id: string | undefined;
|
|
237
|
+
modelId: string | undefined;
|
|
238
|
+
headers: Record<string, string> | undefined;
|
|
239
|
+
};
|
|
240
|
+
}>;
|
|
241
|
+
}
|
|
242
|
+
//#endregion
|
|
204
243
|
//#region src/provider.d.ts
|
|
205
244
|
declare function createDashScope(options?: DashScopeProviderSettings): DashScopeProvider;
|
|
206
245
|
//#endregion
|
|
207
246
|
//#region src/index.d.ts
|
|
208
247
|
declare const dashscope: DashScopeProvider;
|
|
209
248
|
//#endregion
|
|
210
|
-
export { DASHSCOPE_REGION_BASE_URLS,
|
|
249
|
+
export { DASHSCOPE_REGION_BASE_URLS, DashScopeChatOptions, DashScopeEmbeddingModel, DashScopeEmbeddingOptions, DashScopeProvider, DashScopeProviderSettings, DashScopeRegion, DashScopeRerankOptions, DashScopeRerankingModel, DashScopeResponsesNamespace, DashScopeResponsesOptions, DashScopeResponsesTools, createDashScope, dashscope, responsesTools };
|
package/dist/index.mjs
CHANGED
|
@@ -1,23 +1,106 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { OpenAICompatibleEmbeddingModel } from "@ai-sdk/openai-compatible";
|
|
2
2
|
import { combineHeaders, convertToBase64, createEventSourceResponseHandler, createJsonErrorResponseHandler, createJsonResponseHandler, createProviderToolFactoryWithOutputSchema, generateId, isParsableJson, lazySchema, parseProviderOptions, postJsonToApi, zodSchema } from "@ai-sdk/provider-utils";
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
3
|
+
import { z } from "zod/v4";
|
|
4
|
+
import { convertOpenAICompatibleChatUsage, getResponseMetadata, mapOpenAICompatibleFinishReason, prepareTools } from "@ai-sdk/openai-compatible/internal";
|
|
5
|
+
//#region src/embedding.ts
|
|
6
|
+
var DashScopeEmbeddingModel = class extends OpenAICompatibleEmbeddingModel {
|
|
7
|
+
constructor(modelId, config) {
|
|
8
|
+
super(modelId, {
|
|
9
|
+
provider: config.provider,
|
|
10
|
+
url: () => `${config.baseURL}/embeddings`,
|
|
11
|
+
headers: config.headers,
|
|
12
|
+
fetch: config.fetch
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
const failedResponseHandler = createJsonErrorResponseHandler({
|
|
17
|
+
errorSchema: zodSchema(z.object({ error: z.object({
|
|
18
|
+
message: z.string(),
|
|
19
|
+
code: z.string().nullish(),
|
|
20
|
+
type: z.string().nullish()
|
|
21
|
+
}) })),
|
|
22
|
+
errorToMessage: (data) => data.error.message
|
|
23
|
+
});
|
|
24
|
+
function convertResponsesUsage(usage) {
|
|
25
|
+
if (!usage) return {
|
|
26
|
+
inputTokens: {
|
|
27
|
+
total: 0,
|
|
28
|
+
noCache: void 0,
|
|
29
|
+
cacheRead: void 0,
|
|
30
|
+
cacheWrite: void 0
|
|
31
|
+
},
|
|
32
|
+
outputTokens: {
|
|
33
|
+
total: 0,
|
|
34
|
+
text: void 0,
|
|
35
|
+
reasoning: void 0
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
return {
|
|
39
|
+
inputTokens: {
|
|
40
|
+
total: usage.input_tokens ?? 0,
|
|
41
|
+
noCache: void 0,
|
|
42
|
+
cacheRead: usage.input_tokens_details?.cached_tokens ?? void 0,
|
|
43
|
+
cacheWrite: void 0
|
|
44
|
+
},
|
|
45
|
+
outputTokens: {
|
|
46
|
+
total: usage.output_tokens ?? 0,
|
|
47
|
+
text: void 0,
|
|
48
|
+
reasoning: usage.output_tokens_details?.reasoning_tokens ?? void 0
|
|
49
|
+
},
|
|
50
|
+
raw: usage
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
//#endregion
|
|
54
|
+
//#region src/rerank.ts
|
|
55
|
+
const rerankResponseSchema = zodSchema(z.object({
|
|
56
|
+
id: z.string().optional(),
|
|
57
|
+
model: z.string().optional(),
|
|
58
|
+
results: z.array(z.object({
|
|
59
|
+
index: z.number(),
|
|
60
|
+
relevance_score: z.number()
|
|
61
|
+
})).optional()
|
|
62
|
+
}));
|
|
63
|
+
var DashScopeRerankingModel = class {
|
|
64
|
+
specificationVersion = "v3";
|
|
65
|
+
modelId;
|
|
66
|
+
config;
|
|
67
|
+
constructor(modelId, config) {
|
|
68
|
+
this.modelId = modelId;
|
|
69
|
+
this.config = config;
|
|
70
|
+
}
|
|
71
|
+
get provider() {
|
|
72
|
+
return this.config.provider;
|
|
73
|
+
}
|
|
74
|
+
async doRerank(options) {
|
|
75
|
+
const warnings = [];
|
|
76
|
+
const documents = options.documents.type === "text" ? options.documents.values : options.documents.values.map((d) => JSON.stringify(d));
|
|
77
|
+
const body = {
|
|
78
|
+
model: this.modelId,
|
|
79
|
+
query: options.query,
|
|
80
|
+
documents,
|
|
81
|
+
...options.topN != null && { top_n: options.topN }
|
|
82
|
+
};
|
|
83
|
+
const { responseHeaders, value: response } = await postJsonToApi({
|
|
84
|
+
url: `${this.config.baseURL.replace("/compatible-mode/", "/compatible-api/")}/reranks`,
|
|
85
|
+
headers: combineHeaders(this.config.headers(), options.headers),
|
|
86
|
+
body,
|
|
87
|
+
failedResponseHandler,
|
|
88
|
+
successfulResponseHandler: createJsonResponseHandler(rerankResponseSchema),
|
|
89
|
+
abortSignal: options.abortSignal,
|
|
90
|
+
fetch: this.config.fetch
|
|
91
|
+
});
|
|
92
|
+
return {
|
|
93
|
+
ranking: (response.results ?? []).map((r) => ({
|
|
94
|
+
index: r.index,
|
|
95
|
+
relevanceScore: r.relevance_score
|
|
96
|
+
})),
|
|
97
|
+
warnings,
|
|
98
|
+
response: {
|
|
99
|
+
id: response.id ?? void 0,
|
|
100
|
+
modelId: response.model ?? void 0,
|
|
101
|
+
headers: responseHeaders
|
|
102
|
+
}
|
|
103
|
+
};
|
|
21
104
|
}
|
|
22
105
|
};
|
|
23
106
|
//#endregion
|
|
@@ -95,6 +178,26 @@ const responsesTools = {
|
|
|
95
178
|
mcp: (args) => mcpToolFactory(args)
|
|
96
179
|
};
|
|
97
180
|
//#endregion
|
|
181
|
+
//#region src/types.ts
|
|
182
|
+
const DASHSCOPE_REGION_BASE_URLS = {
|
|
183
|
+
beijing: {
|
|
184
|
+
baseURL: "https://dashscope.aliyuncs.com/compatible-mode/v1",
|
|
185
|
+
videoBaseURL: "https://dashscope.aliyuncs.com"
|
|
186
|
+
},
|
|
187
|
+
singapore: {
|
|
188
|
+
baseURL: "https://dashscope-intl.aliyuncs.com/compatible-mode/v1",
|
|
189
|
+
videoBaseURL: "https://dashscope-intl.aliyuncs.com"
|
|
190
|
+
},
|
|
191
|
+
us: {
|
|
192
|
+
baseURL: "https://dashscope-us.aliyuncs.com/compatible-mode/v1",
|
|
193
|
+
videoBaseURL: "https://dashscope-us.aliyuncs.com"
|
|
194
|
+
},
|
|
195
|
+
germany: {
|
|
196
|
+
baseURL: "https://{workspaceId}.eu-central-1.maas.aliyuncs.com/compatible-mode/v1",
|
|
197
|
+
videoBaseURL: "https://{workspaceId}.eu-central-1.maas.aliyuncs.com"
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
//#endregion
|
|
98
201
|
//#region src/chat.ts
|
|
99
202
|
const chatOptionsSchema = z.object({
|
|
100
203
|
enableThinking: z.boolean().optional(),
|
|
@@ -165,45 +268,6 @@ const chatChunkSchema = z.object({
|
|
|
165
268
|
})),
|
|
166
269
|
usage: usageSchema.nullish()
|
|
167
270
|
});
|
|
168
|
-
const failedResponseHandler$1 = createJsonErrorResponseHandler({
|
|
169
|
-
errorSchema: z.object({ error: z.object({
|
|
170
|
-
message: z.string(),
|
|
171
|
-
code: z.string().nullish(),
|
|
172
|
-
type: z.string().nullish()
|
|
173
|
-
}) }),
|
|
174
|
-
errorToMessage: (data) => data.error.message
|
|
175
|
-
});
|
|
176
|
-
function convertUsage$1(usage) {
|
|
177
|
-
if (!usage) return {
|
|
178
|
-
inputTokens: {
|
|
179
|
-
total: 0,
|
|
180
|
-
noCache: void 0,
|
|
181
|
-
cacheRead: void 0,
|
|
182
|
-
cacheWrite: void 0
|
|
183
|
-
},
|
|
184
|
-
outputTokens: {
|
|
185
|
-
total: 0,
|
|
186
|
-
text: void 0,
|
|
187
|
-
reasoning: void 0
|
|
188
|
-
}
|
|
189
|
-
};
|
|
190
|
-
const cacheRead = usage.prompt_tokens_details?.cached_tokens ?? void 0;
|
|
191
|
-
const cacheWrite = usage.prompt_tokens_details?.cache_creation_input_tokens ?? void 0;
|
|
192
|
-
const noCache = cacheRead != null || cacheWrite != null ? (usage.prompt_tokens ?? 0) - (cacheRead ?? 0) - (cacheWrite ?? 0) : void 0;
|
|
193
|
-
return {
|
|
194
|
-
inputTokens: {
|
|
195
|
-
total: usage.prompt_tokens ?? 0,
|
|
196
|
-
noCache,
|
|
197
|
-
cacheRead,
|
|
198
|
-
cacheWrite
|
|
199
|
-
},
|
|
200
|
-
outputTokens: {
|
|
201
|
-
total: usage.completion_tokens ?? 0,
|
|
202
|
-
text: void 0,
|
|
203
|
-
reasoning: usage.completion_tokens_details?.reasoning_tokens ?? void 0
|
|
204
|
-
}
|
|
205
|
-
};
|
|
206
|
-
}
|
|
207
271
|
function convertMessages(prompt) {
|
|
208
272
|
const messages = [];
|
|
209
273
|
for (const { role, content } of prompt) switch (role) {
|
|
@@ -361,7 +425,7 @@ var DashScopeChatLanguageModel = class {
|
|
|
361
425
|
url: `${this.config.baseURL}/chat/completions`,
|
|
362
426
|
headers: combineHeaders(this.config.headers(), options.headers),
|
|
363
427
|
body: args,
|
|
364
|
-
failedResponseHandler
|
|
428
|
+
failedResponseHandler,
|
|
365
429
|
successfulResponseHandler: createJsonResponseHandler(chatResponseSchema),
|
|
366
430
|
abortSignal: options.abortSignal,
|
|
367
431
|
fetch: this.config.fetch
|
|
@@ -388,12 +452,10 @@ var DashScopeChatLanguageModel = class {
|
|
|
388
452
|
unified: mapOpenAICompatibleFinishReason(choice.finish_reason),
|
|
389
453
|
raw: choice.finish_reason ?? void 0
|
|
390
454
|
},
|
|
391
|
-
usage:
|
|
455
|
+
usage: convertOpenAICompatibleChatUsage(response.usage),
|
|
392
456
|
request: { body: JSON.stringify(args) },
|
|
393
457
|
response: {
|
|
394
|
-
|
|
395
|
-
modelId: response.model ?? void 0,
|
|
396
|
-
timestamp: response.created ? /* @__PURE__ */ new Date(response.created * 1e3) : void 0,
|
|
458
|
+
...getResponseMetadata(response),
|
|
397
459
|
headers: responseHeaders
|
|
398
460
|
},
|
|
399
461
|
warnings
|
|
@@ -409,7 +471,7 @@ var DashScopeChatLanguageModel = class {
|
|
|
409
471
|
url: `${this.config.baseURL}/chat/completions`,
|
|
410
472
|
headers: combineHeaders(this.config.headers(), options.headers),
|
|
411
473
|
body,
|
|
412
|
-
failedResponseHandler
|
|
474
|
+
failedResponseHandler,
|
|
413
475
|
successfulResponseHandler: createEventSourceResponseHandler(chatChunkSchema),
|
|
414
476
|
abortSignal: options.abortSignal,
|
|
415
477
|
fetch: this.config.fetch
|
|
@@ -448,9 +510,7 @@ var DashScopeChatLanguageModel = class {
|
|
|
448
510
|
isFirstChunk = false;
|
|
449
511
|
controller.enqueue({
|
|
450
512
|
type: "response-metadata",
|
|
451
|
-
|
|
452
|
-
modelId: value.model ?? void 0,
|
|
453
|
-
timestamp: value.created ? /* @__PURE__ */ new Date(value.created * 1e3) : void 0
|
|
513
|
+
...getResponseMetadata(value)
|
|
454
514
|
});
|
|
455
515
|
}
|
|
456
516
|
if (value.usage != null) usage = value.usage;
|
|
@@ -594,7 +654,7 @@ var DashScopeChatLanguageModel = class {
|
|
|
594
654
|
controller.enqueue({
|
|
595
655
|
type: "finish",
|
|
596
656
|
finishReason,
|
|
597
|
-
usage:
|
|
657
|
+
usage: convertOpenAICompatibleChatUsage(usage)
|
|
598
658
|
});
|
|
599
659
|
}
|
|
600
660
|
})),
|
|
@@ -619,11 +679,6 @@ const responsesOptionsSchema = zodSchema(z.object({
|
|
|
619
679
|
instructions: z.string().optional(),
|
|
620
680
|
includeUsage: z.boolean().optional()
|
|
621
681
|
}));
|
|
622
|
-
const errorSchema = zodSchema(z.object({ error: z.object({
|
|
623
|
-
message: z.string(),
|
|
624
|
-
type: z.string().optional(),
|
|
625
|
-
code: z.string().optional()
|
|
626
|
-
}) }));
|
|
627
682
|
const responseSchema = zodSchema(z.object({
|
|
628
683
|
id: z.string(),
|
|
629
684
|
created_at: z.number().optional(),
|
|
@@ -641,41 +696,6 @@ const responseSchema = zodSchema(z.object({
|
|
|
641
696
|
error: z.object({ message: z.string() }).nullable().optional()
|
|
642
697
|
}).loose());
|
|
643
698
|
const streamChunkSchema = zodSchema(z.object({ type: z.string() }).loose());
|
|
644
|
-
const failedResponseHandler = createJsonErrorResponseHandler({
|
|
645
|
-
errorSchema,
|
|
646
|
-
errorToMessage: (data) => data.error.message
|
|
647
|
-
});
|
|
648
|
-
function convertUsage(usage) {
|
|
649
|
-
if (!usage) return {
|
|
650
|
-
inputTokens: {
|
|
651
|
-
total: 0,
|
|
652
|
-
noCache: void 0,
|
|
653
|
-
cacheRead: void 0,
|
|
654
|
-
cacheWrite: void 0
|
|
655
|
-
},
|
|
656
|
-
outputTokens: {
|
|
657
|
-
total: 0,
|
|
658
|
-
text: void 0,
|
|
659
|
-
reasoning: void 0
|
|
660
|
-
}
|
|
661
|
-
};
|
|
662
|
-
const details = usage.output_tokens_details;
|
|
663
|
-
const inputDetails = usage.input_tokens_details;
|
|
664
|
-
return {
|
|
665
|
-
inputTokens: {
|
|
666
|
-
total: usage.input_tokens ?? 0,
|
|
667
|
-
noCache: void 0,
|
|
668
|
-
cacheRead: inputDetails?.cached_tokens ?? void 0,
|
|
669
|
-
cacheWrite: void 0
|
|
670
|
-
},
|
|
671
|
-
outputTokens: {
|
|
672
|
-
total: usage.output_tokens ?? 0,
|
|
673
|
-
text: void 0,
|
|
674
|
-
reasoning: details?.reasoning_tokens ?? void 0
|
|
675
|
-
},
|
|
676
|
-
raw: usage
|
|
677
|
-
};
|
|
678
|
-
}
|
|
679
699
|
function mapFinishReason(status) {
|
|
680
700
|
switch (status) {
|
|
681
701
|
case "completed": return {
|
|
@@ -1066,7 +1086,7 @@ var DashScopeResponsesLanguageModel = class {
|
|
|
1066
1086
|
return {
|
|
1067
1087
|
content,
|
|
1068
1088
|
finishReason,
|
|
1069
|
-
usage:
|
|
1089
|
+
usage: convertResponsesUsage(response.usage),
|
|
1070
1090
|
request: { body },
|
|
1071
1091
|
response: {
|
|
1072
1092
|
id: response.id ?? void 0,
|
|
@@ -1151,7 +1171,7 @@ var DashScopeResponsesLanguageModel = class {
|
|
|
1151
1171
|
case "response.completed": {
|
|
1152
1172
|
const resp = val.response;
|
|
1153
1173
|
finishReason = mapFinishReason(resp?.status);
|
|
1154
|
-
if (resp?.usage) usage =
|
|
1174
|
+
if (resp?.usage) usage = convertResponsesUsage(resp.usage);
|
|
1155
1175
|
if (resp?.id) controller.enqueue({
|
|
1156
1176
|
type: "response-metadata",
|
|
1157
1177
|
id: resp.id,
|
|
@@ -1211,6 +1231,13 @@ function createDashScope(options = {}) {
|
|
|
1211
1231
|
includeUsage
|
|
1212
1232
|
};
|
|
1213
1233
|
const createChatModel = (modelId) => new DashScopeChatLanguageModel(modelId, chatConfig);
|
|
1234
|
+
const createEmbeddingModel = (modelId) => new DashScopeEmbeddingModel(modelId, chatConfig);
|
|
1235
|
+
const createRerankingModel = (modelId) => new DashScopeRerankingModel(modelId, {
|
|
1236
|
+
provider: "dashscope.rerank",
|
|
1237
|
+
baseURL,
|
|
1238
|
+
headers: getHeaders,
|
|
1239
|
+
fetch: rest.fetch
|
|
1240
|
+
});
|
|
1214
1241
|
const createResponsesModel = (modelId) => new DashScopeResponsesLanguageModel(modelId, {
|
|
1215
1242
|
provider: "dashscope.responses",
|
|
1216
1243
|
baseURL,
|
|
@@ -1220,6 +1247,8 @@ function createDashScope(options = {}) {
|
|
|
1220
1247
|
const responses = Object.assign(createResponsesModel, { tools: responsesTools });
|
|
1221
1248
|
return Object.assign(createChatModel, {
|
|
1222
1249
|
languageModel: createChatModel,
|
|
1250
|
+
embeddingModel: createEmbeddingModel,
|
|
1251
|
+
rerankingModel: createRerankingModel,
|
|
1223
1252
|
chatOptions: (chatOpts) => ({ providerOptions: { dashscope: chatOpts } }),
|
|
1224
1253
|
responsesOptions: (responsesOpts) => ({ providerOptions: { dashscope: responsesOpts } }),
|
|
1225
1254
|
responses
|
|
@@ -1229,4 +1258,4 @@ function createDashScope(options = {}) {
|
|
|
1229
1258
|
//#region src/index.ts
|
|
1230
1259
|
const dashscope = createDashScope();
|
|
1231
1260
|
//#endregion
|
|
1232
|
-
export { DASHSCOPE_REGION_BASE_URLS, createDashScope, dashscope, responsesTools };
|
|
1261
|
+
export { DASHSCOPE_REGION_BASE_URLS, DashScopeEmbeddingModel, DashScopeRerankingModel, createDashScope, dashscope, responsesTools };
|