Compare commits
5 Commits
f7cc9977aa
...
feat/retry
| Author | SHA1 | Date | |
|---|---|---|---|
| a49d37f03d | |||
| cecc14d31e | |||
| 4791125943 | |||
| 6d49f9939c | |||
| c911fc382e |
18
.github/workflows/rust.yml
vendored
18
.github/workflows/rust.yml
vendored
@@ -10,13 +10,19 @@ env:
|
||||
CARGO_TERM_COLOR: always
|
||||
|
||||
jobs:
|
||||
build:
|
||||
|
||||
check:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Build
|
||||
run: cargo build --verbose
|
||||
- name: Run tests
|
||||
run: cargo test --verbose
|
||||
# The Gitea runner image doesn't ship with Rust, unlike GitHub's.
|
||||
- uses: dtolnay/rust-toolchain@stable
|
||||
with:
|
||||
components: clippy, rustfmt
|
||||
- uses: Swatinem/rust-cache@v2
|
||||
- name: Format
|
||||
run: cargo fmt --check
|
||||
- name: Clippy
|
||||
run: cargo clippy --all-targets -- -D warnings
|
||||
- name: Test
|
||||
run: cargo test
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "gemini-rs"
|
||||
version = "0.1.0"
|
||||
version = "0.2.0"
|
||||
edition = "2024"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use console::style;
|
||||
use dialoguer::{theme::ColorfulTheme, Input};
|
||||
use dialoguer::{Input, theme::ColorfulTheme};
|
||||
use gemini_rs::prelude::*;
|
||||
use std::time::Duration;
|
||||
|
||||
|
||||
@@ -19,10 +19,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
.add_text_part("Answer as if you were Yoda")
|
||||
.build();
|
||||
|
||||
let user_prompt = vec![Content::builder()
|
||||
.role(Role::User)
|
||||
.add_text_part("What is the airspeed of an unladen swallow?")
|
||||
.build()];
|
||||
let user_prompt = vec![
|
||||
Content::builder()
|
||||
.role(Role::User)
|
||||
.add_text_part("What is the airspeed of an unladen swallow?")
|
||||
.build(),
|
||||
];
|
||||
|
||||
let request = GenerateContentRequest::builder()
|
||||
.contents(user_prompt)
|
||||
|
||||
@@ -15,10 +15,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
location_id,
|
||||
);
|
||||
|
||||
let prompt = vec![Content::builder()
|
||||
.role(Role::User)
|
||||
.add_text_part("Tell me the story of the genesis of the universe as a bedtime story.")
|
||||
.build()];
|
||||
let prompt = vec![
|
||||
Content::builder()
|
||||
.role(Role::User)
|
||||
.add_text_part("Tell me the story of the genesis of the universe as a bedtime story.")
|
||||
.build(),
|
||||
];
|
||||
|
||||
let request = GenerateContentRequest::builder().contents(prompt).build();
|
||||
|
||||
|
||||
@@ -14,10 +14,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
location_id,
|
||||
);
|
||||
|
||||
let prompt = vec![Content::builder()
|
||||
.role(Role::User)
|
||||
.add_text_part("What is the airspeed of an unladen swallow?")
|
||||
.build()];
|
||||
let prompt = vec![
|
||||
Content::builder()
|
||||
.role(Role::User)
|
||||
.add_text_part("What is the airspeed of an unladen swallow?")
|
||||
.build(),
|
||||
];
|
||||
|
||||
let request = GenerateContentRequest::builder().contents(prompt).build();
|
||||
let response = gemini.generate_content(&request, "gemini-pro").await?;
|
||||
|
||||
29
src/error.rs
29
src/error.rs
@@ -116,16 +116,18 @@ impl Error {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn vertex_error(code: i32, status: &str, details: Option<Vec<serde_json::Value>>) -> Error {
|
||||
Error::VertexError(types::VertexApiError {
|
||||
code,
|
||||
message: "test".to_string(),
|
||||
status: status.to_string(),
|
||||
details,
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn vertex_error_retryable_classification() {
|
||||
let make_err = |code: i32, status: &str| {
|
||||
Error::VertexError(types::VertexApiError {
|
||||
code,
|
||||
message: "test".to_string(),
|
||||
status: status.to_string(),
|
||||
details: None,
|
||||
})
|
||||
};
|
||||
let make_err = |code: i32, status: &str| vertex_error(code, status, None);
|
||||
|
||||
// Retryable codes
|
||||
assert!(make_err(429, "TOO_MANY_REQUESTS").is_retryable());
|
||||
@@ -167,15 +169,14 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn retry_delay_delegates_to_vertex_error() {
|
||||
let err = Error::VertexError(types::VertexApiError {
|
||||
code: 429,
|
||||
message: "Quota exceeded".to_string(),
|
||||
status: "RESOURCE_EXHAUSTED".to_string(),
|
||||
details: Some(vec![serde_json::json!({
|
||||
let err = vertex_error(
|
||||
429,
|
||||
"RESOURCE_EXHAUSTED",
|
||||
Some(vec![serde_json::json!({
|
||||
"@type": "type.googleapis.com/google.rpc.RetryInfo",
|
||||
"retryDelay": "15s"
|
||||
})]),
|
||||
});
|
||||
);
|
||||
assert_eq!(err.retry_delay(), Some(Duration::from_secs(15)));
|
||||
|
||||
assert_eq!(Error::NoCandidatesError.retry_delay(), None);
|
||||
|
||||
@@ -92,7 +92,7 @@ fn parse_duration(s: &str) -> Option<Duration> {
|
||||
return None;
|
||||
}
|
||||
let parsed: u32 = frac.parse().ok()?;
|
||||
parsed.checked_mul(10u32.pow(9 - frac.len() as u32))?
|
||||
parsed * 10u32.pow(9 - frac.len() as u32)
|
||||
}
|
||||
None => 0,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user