@ai-sdk/alibaba 1.0.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/CHANGELOG.md +7 -0
- package/LICENSE +13 -0
- package/README.md +121 -0
- package/dist/index.d.mts +77 -0
- package/dist/index.d.ts +77 -0
- package/dist/index.js +750 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +744 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +76 -0
- package/src/alibaba-chat-language-model.ts +605 -0
- package/src/alibaba-chat-options.ts +53 -0
- package/src/alibaba-chat-prompt.ts +63 -0
- package/src/alibaba-config.ts +9 -0
- package/src/alibaba-provider.ts +137 -0
- package/src/convert-alibaba-usage.ts +37 -0
- package/src/convert-to-alibaba-chat-messages.ts +183 -0
- package/src/get-cache-control.ts +49 -0
- package/src/index.ts +12 -0
- package/src/version.ts +3 -0
package/CHANGELOG.md
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
Copyright 2023 Vercel, Inc.
|
|
2
|
+
|
|
3
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
you may not use this file except in compliance with the License.
|
|
5
|
+
You may obtain a copy of the License at
|
|
6
|
+
|
|
7
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
|
|
9
|
+
Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
See the License for the specific language governing permissions and
|
|
13
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
# AI SDK - Alibaba Provider
|
|
2
|
+
|
|
3
|
+
The **[Alibaba provider](https://ai-sdk.dev/providers/ai-sdk-providers/alibaba)** for the [AI SDK](https://ai-sdk.dev/docs) contains language model support for [Alibaba Cloud Model Studio](https://modelstudio.console.alibabacloud.com/), including the Qwen model series with advanced reasoning capabilities.
|
|
4
|
+
|
|
5
|
+
## Setup
|
|
6
|
+
|
|
7
|
+
The Alibaba provider is available in the `@ai-sdk/alibaba` module. You can install it with
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm i @ai-sdk/alibaba
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Skill for Coding Agents
|
|
14
|
+
|
|
15
|
+
If you use coding agents such as Claude Code or Cursor, we highly recommend adding the AI SDK skill to your repository:
|
|
16
|
+
|
|
17
|
+
```shell
|
|
18
|
+
npx skills add vercel/ai
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Provider Instance
|
|
22
|
+
|
|
23
|
+
You can import the default provider instance `alibaba` from `@ai-sdk/alibaba`:
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
import { alibaba } from '@ai-sdk/alibaba';
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Language Model Example
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
import { alibaba } from '@ai-sdk/alibaba';
|
|
33
|
+
import { generateText } from 'ai';
|
|
34
|
+
|
|
35
|
+
const { text } = await generateText({
|
|
36
|
+
model: alibaba('qwen-plus'),
|
|
37
|
+
prompt: 'Write a vegetarian lasagna recipe for 4 people.',
|
|
38
|
+
});
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Thinking Mode Example (Qwen Reasoning Models)
|
|
42
|
+
|
|
43
|
+
Alibaba's Qwen models support thinking/reasoning mode for complex problem-solving:
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
import { alibaba } from '@ai-sdk/alibaba';
|
|
47
|
+
import { generateText } from 'ai';
|
|
48
|
+
|
|
49
|
+
const { text, reasoningText } = await generateText({
|
|
50
|
+
model: alibaba('qwen3-max'),
|
|
51
|
+
providerOptions: {
|
|
52
|
+
alibaba: {
|
|
53
|
+
enableThinking: true,
|
|
54
|
+
thinkingBudget: 2048,
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
prompt: 'How many "r"s are in the word "strawberry"?',
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
console.log('Reasoning:', reasoningText);
|
|
61
|
+
console.log('Answer:', text);
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## Tool Calling Example
|
|
65
|
+
|
|
66
|
+
```ts
|
|
67
|
+
import { alibaba } from '@ai-sdk/alibaba';
|
|
68
|
+
import { generateText, tool } from 'ai';
|
|
69
|
+
import { z } from 'zod';
|
|
70
|
+
|
|
71
|
+
const { text } = await generateText({
|
|
72
|
+
model: alibaba('qwen-plus'),
|
|
73
|
+
tools: {
|
|
74
|
+
weather: tool({
|
|
75
|
+
description: 'Get the weather in a location',
|
|
76
|
+
parameters: z.object({
|
|
77
|
+
location: z.string().describe('The location to get the weather for'),
|
|
78
|
+
}),
|
|
79
|
+
execute: async ({ location }) => ({
|
|
80
|
+
location,
|
|
81
|
+
temperature: 72 + Math.floor(Math.random() * 21) - 10,
|
|
82
|
+
}),
|
|
83
|
+
}),
|
|
84
|
+
},
|
|
85
|
+
prompt: 'What is the weather in San Francisco?',
|
|
86
|
+
});
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## Explicit Caching Example
|
|
90
|
+
|
|
91
|
+
Alibaba supports both implicit and explicit prompt caching to reduce costs for repeated prompts.
|
|
92
|
+
|
|
93
|
+
**Implicit caching** works automatically - the provider caches appropriate content without any configuration. For more control, you can use **explicit caching** by marking specific messages with `cacheControl`:
|
|
94
|
+
|
|
95
|
+
```ts
|
|
96
|
+
import { alibaba } from '@ai-sdk/alibaba';
|
|
97
|
+
import { generateText } from 'ai';
|
|
98
|
+
|
|
99
|
+
const { text, usage } = await generateText({
|
|
100
|
+
model: alibaba('qwen-plus'),
|
|
101
|
+
messages: [
|
|
102
|
+
{
|
|
103
|
+
role: 'system',
|
|
104
|
+
content: 'You are a helpful assistant. [... long system prompt ...]',
|
|
105
|
+
providerMetadata: {
|
|
106
|
+
alibaba: {
|
|
107
|
+
cacheControl: { type: 'ephemeral' },
|
|
108
|
+
},
|
|
109
|
+
},
|
|
110
|
+
},
|
|
111
|
+
{
|
|
112
|
+
role: 'user',
|
|
113
|
+
content: 'What is the capital of France?',
|
|
114
|
+
},
|
|
115
|
+
],
|
|
116
|
+
});
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
## Documentation
|
|
120
|
+
|
|
121
|
+
Please check out the **[Alibaba provider documentation](https://ai-sdk.dev/providers/ai-sdk-providers/alibaba)** for more information.
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { FetchFunction } from '@ai-sdk/provider-utils';
|
|
2
|
+
import { ProviderV3, LanguageModelV3 } from '@ai-sdk/provider';
|
|
3
|
+
import { z } from 'zod/v4';
|
|
4
|
+
|
|
5
|
+
type AlibabaChatModelId = 'qwen3-max' | 'qwen3-max-preview' | 'qwen-plus' | 'qwen-plus-latest' | 'qwen-flash' | 'qwen-turbo' | 'qwen-turbo-latest' | 'qwen3-235b-a22b' | 'qwen3-32b' | 'qwen3-30b-a3b' | 'qwen3-14b' | 'qwen3-next-80b-a3b-thinking' | 'qwen3-235b-a22b-thinking-2507' | 'qwen3-30b-a3b-thinking-2507' | 'qwq-plus' | 'qwq-plus-latest' | 'qwq-32b' | 'qwen-coder' | 'qwen3-coder-plus' | 'qwen3-coder-flash' | (string & {});
|
|
6
|
+
declare const alibabaProviderOptions: z.ZodObject<{
|
|
7
|
+
enableThinking: z.ZodOptional<z.ZodBoolean>;
|
|
8
|
+
thinkingBudget: z.ZodOptional<z.ZodNumber>;
|
|
9
|
+
parallelToolCalls: z.ZodOptional<z.ZodBoolean>;
|
|
10
|
+
}, z.core.$strip>;
|
|
11
|
+
type AlibabaProviderOptions = z.infer<typeof alibabaProviderOptions>;
|
|
12
|
+
|
|
13
|
+
interface AlibabaProvider extends ProviderV3 {
|
|
14
|
+
(modelId: AlibabaChatModelId): LanguageModelV3;
|
|
15
|
+
/**
|
|
16
|
+
* Creates a model for text generation.
|
|
17
|
+
*/
|
|
18
|
+
languageModel(modelId: AlibabaChatModelId): LanguageModelV3;
|
|
19
|
+
/**
|
|
20
|
+
* Creates a chat model for text generation.
|
|
21
|
+
*/
|
|
22
|
+
chatModel(modelId: AlibabaChatModelId): LanguageModelV3;
|
|
23
|
+
}
|
|
24
|
+
interface AlibabaProviderSettings {
|
|
25
|
+
/**
|
|
26
|
+
* Use a different URL prefix for API calls, e.g. to use proxy servers or regional endpoints.
|
|
27
|
+
* The default prefix is `https://dashscope-intl.aliyuncs.com/compatible-mode/v1`.
|
|
28
|
+
*/
|
|
29
|
+
baseURL?: string;
|
|
30
|
+
/**
|
|
31
|
+
* API key that is being sent using the `Authorization` header.
|
|
32
|
+
* It defaults to the `ALIBABA_API_KEY` environment variable.
|
|
33
|
+
*/
|
|
34
|
+
apiKey?: string;
|
|
35
|
+
/**
|
|
36
|
+
* Custom headers to include in the requests.
|
|
37
|
+
*/
|
|
38
|
+
headers?: Record<string, string>;
|
|
39
|
+
/**
|
|
40
|
+
* Custom fetch implementation. You can use it as a middleware to intercept requests,
|
|
41
|
+
* or to provide a custom fetch implementation for e.g. testing.
|
|
42
|
+
*/
|
|
43
|
+
fetch?: FetchFunction;
|
|
44
|
+
/**
|
|
45
|
+
* Include usage information in streaming responses.
|
|
46
|
+
* When enabled, token usage will be included in the final chunk.
|
|
47
|
+
*
|
|
48
|
+
* @default true
|
|
49
|
+
*/
|
|
50
|
+
includeUsage?: boolean;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Create an Alibaba Cloud (Qwen) provider instance.
|
|
54
|
+
*/
|
|
55
|
+
declare function createAlibaba(options?: AlibabaProviderSettings): AlibabaProvider;
|
|
56
|
+
declare const alibaba: AlibabaProvider;
|
|
57
|
+
|
|
58
|
+
type AlibabaCacheControl = {
|
|
59
|
+
type: string;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
type AlibabaUsage = {
|
|
63
|
+
prompt_tokens?: number | null;
|
|
64
|
+
completion_tokens?: number | null;
|
|
65
|
+
total_tokens?: number | null;
|
|
66
|
+
prompt_tokens_details?: {
|
|
67
|
+
cached_tokens?: number | null;
|
|
68
|
+
cache_creation_input_tokens?: number | null;
|
|
69
|
+
} | null;
|
|
70
|
+
completion_tokens_details?: {
|
|
71
|
+
reasoning_tokens?: number | null;
|
|
72
|
+
} | null;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
declare const VERSION: string;
|
|
76
|
+
|
|
77
|
+
export { type AlibabaCacheControl, type AlibabaChatModelId, type AlibabaProvider, type AlibabaProviderOptions, type AlibabaProviderSettings, type AlibabaUsage, VERSION, alibaba, createAlibaba };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { FetchFunction } from '@ai-sdk/provider-utils';
|
|
2
|
+
import { ProviderV3, LanguageModelV3 } from '@ai-sdk/provider';
|
|
3
|
+
import { z } from 'zod/v4';
|
|
4
|
+
|
|
5
|
+
type AlibabaChatModelId = 'qwen3-max' | 'qwen3-max-preview' | 'qwen-plus' | 'qwen-plus-latest' | 'qwen-flash' | 'qwen-turbo' | 'qwen-turbo-latest' | 'qwen3-235b-a22b' | 'qwen3-32b' | 'qwen3-30b-a3b' | 'qwen3-14b' | 'qwen3-next-80b-a3b-thinking' | 'qwen3-235b-a22b-thinking-2507' | 'qwen3-30b-a3b-thinking-2507' | 'qwq-plus' | 'qwq-plus-latest' | 'qwq-32b' | 'qwen-coder' | 'qwen3-coder-plus' | 'qwen3-coder-flash' | (string & {});
|
|
6
|
+
declare const alibabaProviderOptions: z.ZodObject<{
|
|
7
|
+
enableThinking: z.ZodOptional<z.ZodBoolean>;
|
|
8
|
+
thinkingBudget: z.ZodOptional<z.ZodNumber>;
|
|
9
|
+
parallelToolCalls: z.ZodOptional<z.ZodBoolean>;
|
|
10
|
+
}, z.core.$strip>;
|
|
11
|
+
type AlibabaProviderOptions = z.infer<typeof alibabaProviderOptions>;
|
|
12
|
+
|
|
13
|
+
interface AlibabaProvider extends ProviderV3 {
|
|
14
|
+
(modelId: AlibabaChatModelId): LanguageModelV3;
|
|
15
|
+
/**
|
|
16
|
+
* Creates a model for text generation.
|
|
17
|
+
*/
|
|
18
|
+
languageModel(modelId: AlibabaChatModelId): LanguageModelV3;
|
|
19
|
+
/**
|
|
20
|
+
* Creates a chat model for text generation.
|
|
21
|
+
*/
|
|
22
|
+
chatModel(modelId: AlibabaChatModelId): LanguageModelV3;
|
|
23
|
+
}
|
|
24
|
+
interface AlibabaProviderSettings {
|
|
25
|
+
/**
|
|
26
|
+
* Use a different URL prefix for API calls, e.g. to use proxy servers or regional endpoints.
|
|
27
|
+
* The default prefix is `https://dashscope-intl.aliyuncs.com/compatible-mode/v1`.
|
|
28
|
+
*/
|
|
29
|
+
baseURL?: string;
|
|
30
|
+
/**
|
|
31
|
+
* API key that is being sent using the `Authorization` header.
|
|
32
|
+
* It defaults to the `ALIBABA_API_KEY` environment variable.
|
|
33
|
+
*/
|
|
34
|
+
apiKey?: string;
|
|
35
|
+
/**
|
|
36
|
+
* Custom headers to include in the requests.
|
|
37
|
+
*/
|
|
38
|
+
headers?: Record<string, string>;
|
|
39
|
+
/**
|
|
40
|
+
* Custom fetch implementation. You can use it as a middleware to intercept requests,
|
|
41
|
+
* or to provide a custom fetch implementation for e.g. testing.
|
|
42
|
+
*/
|
|
43
|
+
fetch?: FetchFunction;
|
|
44
|
+
/**
|
|
45
|
+
* Include usage information in streaming responses.
|
|
46
|
+
* When enabled, token usage will be included in the final chunk.
|
|
47
|
+
*
|
|
48
|
+
* @default true
|
|
49
|
+
*/
|
|
50
|
+
includeUsage?: boolean;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Create an Alibaba Cloud (Qwen) provider instance.
|
|
54
|
+
*/
|
|
55
|
+
declare function createAlibaba(options?: AlibabaProviderSettings): AlibabaProvider;
|
|
56
|
+
declare const alibaba: AlibabaProvider;
|
|
57
|
+
|
|
58
|
+
type AlibabaCacheControl = {
|
|
59
|
+
type: string;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
type AlibabaUsage = {
|
|
63
|
+
prompt_tokens?: number | null;
|
|
64
|
+
completion_tokens?: number | null;
|
|
65
|
+
total_tokens?: number | null;
|
|
66
|
+
prompt_tokens_details?: {
|
|
67
|
+
cached_tokens?: number | null;
|
|
68
|
+
cache_creation_input_tokens?: number | null;
|
|
69
|
+
} | null;
|
|
70
|
+
completion_tokens_details?: {
|
|
71
|
+
reasoning_tokens?: number | null;
|
|
72
|
+
} | null;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
declare const VERSION: string;
|
|
76
|
+
|
|
77
|
+
export { type AlibabaCacheControl, type AlibabaChatModelId, type AlibabaProvider, type AlibabaProviderOptions, type AlibabaProviderSettings, type AlibabaUsage, VERSION, alibaba, createAlibaba };
|