Skip to content

Commit de578dd

Browse files
feat: add support for Vertex AI provider and models (tailcallhq#1553)
Co-authored-by: graphite-app[bot] <96075541+graphite-app[bot]@users.noreply.github.com>
1 parent 7d541ed commit de578dd

5 files changed

Lines changed: 290 additions & 21 deletions

File tree

‎README.md‎

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
- [Command-Line Options](#command-line-options)
2222
- [Advanced Configuration](#advanced-configuration)
2323
- [Provider Configuration](#provider-configuration)
24+
- [Setup Instructions](#setup-instructions)
25+
- [Available Models](#available-models)
2426
- [forge.yaml Configuration Options](#forgeyaml-configuration-options)
2527
- [Environment Variables](#environment-variables)
2628
- [MCP Configuration](#mcp-configuration)
@@ -274,15 +276,40 @@ model: claude-3.7-sonnet
274276
# .env
275277
PROJECT_ID=<your_project_id>
276278
LOCATION=<your_location>
277-
OPENAI_API_KEY=<vertex_ai_key>
278-
OPENAI_URL=https://${LOCATION}-aiplatform.googleapis.com/v1beta1/projects/${PROJECT_ID}/locations/${LOCATION}/endpoints/openapi
279+
VERTEX_AI_AUTH_TOKEN=<your_auth_token>
279280
```
280281

281282
```yaml
282283
# forge.yaml
283-
model: publishers/anthropic/models/claude-3-7-sonnet
284+
model: google/gemini-2.5-pro
284285
```
285286
287+
### Setup Instructions
288+
289+
1. **Install Google Cloud CLI** and authenticate:
290+
```bash
291+
gcloud auth login
292+
gcloud config set project YOUR_PROJECT_ID
293+
```
294+
295+
2. **Get your authentication token**:
296+
```bash
297+
gcloud auth print-access-token
298+
```
299+
Use this token as `VERTEX_AI_AUTH_TOKEN`.
300+
301+
3. **Find your project ID and location**:
302+
- Project ID: Available in Google Cloud Console or via `gcloud config get-value project`
303+
- Location: Your Google Cloud region (e.g., `us-central1`, `europe-west1`)
304+
305+
### Available Models
306+
307+
Forge loads Vertex AI models from a static configuration file, including:
308+
- Claude models: `claude-sonnet-4@20250514`
309+
- Gemini models: `gemini-2.5-pro`, `gemini-2.0-flash`
310+
311+
Use the `/model` command in Forge CLI to see all available models.
312+
286313
</details>
287314

288315
<details>

‎crates/forge_domain/src/provider.rs‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,21 @@ impl Provider {
119119
}
120120
}
121121

122+
pub fn vertex_ai(key: &str, project_id: &str, location: &str) -> anyhow::Result<Provider> {
123+
let url = if location == "global" {
124+
format!(
125+
"https://aiplatform.googleapis.com/v1/projects/{}/locations/{}/endpoints/openapi/",
126+
project_id, location
127+
)
128+
} else {
129+
format!(
130+
"https://{}-aiplatform.googleapis.com/v1/projects/{}/locations/{}/endpoints/openapi/",
131+
location, project_id, location
132+
)
133+
};
134+
Ok(Provider::OpenAI { url: Url::parse(&url)?, key: Some(key.into()) })
135+
}
136+
122137
pub fn key(&self) -> Option<&str> {
123138
match self {
124139
Provider::OpenAI { key, .. } => key.as_deref(),
@@ -222,6 +237,15 @@ impl Provider {
222237
Provider::Anthropic { url, .. } => url.as_str().starts_with(Self::ANTHROPIC_URL),
223238
}
224239
}
240+
241+
pub fn is_vertex_ai(&self) -> bool {
242+
match self {
243+
Provider::OpenAI { url, key: _ } => url
244+
.as_str()
245+
.contains("aiplatform.googleapis.com/v1/projects/"),
246+
Provider::Anthropic { .. } => false,
247+
}
248+
}
225249
}
226250

227251
#[cfg(test)]
@@ -393,4 +417,20 @@ mod tests {
393417
let model_url = fixture.model_url();
394418
assert_eq!(base_url.join("models").unwrap(), model_url);
395419
}
420+
421+
#[test]
422+
fn test_vertex_ai_global_location() {
423+
let fixture = Provider::vertex_ai("test_token", "forge-452914", "global").unwrap();
424+
let actual = fixture.to_base_url();
425+
let expected = Url::parse("https://aiplatform.googleapis.com/v1/projects/forge-452914/locations/global/endpoints/openapi/").unwrap();
426+
assert_eq!(actual, expected);
427+
}
428+
429+
#[test]
430+
fn test_vertex_ai_regular_location() {
431+
let fixture = Provider::vertex_ai("test_token", "test_project", "us-central1").unwrap();
432+
let actual = fixture.to_base_url();
433+
let expected = Url::parse("https://us-central1-aiplatform.googleapis.com/v1/projects/test_project/locations/us-central1/endpoints/openapi/").unwrap();
434+
assert_eq!(actual, expected);
435+
}
396436
}

‎crates/forge_services/src/provider/openai.rs‎

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use forge_app::domain::{
66
ChatCompletionMessage, Context as ChatContext, ModelId, Provider, ResultStream, Transformer,
77
};
88
use forge_app::dto::openai::{ListModelResponse, ProviderPipeline, Request, Response};
9+
use lazy_static::lazy_static;
910
use reqwest::header::AUTHORIZATION;
1011
use tracing::{debug, info};
1112

@@ -91,18 +92,24 @@ impl<H: HttpClientService> OpenAIProvider<H> {
9192
}
9293

9394
async fn inner_models(&self) -> Result<Vec<forge_app::domain::Model>> {
94-
let url = self.provider.model_url();
95-
debug!(url = %url, "Fetching models");
96-
match self.fetch_models(url.as_str()).await {
97-
Err(error) => {
98-
tracing::error!(error = ?error, "Failed to fetch models");
99-
anyhow::bail!(error)
100-
}
101-
Ok(response) => {
102-
let data: ListModelResponse = serde_json::from_str(&response)
103-
.with_context(|| format_http_context(None, "GET", &url))
104-
.with_context(|| "Failed to deserialize models response")?;
105-
Ok(data.data.into_iter().map(Into::into).collect())
95+
// For Vertex AI, load models from static JSON file using VertexProvider logic
96+
if self.provider.is_vertex_ai() {
97+
debug!("Loading Vertex AI models from static JSON file");
98+
Ok(self.inner_vertex_models())
99+
} else {
100+
let url = self.provider.model_url();
101+
debug!(url = %url, "Fetching models");
102+
match self.fetch_models(url.as_str()).await {
103+
Err(error) => {
104+
tracing::error!(error = ?error, "Failed to fetch models");
105+
anyhow::bail!(error)
106+
}
107+
Ok(response) => {
108+
let data: ListModelResponse = serde_json::from_str(&response)
109+
.with_context(|| format_http_context(None, "GET", &url))
110+
.with_context(|| "Failed to deserialize models response")?;
111+
Ok(data.data.into_iter().map(Into::into).collect())
112+
}
106113
}
107114
}
108115
}
@@ -136,6 +143,18 @@ impl<H: HttpClientService> OpenAIProvider<H> {
136143
.with_context(|| "Failed to fetch the models")
137144
}
138145
}
146+
147+
/// Load Vertex AI models from static JSON file
148+
fn inner_vertex_models(&self) -> Vec<forge_app::domain::Model> {
149+
lazy_static! {
150+
static ref VERTEX_MODELS: Vec<forge_app::domain::Model> = {
151+
let models =
152+
include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../vertex.json"));
153+
serde_json::from_str(models).unwrap()
154+
};
155+
}
156+
VERTEX_MODELS.clone()
157+
}
139158
}
140159

141160
impl<T: HttpClientService> OpenAIProvider<T> {

‎crates/forge_services/src/provider/registry.rs‎

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,25 @@ impl<F: EnvironmentInfra> ProviderRegistry for ForgeProviderRegistry<F> {
5151

5252
let provider = self
5353
.get_provider(config)
54-
.context("No valid provider configuration found. Please set one of the following environment variables: OPENROUTER_API_KEY, REQUESTY_API_KEY, XAI_API_KEY, OPENAI_API_KEY, or ANTHROPIC_API_KEY. For more details, visit: https://forgecode.dev/docs/custom-providers/")?;
54+
.context("No valid provider configuration found. Please set one of the following environment variables: OPENROUTER_API_KEY, REQUESTY_API_KEY, XAI_API_KEY, OPENAI_API_KEY, ANTHROPIC_API_KEY, or VERTEX_AI_AUTH_TOKEN (with PROJECT_ID and LOCATION). For more details, visit: https://forgecode.dev/docs/custom-providers/")?;
5555
self.cache.write().await.replace(provider.clone());
5656
Ok(provider)
5757
}
5858
}
5959

60+
fn resolve_vertex_env_provider<F: EnvironmentInfra>(
61+
key: &str,
62+
env: &F,
63+
) -> anyhow::Result<Provider> {
64+
let project_id = env.get_env_var("PROJECT_ID").ok_or(anyhow::anyhow!(
65+
"PROJECT_ID is missing. Please set the PROJECT_ID environment variable."
66+
))?;
67+
let location = env.get_env_var("LOCATION").ok_or(anyhow::anyhow!(
68+
"LOCATION is missing. Please set the LOCATION environment variable."
69+
))?;
70+
Provider::vertex_ai(key, &project_id, &location)
71+
}
72+
6073
fn resolve_env_provider<F: EnvironmentInfra>(
6174
url: Option<ProviderUrl>,
6275
env: &F,
@@ -73,12 +86,18 @@ fn resolve_env_provider<F: EnvironmentInfra>(
7386
("ZAI_CODING_API_KEY", Box::new(Provider::zai_coding)),
7487
];
7588

76-
keys.into_iter().find_map(|(key, fun)| {
77-
env.get_env_var(key).map(|key| {
78-
let provider = fun(&key);
79-
override_url(provider, url.clone())
89+
keys.into_iter()
90+
.find_map(|(key, fun)| {
91+
env.get_env_var(key).map(|key| {
92+
let provider = fun(&key);
93+
override_url(provider, url.clone())
94+
})
95+
})
96+
.or_else(|| {
97+
// Check for Vertex AI last since it requires multiple environment variables
98+
env.get_env_var("VERTEX_AI_AUTH_TOKEN")
99+
.and_then(|key| resolve_vertex_env_provider(&key, env).ok())
80100
})
81-
})
82101
}
83102

84103
fn override_url(mut provider: Provider, url: Option<ProviderUrl>) -> Provider {

‎vertex.json‎

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
[
2+
{
3+
"id": "anthropic/claude-sonnet-4@20250514",
4+
"name": "Claude Sonnet 4",
5+
"description": "Anthropic's most powerful Sonnet model, optimized for complex tasks",
6+
"context_length": 200000,
7+
"tools_supported": true,
8+
"supports_parallel_tool_calls": true,
9+
"supports_reasoning": true,
10+
"anthropic_version": "vertex-2023-10-16"
11+
},
12+
{
13+
"id": "anthropic/claude-opus-4-1@20250805",
14+
"name": "Claude Opus 4.1",
15+
"description": "Anthropic's most intelligent model with enhanced capabilities",
16+
"context_length": 200000,
17+
"tools_supported": true,
18+
"supports_parallel_tool_calls": true,
19+
"supports_reasoning": true,
20+
"anthropic_version": "vertex-2023-10-16"
21+
},
22+
{
23+
"id": "anthropic/claude-opus-4@20250514",
24+
"name": "Claude Opus 4",
25+
"description": "Anthropic's most intelligent model for highly complex tasks",
26+
"context_length": 200000,
27+
"tools_supported": true,
28+
"supports_parallel_tool_calls": true,
29+
"supports_reasoning": true,
30+
"anthropic_version": "vertex-2023-10-16"
31+
},
32+
{
33+
"id": "anthropic/claude-3-7-sonnet@20250219",
34+
"name": "Claude 3.7 Sonnet",
35+
"description": "Anthropic's balanced model with thinking capabilities",
36+
"context_length": 200000,
37+
"tools_supported": true,
38+
"supports_parallel_tool_calls": true,
39+
"supports_reasoning": true,
40+
"anthropic_version": "vertex-2023-10-16"
41+
},
42+
{
43+
"id": "anthropic/claude-3-5-sonnet-v2@20241022",
44+
"name": "Claude 3.5 Sonnet v2",
45+
"description": "Anthropic's enhanced Sonnet model with improved performance",
46+
"context_length": 200000,
47+
"tools_supported": true,
48+
"supports_parallel_tool_calls": true,
49+
"supports_reasoning": true,
50+
"anthropic_version": "vertex-2023-10-16"
51+
},
52+
{
53+
"id": "anthropic/claude-3-5-sonnet@20240620",
54+
"name": "Claude 3.5 Sonnet",
55+
"description": "Anthropic's balanced model for most tasks",
56+
"context_length": 200000,
57+
"tools_supported": true,
58+
"supports_parallel_tool_calls": true,
59+
"supports_reasoning": true,
60+
"anthropic_version": "vertex-2023-10-16"
61+
},
62+
{
63+
"id": "anthropic/claude-3-5-haiku@20241022",
64+
"name": "Claude 3.5 Haiku",
65+
"description": "Anthropic's fast and efficient model for quick tasks",
66+
"context_length": 200000,
67+
"tools_supported": true,
68+
"supports_parallel_tool_calls": true,
69+
"supports_reasoning": true,
70+
"anthropic_version": "vertex-2023-10-16"
71+
},
72+
{
73+
"id": "anthropic/claude-3-opus@20240229",
74+
"name": "Claude 3 Opus",
75+
"description": "Anthropic's most intelligent model from the Claude 3 family",
76+
"context_length": 200000,
77+
"tools_supported": true,
78+
"supports_parallel_tool_calls": true,
79+
"supports_reasoning": true,
80+
"anthropic_version": "vertex-2023-10-16"
81+
},
82+
{
83+
"id": "anthropic/claude-3-haiku@20240307",
84+
"name": "Claude 3 Haiku",
85+
"description": "Anthropic's fastest and most compact model",
86+
"context_length": 200000,
87+
"tools_supported": true,
88+
"supports_parallel_tool_calls": true,
89+
"supports_reasoning": true,
90+
"anthropic_version": "vertex-2023-10-16"
91+
},
92+
{
93+
"id": "mistral/mistral-large-2411",
94+
"name": "Mistral Large 2411",
95+
"description": "Mistral's powerful large language model",
96+
"context_length": 128000,
97+
"tools_supported": true,
98+
"supports_parallel_tool_calls": true,
99+
"supports_reasoning": false
100+
},
101+
{
102+
"id": "mistral/mistral-small-2503",
103+
"name": "Mistral Small 2503",
104+
"description": "Mistral's efficient small language model",
105+
"context_length": 128000,
106+
"tools_supported": true,
107+
"supports_parallel_tool_calls": true,
108+
"supports_reasoning": false
109+
},
110+
{
111+
"id": "mistral/codestral-2501",
112+
"name": "Codestral 2501",
113+
"description": "Mistral's code-specialized model",
114+
"context_length": 256000,
115+
"tools_supported": true,
116+
"supports_parallel_tool_calls": true,
117+
"supports_reasoning": false
118+
},
119+
{
120+
"id": "meta/llama-4-maverick-17b-128e-instruct-maas",
121+
"name": "Llama 4 Maverick 17B",
122+
"description": "Meta's large instruction-tuned model with extended context",
123+
"context_length": 1048576,
124+
"tools_supported": true,
125+
"supports_parallel_tool_calls": true,
126+
"supports_reasoning": false
127+
},
128+
{
129+
"id": "meta/llama-4-scout-17b-16e-instruct-maas",
130+
"name": "Llama 4 Scout 17B",
131+
"description": "Meta's efficient instruction-tuned model with massive context",
132+
"context_length": 10485760,
133+
"tools_supported": true,
134+
"supports_parallel_tool_calls": true,
135+
"supports_reasoning": false
136+
},
137+
{
138+
"id": "google/gemini-2.0-flash-001",
139+
"name": "Gemini 2.0 Flash 001",
140+
"description": "Google's fast and capable multimodal model",
141+
"context_length": 1048576,
142+
"tools_supported": true,
143+
"supports_parallel_tool_calls": true,
144+
"supports_reasoning": true
145+
},
146+
{
147+
"id": "google/gemini-2.5-pro",
148+
"name": "Gemini 2.5 Pro",
149+
"description": "Google's most capable multimodal model",
150+
"context_length": 1048576,
151+
"tools_supported": true,
152+
"supports_parallel_tool_calls": true,
153+
"supports_reasoning": true
154+
},
155+
{
156+
"id": "google/gemini-2.5-flash",
157+
"name": "Gemini 2.5 Flash",
158+
"description": "Google's fast and capable model with thinking support",
159+
"context_length": 1048576,
160+
"tools_supported": true,
161+
"supports_parallel_tool_calls": true,
162+
"supports_reasoning": true
163+
}
164+
]

0 commit comments

Comments
 (0)