Skip to content
This repository was archived by the owner on Jul 16, 2025. It is now read-only.

Commit d710694

Browse files
committed
feat: Vertex AI plain generateContent is now functioning
1 parent 732c8eb commit d710694

File tree

5 files changed

+84
-29
lines changed

5 files changed

+84
-29
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ Cargo.lock
1616
# Sensitive or local environment variables
1717
.env
1818

19-
# Added by cargo
19+
# Local test files
20+
request.json
2021

22+
# Added by cargo
2123
/target

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "google-generative-ai-rs"
3-
version = "0.2.1"
3+
version = "0.2.2"
44
edition = "2021"
55
authors = ["Mick Clarke <avastmick@outlook.com>"]
66
license = "MIT"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Please see [contributing](CONTRIBUTING.md) for the rules; they are standard thou
3030

3131
I do my best to release working code.
3232

33-
Status today is: *"Happy path for both public and Vertex AI endpoints work for Gemini, but some things are NOT working. See Issue #12"*
33+
Status today is: *"Happy path for both public and Vertex AI endpoints work for Gemini, but some things may be fragile."*
3434

3535
## Outline tasks
3636

examples/vertex_text_request.rs

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1+
use log::info;
12
use std::env;
2-
use std::io::{stdout, Write};
33

44
use google_generative_ai_rs::v1::{
55
api::Client,
6-
gemini::{request::Request, response::GeminiResponse, Content, Part, Role},
6+
gemini::{request::Request, Content, Part, ResponseType, Role},
77
};
88

9-
/// Simple text request using Vertex AI API endpoint and GCP application default credentials (ADC) authn
9+
/// Simple text request using the public API and an API key for authn
1010
///
1111
/// You'll need to install the GCP cli tools and set up your GCP project and region.
1212
///
@@ -15,19 +15,22 @@ use google_generative_ai_rs::v1::{
1515
/// gcloud init
1616
/// gcloud auth application-default login
1717
/// ```
18-
/// Note: right now there seems to be no non-streamed URL for the Vertex AI endpoint, so we're using the streamed URL only
1918
///
2019
/// To run:
2120
/// ```
22-
/// GCP_REGION_NAME=[THE REGION WHERE YOUR ENDPOINT IS HOSTED] GCP_PROJECT_ID=[YOUR GCP PROJECT_ID] RUST_LOG=info cargo run --package google-generative-ai-rs --example vertex_text_request
21+
/// GCP_REGION_NAME=[THE REGION WHERE YOUR ENDPOINT IS HOSTED] GCP_PROJECT_ID=[YOUR GCP PROJECT_ID] RUST_LOG=info cargo run --package google-generative-ai-rs --example vertex_text_request
2322
/// ``
2423
#[tokio::main]
2524
async fn main() -> Result<(), Box<dyn std::error::Error>> {
2625
env_logger::init();
2726
let region = env::var("GCP_REGION_NAME").unwrap().to_string();
2827
let project_id = env::var("GCP_PROJECT_ID").unwrap().to_string();
2928

30-
let client = Client::new_from_region_project_id(region.to_string(), project_id.to_string());
29+
let client = Client::new_from_region_project_id_response_type(
30+
region.to_string(),
31+
project_id.to_string(),
32+
ResponseType::GenerateContent,
33+
);
3134

3235
let txt_request = Request {
3336
contents: vec![Content {
@@ -46,26 +49,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
4649

4750
let response = client.post(30, &txt_request).await?;
4851

49-
println!("output streaming content");
50-
51-
if let Some(stream_response) = response.streamed() {
52-
if let Some(json_stream) = stream_response.response_stream {
53-
Client::for_each_async(json_stream, move |response: GeminiResponse| async move {
54-
let mut lock = stdout().lock();
55-
write!(
56-
lock,
57-
"{}",
58-
response.candidates[0].content.parts[0]
59-
.text
60-
.clone()
61-
.unwrap()
62-
.as_str()
63-
)
64-
.unwrap();
65-
})
66-
.await
67-
}
68-
}
52+
info!("{:#?}", response);
6953

7054
Ok(())
7155
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
use std::env;
2+
use std::io::{stdout, Write};
3+
4+
use google_generative_ai_rs::v1::{
5+
api::Client,
6+
gemini::{request::Request, response::GeminiResponse, Content, Part, Role},
7+
};
8+
9+
/// Streamed text request using Vertex AI API endpoint and GCP application default credentials (ADC) authn
10+
///
11+
/// You'll need to install the GCP cli tools and set up your GCP project and region.
12+
///
13+
/// The ensure you locally authenticated with GCP using the following commands:
14+
/// ```
15+
/// gcloud init
16+
/// gcloud auth application-default login
17+
/// ```
18+
/// To run:
19+
/// ```
20+
/// GCP_REGION_NAME=[THE REGION WHERE YOUR ENDPOINT IS HOSTED] GCP_PROJECT_ID=[YOUR GCP PROJECT_ID] RUST_LOG=info cargo run --package google-generative-ai-rs --example vertex_text_request
21+
/// ``
22+
#[tokio::main]
23+
async fn main() -> Result<(), Box<dyn std::error::Error>> {
24+
env_logger::init();
25+
let region = env::var("GCP_REGION_NAME").unwrap().to_string();
26+
let project_id = env::var("GCP_PROJECT_ID").unwrap().to_string();
27+
28+
let client = Client::new_from_region_project_id(region.to_string(), project_id.to_string());
29+
30+
let txt_request = Request {
31+
contents: vec![Content {
32+
role: Role::User,
33+
parts: vec![Part {
34+
text: Some("Give me a recipe for banana bread.".to_string()),
35+
inline_data: None,
36+
file_data: None,
37+
video_metadata: None,
38+
}],
39+
}],
40+
tools: vec![],
41+
safety_settings: vec![],
42+
generation_config: None,
43+
};
44+
45+
let response = client.post(30, &txt_request).await?;
46+
47+
println!("output streaming content");
48+
49+
if let Some(stream_response) = response.streamed() {
50+
if let Some(json_stream) = stream_response.response_stream {
51+
Client::for_each_async(json_stream, move |response: GeminiResponse| async move {
52+
let mut lock = stdout().lock();
53+
write!(
54+
lock,
55+
"{}",
56+
response.candidates[0].content.parts[0]
57+
.text
58+
.clone()
59+
.unwrap()
60+
.as_str()
61+
)
62+
.unwrap();
63+
})
64+
.await
65+
}
66+
}
67+
68+
Ok(())
69+
}

0 commit comments

Comments
 (0)