avastmick / google-generative-ai-rs

An unofficial rust-based client library to interact with the Google Gemini generative AI API
https://docs.rs/google-generative-ai-rs/
MIT License
21 stars 7 forks source link

'streamGenerateContent' generates a '403' error on Public Gemini API #12

Closed avastmick closed 7 months ago

avastmick commented 8 months ago

Describe the bug The public Gemini API supports streamed API responses. See: Stream Generate Content in API docs.

When using a 'streamGenerateContent' generates a '403' error on Public Gemini API.

Error: GoogleAPIError { message: "HTTP Error: 403: Forbidden", code: Some(403) }

To Reproduce In examples/test_request.rs

    // Either run as a standard text request or a stream generate content request
    // let client = Client::new(env::var("API_KEY").unwrap().to_string());

    // UNCOMMENT THIS SECTION
    let client = Client::new_from_model_reponse_type(
        google_generative_ai_rs::v1::gemini::Model::GeminiPro,
        env::var("API_KEY").unwrap().to_string(),
        google_generative_ai_rs::v1::gemini::ResponseType::StreamGenerateContent,
    );
API_KEY=[YOUR_API_KEY] RUST_LOG=info cargo run --package google-generative-ai-rs  --example text_request
Compiling google-generative-ai-rs v0.1.5 (/Users/[USER_NAME]/repos/google-generative-ai-rs)
    Finished dev [unoptimized + debuginfo] target(s) in 0.91s
     Running `target/debug/examples/text_request`
Error: GoogleAPIError { message: "HTTP Error: 403: Forbidden", code: Some(403) }

Expected behavior It should support 'streamGenerateContent' as expected, as per the Vertex AI endpoint and as per cURL

Version (please complete the following information):

Additional context Using cURL works fine for the Public API endpoint.

curl https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:streamGenerateContent?key=${API_KEY} \
        -H 'Content-Type: application/json' \
        --no-buffer \
        -d '{ "contents":[{"parts":[{"text": "Write long a story about a magic backpack."}]}]}' 
hitsmaxft commented 7 months ago

invoking google auth manager is not necessary if key exists.

avastmick commented 7 months ago

@hitsmaxft You are correct sir! Thank you - I wasn't thinking. I do not use the public API, only the Vertex endpoint, so I didn't think of it! 🤪