@ai-sdk/google-vertex 2.0.0 → 2.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/CHANGELOG.md +8 -0
- package/README.md +71 -5
- package/anthropic/dist/index.d.mts +62 -0
- package/anthropic/dist/index.d.ts +62 -0
- package/anthropic/dist/index.js +121 -0
- package/anthropic/dist/index.js.map +1 -0
- package/anthropic/dist/index.mjs +101 -0
- package/anthropic/dist/index.mjs.map +1 -0
- package/anthropic/edge/dist/index.d.mts +78 -0
- package/anthropic/edge/dist/index.d.ts +78 -0
- package/anthropic/edge/dist/index.js +202 -0
- package/anthropic/edge/dist/index.js.map +1 -0
- package/anthropic/edge/dist/index.mjs +182 -0
- package/anthropic/edge/dist/index.mjs.map +1 -0
- package/package.json +15 -2
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
@@ -2,15 +2,17 @@
|
|
2
2
|
|
3
3
|
The **[Google Vertex provider](https://sdk.vercel.ai/providers/ai-sdk-providers/google-vertex)** for the [AI SDK](https://sdk.vercel.ai/docs) contains language model support for the [Google Vertex AI](https://cloud.google.com/vertex-ai) APIs.
|
4
4
|
|
5
|
+
This library includes a Google Vertex Anthropic provider. This provider closely follows the core Google Vertex library's usage patterns. See more in the [Google Vertex Anthropic Provider](#google-vertex-anthropic-provider) section below.
|
6
|
+
|
5
7
|
## Setup
|
6
8
|
|
7
|
-
The Google provider is available in the `@ai-sdk/google-vertex` module. You can install it with
|
9
|
+
The Google Vertex provider is available in the `@ai-sdk/google-vertex` module. You can install it with
|
8
10
|
|
9
11
|
```bash
|
10
12
|
npm i @ai-sdk/google-vertex
|
11
13
|
```
|
12
14
|
|
13
|
-
##
|
15
|
+
## Google Vertex Provider
|
14
16
|
|
15
17
|
The Google Vertex provider has two different authentication implementations depending on your runtime environment:
|
16
18
|
|
@@ -28,7 +30,7 @@ const { text } = await generateText({
|
|
28
30
|
});
|
29
31
|
```
|
30
32
|
|
31
|
-
This provider supports all standard Google Cloud authentication options through the [`google-auth-library`
|
33
|
+
This provider supports all standard Google Cloud authentication options through the [`google-auth-library`](https://github.com/googleapis/google-auth-library-nodejs?tab=readme-ov-file#ways-to-authenticate). The most common authentication method is to set the path to a json credentials file in the `GOOGLE_APPLICATION_CREDENTIALS` environment variable. Credentials can be obtained from the [Google Cloud Console](https://console.cloud.google.com/apis/credentials).
|
32
34
|
|
33
35
|
### Edge Runtime
|
34
36
|
|
@@ -48,6 +50,34 @@ const { text } = await generateText({
|
|
48
50
|
|
49
51
|
This method supports Google's [Application Default Credentials](https://github.com/googleapis/google-auth-library-nodejs?tab=readme-ov-file#application-default-credentials) through the environment variables `GOOGLE_CLIENT_EMAIL`, `GOOGLE_PRIVATE_KEY`, and (optionally) `GOOGLE_PRIVATE_KEY_ID`. The values can be obtained from a json credentials file obtained from the [Google Cloud Console](https://console.cloud.google.com/apis/credentials).
|
50
52
|
|
53
|
+
## Google Vertex Anthropic Provider
|
54
|
+
|
55
|
+
The Google Vertex Anthropic provider is available for both Node.js and Edge runtimes. It follows a similar usage pattern to the [core Google Vertex provider](#google-vertex-provider).
|
56
|
+
|
57
|
+
### Node.js Runtime
|
58
|
+
|
59
|
+
```ts
|
60
|
+
import { vertexAnthropic } from '@ai-sdk/google-vertex/anthropic';
|
61
|
+
import { generateText } from 'ai';
|
62
|
+
|
63
|
+
const { text } = await generateText({
|
64
|
+
model: vertexAnthropic('claude-3-5-sonnet@20240620'),
|
65
|
+
prompt: 'Write a vegetarian lasagna recipe.',
|
66
|
+
});
|
67
|
+
```
|
68
|
+
|
69
|
+
### Edge Runtime
|
70
|
+
|
71
|
+
```ts
|
72
|
+
import { vertexAnthropic } from '@ai-sdk/google-vertex/anthropic/edge';
|
73
|
+
import { generateText } from 'ai';
|
74
|
+
|
75
|
+
const { text } = await generateText({
|
76
|
+
model: vertexAnthropic('claude-3-5-sonnet@20240620'),
|
77
|
+
prompt: 'Write a vegetarian lasagna recipe.',
|
78
|
+
});
|
79
|
+
```
|
80
|
+
|
51
81
|
## Custom Provider Configuration
|
52
82
|
|
53
83
|
You can create a custom provider instance using the `createVertex` function. This allows you to specify additional configuration options. Below is an example with the default Node.js provider which includes a `googleAuthOptions` object.
|
@@ -57,7 +87,7 @@ import { createVertex } from '@ai-sdk/google-vertex';
|
|
57
87
|
import { generateText } from 'ai';
|
58
88
|
|
59
89
|
const customProvider = createVertex({
|
60
|
-
|
90
|
+
project: 'your-project-id',
|
61
91
|
location: 'us-central1',
|
62
92
|
googleAuthOptions: {
|
63
93
|
credentials: {
|
@@ -82,7 +112,7 @@ import { createVertex } from '@ai-sdk/google-vertex/edge';
|
|
82
112
|
import { generateText } from 'ai';
|
83
113
|
|
84
114
|
const customProvider = createVertex({
|
85
|
-
|
115
|
+
project: 'your-project-id',
|
86
116
|
location: 'us-central1',
|
87
117
|
googleCredentials: {
|
88
118
|
clientEmail: 'your-client-email',
|
@@ -96,6 +126,42 @@ const { text } = await generateText({
|
|
96
126
|
});
|
97
127
|
```
|
98
128
|
|
129
|
+
### Google Vertex Anthropic Provider Custom Configuration
|
130
|
+
|
131
|
+
The Google Vertex Anthropic provider custom configuration is analogous to the above:
|
132
|
+
|
133
|
+
```ts
|
134
|
+
import { createVertexAnthropic } from '@ai-sdk/google-vertex/anthropic';
|
135
|
+
import { generateText } from 'ai';
|
136
|
+
|
137
|
+
const customProvider = createVertexAnthropic({
|
138
|
+
project: 'your-project-id',
|
139
|
+
location: 'us-east5',
|
140
|
+
});
|
141
|
+
|
142
|
+
const { text } = await generateText({
|
143
|
+
model: customProvider('claude-3-5-sonnet@20240620'),
|
144
|
+
prompt: 'Write a vegetarian lasagna recipe.',
|
145
|
+
});
|
146
|
+
```
|
147
|
+
|
148
|
+
And for the Edge runtime:
|
149
|
+
|
150
|
+
```ts
|
151
|
+
import { vertexAnthropic } from '@ai-sdk/google-vertex/anthropic/edge';
|
152
|
+
import { generateText } from 'ai';
|
153
|
+
|
154
|
+
const customProvider = createVertexAnthropic({
|
155
|
+
project: 'your-project-id',
|
156
|
+
location: 'us-east5',
|
157
|
+
});
|
158
|
+
|
159
|
+
const { text } = await generateText({
|
160
|
+
model: customProvider('claude-3-5-sonnet@20240620'),
|
161
|
+
prompt: 'Write a vegetarian lasagna recipe.',
|
162
|
+
});
|
163
|
+
```
|
164
|
+
|
99
165
|
## Documentation
|
100
166
|
|
101
167
|
Please check out the **[Google Vertex provider](https://sdk.vercel.ai/providers/ai-sdk-providers/google-vertex)** for more information.
|
@@ -0,0 +1,62 @@
|
|
1
|
+
import { ProviderV1, LanguageModelV1 } from '@ai-sdk/provider';
|
2
|
+
import { Resolvable, FetchFunction } from '@ai-sdk/provider-utils';
|
3
|
+
import { AnthropicMessagesSettings, anthropicTools } from '@ai-sdk/anthropic/internal';
|
4
|
+
import { GoogleAuthOptions } from 'google-auth-library';
|
5
|
+
|
6
|
+
type GoogleVertexAnthropicMessagesModelId = 'claude-3-5-sonnet-v2@20241022' | 'claude-3-5-haiku@20241022' | 'claude-3-5-sonnet@20240620' | 'claude-3-haiku@20240307' | 'claude-3-sonnet@20240229' | 'claude-3-opus@20240229' | (string & {});
|
7
|
+
|
8
|
+
interface GoogleVertexAnthropicProvider extends ProviderV1 {
|
9
|
+
/**
|
10
|
+
Creates a model for text generation.
|
11
|
+
*/
|
12
|
+
(modelId: GoogleVertexAnthropicMessagesModelId, settings?: AnthropicMessagesSettings): LanguageModelV1;
|
13
|
+
/**
|
14
|
+
Creates a model for text generation.
|
15
|
+
*/
|
16
|
+
languageModel(modelId: GoogleVertexAnthropicMessagesModelId, settings?: AnthropicMessagesSettings): LanguageModelV1;
|
17
|
+
/**
|
18
|
+
Anthropic-specific computer use tool.
|
19
|
+
*/
|
20
|
+
tools: typeof anthropicTools;
|
21
|
+
}
|
22
|
+
interface GoogleVertexAnthropicProviderSettings$1 {
|
23
|
+
/**
|
24
|
+
* Google Cloud project ID. Defaults to the value of the `GOOGLE_VERTEX_PROJECT` environment variable.
|
25
|
+
*/
|
26
|
+
project?: string;
|
27
|
+
/**
|
28
|
+
* Google Cloud region. Defaults to the value of the `GOOGLE_VERTEX_LOCATION` environment variable.
|
29
|
+
*/
|
30
|
+
location?: string;
|
31
|
+
/**
|
32
|
+
Use a different URL prefix for API calls, e.g. to use proxy servers.
|
33
|
+
The default prefix is `https://api.anthropic.com/v1`.
|
34
|
+
*/
|
35
|
+
baseURL?: string;
|
36
|
+
/**
|
37
|
+
Custom headers to include in the requests.
|
38
|
+
*/
|
39
|
+
headers?: Resolvable<Record<string, string | undefined>>;
|
40
|
+
/**
|
41
|
+
Custom fetch implementation. You can use it as a middleware to intercept requests,
|
42
|
+
or to provide a custom fetch implementation for e.g. testing.
|
43
|
+
*/
|
44
|
+
fetch?: FetchFunction;
|
45
|
+
}
|
46
|
+
|
47
|
+
interface GoogleVertexAnthropicProviderSettings extends GoogleVertexAnthropicProviderSettings$1 {
|
48
|
+
/**
|
49
|
+
Optional. The Authentication options provided by google-auth-library.
|
50
|
+
Complete list of authentication options is documented in the
|
51
|
+
GoogleAuthOptions interface:
|
52
|
+
https://github.com/googleapis/google-auth-library-nodejs/blob/main/src/auth/googleauth.ts.
|
53
|
+
*/
|
54
|
+
googleAuthOptions?: GoogleAuthOptions;
|
55
|
+
}
|
56
|
+
declare function createVertexAnthropic(options?: GoogleVertexAnthropicProviderSettings): GoogleVertexAnthropicProvider;
|
57
|
+
/**
|
58
|
+
Default Google Vertex Anthropic provider instance.
|
59
|
+
*/
|
60
|
+
declare const vertexAnthropic: GoogleVertexAnthropicProvider;
|
61
|
+
|
62
|
+
export { type GoogleVertexAnthropicProvider, type GoogleVertexAnthropicProviderSettings, createVertexAnthropic, vertexAnthropic };
|
@@ -0,0 +1,62 @@
|
|
1
|
+
import { ProviderV1, LanguageModelV1 } from '@ai-sdk/provider';
|
2
|
+
import { Resolvable, FetchFunction } from '@ai-sdk/provider-utils';
|
3
|
+
import { AnthropicMessagesSettings, anthropicTools } from '@ai-sdk/anthropic/internal';
|
4
|
+
import { GoogleAuthOptions } from 'google-auth-library';
|
5
|
+
|
6
|
+
type GoogleVertexAnthropicMessagesModelId = 'claude-3-5-sonnet-v2@20241022' | 'claude-3-5-haiku@20241022' | 'claude-3-5-sonnet@20240620' | 'claude-3-haiku@20240307' | 'claude-3-sonnet@20240229' | 'claude-3-opus@20240229' | (string & {});
|
7
|
+
|
8
|
+
interface GoogleVertexAnthropicProvider extends ProviderV1 {
|
9
|
+
/**
|
10
|
+
Creates a model for text generation.
|
11
|
+
*/
|
12
|
+
(modelId: GoogleVertexAnthropicMessagesModelId, settings?: AnthropicMessagesSettings): LanguageModelV1;
|
13
|
+
/**
|
14
|
+
Creates a model for text generation.
|
15
|
+
*/
|
16
|
+
languageModel(modelId: GoogleVertexAnthropicMessagesModelId, settings?: AnthropicMessagesSettings): LanguageModelV1;
|
17
|
+
/**
|
18
|
+
Anthropic-specific computer use tool.
|
19
|
+
*/
|
20
|
+
tools: typeof anthropicTools;
|
21
|
+
}
|
22
|
+
interface GoogleVertexAnthropicProviderSettings$1 {
|
23
|
+
/**
|
24
|
+
* Google Cloud project ID. Defaults to the value of the `GOOGLE_VERTEX_PROJECT` environment variable.
|
25
|
+
*/
|
26
|
+
project?: string;
|
27
|
+
/**
|
28
|
+
* Google Cloud region. Defaults to the value of the `GOOGLE_VERTEX_LOCATION` environment variable.
|
29
|
+
*/
|
30
|
+
location?: string;
|
31
|
+
/**
|
32
|
+
Use a different URL prefix for API calls, e.g. to use proxy servers.
|
33
|
+
The default prefix is `https://api.anthropic.com/v1`.
|
34
|
+
*/
|
35
|
+
baseURL?: string;
|
36
|
+
/**
|
37
|
+
Custom headers to include in the requests.
|
38
|
+
*/
|
39
|
+
headers?: Resolvable<Record<string, string | undefined>>;
|
40
|
+
/**
|
41
|
+
Custom fetch implementation. You can use it as a middleware to intercept requests,
|
42
|
+
or to provide a custom fetch implementation for e.g. testing.
|
43
|
+
*/
|
44
|
+
fetch?: FetchFunction;
|
45
|
+
}
|
46
|
+
|
47
|
+
interface GoogleVertexAnthropicProviderSettings extends GoogleVertexAnthropicProviderSettings$1 {
|
48
|
+
/**
|
49
|
+
Optional. The Authentication options provided by google-auth-library.
|
50
|
+
Complete list of authentication options is documented in the
|
51
|
+
GoogleAuthOptions interface:
|
52
|
+
https://github.com/googleapis/google-auth-library-nodejs/blob/main/src/auth/googleauth.ts.
|
53
|
+
*/
|
54
|
+
googleAuthOptions?: GoogleAuthOptions;
|
55
|
+
}
|
56
|
+
declare function createVertexAnthropic(options?: GoogleVertexAnthropicProviderSettings): GoogleVertexAnthropicProvider;
|
57
|
+
/**
|
58
|
+
Default Google Vertex Anthropic provider instance.
|
59
|
+
*/
|
60
|
+
declare const vertexAnthropic: GoogleVertexAnthropicProvider;
|
61
|
+
|
62
|
+
export { type GoogleVertexAnthropicProvider, type GoogleVertexAnthropicProviderSettings, createVertexAnthropic, vertexAnthropic };
|
@@ -0,0 +1,121 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __defProp = Object.defineProperty;
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
6
|
+
var __export = (target, all) => {
|
7
|
+
for (var name in all)
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
9
|
+
};
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
12
|
+
for (let key of __getOwnPropNames(from))
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
15
|
+
}
|
16
|
+
return to;
|
17
|
+
};
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
19
|
+
|
20
|
+
// src/anthropic/index.ts
|
21
|
+
var anthropic_exports = {};
|
22
|
+
__export(anthropic_exports, {
|
23
|
+
createVertexAnthropic: () => createVertexAnthropic2,
|
24
|
+
vertexAnthropic: () => vertexAnthropic
|
25
|
+
});
|
26
|
+
module.exports = __toCommonJS(anthropic_exports);
|
27
|
+
|
28
|
+
// src/google-vertex-auth-google-auth-library.ts
|
29
|
+
var import_google_auth_library = require("google-auth-library");
|
30
|
+
var authInstance = null;
|
31
|
+
var authOptions = null;
|
32
|
+
function getAuth(options) {
|
33
|
+
if (!authInstance || options !== authOptions) {
|
34
|
+
authInstance = new import_google_auth_library.GoogleAuth({
|
35
|
+
scopes: ["https://www.googleapis.com/auth/cloud-platform"],
|
36
|
+
...options
|
37
|
+
});
|
38
|
+
authOptions = options;
|
39
|
+
}
|
40
|
+
return authInstance;
|
41
|
+
}
|
42
|
+
async function generateAuthToken(options) {
|
43
|
+
const auth = getAuth(options || {});
|
44
|
+
const client = await auth.getClient();
|
45
|
+
const token = await client.getAccessToken();
|
46
|
+
return (token == null ? void 0 : token.token) || null;
|
47
|
+
}
|
48
|
+
|
49
|
+
// src/anthropic/google-vertex-anthropic-provider.ts
|
50
|
+
var import_provider = require("@ai-sdk/provider");
|
51
|
+
var import_provider_utils = require("@ai-sdk/provider-utils");
|
52
|
+
var import_internal = require("@ai-sdk/anthropic/internal");
|
53
|
+
function createVertexAnthropic(options = {}) {
|
54
|
+
var _a;
|
55
|
+
const location = (0, import_provider_utils.loadOptionalSetting)({
|
56
|
+
settingValue: options.location,
|
57
|
+
environmentVariableName: "GOOGLE_VERTEX_LOCATION"
|
58
|
+
});
|
59
|
+
const project = (0, import_provider_utils.loadOptionalSetting)({
|
60
|
+
settingValue: options.project,
|
61
|
+
environmentVariableName: "GOOGLE_VERTEX_PROJECT"
|
62
|
+
});
|
63
|
+
const baseURL = (_a = (0, import_provider_utils.withoutTrailingSlash)(options.baseURL)) != null ? _a : `https://${location}-aiplatform.googleapis.com/v1/projects/${project}/locations/${location}/publishers/anthropic/models`;
|
64
|
+
const createChatModel = (modelId, settings = {}) => {
|
65
|
+
var _a2;
|
66
|
+
return new import_internal.AnthropicMessagesLanguageModel(
|
67
|
+
modelId,
|
68
|
+
settings,
|
69
|
+
{
|
70
|
+
provider: "vertex.anthropic.messages",
|
71
|
+
baseURL,
|
72
|
+
headers: (_a2 = options.headers) != null ? _a2 : {},
|
73
|
+
fetch: options.fetch,
|
74
|
+
buildRequestUrl: (baseURL2, isStreaming) => `${baseURL2}/${modelId}:${isStreaming ? "streamRawPredict" : "rawPredict"}`,
|
75
|
+
transformRequestBody: (args) => {
|
76
|
+
const { model, ...rest } = args;
|
77
|
+
return {
|
78
|
+
...rest,
|
79
|
+
anthropic_version: "vertex-2023-10-16"
|
80
|
+
};
|
81
|
+
}
|
82
|
+
}
|
83
|
+
);
|
84
|
+
};
|
85
|
+
const provider = function(modelId, settings) {
|
86
|
+
if (new.target) {
|
87
|
+
throw new Error(
|
88
|
+
"The Anthropic model function cannot be called with the new keyword."
|
89
|
+
);
|
90
|
+
}
|
91
|
+
return createChatModel(modelId, settings);
|
92
|
+
};
|
93
|
+
provider.languageModel = createChatModel;
|
94
|
+
provider.chat = createChatModel;
|
95
|
+
provider.messages = createChatModel;
|
96
|
+
provider.textEmbeddingModel = (modelId) => {
|
97
|
+
throw new import_provider.NoSuchModelError({ modelId, modelType: "textEmbeddingModel" });
|
98
|
+
};
|
99
|
+
provider.tools = import_internal.anthropicTools;
|
100
|
+
return provider;
|
101
|
+
}
|
102
|
+
|
103
|
+
// src/anthropic/google-vertex-anthropic-provider-node.ts
|
104
|
+
function createVertexAnthropic2(options = {}) {
|
105
|
+
var _a;
|
106
|
+
return createVertexAnthropic({
|
107
|
+
...options,
|
108
|
+
headers: (_a = options.headers) != null ? _a : async () => ({
|
109
|
+
Authorization: `Bearer ${await generateAuthToken(
|
110
|
+
options.googleAuthOptions
|
111
|
+
)}`
|
112
|
+
})
|
113
|
+
});
|
114
|
+
}
|
115
|
+
var vertexAnthropic = createVertexAnthropic2();
|
116
|
+
// Annotate the CommonJS export names for ESM import in node:
|
117
|
+
0 && (module.exports = {
|
118
|
+
createVertexAnthropic,
|
119
|
+
vertexAnthropic
|
120
|
+
});
|
121
|
+
//# sourceMappingURL=index.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../../src/anthropic/index.ts","../../src/google-vertex-auth-google-auth-library.ts","../../src/anthropic/google-vertex-anthropic-provider.ts","../../src/anthropic/google-vertex-anthropic-provider-node.ts"],"sourcesContent":["export {\n vertexAnthropic,\n createVertexAnthropic,\n} from './google-vertex-anthropic-provider-node';\nexport type {\n GoogleVertexAnthropicProvider,\n GoogleVertexAnthropicProviderSettings,\n} from './google-vertex-anthropic-provider-node';\n","import { GoogleAuth, GoogleAuthOptions } from 'google-auth-library';\n\nlet authInstance: GoogleAuth | null = null;\nlet authOptions: GoogleAuthOptions | null = null;\n\nfunction getAuth(options: GoogleAuthOptions) {\n if (!authInstance || options !== authOptions) {\n authInstance = new GoogleAuth({\n scopes: ['https://www.googleapis.com/auth/cloud-platform'],\n ...options,\n });\n authOptions = options;\n }\n return authInstance;\n}\n\nexport async function generateAuthToken(options?: GoogleAuthOptions) {\n const auth = getAuth(options || {});\n const client = await auth.getClient();\n const token = await client.getAccessToken();\n return token?.token || null;\n}\n\n// For testing purposes only\nexport function _resetAuthInstance() {\n authInstance = null;\n}\n","import {\n LanguageModelV1,\n NoSuchModelError,\n ProviderV1,\n} from '@ai-sdk/provider';\nimport {\n FetchFunction,\n Resolvable,\n loadOptionalSetting,\n withoutTrailingSlash,\n} from '@ai-sdk/provider-utils';\nimport {\n anthropicTools,\n AnthropicMessagesLanguageModel,\n AnthropicMessagesModelId,\n AnthropicMessagesSettings,\n} from '@ai-sdk/anthropic/internal';\nimport {\n GoogleVertexAnthropicMessagesModelId,\n GoogleVertexAnthropicMessagesSettings,\n} from './google-vertex-anthropic-messages-settings';\nexport interface GoogleVertexAnthropicProvider extends ProviderV1 {\n /**\nCreates a model for text generation.\n*/\n (\n modelId: GoogleVertexAnthropicMessagesModelId,\n settings?: AnthropicMessagesSettings,\n ): LanguageModelV1;\n\n /**\nCreates a model for text generation.\n*/\n languageModel(\n modelId: GoogleVertexAnthropicMessagesModelId,\n settings?: AnthropicMessagesSettings,\n ): LanguageModelV1;\n\n /**\nAnthropic-specific computer use tool.\n */\n tools: typeof anthropicTools;\n}\n\nexport interface GoogleVertexAnthropicProviderSettings {\n /**\n * Google Cloud project ID. Defaults to the value of the `GOOGLE_VERTEX_PROJECT` environment variable.\n */\n project?: string;\n\n /**\n * Google Cloud region. Defaults to the value of the `GOOGLE_VERTEX_LOCATION` environment variable.\n */\n location?: string;\n\n /**\nUse a different URL prefix for API calls, e.g. to use proxy servers.\nThe default prefix is `https://api.anthropic.com/v1`.\n */\n baseURL?: string;\n\n /**\nCustom headers to include in the requests.\n */\n headers?: Resolvable<Record<string, string | undefined>>;\n\n /**\nCustom fetch implementation. You can use it as a middleware to intercept requests,\nor to provide a custom fetch implementation for e.g. testing.\n */\n fetch?: FetchFunction;\n}\n\n/**\nCreate a Google Vertex Anthropic provider instance.\n */\nexport function createVertexAnthropic(\n options: GoogleVertexAnthropicProviderSettings = {},\n): GoogleVertexAnthropicProvider {\n const location = loadOptionalSetting({\n settingValue: options.location,\n environmentVariableName: 'GOOGLE_VERTEX_LOCATION',\n });\n const project = loadOptionalSetting({\n settingValue: options.project,\n environmentVariableName: 'GOOGLE_VERTEX_PROJECT',\n });\n const baseURL =\n withoutTrailingSlash(options.baseURL) ??\n `https://${location}-aiplatform.googleapis.com/v1/projects/${project}/locations/${location}/publishers/anthropic/models`;\n\n const createChatModel = (\n modelId: GoogleVertexAnthropicMessagesModelId,\n settings: GoogleVertexAnthropicMessagesSettings = {},\n ) =>\n new AnthropicMessagesLanguageModel(\n modelId as AnthropicMessagesModelId,\n settings,\n {\n provider: 'vertex.anthropic.messages',\n baseURL,\n headers: options.headers ?? {},\n fetch: options.fetch,\n buildRequestUrl: (baseURL, isStreaming) =>\n `${baseURL}/${modelId}:${\n isStreaming ? 'streamRawPredict' : 'rawPredict'\n }`,\n transformRequestBody: args => {\n // Remove model from args and add anthropic version\n const { model, ...rest } = args;\n return {\n ...rest,\n anthropic_version: 'vertex-2023-10-16',\n };\n },\n },\n );\n\n const provider = function (\n modelId: GoogleVertexAnthropicMessagesModelId,\n settings?: GoogleVertexAnthropicMessagesSettings,\n ) {\n if (new.target) {\n throw new Error(\n 'The Anthropic model function cannot be called with the new keyword.',\n );\n }\n\n return createChatModel(modelId, settings);\n };\n\n provider.languageModel = createChatModel;\n provider.chat = createChatModel;\n provider.messages = createChatModel;\n provider.textEmbeddingModel = (modelId: string) => {\n throw new NoSuchModelError({ modelId, modelType: 'textEmbeddingModel' });\n };\n\n provider.tools = anthropicTools;\n\n return provider as GoogleVertexAnthropicProvider;\n}\n","import { generateAuthToken } from '../google-vertex-auth-google-auth-library';\nimport {\n createVertexAnthropic as createVertexAnthropicOriginal,\n GoogleVertexAnthropicProvider,\n GoogleVertexAnthropicProviderSettings as GoogleVertexAnthropicProviderSettingsOriginal,\n} from './google-vertex-anthropic-provider';\nimport { GoogleAuthOptions } from 'google-auth-library';\n\nexport type { GoogleVertexAnthropicProvider };\n\nexport interface GoogleVertexAnthropicProviderSettings\n extends GoogleVertexAnthropicProviderSettingsOriginal {\n /**\n Optional. The Authentication options provided by google-auth-library.\nComplete list of authentication options is documented in the\nGoogleAuthOptions interface:\nhttps://github.com/googleapis/google-auth-library-nodejs/blob/main/src/auth/googleauth.ts.\n */\n googleAuthOptions?: GoogleAuthOptions;\n}\n\nexport function createVertexAnthropic(\n options: GoogleVertexAnthropicProviderSettings = {},\n): GoogleVertexAnthropicProvider {\n return createVertexAnthropicOriginal({\n ...options,\n headers:\n options.headers ??\n (async () => ({\n Authorization: `Bearer ${await generateAuthToken(\n options.googleAuthOptions,\n )}`,\n })),\n });\n}\n\n/**\nDefault Google Vertex Anthropic provider instance.\n */\nexport const vertexAnthropic = createVertexAnthropic();\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAA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O;AAAA,YACL,GAAG;AAAA,YACH,mBAAmB;AAAA,UACrB;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA;AAEF,QAAM,WAAW,SACf,SACA,UACA;AACA,QAAI,YAAY;AACd,YAAM,IAAI;AAAA,QACR;AAAA,MACF;AAAA,IACF;AAEA,WAAO,gBAAgB,SAAS,QAAQ;AAAA,EAC1C;AAEA,WAAS,gBAAgB;AACzB,WAAS,OAAO;AAChB,WAAS,WAAW;AACpB,WAAS,qBAAqB,CAAC,YAAoB;AACjD,UAAM,IAAI,iCAAiB,EAAE,SAAS,WAAW,qBAAqB,CAAC;AAAA,EACzE;AAEA,WAAS,QAAQ;AAEjB,SAAO;AACT;;;ACxHO,SAASC,uBACd,UAAiD,CAAC,GACnB;AAvBjC;AAwBE,SAAO,sBAA8B;AAAA,IACnC,GAAG;AAAA,IACH,UACE,aAAQ,YAAR,YACC,aAAa;AAAA,MACZ,eAAe,UAAU,MAAM;AAAA,QAC7B,QAAQ;AAAA,MACV,CAAC;AAAA,IACH;AAAA,EACJ,CAAC;AACH;AAKO,IAAM,kBAAkBA,uBAAsB;","names":["createVertexAnthropic","_a","baseURL","createVertexAnthropic"]}
|
@@ -0,0 +1,101 @@
|
|
1
|
+
// src/google-vertex-auth-google-auth-library.ts
|
2
|
+
import { GoogleAuth } from "google-auth-library";
|
3
|
+
var authInstance = null;
|
4
|
+
var authOptions = null;
|
5
|
+
function getAuth(options) {
|
6
|
+
if (!authInstance || options !== authOptions) {
|
7
|
+
authInstance = new GoogleAuth({
|
8
|
+
scopes: ["https://www.googleapis.com/auth/cloud-platform"],
|
9
|
+
...options
|
10
|
+
});
|
11
|
+
authOptions = options;
|
12
|
+
}
|
13
|
+
return authInstance;
|
14
|
+
}
|
15
|
+
async function generateAuthToken(options) {
|
16
|
+
const auth = getAuth(options || {});
|
17
|
+
const client = await auth.getClient();
|
18
|
+
const token = await client.getAccessToken();
|
19
|
+
return (token == null ? void 0 : token.token) || null;
|
20
|
+
}
|
21
|
+
|
22
|
+
// src/anthropic/google-vertex-anthropic-provider.ts
|
23
|
+
import {
|
24
|
+
NoSuchModelError
|
25
|
+
} from "@ai-sdk/provider";
|
26
|
+
import {
|
27
|
+
loadOptionalSetting,
|
28
|
+
withoutTrailingSlash
|
29
|
+
} from "@ai-sdk/provider-utils";
|
30
|
+
import {
|
31
|
+
anthropicTools,
|
32
|
+
AnthropicMessagesLanguageModel
|
33
|
+
} from "@ai-sdk/anthropic/internal";
|
34
|
+
function createVertexAnthropic(options = {}) {
|
35
|
+
var _a;
|
36
|
+
const location = loadOptionalSetting({
|
37
|
+
settingValue: options.location,
|
38
|
+
environmentVariableName: "GOOGLE_VERTEX_LOCATION"
|
39
|
+
});
|
40
|
+
const project = loadOptionalSetting({
|
41
|
+
settingValue: options.project,
|
42
|
+
environmentVariableName: "GOOGLE_VERTEX_PROJECT"
|
43
|
+
});
|
44
|
+
const baseURL = (_a = withoutTrailingSlash(options.baseURL)) != null ? _a : `https://${location}-aiplatform.googleapis.com/v1/projects/${project}/locations/${location}/publishers/anthropic/models`;
|
45
|
+
const createChatModel = (modelId, settings = {}) => {
|
46
|
+
var _a2;
|
47
|
+
return new AnthropicMessagesLanguageModel(
|
48
|
+
modelId,
|
49
|
+
settings,
|
50
|
+
{
|
51
|
+
provider: "vertex.anthropic.messages",
|
52
|
+
baseURL,
|
53
|
+
headers: (_a2 = options.headers) != null ? _a2 : {},
|
54
|
+
fetch: options.fetch,
|
55
|
+
buildRequestUrl: (baseURL2, isStreaming) => `${baseURL2}/${modelId}:${isStreaming ? "streamRawPredict" : "rawPredict"}`,
|
56
|
+
transformRequestBody: (args) => {
|
57
|
+
const { model, ...rest } = args;
|
58
|
+
return {
|
59
|
+
...rest,
|
60
|
+
anthropic_version: "vertex-2023-10-16"
|
61
|
+
};
|
62
|
+
}
|
63
|
+
}
|
64
|
+
);
|
65
|
+
};
|
66
|
+
const provider = function(modelId, settings) {
|
67
|
+
if (new.target) {
|
68
|
+
throw new Error(
|
69
|
+
"The Anthropic model function cannot be called with the new keyword."
|
70
|
+
);
|
71
|
+
}
|
72
|
+
return createChatModel(modelId, settings);
|
73
|
+
};
|
74
|
+
provider.languageModel = createChatModel;
|
75
|
+
provider.chat = createChatModel;
|
76
|
+
provider.messages = createChatModel;
|
77
|
+
provider.textEmbeddingModel = (modelId) => {
|
78
|
+
throw new NoSuchModelError({ modelId, modelType: "textEmbeddingModel" });
|
79
|
+
};
|
80
|
+
provider.tools = anthropicTools;
|
81
|
+
return provider;
|
82
|
+
}
|
83
|
+
|
84
|
+
// src/anthropic/google-vertex-anthropic-provider-node.ts
|
85
|
+
function createVertexAnthropic2(options = {}) {
|
86
|
+
var _a;
|
87
|
+
return createVertexAnthropic({
|
88
|
+
...options,
|
89
|
+
headers: (_a = options.headers) != null ? _a : async () => ({
|
90
|
+
Authorization: `Bearer ${await generateAuthToken(
|
91
|
+
options.googleAuthOptions
|
92
|
+
)}`
|
93
|
+
})
|
94
|
+
});
|
95
|
+
}
|
96
|
+
var vertexAnthropic = createVertexAnthropic2();
|
97
|
+
export {
|
98
|
+
createVertexAnthropic2 as createVertexAnthropic,
|
99
|
+
vertexAnthropic
|
100
|
+
};
|
101
|
+
//# sourceMappingURL=index.mjs.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../../src/google-vertex-auth-google-auth-library.ts","../../src/anthropic/google-vertex-anthropic-provider.ts","../../src/anthropic/google-vertex-anthropic-provider-node.ts"],"sourcesContent":["import { GoogleAuth, GoogleAuthOptions } from 'google-auth-library';\n\nlet authInstance: GoogleAuth | null = null;\nlet authOptions: GoogleAuthOptions | null = null;\n\nfunction getAuth(options: GoogleAuthOptions) {\n if (!authInstance || options !== authOptions) {\n authInstance = new GoogleAuth({\n scopes: ['https://www.googleapis.com/auth/cloud-platform'],\n ...options,\n });\n authOptions = options;\n }\n return authInstance;\n}\n\nexport async function generateAuthToken(options?: GoogleAuthOptions) {\n const auth = getAuth(options || {});\n const client = await auth.getClient();\n const token = await client.getAccessToken();\n return token?.token || null;\n}\n\n// For testing purposes only\nexport function _resetAuthInstance() {\n authInstance = null;\n}\n","import {\n LanguageModelV1,\n NoSuchModelError,\n ProviderV1,\n} from '@ai-sdk/provider';\nimport {\n FetchFunction,\n Resolvable,\n loadOptionalSetting,\n withoutTrailingSlash,\n} from '@ai-sdk/provider-utils';\nimport {\n anthropicTools,\n AnthropicMessagesLanguageModel,\n AnthropicMessagesModelId,\n AnthropicMessagesSettings,\n} from '@ai-sdk/anthropic/internal';\nimport {\n GoogleVertexAnthropicMessagesModelId,\n GoogleVertexAnthropicMessagesSettings,\n} from './google-vertex-anthropic-messages-settings';\nexport interface GoogleVertexAnthropicProvider extends ProviderV1 {\n /**\nCreates a model for text generation.\n*/\n (\n modelId: GoogleVertexAnthropicMessagesModelId,\n settings?: AnthropicMessagesSettings,\n ): LanguageModelV1;\n\n /**\nCreates a model for text generation.\n*/\n languageModel(\n modelId: GoogleVertexAnthropicMessagesModelId,\n settings?: AnthropicMessagesSettings,\n ): LanguageModelV1;\n\n /**\nAnthropic-specific computer use tool.\n */\n tools: typeof anthropicTools;\n}\n\nexport interface GoogleVertexAnthropicProviderSettings {\n /**\n * Google Cloud project ID. Defaults to the value of the `GOOGLE_VERTEX_PROJECT` environment variable.\n */\n project?: string;\n\n /**\n * Google Cloud region. Defaults to the value of the `GOOGLE_VERTEX_LOCATION` environment variable.\n */\n location?: string;\n\n /**\nUse a different URL prefix for API calls, e.g. to use proxy servers.\nThe default prefix is `https://api.anthropic.com/v1`.\n */\n baseURL?: string;\n\n /**\nCustom headers to include in the requests.\n */\n headers?: Resolvable<Record<string, string | undefined>>;\n\n /**\nCustom fetch implementation. You can use it as a middleware to intercept requests,\nor to provide a custom fetch implementation for e.g. testing.\n */\n fetch?: FetchFunction;\n}\n\n/**\nCreate a Google Vertex Anthropic provider instance.\n */\nexport function createVertexAnthropic(\n options: GoogleVertexAnthropicProviderSettings = {},\n): GoogleVertexAnthropicProvider {\n const location = loadOptionalSetting({\n settingValue: options.location,\n environmentVariableName: 'GOOGLE_VERTEX_LOCATION',\n });\n const project = loadOptionalSetting({\n settingValue: options.project,\n environmentVariableName: 'GOOGLE_VERTEX_PROJECT',\n });\n const baseURL =\n withoutTrailingSlash(options.baseURL) ??\n `https://${location}-aiplatform.googleapis.com/v1/projects/${project}/locations/${location}/publishers/anthropic/models`;\n\n const createChatModel = (\n modelId: GoogleVertexAnthropicMessagesModelId,\n settings: GoogleVertexAnthropicMessagesSettings = {},\n ) =>\n new AnthropicMessagesLanguageModel(\n modelId as AnthropicMessagesModelId,\n settings,\n {\n provider: 'vertex.anthropic.messages',\n baseURL,\n headers: options.headers ?? {},\n fetch: options.fetch,\n buildRequestUrl: (baseURL, isStreaming) =>\n `${baseURL}/${modelId}:${\n isStreaming ? 'streamRawPredict' : 'rawPredict'\n }`,\n transformRequestBody: args => {\n // Remove model from args and add anthropic version\n const { model, ...rest } = args;\n return {\n ...rest,\n anthropic_version: 'vertex-2023-10-16',\n };\n },\n },\n );\n\n const provider = function (\n modelId: GoogleVertexAnthropicMessagesModelId,\n settings?: GoogleVertexAnthropicMessagesSettings,\n ) {\n if (new.target) {\n throw new Error(\n 'The Anthropic model function cannot be called with the new keyword.',\n );\n }\n\n return createChatModel(modelId, settings);\n };\n\n provider.languageModel = createChatModel;\n provider.chat = createChatModel;\n provider.messages = createChatModel;\n provider.textEmbeddingModel = (modelId: string) => {\n throw new NoSuchModelError({ modelId, modelType: 'textEmbeddingModel' });\n };\n\n provider.tools = anthropicTools;\n\n return provider as GoogleVertexAnthropicProvider;\n}\n","import { generateAuthToken } from '../google-vertex-auth-google-auth-library';\nimport {\n createVertexAnthropic as createVertexAnthropicOriginal,\n GoogleVertexAnthropicProvider,\n GoogleVertexAnthropicProviderSettings as GoogleVertexAnthropicProviderSettingsOriginal,\n} from './google-vertex-anthropic-provider';\nimport { GoogleAuthOptions } from 'google-auth-library';\n\nexport type { GoogleVertexAnthropicProvider };\n\nexport interface GoogleVertexAnthropicProviderSettings\n extends GoogleVertexAnthropicProviderSettingsOriginal {\n /**\n Optional. The Authentication options provided by google-auth-library.\nComplete list of authentication options is documented in the\nGoogleAuthOptions interface:\nhttps://github.com/googleapis/google-auth-library-nodejs/blob/main/src/auth/googleauth.ts.\n */\n googleAuthOptions?: GoogleAuthOptions;\n}\n\nexport function createVertexAnthropic(\n options: GoogleVertexAnthropicProviderSettings = {},\n): GoogleVertexAnthropicProvider {\n return createVertexAnthropicOriginal({\n ...options,\n headers:\n options.headers ??\n (async () => ({\n Authorization: `Bearer ${await generateAuthToken(\n options.googleAuthOptions,\n )}`,\n })),\n });\n}\n\n/**\nDefault Google Vertex Anthropic provider instance.\n */\nexport const vertexAnthropic = createVertexAnthropic();\n"],"mappings":";AAAA,SAAS,kBAAqC;AAE9C,IAAI,eAAkC;AACtC,IAAI,cAAwC;AAE5C,SAAS,QAAQ,SAA4B;AAC3C,MAAI,CAAC,gBAAgB,YAAY,aAAa;AAC5C,mBAAe,IAAI,WAAW;AAAA,MAC5B,QAAQ,CAAC,gDAAgD;AAAA,MACzD,GAAG;AAAA,IACL,CAAC;AACD,kBAAc;AAAA,EAChB;AACA,SAAO;AACT;AAEA,eAAsB,kBAAkB,SAA6B;AACnE,QAAM,OAAO,QAAQ,WAAW,CAAC,CAAC;AAClC,QAAM,SAAS,MAAM,KAAK,UAAU;AACpC,QAAM,QAAQ,MAAM,OAAO,eAAe;AAC1C,UAAO,+BAAO,UAAS;AACzB;;;ACrBA;AAAA,EAEE;AAAA,OAEK;AACP;AAAA,EAGE;AAAA,EACA;AAAA,OACK;AACP;AAAA,EACE;AAAA,EACA;AAAA,OAGK;AA4DA,SAAS,sBACd,UAAiD,CAAC,GACnB;AA9EjC;AA+EE,QAAM,WAAW,oBAAoB;AAAA,IACnC,cAAc,QAAQ;AAAA,IACtB,yBAAyB;AAAA,EAC3B,CAAC;AACD,QAAM,UAAU,oBAAoB;AAAA,IAClC,cAAc,QAAQ;AAAA,IACtB,yBAAyB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
@@ -0,0 +1,78 @@
|
|
1
|
+
import { ProviderV1, LanguageModelV1 } from '@ai-sdk/provider';
|
2
|
+
import { Resolvable, FetchFunction } from '@ai-sdk/provider-utils';
|
3
|
+
import { AnthropicMessagesSettings, anthropicTools } from '@ai-sdk/anthropic/internal';
|
4
|
+
|
5
|
+
interface GoogleCredentials {
|
6
|
+
/**
|
7
|
+
* The client email for the Google Cloud service account. Defaults to the
|
8
|
+
* value of the `GOOGLE_CLIENT_EMAIL` environment variable.
|
9
|
+
*/
|
10
|
+
clientEmail: string;
|
11
|
+
/**
|
12
|
+
* The private key for the Google Cloud service account. Defaults to the
|
13
|
+
* value of the `GOOGLE_PRIVATE_KEY` environment variable.
|
14
|
+
*/
|
15
|
+
privateKey: string;
|
16
|
+
/**
|
17
|
+
* Optional. The private key ID for the Google Cloud service account. Defaults
|
18
|
+
* to the value of the `GOOGLE_PRIVATE_KEY_ID` environment variable.
|
19
|
+
*/
|
20
|
+
privateKeyId?: string;
|
21
|
+
}
|
22
|
+
|
23
|
+
type GoogleVertexAnthropicMessagesModelId = 'claude-3-5-sonnet-v2@20241022' | 'claude-3-5-haiku@20241022' | 'claude-3-5-sonnet@20240620' | 'claude-3-haiku@20240307' | 'claude-3-sonnet@20240229' | 'claude-3-opus@20240229' | (string & {});
|
24
|
+
|
25
|
+
interface GoogleVertexAnthropicProvider extends ProviderV1 {
|
26
|
+
/**
|
27
|
+
Creates a model for text generation.
|
28
|
+
*/
|
29
|
+
(modelId: GoogleVertexAnthropicMessagesModelId, settings?: AnthropicMessagesSettings): LanguageModelV1;
|
30
|
+
/**
|
31
|
+
Creates a model for text generation.
|
32
|
+
*/
|
33
|
+
languageModel(modelId: GoogleVertexAnthropicMessagesModelId, settings?: AnthropicMessagesSettings): LanguageModelV1;
|
34
|
+
/**
|
35
|
+
Anthropic-specific computer use tool.
|
36
|
+
*/
|
37
|
+
tools: typeof anthropicTools;
|
38
|
+
}
|
39
|
+
interface GoogleVertexAnthropicProviderSettings$1 {
|
40
|
+
/**
|
41
|
+
* Google Cloud project ID. Defaults to the value of the `GOOGLE_VERTEX_PROJECT` environment variable.
|
42
|
+
*/
|
43
|
+
project?: string;
|
44
|
+
/**
|
45
|
+
* Google Cloud region. Defaults to the value of the `GOOGLE_VERTEX_LOCATION` environment variable.
|
46
|
+
*/
|
47
|
+
location?: string;
|
48
|
+
/**
|
49
|
+
Use a different URL prefix for API calls, e.g. to use proxy servers.
|
50
|
+
The default prefix is `https://api.anthropic.com/v1`.
|
51
|
+
*/
|
52
|
+
baseURL?: string;
|
53
|
+
/**
|
54
|
+
Custom headers to include in the requests.
|
55
|
+
*/
|
56
|
+
headers?: Resolvable<Record<string, string | undefined>>;
|
57
|
+
/**
|
58
|
+
Custom fetch implementation. You can use it as a middleware to intercept requests,
|
59
|
+
or to provide a custom fetch implementation for e.g. testing.
|
60
|
+
*/
|
61
|
+
fetch?: FetchFunction;
|
62
|
+
}
|
63
|
+
|
64
|
+
interface GoogleVertexAnthropicProviderSettings extends GoogleVertexAnthropicProviderSettings$1 {
|
65
|
+
/**
|
66
|
+
* Optional. The Google credentials for the Google Cloud service account. If
|
67
|
+
* not provided, the Google Vertex provider will use environment variables to
|
68
|
+
* load the credentials.
|
69
|
+
*/
|
70
|
+
googleCredentials?: GoogleCredentials;
|
71
|
+
}
|
72
|
+
declare function createVertexAnthropic(options?: GoogleVertexAnthropicProviderSettings): GoogleVertexAnthropicProvider;
|
73
|
+
/**
|
74
|
+
* Default Google Vertex AI Anthropic provider instance.
|
75
|
+
*/
|
76
|
+
declare const vertexAnthropic: GoogleVertexAnthropicProvider;
|
77
|
+
|
78
|
+
export { type GoogleVertexAnthropicProvider, type GoogleVertexAnthropicProviderSettings, createVertexAnthropic, vertexAnthropic };
|
@@ -0,0 +1,78 @@
|
|
1
|
+
import { ProviderV1, LanguageModelV1 } from '@ai-sdk/provider';
|
2
|
+
import { Resolvable, FetchFunction } from '@ai-sdk/provider-utils';
|
3
|
+
import { AnthropicMessagesSettings, anthropicTools } from '@ai-sdk/anthropic/internal';
|
4
|
+
|
5
|
+
interface GoogleCredentials {
|
6
|
+
/**
|
7
|
+
* The client email for the Google Cloud service account. Defaults to the
|
8
|
+
* value of the `GOOGLE_CLIENT_EMAIL` environment variable.
|
9
|
+
*/
|
10
|
+
clientEmail: string;
|
11
|
+
/**
|
12
|
+
* The private key for the Google Cloud service account. Defaults to the
|
13
|
+
* value of the `GOOGLE_PRIVATE_KEY` environment variable.
|
14
|
+
*/
|
15
|
+
privateKey: string;
|
16
|
+
/**
|
17
|
+
* Optional. The private key ID for the Google Cloud service account. Defaults
|
18
|
+
* to the value of the `GOOGLE_PRIVATE_KEY_ID` environment variable.
|
19
|
+
*/
|
20
|
+
privateKeyId?: string;
|
21
|
+
}
|
22
|
+
|
23
|
+
type GoogleVertexAnthropicMessagesModelId = 'claude-3-5-sonnet-v2@20241022' | 'claude-3-5-haiku@20241022' | 'claude-3-5-sonnet@20240620' | 'claude-3-haiku@20240307' | 'claude-3-sonnet@20240229' | 'claude-3-opus@20240229' | (string & {});
|
24
|
+
|
25
|
+
interface GoogleVertexAnthropicProvider extends ProviderV1 {
|
26
|
+
/**
|
27
|
+
Creates a model for text generation.
|
28
|
+
*/
|
29
|
+
(modelId: GoogleVertexAnthropicMessagesModelId, settings?: AnthropicMessagesSettings): LanguageModelV1;
|
30
|
+
/**
|
31
|
+
Creates a model for text generation.
|
32
|
+
*/
|
33
|
+
languageModel(modelId: GoogleVertexAnthropicMessagesModelId, settings?: AnthropicMessagesSettings): LanguageModelV1;
|
34
|
+
/**
|
35
|
+
Anthropic-specific computer use tool.
|
36
|
+
*/
|
37
|
+
tools: typeof anthropicTools;
|
38
|
+
}
|
39
|
+
interface GoogleVertexAnthropicProviderSettings$1 {
|
40
|
+
/**
|
41
|
+
* Google Cloud project ID. Defaults to the value of the `GOOGLE_VERTEX_PROJECT` environment variable.
|
42
|
+
*/
|
43
|
+
project?: string;
|
44
|
+
/**
|
45
|
+
* Google Cloud region. Defaults to the value of the `GOOGLE_VERTEX_LOCATION` environment variable.
|
46
|
+
*/
|
47
|
+
location?: string;
|
48
|
+
/**
|
49
|
+
Use a different URL prefix for API calls, e.g. to use proxy servers.
|
50
|
+
The default prefix is `https://api.anthropic.com/v1`.
|
51
|
+
*/
|
52
|
+
baseURL?: string;
|
53
|
+
/**
|
54
|
+
Custom headers to include in the requests.
|
55
|
+
*/
|
56
|
+
headers?: Resolvable<Record<string, string | undefined>>;
|
57
|
+
/**
|
58
|
+
Custom fetch implementation. You can use it as a middleware to intercept requests,
|
59
|
+
or to provide a custom fetch implementation for e.g. testing.
|
60
|
+
*/
|
61
|
+
fetch?: FetchFunction;
|
62
|
+
}
|
63
|
+
|
64
|
+
interface GoogleVertexAnthropicProviderSettings extends GoogleVertexAnthropicProviderSettings$1 {
|
65
|
+
/**
|
66
|
+
* Optional. The Google credentials for the Google Cloud service account. If
|
67
|
+
* not provided, the Google Vertex provider will use environment variables to
|
68
|
+
* load the credentials.
|
69
|
+
*/
|
70
|
+
googleCredentials?: GoogleCredentials;
|
71
|
+
}
|
72
|
+
declare function createVertexAnthropic(options?: GoogleVertexAnthropicProviderSettings): GoogleVertexAnthropicProvider;
|
73
|
+
/**
|
74
|
+
* Default Google Vertex AI Anthropic provider instance.
|
75
|
+
*/
|
76
|
+
declare const vertexAnthropic: GoogleVertexAnthropicProvider;
|
77
|
+
|
78
|
+
export { type GoogleVertexAnthropicProvider, type GoogleVertexAnthropicProviderSettings, createVertexAnthropic, vertexAnthropic };
|
@@ -0,0 +1,202 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __defProp = Object.defineProperty;
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
6
|
+
var __export = (target, all) => {
|
7
|
+
for (var name in all)
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
9
|
+
};
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
12
|
+
for (let key of __getOwnPropNames(from))
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
15
|
+
}
|
16
|
+
return to;
|
17
|
+
};
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
19
|
+
|
20
|
+
// src/anthropic/edge/index.ts
|
21
|
+
var edge_exports = {};
|
22
|
+
__export(edge_exports, {
|
23
|
+
createVertexAnthropic: () => createVertexAnthropic2,
|
24
|
+
vertexAnthropic: () => vertexAnthropic
|
25
|
+
});
|
26
|
+
module.exports = __toCommonJS(edge_exports);
|
27
|
+
|
28
|
+
// src/edge/google-vertex-auth-edge.ts
|
29
|
+
var import_provider_utils = require("@ai-sdk/provider-utils");
|
30
|
+
var loadCredentials = async () => {
|
31
|
+
try {
|
32
|
+
return {
|
33
|
+
clientEmail: (0, import_provider_utils.loadSetting)({
|
34
|
+
settingValue: void 0,
|
35
|
+
settingName: "clientEmail",
|
36
|
+
environmentVariableName: "GOOGLE_CLIENT_EMAIL",
|
37
|
+
description: "Google client email"
|
38
|
+
}),
|
39
|
+
privateKey: (0, import_provider_utils.loadSetting)({
|
40
|
+
settingValue: void 0,
|
41
|
+
settingName: "privateKey",
|
42
|
+
environmentVariableName: "GOOGLE_PRIVATE_KEY",
|
43
|
+
description: "Google private key"
|
44
|
+
}),
|
45
|
+
privateKeyId: (0, import_provider_utils.loadOptionalSetting)({
|
46
|
+
settingValue: void 0,
|
47
|
+
environmentVariableName: "GOOGLE_PRIVATE_KEY_ID"
|
48
|
+
})
|
49
|
+
};
|
50
|
+
} catch (error) {
|
51
|
+
throw new Error(`Failed to load Google credentials: ${error.message}`);
|
52
|
+
}
|
53
|
+
};
|
54
|
+
var base64url = (str) => {
|
55
|
+
return btoa(str).replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
|
56
|
+
};
|
57
|
+
var importPrivateKey = async (pemKey) => {
|
58
|
+
const pemHeader = "-----BEGIN PRIVATE KEY-----";
|
59
|
+
const pemFooter = "-----END PRIVATE KEY-----";
|
60
|
+
const pemContents = pemKey.replace(pemHeader, "").replace(pemFooter, "").replace(/\s/g, "");
|
61
|
+
const binaryString = atob(pemContents);
|
62
|
+
const binaryData = new Uint8Array(binaryString.length);
|
63
|
+
for (let i = 0; i < binaryString.length; i++) {
|
64
|
+
binaryData[i] = binaryString.charCodeAt(i);
|
65
|
+
}
|
66
|
+
return await crypto.subtle.importKey(
|
67
|
+
"pkcs8",
|
68
|
+
binaryData,
|
69
|
+
{ name: "RSASSA-PKCS1-v1_5", hash: "SHA-256" },
|
70
|
+
true,
|
71
|
+
["sign"]
|
72
|
+
);
|
73
|
+
};
|
74
|
+
var buildJwt = async (credentials) => {
|
75
|
+
const now = Math.floor(Date.now() / 1e3);
|
76
|
+
const header = {
|
77
|
+
alg: "RS256",
|
78
|
+
typ: "JWT"
|
79
|
+
};
|
80
|
+
if (credentials.privateKeyId) {
|
81
|
+
header.kid = credentials.privateKeyId;
|
82
|
+
}
|
83
|
+
const payload = {
|
84
|
+
iss: credentials.clientEmail,
|
85
|
+
scope: "https://www.googleapis.com/auth/cloud-platform",
|
86
|
+
aud: "https://oauth2.googleapis.com/token",
|
87
|
+
exp: now + 3600,
|
88
|
+
iat: now
|
89
|
+
};
|
90
|
+
const privateKey = await importPrivateKey(credentials.privateKey);
|
91
|
+
const signingInput = `${base64url(JSON.stringify(header))}.${base64url(
|
92
|
+
JSON.stringify(payload)
|
93
|
+
)}`;
|
94
|
+
const encoder = new TextEncoder();
|
95
|
+
const data = encoder.encode(signingInput);
|
96
|
+
const signature = await crypto.subtle.sign(
|
97
|
+
"RSASSA-PKCS1-v1_5",
|
98
|
+
privateKey,
|
99
|
+
data
|
100
|
+
);
|
101
|
+
const signatureBase64 = base64url(
|
102
|
+
String.fromCharCode(...new Uint8Array(signature))
|
103
|
+
);
|
104
|
+
return `${base64url(JSON.stringify(header))}.${base64url(
|
105
|
+
JSON.stringify(payload)
|
106
|
+
)}.${signatureBase64}`;
|
107
|
+
};
|
108
|
+
async function generateAuthToken(credentials) {
|
109
|
+
try {
|
110
|
+
const creds = credentials || await loadCredentials();
|
111
|
+
const jwt = await buildJwt(creds);
|
112
|
+
const response = await fetch("https://oauth2.googleapis.com/token", {
|
113
|
+
method: "POST",
|
114
|
+
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
115
|
+
body: new URLSearchParams({
|
116
|
+
grant_type: "urn:ietf:params:oauth:grant-type:jwt-bearer",
|
117
|
+
assertion: jwt
|
118
|
+
})
|
119
|
+
});
|
120
|
+
if (!response.ok) {
|
121
|
+
throw new Error(`Token request failed: ${response.statusText}`);
|
122
|
+
}
|
123
|
+
const data = await response.json();
|
124
|
+
return data.access_token;
|
125
|
+
} catch (error) {
|
126
|
+
throw error;
|
127
|
+
}
|
128
|
+
}
|
129
|
+
|
130
|
+
// src/anthropic/google-vertex-anthropic-provider.ts
|
131
|
+
var import_provider = require("@ai-sdk/provider");
|
132
|
+
var import_provider_utils2 = require("@ai-sdk/provider-utils");
|
133
|
+
var import_internal = require("@ai-sdk/anthropic/internal");
|
134
|
+
function createVertexAnthropic(options = {}) {
|
135
|
+
var _a;
|
136
|
+
const location = (0, import_provider_utils2.loadOptionalSetting)({
|
137
|
+
settingValue: options.location,
|
138
|
+
environmentVariableName: "GOOGLE_VERTEX_LOCATION"
|
139
|
+
});
|
140
|
+
const project = (0, import_provider_utils2.loadOptionalSetting)({
|
141
|
+
settingValue: options.project,
|
142
|
+
environmentVariableName: "GOOGLE_VERTEX_PROJECT"
|
143
|
+
});
|
144
|
+
const baseURL = (_a = (0, import_provider_utils2.withoutTrailingSlash)(options.baseURL)) != null ? _a : `https://${location}-aiplatform.googleapis.com/v1/projects/${project}/locations/${location}/publishers/anthropic/models`;
|
145
|
+
const createChatModel = (modelId, settings = {}) => {
|
146
|
+
var _a2;
|
147
|
+
return new import_internal.AnthropicMessagesLanguageModel(
|
148
|
+
modelId,
|
149
|
+
settings,
|
150
|
+
{
|
151
|
+
provider: "vertex.anthropic.messages",
|
152
|
+
baseURL,
|
153
|
+
headers: (_a2 = options.headers) != null ? _a2 : {},
|
154
|
+
fetch: options.fetch,
|
155
|
+
buildRequestUrl: (baseURL2, isStreaming) => `${baseURL2}/${modelId}:${isStreaming ? "streamRawPredict" : "rawPredict"}`,
|
156
|
+
transformRequestBody: (args) => {
|
157
|
+
const { model, ...rest } = args;
|
158
|
+
return {
|
159
|
+
...rest,
|
160
|
+
anthropic_version: "vertex-2023-10-16"
|
161
|
+
};
|
162
|
+
}
|
163
|
+
}
|
164
|
+
);
|
165
|
+
};
|
166
|
+
const provider = function(modelId, settings) {
|
167
|
+
if (new.target) {
|
168
|
+
throw new Error(
|
169
|
+
"The Anthropic model function cannot be called with the new keyword."
|
170
|
+
);
|
171
|
+
}
|
172
|
+
return createChatModel(modelId, settings);
|
173
|
+
};
|
174
|
+
provider.languageModel = createChatModel;
|
175
|
+
provider.chat = createChatModel;
|
176
|
+
provider.messages = createChatModel;
|
177
|
+
provider.textEmbeddingModel = (modelId) => {
|
178
|
+
throw new import_provider.NoSuchModelError({ modelId, modelType: "textEmbeddingModel" });
|
179
|
+
};
|
180
|
+
provider.tools = import_internal.anthropicTools;
|
181
|
+
return provider;
|
182
|
+
}
|
183
|
+
|
184
|
+
// src/anthropic/edge/google-vertex-anthropic-provider-edge.ts
|
185
|
+
function createVertexAnthropic2(options = {}) {
|
186
|
+
var _a;
|
187
|
+
return createVertexAnthropic({
|
188
|
+
...options,
|
189
|
+
headers: (_a = options.headers) != null ? _a : async () => ({
|
190
|
+
Authorization: `Bearer ${await generateAuthToken(
|
191
|
+
options.googleCredentials
|
192
|
+
)}`
|
193
|
+
})
|
194
|
+
});
|
195
|
+
}
|
196
|
+
var vertexAnthropic = createVertexAnthropic2();
|
197
|
+
// Annotate the CommonJS export names for ESM import in node:
|
198
|
+
0 && (module.exports = {
|
199
|
+
createVertexAnthropic,
|
200
|
+
vertexAnthropic
|
201
|
+
});
|
202
|
+
//# sourceMappingURL=index.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../../../src/anthropic/edge/index.ts","../../../src/edge/google-vertex-auth-edge.ts","../../../src/anthropic/google-vertex-anthropic-provider.ts","../../../src/anthropic/edge/google-vertex-anthropic-provider-edge.ts"],"sourcesContent":["export {\n createVertexAnthropic,\n vertexAnthropic,\n} from './google-vertex-anthropic-provider-edge';\nexport type {\n GoogleVertexAnthropicProviderSettings,\n GoogleVertexAnthropicProvider,\n} from './google-vertex-anthropic-provider-edge';\n","import { loadOptionalSetting, loadSetting } from '@ai-sdk/provider-utils';\n\nexport interface GoogleCredentials {\n /**\n * The client email for the Google Cloud service account. Defaults to the\n * value of the `GOOGLE_CLIENT_EMAIL` environment variable.\n */\n clientEmail: string;\n\n /**\n * The private key for the Google Cloud service account. Defaults to the\n * value of the `GOOGLE_PRIVATE_KEY` environment variable.\n */\n privateKey: string;\n\n /**\n * Optional. The private key ID for the Google Cloud service account. Defaults\n * to the value of the `GOOGLE_PRIVATE_KEY_ID` environment variable.\n */\n privateKeyId?: string;\n}\n\nconst loadCredentials = async (): Promise<GoogleCredentials> => {\n try {\n return {\n clientEmail: loadSetting({\n settingValue: undefined,\n settingName: 'clientEmail',\n environmentVariableName: 'GOOGLE_CLIENT_EMAIL',\n description: 'Google client email',\n }),\n privateKey: loadSetting({\n settingValue: undefined,\n settingName: 'privateKey',\n environmentVariableName: 'GOOGLE_PRIVATE_KEY',\n description: 'Google private key',\n }),\n privateKeyId: loadOptionalSetting({\n settingValue: undefined,\n environmentVariableName: 'GOOGLE_PRIVATE_KEY_ID',\n }),\n };\n } catch (error: any) {\n throw new Error(`Failed to load Google credentials: ${error.message}`);\n }\n};\n\n// Convert a string to base64url\nconst base64url = (str: string) => {\n return btoa(str).replace(/\\+/g, '-').replace(/\\//g, '_').replace(/=/g, '');\n};\nconst importPrivateKey = async (pemKey: string) => {\n const pemHeader = '-----BEGIN PRIVATE KEY-----';\n const pemFooter = '-----END PRIVATE KEY-----';\n\n // Remove header, footer, and any whitespace/newlines\n const pemContents = pemKey\n .replace(pemHeader, '')\n .replace(pemFooter, '')\n .replace(/\\s/g, '');\n\n // Decode base64 to binary\n const binaryString = atob(pemContents);\n\n // Convert binary string to Uint8Array\n const binaryData = new Uint8Array(binaryString.length);\n for (let i = 0; i < binaryString.length; i++) {\n binaryData[i] = binaryString.charCodeAt(i);\n }\n\n return await crypto.subtle.importKey(\n 'pkcs8',\n binaryData,\n { name: 'RSASSA-PKCS1-v1_5', hash: 'SHA-256' },\n true,\n ['sign'],\n );\n};\n\nconst buildJwt = async (credentials: GoogleCredentials) => {\n const now = Math.floor(Date.now() / 1000);\n\n // Only include kid in header if privateKeyId is provided\n const header: { alg: string; typ: string; kid?: string } = {\n alg: 'RS256',\n typ: 'JWT',\n };\n\n if (credentials.privateKeyId) {\n header.kid = credentials.privateKeyId;\n }\n\n const payload = {\n iss: credentials.clientEmail,\n scope: 'https://www.googleapis.com/auth/cloud-platform',\n aud: 'https://oauth2.googleapis.com/token',\n exp: now + 3600,\n iat: now,\n };\n\n const privateKey = await importPrivateKey(credentials.privateKey);\n\n const signingInput = `${base64url(JSON.stringify(header))}.${base64url(\n JSON.stringify(payload),\n )}`;\n const encoder = new TextEncoder();\n const data = encoder.encode(signingInput);\n\n const signature = await crypto.subtle.sign(\n 'RSASSA-PKCS1-v1_5',\n privateKey,\n data,\n );\n\n const signatureBase64 = base64url(\n String.fromCharCode(...new Uint8Array(signature)),\n );\n\n return `${base64url(JSON.stringify(header))}.${base64url(\n JSON.stringify(payload),\n )}.${signatureBase64}`;\n};\n\n/**\n * Generate an authentication token for Google Vertex AI in a manner compatible\n * with the Edge runtime.\n */\nexport async function generateAuthToken(credentials?: GoogleCredentials) {\n try {\n const creds = credentials || (await loadCredentials());\n const jwt = await buildJwt(creds);\n\n const response = await fetch('https://oauth2.googleapis.com/token', {\n method: 'POST',\n headers: { 'Content-Type': 'application/x-www-form-urlencoded' },\n body: new URLSearchParams({\n grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',\n assertion: jwt,\n }),\n });\n\n if (!response.ok) {\n throw new Error(`Token request failed: ${response.statusText}`);\n }\n\n const data = await response.json();\n return data.access_token;\n } catch (error) {\n throw error;\n }\n}\n","import {\n LanguageModelV1,\n NoSuchModelError,\n ProviderV1,\n} from '@ai-sdk/provider';\nimport {\n FetchFunction,\n Resolvable,\n loadOptionalSetting,\n withoutTrailingSlash,\n} from '@ai-sdk/provider-utils';\nimport {\n anthropicTools,\n AnthropicMessagesLanguageModel,\n AnthropicMessagesModelId,\n AnthropicMessagesSettings,\n} from '@ai-sdk/anthropic/internal';\nimport {\n GoogleVertexAnthropicMessagesModelId,\n GoogleVertexAnthropicMessagesSettings,\n} from './google-vertex-anthropic-messages-settings';\nexport interface GoogleVertexAnthropicProvider extends ProviderV1 {\n /**\nCreates a model for text generation.\n*/\n (\n modelId: GoogleVertexAnthropicMessagesModelId,\n settings?: AnthropicMessagesSettings,\n ): LanguageModelV1;\n\n /**\nCreates a model for text generation.\n*/\n languageModel(\n modelId: GoogleVertexAnthropicMessagesModelId,\n settings?: AnthropicMessagesSettings,\n ): LanguageModelV1;\n\n /**\nAnthropic-specific computer use tool.\n */\n tools: typeof anthropicTools;\n}\n\nexport interface GoogleVertexAnthropicProviderSettings {\n /**\n * Google Cloud project ID. Defaults to the value of the `GOOGLE_VERTEX_PROJECT` environment variable.\n */\n project?: string;\n\n /**\n * Google Cloud region. Defaults to the value of the `GOOGLE_VERTEX_LOCATION` environment variable.\n */\n location?: string;\n\n /**\nUse a different URL prefix for API calls, e.g. to use proxy servers.\nThe default prefix is `https://api.anthropic.com/v1`.\n */\n baseURL?: string;\n\n /**\nCustom headers to include in the requests.\n */\n headers?: Resolvable<Record<string, string | undefined>>;\n\n /**\nCustom fetch implementation. You can use it as a middleware to intercept requests,\nor to provide a custom fetch implementation for e.g. testing.\n */\n fetch?: FetchFunction;\n}\n\n/**\nCreate a Google Vertex Anthropic provider instance.\n */\nexport function createVertexAnthropic(\n options: GoogleVertexAnthropicProviderSettings = {},\n): GoogleVertexAnthropicProvider {\n const location = loadOptionalSetting({\n settingValue: options.location,\n environmentVariableName: 'GOOGLE_VERTEX_LOCATION',\n });\n const project = loadOptionalSetting({\n settingValue: options.project,\n environmentVariableName: 'GOOGLE_VERTEX_PROJECT',\n });\n const baseURL =\n withoutTrailingSlash(options.baseURL) ??\n `https://${location}-aiplatform.googleapis.com/v1/projects/${project}/locations/${location}/publishers/anthropic/models`;\n\n const createChatModel = (\n modelId: GoogleVertexAnthropicMessagesModelId,\n settings: GoogleVertexAnthropicMessagesSettings = {},\n ) =>\n new AnthropicMessagesLanguageModel(\n modelId as AnthropicMessagesModelId,\n settings,\n {\n provider: 'vertex.anthropic.messages',\n baseURL,\n headers: options.headers ?? {},\n fetch: options.fetch,\n buildRequestUrl: (baseURL, isStreaming) =>\n `${baseURL}/${modelId}:${\n isStreaming ? 'streamRawPredict' : 'rawPredict'\n }`,\n transformRequestBody: args => {\n // Remove model from args and add anthropic version\n const { model, ...rest } = args;\n return {\n ...rest,\n anthropic_version: 'vertex-2023-10-16',\n };\n },\n },\n );\n\n const provider = function (\n modelId: GoogleVertexAnthropicMessagesModelId,\n settings?: GoogleVertexAnthropicMessagesSettings,\n ) {\n if (new.target) {\n throw new Error(\n 'The Anthropic model function cannot be called with the new keyword.',\n );\n }\n\n return createChatModel(modelId, settings);\n };\n\n provider.languageModel = createChatModel;\n provider.chat = createChatModel;\n provider.messages = createChatModel;\n provider.textEmbeddingModel = (modelId: string) => {\n throw new NoSuchModelError({ modelId, modelType: 'textEmbeddingModel' });\n };\n\n provider.tools = anthropicTools;\n\n return provider as GoogleVertexAnthropicProvider;\n}\n","import {\n generateAuthToken,\n GoogleCredentials,\n} from '../../edge/google-vertex-auth-edge';\nimport {\n createVertexAnthropic as createVertexAnthropicOriginal,\n GoogleVertexAnthropicProvider,\n GoogleVertexAnthropicProviderSettings as GoogleVertexAnthropicProviderSettingsOriginal,\n} from '../google-vertex-anthropic-provider';\n\nexport type { GoogleVertexAnthropicProvider };\n\nexport interface GoogleVertexAnthropicProviderSettings\n extends GoogleVertexAnthropicProviderSettingsOriginal {\n /**\n * Optional. The Google credentials for the Google Cloud service account. If\n * not provided, the Google Vertex provider will use environment variables to\n * load the credentials.\n */\n googleCredentials?: GoogleCredentials;\n}\n\nexport function createVertexAnthropic(\n options: GoogleVertexAnthropicProviderSettings = {},\n): GoogleVertexAnthropicProvider {\n return createVertexAnthropicOriginal({\n ...options,\n headers:\n options.headers ??\n (async () => ({\n Authorization: `Bearer ${await generateAuthToken(\n options.googleCredentials,\n )}`,\n })),\n });\n}\n\n/**\n * Default Google Vertex AI Anthropic provider instance.\n */\nexport const vertexAnthropic = createVertexAnthropic();\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA,+BAAAA;AAAA,EAAA;AAAA;AAAA;;;ACAA,4BAAiD;AAsBjD,IAAM,kBAAkB,YAAwC;AAC9D,MAAI;AACF,WAAO;AAAA,MACL,iBAAa,mCAAY;AAAA,QACvB,cAAc;AAAA,QACd,aAAa;AAAA,QACb,yBAAyB;AAAA,QACzB,aAAa;AAAA,MACf,CAAC;AAAA,MACD,gBAAY,mCAAY;AAAA,QACtB,cAAc;AAAA,QACd,aAAa;AAAA,QACb,yBAAyB;AAAA,QACzB,aAAa;AAAA,MACf,CAAC;AAAA,MACD,kBAAc,2CAAoB;AAAA,QAChC,cAAc;AAAA,QACd,yBAAyB;AAAA,MAC3B,CAAC;AAAA,IACH;AAAA,EACF,SAAS,OAAY;AACnB,UAAM,IAAI,MAAM,sCAAsC,MAAM,OAAO,EAAE;AAAA,EACvE;AACF;AAGA,IAAM,YAAY,CAAC,QAAgB;AACjC,SAAO,KAAK,GAAG,EAAE,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A,UAASA,MAAA,QAAQ,YAAR,OAAAA,MAAmB,CAAC;AAAA,QAC7B,OAAO,QAAQ;AAAA,QACf,iBAAiB,CAACC,UAAS,gBACzB,GAAGA,QAAO,IAAI,OAAO,IACnB,cAAc,qBAAqB,YACrC;AAAA,QACF,sBAAsB,UAAQ;AAE5B,gBAAM,EAAE,OAAO,GAAG,KAAK,IAAI;AAC3B,iBAAO;AAAA,YACL,GAAG;AAAA,YACH,mBAAmB;AAAA,UACrB;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA;AAEF,QAAM,WAAW,SACf,SACA,UACA;AACA,QAAI,YAAY;AACd,YAAM,IAAI;AAAA,QACR;AAAA,MACF;AAAA,IACF;AAEA,WAAO,gBAAgB,SAAS,QAAQ;AAAA,EAC1C;AAEA,WAAS,gBAAgB;AACzB,WAAS,OAAO;AAChB,WAAS,WAAW;AACpB,WAAS,qBAAqB,CAAC,YAAoB;AACjD,UAAM,IAAI,iCAAiB,EAAE,SAAS,WAAW,qBAAqB,CAAC;AAAA,EACzE;AAEA,WAAS,QAAQ;AAEjB,SAAO;AACT;;;ACvHO,SAASC,uBACd,UAAiD,CAAC,GACnB;AAxBjC;AAyBE,SAAO,sBAA8B;AAAA,IACnC,GAAG;AAAA,IACH,UACE,aAAQ,YAAR,YACC,aAAa;AAAA,MACZ,eAAe,UAAU,MAAM;AAAA,QAC7B,QAAQ;AAAA,MACV,CAAC;AAAA,IACH;AAAA,EACJ,CAAC;AACH;AAKO,IAAM,kBAAkBA,uBAAsB;","names":["createVertexAnthropic","import_provider_utils","_a","baseURL","createVertexAnthropic"]}
|
@@ -0,0 +1,182 @@
|
|
1
|
+
// src/edge/google-vertex-auth-edge.ts
|
2
|
+
import { loadOptionalSetting, loadSetting } from "@ai-sdk/provider-utils";
|
3
|
+
var loadCredentials = async () => {
|
4
|
+
try {
|
5
|
+
return {
|
6
|
+
clientEmail: loadSetting({
|
7
|
+
settingValue: void 0,
|
8
|
+
settingName: "clientEmail",
|
9
|
+
environmentVariableName: "GOOGLE_CLIENT_EMAIL",
|
10
|
+
description: "Google client email"
|
11
|
+
}),
|
12
|
+
privateKey: loadSetting({
|
13
|
+
settingValue: void 0,
|
14
|
+
settingName: "privateKey",
|
15
|
+
environmentVariableName: "GOOGLE_PRIVATE_KEY",
|
16
|
+
description: "Google private key"
|
17
|
+
}),
|
18
|
+
privateKeyId: loadOptionalSetting({
|
19
|
+
settingValue: void 0,
|
20
|
+
environmentVariableName: "GOOGLE_PRIVATE_KEY_ID"
|
21
|
+
})
|
22
|
+
};
|
23
|
+
} catch (error) {
|
24
|
+
throw new Error(`Failed to load Google credentials: ${error.message}`);
|
25
|
+
}
|
26
|
+
};
|
27
|
+
var base64url = (str) => {
|
28
|
+
return btoa(str).replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
|
29
|
+
};
|
30
|
+
var importPrivateKey = async (pemKey) => {
|
31
|
+
const pemHeader = "-----BEGIN PRIVATE KEY-----";
|
32
|
+
const pemFooter = "-----END PRIVATE KEY-----";
|
33
|
+
const pemContents = pemKey.replace(pemHeader, "").replace(pemFooter, "").replace(/\s/g, "");
|
34
|
+
const binaryString = atob(pemContents);
|
35
|
+
const binaryData = new Uint8Array(binaryString.length);
|
36
|
+
for (let i = 0; i < binaryString.length; i++) {
|
37
|
+
binaryData[i] = binaryString.charCodeAt(i);
|
38
|
+
}
|
39
|
+
return await crypto.subtle.importKey(
|
40
|
+
"pkcs8",
|
41
|
+
binaryData,
|
42
|
+
{ name: "RSASSA-PKCS1-v1_5", hash: "SHA-256" },
|
43
|
+
true,
|
44
|
+
["sign"]
|
45
|
+
);
|
46
|
+
};
|
47
|
+
var buildJwt = async (credentials) => {
|
48
|
+
const now = Math.floor(Date.now() / 1e3);
|
49
|
+
const header = {
|
50
|
+
alg: "RS256",
|
51
|
+
typ: "JWT"
|
52
|
+
};
|
53
|
+
if (credentials.privateKeyId) {
|
54
|
+
header.kid = credentials.privateKeyId;
|
55
|
+
}
|
56
|
+
const payload = {
|
57
|
+
iss: credentials.clientEmail,
|
58
|
+
scope: "https://www.googleapis.com/auth/cloud-platform",
|
59
|
+
aud: "https://oauth2.googleapis.com/token",
|
60
|
+
exp: now + 3600,
|
61
|
+
iat: now
|
62
|
+
};
|
63
|
+
const privateKey = await importPrivateKey(credentials.privateKey);
|
64
|
+
const signingInput = `${base64url(JSON.stringify(header))}.${base64url(
|
65
|
+
JSON.stringify(payload)
|
66
|
+
)}`;
|
67
|
+
const encoder = new TextEncoder();
|
68
|
+
const data = encoder.encode(signingInput);
|
69
|
+
const signature = await crypto.subtle.sign(
|
70
|
+
"RSASSA-PKCS1-v1_5",
|
71
|
+
privateKey,
|
72
|
+
data
|
73
|
+
);
|
74
|
+
const signatureBase64 = base64url(
|
75
|
+
String.fromCharCode(...new Uint8Array(signature))
|
76
|
+
);
|
77
|
+
return `${base64url(JSON.stringify(header))}.${base64url(
|
78
|
+
JSON.stringify(payload)
|
79
|
+
)}.${signatureBase64}`;
|
80
|
+
};
|
81
|
+
async function generateAuthToken(credentials) {
|
82
|
+
try {
|
83
|
+
const creds = credentials || await loadCredentials();
|
84
|
+
const jwt = await buildJwt(creds);
|
85
|
+
const response = await fetch("https://oauth2.googleapis.com/token", {
|
86
|
+
method: "POST",
|
87
|
+
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
88
|
+
body: new URLSearchParams({
|
89
|
+
grant_type: "urn:ietf:params:oauth:grant-type:jwt-bearer",
|
90
|
+
assertion: jwt
|
91
|
+
})
|
92
|
+
});
|
93
|
+
if (!response.ok) {
|
94
|
+
throw new Error(`Token request failed: ${response.statusText}`);
|
95
|
+
}
|
96
|
+
const data = await response.json();
|
97
|
+
return data.access_token;
|
98
|
+
} catch (error) {
|
99
|
+
throw error;
|
100
|
+
}
|
101
|
+
}
|
102
|
+
|
103
|
+
// src/anthropic/google-vertex-anthropic-provider.ts
|
104
|
+
import {
|
105
|
+
NoSuchModelError
|
106
|
+
} from "@ai-sdk/provider";
|
107
|
+
import {
|
108
|
+
loadOptionalSetting as loadOptionalSetting2,
|
109
|
+
withoutTrailingSlash
|
110
|
+
} from "@ai-sdk/provider-utils";
|
111
|
+
import {
|
112
|
+
anthropicTools,
|
113
|
+
AnthropicMessagesLanguageModel
|
114
|
+
} from "@ai-sdk/anthropic/internal";
|
115
|
+
function createVertexAnthropic(options = {}) {
|
116
|
+
var _a;
|
117
|
+
const location = loadOptionalSetting2({
|
118
|
+
settingValue: options.location,
|
119
|
+
environmentVariableName: "GOOGLE_VERTEX_LOCATION"
|
120
|
+
});
|
121
|
+
const project = loadOptionalSetting2({
|
122
|
+
settingValue: options.project,
|
123
|
+
environmentVariableName: "GOOGLE_VERTEX_PROJECT"
|
124
|
+
});
|
125
|
+
const baseURL = (_a = withoutTrailingSlash(options.baseURL)) != null ? _a : `https://${location}-aiplatform.googleapis.com/v1/projects/${project}/locations/${location}/publishers/anthropic/models`;
|
126
|
+
const createChatModel = (modelId, settings = {}) => {
|
127
|
+
var _a2;
|
128
|
+
return new AnthropicMessagesLanguageModel(
|
129
|
+
modelId,
|
130
|
+
settings,
|
131
|
+
{
|
132
|
+
provider: "vertex.anthropic.messages",
|
133
|
+
baseURL,
|
134
|
+
headers: (_a2 = options.headers) != null ? _a2 : {},
|
135
|
+
fetch: options.fetch,
|
136
|
+
buildRequestUrl: (baseURL2, isStreaming) => `${baseURL2}/${modelId}:${isStreaming ? "streamRawPredict" : "rawPredict"}`,
|
137
|
+
transformRequestBody: (args) => {
|
138
|
+
const { model, ...rest } = args;
|
139
|
+
return {
|
140
|
+
...rest,
|
141
|
+
anthropic_version: "vertex-2023-10-16"
|
142
|
+
};
|
143
|
+
}
|
144
|
+
}
|
145
|
+
);
|
146
|
+
};
|
147
|
+
const provider = function(modelId, settings) {
|
148
|
+
if (new.target) {
|
149
|
+
throw new Error(
|
150
|
+
"The Anthropic model function cannot be called with the new keyword."
|
151
|
+
);
|
152
|
+
}
|
153
|
+
return createChatModel(modelId, settings);
|
154
|
+
};
|
155
|
+
provider.languageModel = createChatModel;
|
156
|
+
provider.chat = createChatModel;
|
157
|
+
provider.messages = createChatModel;
|
158
|
+
provider.textEmbeddingModel = (modelId) => {
|
159
|
+
throw new NoSuchModelError({ modelId, modelType: "textEmbeddingModel" });
|
160
|
+
};
|
161
|
+
provider.tools = anthropicTools;
|
162
|
+
return provider;
|
163
|
+
}
|
164
|
+
|
165
|
+
// src/anthropic/edge/google-vertex-anthropic-provider-edge.ts
|
166
|
+
function createVertexAnthropic2(options = {}) {
|
167
|
+
var _a;
|
168
|
+
return createVertexAnthropic({
|
169
|
+
...options,
|
170
|
+
headers: (_a = options.headers) != null ? _a : async () => ({
|
171
|
+
Authorization: `Bearer ${await generateAuthToken(
|
172
|
+
options.googleCredentials
|
173
|
+
)}`
|
174
|
+
})
|
175
|
+
});
|
176
|
+
}
|
177
|
+
var vertexAnthropic = createVertexAnthropic2();
|
178
|
+
export {
|
179
|
+
createVertexAnthropic2 as createVertexAnthropic,
|
180
|
+
vertexAnthropic
|
181
|
+
};
|
182
|
+
//# sourceMappingURL=index.mjs.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../../../src/edge/google-vertex-auth-edge.ts","../../../src/anthropic/google-vertex-anthropic-provider.ts","../../../src/anthropic/edge/google-vertex-anthropic-provider-edge.ts"],"sourcesContent":["import { loadOptionalSetting, loadSetting } from '@ai-sdk/provider-utils';\n\nexport interface GoogleCredentials {\n /**\n * The client email for the Google Cloud service account. Defaults to the\n * value of the `GOOGLE_CLIENT_EMAIL` environment variable.\n */\n clientEmail: string;\n\n /**\n * The private key for the Google Cloud service account. Defaults to the\n * value of the `GOOGLE_PRIVATE_KEY` environment variable.\n */\n privateKey: string;\n\n /**\n * Optional. The private key ID for the Google Cloud service account. Defaults\n * to the value of the `GOOGLE_PRIVATE_KEY_ID` environment variable.\n */\n privateKeyId?: string;\n}\n\nconst loadCredentials = async (): Promise<GoogleCredentials> => {\n try {\n return {\n clientEmail: loadSetting({\n settingValue: undefined,\n settingName: 'clientEmail',\n environmentVariableName: 'GOOGLE_CLIENT_EMAIL',\n description: 'Google client email',\n }),\n privateKey: loadSetting({\n settingValue: undefined,\n settingName: 'privateKey',\n environmentVariableName: 'GOOGLE_PRIVATE_KEY',\n description: 'Google private key',\n }),\n privateKeyId: loadOptionalSetting({\n settingValue: undefined,\n environmentVariableName: 'GOOGLE_PRIVATE_KEY_ID',\n }),\n };\n } catch (error: any) {\n throw new Error(`Failed to load Google credentials: ${error.message}`);\n }\n};\n\n// Convert a string to base64url\nconst base64url = (str: string) => {\n return btoa(str).replace(/\\+/g, '-').replace(/\\//g, '_').replace(/=/g, '');\n};\nconst importPrivateKey = async (pemKey: string) => {\n const pemHeader = '-----BEGIN PRIVATE KEY-----';\n const pemFooter = '-----END PRIVATE KEY-----';\n\n // Remove header, footer, and any whitespace/newlines\n const pemContents = pemKey\n .replace(pemHeader, '')\n .replace(pemFooter, '')\n .replace(/\\s/g, '');\n\n // Decode base64 to binary\n const binaryString = atob(pemContents);\n\n // Convert binary string to Uint8Array\n const binaryData = new Uint8Array(binaryString.length);\n for (let i = 0; i < binaryString.length; i++) {\n binaryData[i] = binaryString.charCodeAt(i);\n }\n\n return await crypto.subtle.importKey(\n 'pkcs8',\n binaryData,\n { name: 'RSASSA-PKCS1-v1_5', hash: 'SHA-256' },\n true,\n ['sign'],\n );\n};\n\nconst buildJwt = async (credentials: GoogleCredentials) => {\n const now = Math.floor(Date.now() / 1000);\n\n // Only include kid in header if privateKeyId is provided\n const header: { alg: string; typ: string; kid?: string } = {\n alg: 'RS256',\n typ: 'JWT',\n };\n\n if (credentials.privateKeyId) {\n header.kid = credentials.privateKeyId;\n }\n\n const payload = {\n iss: credentials.clientEmail,\n scope: 'https://www.googleapis.com/auth/cloud-platform',\n aud: 'https://oauth2.googleapis.com/token',\n exp: now + 3600,\n iat: now,\n };\n\n const privateKey = await importPrivateKey(credentials.privateKey);\n\n const signingInput = `${base64url(JSON.stringify(header))}.${base64url(\n JSON.stringify(payload),\n )}`;\n const encoder = new TextEncoder();\n const data = encoder.encode(signingInput);\n\n const signature = await crypto.subtle.sign(\n 'RSASSA-PKCS1-v1_5',\n privateKey,\n data,\n );\n\n const signatureBase64 = base64url(\n String.fromCharCode(...new Uint8Array(signature)),\n );\n\n return `${base64url(JSON.stringify(header))}.${base64url(\n JSON.stringify(payload),\n )}.${signatureBase64}`;\n};\n\n/**\n * Generate an authentication token for Google Vertex AI in a manner compatible\n * with the Edge runtime.\n */\nexport async function generateAuthToken(credentials?: GoogleCredentials) {\n try {\n const creds = credentials || (await loadCredentials());\n const jwt = await buildJwt(creds);\n\n const response = await fetch('https://oauth2.googleapis.com/token', {\n method: 'POST',\n headers: { 'Content-Type': 'application/x-www-form-urlencoded' },\n body: new URLSearchParams({\n grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',\n assertion: jwt,\n }),\n });\n\n if (!response.ok) {\n throw new Error(`Token request failed: ${response.statusText}`);\n }\n\n const data = await response.json();\n return data.access_token;\n } catch (error) {\n throw error;\n }\n}\n","import {\n LanguageModelV1,\n NoSuchModelError,\n ProviderV1,\n} from '@ai-sdk/provider';\nimport {\n FetchFunction,\n Resolvable,\n loadOptionalSetting,\n withoutTrailingSlash,\n} from '@ai-sdk/provider-utils';\nimport {\n anthropicTools,\n AnthropicMessagesLanguageModel,\n AnthropicMessagesModelId,\n AnthropicMessagesSettings,\n} from '@ai-sdk/anthropic/internal';\nimport {\n GoogleVertexAnthropicMessagesModelId,\n GoogleVertexAnthropicMessagesSettings,\n} from './google-vertex-anthropic-messages-settings';\nexport interface GoogleVertexAnthropicProvider extends ProviderV1 {\n /**\nCreates a model for text generation.\n*/\n (\n modelId: GoogleVertexAnthropicMessagesModelId,\n settings?: AnthropicMessagesSettings,\n ): LanguageModelV1;\n\n /**\nCreates a model for text generation.\n*/\n languageModel(\n modelId: GoogleVertexAnthropicMessagesModelId,\n settings?: AnthropicMessagesSettings,\n ): LanguageModelV1;\n\n /**\nAnthropic-specific computer use tool.\n */\n tools: typeof anthropicTools;\n}\n\nexport interface GoogleVertexAnthropicProviderSettings {\n /**\n * Google Cloud project ID. Defaults to the value of the `GOOGLE_VERTEX_PROJECT` environment variable.\n */\n project?: string;\n\n /**\n * Google Cloud region. Defaults to the value of the `GOOGLE_VERTEX_LOCATION` environment variable.\n */\n location?: string;\n\n /**\nUse a different URL prefix for API calls, e.g. to use proxy servers.\nThe default prefix is `https://api.anthropic.com/v1`.\n */\n baseURL?: string;\n\n /**\nCustom headers to include in the requests.\n */\n headers?: Resolvable<Record<string, string | undefined>>;\n\n /**\nCustom fetch implementation. You can use it as a middleware to intercept requests,\nor to provide a custom fetch implementation for e.g. testing.\n */\n fetch?: FetchFunction;\n}\n\n/**\nCreate a Google Vertex Anthropic provider instance.\n */\nexport function createVertexAnthropic(\n options: GoogleVertexAnthropicProviderSettings = {},\n): GoogleVertexAnthropicProvider {\n const location = loadOptionalSetting({\n settingValue: options.location,\n environmentVariableName: 'GOOGLE_VERTEX_LOCATION',\n });\n const project = loadOptionalSetting({\n settingValue: options.project,\n environmentVariableName: 'GOOGLE_VERTEX_PROJECT',\n });\n const baseURL =\n withoutTrailingSlash(options.baseURL) ??\n `https://${location}-aiplatform.googleapis.com/v1/projects/${project}/locations/${location}/publishers/anthropic/models`;\n\n const createChatModel = (\n modelId: GoogleVertexAnthropicMessagesModelId,\n settings: GoogleVertexAnthropicMessagesSettings = {},\n ) =>\n new AnthropicMessagesLanguageModel(\n modelId as AnthropicMessagesModelId,\n settings,\n {\n provider: 'vertex.anthropic.messages',\n baseURL,\n headers: options.headers ?? {},\n fetch: options.fetch,\n buildRequestUrl: (baseURL, isStreaming) =>\n `${baseURL}/${modelId}:${\n isStreaming ? 'streamRawPredict' : 'rawPredict'\n }`,\n transformRequestBody: args => {\n // Remove model from args and add anthropic version\n const { model, ...rest } = args;\n return {\n ...rest,\n anthropic_version: 'vertex-2023-10-16',\n };\n },\n },\n );\n\n const provider = function (\n modelId: GoogleVertexAnthropicMessagesModelId,\n settings?: GoogleVertexAnthropicMessagesSettings,\n ) {\n if (new.target) {\n throw new Error(\n 'The Anthropic model function cannot be called with the new keyword.',\n );\n }\n\n return createChatModel(modelId, settings);\n };\n\n provider.languageModel = createChatModel;\n provider.chat = createChatModel;\n provider.messages = createChatModel;\n provider.textEmbeddingModel = (modelId: string) => {\n throw new NoSuchModelError({ modelId, modelType: 'textEmbeddingModel' });\n };\n\n provider.tools = anthropicTools;\n\n return provider as GoogleVertexAnthropicProvider;\n}\n","import {\n generateAuthToken,\n GoogleCredentials,\n} from '../../edge/google-vertex-auth-edge';\nimport {\n createVertexAnthropic as createVertexAnthropicOriginal,\n GoogleVertexAnthropicProvider,\n GoogleVertexAnthropicProviderSettings as GoogleVertexAnthropicProviderSettingsOriginal,\n} from '../google-vertex-anthropic-provider';\n\nexport type { GoogleVertexAnthropicProvider };\n\nexport interface GoogleVertexAnthropicProviderSettings\n extends GoogleVertexAnthropicProviderSettingsOriginal {\n /**\n * Optional. The Google credentials for the Google Cloud service account. If\n * not provided, the Google Vertex provider will use environment variables to\n * load the credentials.\n */\n googleCredentials?: GoogleCredentials;\n}\n\nexport function createVertexAnthropic(\n options: GoogleVertexAnthropicProviderSettings = {},\n): GoogleVertexAnthropicProvider {\n return createVertexAnthropicOriginal({\n ...options,\n headers:\n options.headers ??\n (async () => ({\n Authorization: `Bearer ${await generateAuthToken(\n options.googleCredentials,\n )}`,\n })),\n });\n}\n\n/**\n * Default Google Vertex AI Anthropic provider instance.\n */\nexport const vertexAnthropic = createVertexAnthropic();\n"],"mappings":";AAAA,SAAS,qBAAqB,mBAAmB;AAsBjD,IAAM,kBAAkB,YAAwC;AAC9D,MAAI;AACF,WAAO;AAAA,MACL,aAAa,YAAY;AAAA,QACvB,cAAc;AAAA,QACd,aAAa;AAAA,QACb,yBAAyB;AAAA,QACzB,aAAa;AAAA,MACf,CAAC;AAAA,MACD,YAAY,YAAY;AAAA,QACtB,cAAc;AAAA,QACd,aAAa;AAAA,QACb,y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AAC;AAAA,IACH;AAAA,EACJ,CAAC;AACH;AAKO,IAAM,kBAAkBA,uBAAsB;","names":["loadOptionalSetting","_a","baseURL","createVertexAnthropic"]}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@ai-sdk/google-vertex",
|
3
|
-
"version": "2.0.
|
3
|
+
"version": "2.0.1",
|
4
4
|
"license": "Apache-2.0",
|
5
5
|
"sideEffects": false,
|
6
6
|
"main": "./dist/index.js",
|
@@ -9,6 +9,8 @@
|
|
9
9
|
"files": [
|
10
10
|
"dist/**/*",
|
11
11
|
"edge/dist/**/*",
|
12
|
+
"anthropic/dist/**/*",
|
13
|
+
"anthropic/edge/dist/**/*",
|
12
14
|
"CHANGELOG.md"
|
13
15
|
],
|
14
16
|
"exports": {
|
@@ -22,9 +24,20 @@
|
|
22
24
|
"types": "./edge/dist/index.d.ts",
|
23
25
|
"import": "./edge/dist/index.mjs",
|
24
26
|
"require": "./edge/dist/index.js"
|
27
|
+
},
|
28
|
+
"./anthropic": {
|
29
|
+
"types": "./anthropic/dist/index.d.ts",
|
30
|
+
"import": "./anthropic/dist/index.mjs",
|
31
|
+
"require": "./anthropic/dist/index.js"
|
32
|
+
},
|
33
|
+
"./anthropic/edge": {
|
34
|
+
"types": "./anthropic/edge/dist/index.d.ts",
|
35
|
+
"import": "./anthropic/edge/dist/index.mjs",
|
36
|
+
"require": "./anthropic/edge/dist/index.js"
|
25
37
|
}
|
26
38
|
},
|
27
39
|
"dependencies": {
|
40
|
+
"@ai-sdk/anthropic": "1.0.4",
|
28
41
|
"@ai-sdk/google": "1.0.5",
|
29
42
|
"@ai-sdk/provider": "1.0.1",
|
30
43
|
"@ai-sdk/provider-utils": "2.0.3",
|
@@ -60,7 +73,7 @@
|
|
60
73
|
"scripts": {
|
61
74
|
"build": "tsup",
|
62
75
|
"build:watch": "tsup --watch",
|
63
|
-
"clean": "rm -rf dist && rm -rf edge/dist",
|
76
|
+
"clean": "rm -rf dist && rm -rf edge/dist && rm -rf anthropic/dist && rm -rf anthropic/edge/dist",
|
64
77
|
"lint": "eslint \"./**/*.ts*\"",
|
65
78
|
"type-check": "tsc --noEmit",
|
66
79
|
"prettier-check": "prettier --check \"./**/*.ts*\"",
|