Add shortcuts for commonly used functions

This commit is contained in:
2024-11-27 15:57:18 +00:00
parent db5a01afef
commit 56d6b95c53
5 changed files with 20 additions and 24 deletions

View File

@@ -18,17 +18,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let system_instruction = "Answer as if you were Yoda"; let system_instruction = "Answer as if you were Yoda";
let prompt = "What is the airspeed of an unladen swallow?"; let prompt = "What is the airspeed of an unladen swallow?";
let request = GenerateContentRequest { let request = GenerateContentRequest::builder()
contents: vec![Content { .add_text_content(Role::User, prompt)
role: Some(Role::User), .system_instruction_text(system_instruction)
parts: Some(vec![Part::Text(prompt.to_string())]), .build();
}],
system_instruction: Some(Content {
role: None,
parts: Some(vec![Part::Text(system_instruction.to_string())]),
}),
..Default::default()
};
let result = gemini let result = gemini
.generate_content(&request, "gemini-1.0-pro-002") .generate_content(&request, "gemini-1.0-pro-002")

View File

@@ -16,12 +16,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let prompt = "Tell me the story of the genesis of the universe as a bedtime story."; let prompt = "Tell me the story of the genesis of the universe as a bedtime story.";
let request = GenerateContentRequest::builder() let request = GenerateContentRequest::builder()
.add_content( .add_text_content(Role::User, prompt)
Content::builder()
.role(Role::User)
.add_part(Part::Text(prompt.to_string()))
.build(),
)
.build(); .build();
let queue = gemini.stream_generate_content(&request, "gemini-pro").await; let queue = gemini.stream_generate_content(&request, "gemini-pro").await;

View File

@@ -16,12 +16,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let prompt = "What is the airspeed of an unladen swallow?"; let prompt = "What is the airspeed of an unladen swallow?";
let request = GenerateContentRequest::builder() let request = GenerateContentRequest::builder()
.add_content( .add_text_content(Role::User, prompt)
Content::builder()
.role(Role::User)
.add_part(Part::Text(prompt.to_string()))
.build(),
)
.build(); .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());

View File

@@ -37,6 +37,10 @@ impl ContentBuilder {
} }
} }
pub fn add_text_part<T: Into<String>>(self, text: T) -> Self {
self.add_part(Part::Text(text.into()))
}
pub fn add_part(mut self, part: Part) -> Self { pub fn add_part(mut self, part: Part) -> Self {
match &mut self.content.parts { match &mut self.content.parts {
Some(parts) => parts.push(part), Some(parts) => parts.push(part),

View File

@@ -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, VertexApiError}; use super::{Content, Role, VertexApiError};
use crate::error::Result; use crate::error::Result;
#[derive(Clone, Default, Serialize, Deserialize)] #[derive(Clone, Default, Serialize, Deserialize)]
@@ -37,6 +37,11 @@ impl GenerateContentRequestBuilder {
} }
} }
pub fn add_text_content<T: Into<String>>(self, role: Role, text: T) -> Self {
let content = Content::builder().role(role).add_text_part(text).build();
self.add_content(content)
}
pub fn add_content(mut self, content: Content) -> Self { pub fn add_content(mut self, content: Content) -> Self {
self.request.contents.push(content); self.request.contents.push(content);
self self
@@ -57,6 +62,10 @@ 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