PostResult

Enum PostResult 

Source
pub enum PostResult {
    Rest(GeminiResponse),
    Streamed(StreamedGeminiResponse),
    Count(TokenCount),
}
Expand description

Enables a streamed or non-streamed response to be returned from the API.

Variants§

Implementations§

Source§

impl PostResult

Source

pub fn rest(self) -> Option<GeminiResponse>

Source

pub fn streamed(self) -> Option<StreamedGeminiResponse>

Examples found in repository?
examples/vertex_text_request_stream.rs (line 52)
23async 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        #[cfg(feature = "beta")]
45        system_instruction: None,
46    };
47
48    let response = client.post(30, &txt_request).await?;
49
50    println!("output streaming content");
51
52    if let Some(stream_response) = response.streamed() {
53        if let Some(json_stream) = stream_response.response_stream {
54            Client::for_each_async(json_stream, move |response: GeminiResponse| async move {
55                let mut lock = stdout().lock();
56                write!(
57                    lock,
58                    "{}",
59                    response.candidates[0].content.parts[0]
60                        .text
61                        .clone()
62                        .unwrap()
63                        .as_str()
64                )
65                .unwrap();
66            })
67            .await
68        }
69    }
70
71    Ok(())
72}
More examples
Hide additional examples
examples/text_request_stream.rs (line 57)
15async fn main() -> Result<(), Box<dyn std::error::Error>> {
16    env_logger::init();
17
18    let token = match env::var("API_KEY") {
19        Ok(v) => v,
20        Err(e) => {
21            let msg = "$API_KEY not found".to_string();
22            panic!("{e:?}:{msg}");
23        }
24    };
25
26    // Either run as a standard text request or a stream generate content request
27    let client = Client::new_from_model_response_type(
28        google_generative_ai_rs::v1::gemini::Model::Gemini1_0Pro,
29        token.clone(),
30        ResponseType::StreamGenerateContent,
31    );
32
33    println!("token {:#?}", token);
34
35    let txt_request = Request {
36        contents: vec![Content {
37            role: Role::User,
38            parts: vec![Part {
39                text: Some("Give me a recipe for banana bread.".to_string()),
40                inline_data: None,
41                file_data: None,
42                video_metadata: None,
43            }],
44        }],
45        tools: vec![],
46        safety_settings: vec![],
47        generation_config: None,
48
49        #[cfg(feature = "beta")]
50        system_instruction: None,
51    };
52
53    let response = client.post(30, &txt_request).await?;
54
55    println!("output streaming content");
56
57    if let Some(stream_response) = response.streamed() {
58        if let Some(json_stream) = stream_response.response_stream {
59            Client::for_each_async(json_stream, move |response: GeminiResponse| async move {
60                let mut lock = stdout().lock();
61                write!(
62                    lock,
63                    "{}",
64                    response.candidates[0].content.parts[0]
65                        .text
66                        .clone()
67                        .unwrap()
68                        .as_str()
69                )
70                .unwrap();
71            })
72            .await
73        }
74    }
75
76    Ok(())
77}
Source

pub fn count(self) -> Option<TokenCount>

Trait Implementations§

Source§

impl Debug for PostResult

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,