-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor to use google gemini ai instead
- Loading branch information
1 parent
c1aff19
commit cb926ba
Showing
9 changed files
with
412 additions
and
257 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod api; | ||
pub mod api; | ||
pub mod provider; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
use anyhow::{anyhow, Result}; | ||
use google_generative_ai_rs::v1::api::Client; | ||
use google_generative_ai_rs::v1::gemini::{Content, Model, Part, Role}; | ||
use google_generative_ai_rs::v1::gemini::request::{GenerationConfig, Request, SystemInstructionContent, SystemInstructionPart}; | ||
use log::info; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Serialize, Deserialize, Debug, Clone)] | ||
pub struct DebriefResponse { | ||
pub description: String, | ||
pub url: String, | ||
pub type_of_change: String, | ||
} | ||
|
||
pub trait ChatProvider { | ||
async fn send_message( | ||
&self, | ||
system_instruction: String, | ||
message: String, | ||
) -> Result<Vec<DebriefResponse>>; | ||
} | ||
|
||
pub struct GeminiChatProvider { | ||
client: Client, | ||
} | ||
|
||
impl GeminiChatProvider { | ||
pub fn new(api_token: String) -> Self { | ||
let client = Client::new_from_model(Model::Gemini1_5Pro, api_token); | ||
|
||
GeminiChatProvider { | ||
client | ||
} | ||
} | ||
} | ||
|
||
impl ChatProvider for GeminiChatProvider { | ||
async fn send_message( | ||
&self, | ||
system_instruction: String, | ||
message: String, | ||
) -> Result<Vec<DebriefResponse>> { | ||
let request = Request { | ||
system_instruction: Some(SystemInstructionContent { | ||
parts: vec![SystemInstructionPart { | ||
text: Some(system_instruction) | ||
}] | ||
}), | ||
tools: vec![], | ||
generation_config: Some(GenerationConfig { | ||
temperature: None, | ||
top_p: None, | ||
top_k: None, | ||
candidate_count: None, | ||
max_output_tokens: None, | ||
stop_sequences: None, | ||
response_mime_type: Some("application/json".to_string()), | ||
}), | ||
contents: vec![Content { | ||
role: Role::User, | ||
parts: vec![Part { | ||
text: Some(message), | ||
inline_data: None, | ||
file_data: None, | ||
video_metadata: None, | ||
}], | ||
}], | ||
safety_settings: vec![] | ||
}; | ||
|
||
let response = self.client.post(30, &request).await?; | ||
|
||
match response.rest() { | ||
Some(rest) => { | ||
let first_candidate = rest.candidates.first(); | ||
|
||
match first_candidate.and_then(|candidate| candidate.content.parts.first().and_then(|part| part.text.clone())) { | ||
Some(text) => { | ||
match serde_json::from_str::<Vec<DebriefResponse>>(text.as_str()) { | ||
Ok(debrief) => { | ||
info!("Successfully parsed response from Gemini: {:?}", debrief); | ||
Ok(debrief) | ||
} | ||
Err(e) => { | ||
Err(anyhow!("Error parsing response from Gemini: {:?}", e)) | ||
} | ||
} | ||
} | ||
None => { | ||
Err(anyhow!("No valid candidate, part or text parsed as response received from Gemini")) | ||
} | ||
} | ||
} | ||
None => { | ||
Err(anyhow!("Error generating candidates from Gemini")) | ||
}, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters