More builder refactoring
This commit is contained in:
@@ -15,12 +15,18 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
location_id,
|
location_id,
|
||||||
);
|
);
|
||||||
|
|
||||||
let system_instruction = "Answer as if you were Yoda";
|
let system_instruction = Content::builder()
|
||||||
let prompt = "What is the airspeed of an unladen swallow?";
|
.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 request = GenerateContentRequest::builder()
|
let request = GenerateContentRequest::builder()
|
||||||
.add_text_content(Role::User, prompt)
|
.contents(user_prompt)
|
||||||
.system_instruction_text(system_instruction)
|
.system_instruction(system_instruction)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
let result = gemini
|
let result = gemini
|
||||||
|
|||||||
@@ -14,10 +14,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
location_id,
|
location_id,
|
||||||
);
|
);
|
||||||
|
|
||||||
let prompt = "Tell me the story of the genesis of the universe as a bedtime story.";
|
let prompt = vec![Content::builder()
|
||||||
let request = GenerateContentRequest::builder()
|
.role(Role::User)
|
||||||
.add_text_content(Role::User, prompt)
|
.add_text_part("Tell me the story of the genesis of the universe as a bedtime story.")
|
||||||
.build();
|
.build()];
|
||||||
|
|
||||||
|
let request = GenerateContentRequest::builder().contents(prompt).build();
|
||||||
|
|
||||||
let queue = gemini.stream_generate_content(&request, "gemini-pro").await;
|
let queue = gemini.stream_generate_content(&request, "gemini-pro").await;
|
||||||
|
|
||||||
|
|||||||
@@ -14,10 +14,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
location_id,
|
location_id,
|
||||||
);
|
);
|
||||||
|
|
||||||
let prompt = "What is the airspeed of an unladen swallow?";
|
let prompt = vec![Content::builder()
|
||||||
let request = GenerateContentRequest::builder()
|
.role(Role::User)
|
||||||
.add_text_content(Role::User, prompt)
|
.add_text_part("What is the airspeed of an unladen swallow?")
|
||||||
.build();
|
.build()];
|
||||||
|
|
||||||
|
let request = GenerateContentRequest::builder().contents(prompt).build();
|
||||||
let response = gemini.generate_content(&request, "gemini-pro").await?;
|
let response = gemini.generate_content(&request, "gemini-pro").await?;
|
||||||
println!("Response: {:?}", response.candidates[0].get_text().unwrap());
|
println!("Response: {:?}", response.candidates[0].get_text().unwrap());
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ use std::collections::HashMap;
|
|||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
|
|
||||||
use super::{Content, Role, VertexApiError};
|
use super::{Content, VertexApiError};
|
||||||
use crate::error::Result;
|
use crate::error::Result;
|
||||||
|
|
||||||
#[derive(Clone, Default, Serialize, Deserialize)]
|
#[derive(Clone, Default, Serialize, Deserialize)]
|
||||||
@@ -37,13 +37,8 @@ impl GenerateContentRequestBuilder {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_text_content<T: Into<String>>(self, role: Role, text: T) -> Self {
|
pub fn contents(mut self, contents: Vec<Content>) -> Self {
|
||||||
let content = Content::builder().role(role).add_text_part(text).build();
|
self.request.contents = contents;
|
||||||
self.add_content(content)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn add_content(mut self, content: Content) -> Self {
|
|
||||||
self.request.contents.push(content);
|
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,10 +57,6 @@ impl GenerateContentRequestBuilder {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn system_instruction_text<T: Into<String>>(self, text: T) -> Self {
|
|
||||||
self.system_instruction(Content::builder().add_text_part(text).build())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn system_instruction(mut self, system_instruction: Content) -> Self {
|
pub fn system_instruction(mut self, system_instruction: Content) -> Self {
|
||||||
self.request.system_instruction = Some(system_instruction);
|
self.request.system_instruction = Some(system_instruction);
|
||||||
self
|
self
|
||||||
|
|||||||
Reference in New Issue
Block a user