google_ai.rs

  1mod supported_countries;
  2
  3use anyhow::{anyhow, Result};
  4use futures::{io::BufReader, stream::BoxStream, AsyncBufReadExt, AsyncReadExt, Stream, StreamExt};
  5use http_client::{AsyncBody, HttpClient, Method, Request as HttpRequest};
  6use isahc::config::Configurable;
  7use serde::{Deserialize, Serialize};
  8use std::time::Duration;
  9
 10pub use supported_countries::*;
 11
 12pub const API_URL: &str = "https://generativelanguage.googleapis.com";
 13
 14pub async fn stream_generate_content(
 15    client: &dyn HttpClient,
 16    api_url: &str,
 17    api_key: &str,
 18    mut request: GenerateContentRequest,
 19    low_speed_timeout: Option<Duration>,
 20) -> Result<BoxStream<'static, Result<GenerateContentResponse>>> {
 21    let uri = format!(
 22        "{api_url}/v1beta/models/{model}:streamGenerateContent?alt=sse&key={api_key}",
 23        model = request.model
 24    );
 25    request.model.clear();
 26
 27    let mut request_builder = HttpRequest::builder()
 28        .method(Method::POST)
 29        .uri(uri)
 30        .header("Content-Type", "application/json");
 31
 32    if let Some(low_speed_timeout) = low_speed_timeout {
 33        request_builder = request_builder.low_speed_timeout(100, low_speed_timeout);
 34    };
 35
 36    let request = request_builder.body(AsyncBody::from(serde_json::to_string(&request)?))?;
 37    let mut response = client.send(request).await?;
 38    if response.status().is_success() {
 39        let reader = BufReader::new(response.into_body());
 40        Ok(reader
 41            .lines()
 42            .filter_map(|line| async move {
 43                match line {
 44                    Ok(line) => {
 45                        if let Some(line) = line.strip_prefix("data: ") {
 46                            match serde_json::from_str(line) {
 47                                Ok(response) => Some(Ok(response)),
 48                                Err(error) => Some(Err(anyhow!(error))),
 49                            }
 50                        } else {
 51                            None
 52                        }
 53                    }
 54                    Err(error) => Some(Err(anyhow!(error))),
 55                }
 56            })
 57            .boxed())
 58    } else {
 59        let mut text = String::new();
 60        response.body_mut().read_to_string(&mut text).await?;
 61        Err(anyhow!(
 62            "error during streamGenerateContent, status code: {:?}, body: {}",
 63            response.status(),
 64            text
 65        ))
 66    }
 67}
 68
 69pub async fn count_tokens(
 70    client: &dyn HttpClient,
 71    api_url: &str,
 72    api_key: &str,
 73    request: CountTokensRequest,
 74    low_speed_timeout: Option<Duration>,
 75) -> Result<CountTokensResponse> {
 76    let uri = format!(
 77        "{}/v1beta/models/gemini-pro:countTokens?key={}",
 78        api_url, api_key
 79    );
 80    let request = serde_json::to_string(&request)?;
 81
 82    let mut request_builder = HttpRequest::builder()
 83        .method(Method::POST)
 84        .uri(&uri)
 85        .header("Content-Type", "application/json");
 86
 87    if let Some(low_speed_timeout) = low_speed_timeout {
 88        request_builder = request_builder.low_speed_timeout(100, low_speed_timeout);
 89    }
 90
 91    let http_request = request_builder.body(AsyncBody::from(request))?;
 92    let mut response = client.send(http_request).await?;
 93    let mut text = String::new();
 94    response.body_mut().read_to_string(&mut text).await?;
 95    if response.status().is_success() {
 96        Ok(serde_json::from_str::<CountTokensResponse>(&text)?)
 97    } else {
 98        Err(anyhow!(
 99            "error during countTokens, status code: {:?}, body: {}",
100            response.status(),
101            text
102        ))
103    }
104}
105
106#[derive(Debug, Serialize, Deserialize)]
107pub enum Task {
108    #[serde(rename = "generateContent")]
109    GenerateContent,
110    #[serde(rename = "streamGenerateContent")]
111    StreamGenerateContent,
112    #[serde(rename = "countTokens")]
113    CountTokens,
114    #[serde(rename = "embedContent")]
115    EmbedContent,
116    #[serde(rename = "batchEmbedContents")]
117    BatchEmbedContents,
118}
119
120#[derive(Debug, Serialize, Deserialize)]
121#[serde(rename_all = "camelCase")]
122pub struct GenerateContentRequest {
123    #[serde(default, skip_serializing_if = "String::is_empty")]
124    pub model: String,
125    pub contents: Vec<Content>,
126    pub generation_config: Option<GenerationConfig>,
127    pub safety_settings: Option<Vec<SafetySetting>>,
128}
129
130#[derive(Debug, Serialize, Deserialize)]
131#[serde(rename_all = "camelCase")]
132pub struct GenerateContentResponse {
133    pub candidates: Option<Vec<GenerateContentCandidate>>,
134    pub prompt_feedback: Option<PromptFeedback>,
135}
136
137#[derive(Debug, Serialize, Deserialize)]
138#[serde(rename_all = "camelCase")]
139pub struct GenerateContentCandidate {
140    pub index: usize,
141    pub content: Content,
142    pub finish_reason: Option<String>,
143    pub finish_message: Option<String>,
144    pub safety_ratings: Option<Vec<SafetyRating>>,
145    pub citation_metadata: Option<CitationMetadata>,
146}
147
148#[derive(Debug, Serialize, Deserialize)]
149#[serde(rename_all = "camelCase")]
150pub struct Content {
151    pub parts: Vec<Part>,
152    pub role: Role,
153}
154
155#[derive(Debug, Deserialize, Serialize)]
156#[serde(rename_all = "camelCase")]
157pub enum Role {
158    User,
159    Model,
160}
161
162#[derive(Debug, Serialize, Deserialize)]
163#[serde(untagged)]
164pub enum Part {
165    TextPart(TextPart),
166    InlineDataPart(InlineDataPart),
167}
168
169#[derive(Debug, Serialize, Deserialize)]
170#[serde(rename_all = "camelCase")]
171pub struct TextPart {
172    pub text: String,
173}
174
175#[derive(Debug, Serialize, Deserialize)]
176#[serde(rename_all = "camelCase")]
177pub struct InlineDataPart {
178    pub inline_data: GenerativeContentBlob,
179}
180
181#[derive(Debug, Serialize, Deserialize)]
182#[serde(rename_all = "camelCase")]
183pub struct GenerativeContentBlob {
184    pub mime_type: String,
185    pub data: String,
186}
187
188#[derive(Debug, Serialize, Deserialize)]
189#[serde(rename_all = "camelCase")]
190pub struct CitationSource {
191    pub start_index: Option<usize>,
192    pub end_index: Option<usize>,
193    pub uri: Option<String>,
194    pub license: Option<String>,
195}
196
197#[derive(Debug, Serialize, Deserialize)]
198#[serde(rename_all = "camelCase")]
199pub struct CitationMetadata {
200    pub citation_sources: Vec<CitationSource>,
201}
202
203#[derive(Debug, Serialize, Deserialize)]
204#[serde(rename_all = "camelCase")]
205pub struct PromptFeedback {
206    pub block_reason: Option<String>,
207    pub safety_ratings: Vec<SafetyRating>,
208    pub block_reason_message: Option<String>,
209}
210
211#[derive(Debug, Deserialize, Serialize)]
212#[serde(rename_all = "camelCase")]
213pub struct GenerationConfig {
214    pub candidate_count: Option<usize>,
215    pub stop_sequences: Option<Vec<String>>,
216    pub max_output_tokens: Option<usize>,
217    pub temperature: Option<f64>,
218    pub top_p: Option<f64>,
219    pub top_k: Option<usize>,
220}
221
222#[derive(Debug, Serialize, Deserialize)]
223#[serde(rename_all = "camelCase")]
224pub struct SafetySetting {
225    pub category: HarmCategory,
226    pub threshold: HarmBlockThreshold,
227}
228
229#[derive(Debug, Serialize, Deserialize)]
230pub enum HarmCategory {
231    #[serde(rename = "HARM_CATEGORY_UNSPECIFIED")]
232    Unspecified,
233    #[serde(rename = "HARM_CATEGORY_DEROGATORY")]
234    Derogatory,
235    #[serde(rename = "HARM_CATEGORY_TOXICITY")]
236    Toxicity,
237    #[serde(rename = "HARM_CATEGORY_VIOLENCE")]
238    Violence,
239    #[serde(rename = "HARM_CATEGORY_SEXUAL")]
240    Sexual,
241    #[serde(rename = "HARM_CATEGORY_MEDICAL")]
242    Medical,
243    #[serde(rename = "HARM_CATEGORY_DANGEROUS")]
244    Dangerous,
245    #[serde(rename = "HARM_CATEGORY_HARASSMENT")]
246    Harassment,
247    #[serde(rename = "HARM_CATEGORY_HATE_SPEECH")]
248    HateSpeech,
249    #[serde(rename = "HARM_CATEGORY_SEXUALLY_EXPLICIT")]
250    SexuallyExplicit,
251    #[serde(rename = "HARM_CATEGORY_DANGEROUS_CONTENT")]
252    DangerousContent,
253}
254
255#[derive(Debug, Serialize, Deserialize)]
256pub enum HarmBlockThreshold {
257    #[serde(rename = "HARM_BLOCK_THRESHOLD_UNSPECIFIED")]
258    Unspecified,
259    #[serde(rename = "BLOCK_LOW_AND_ABOVE")]
260    BlockLowAndAbove,
261    #[serde(rename = "BLOCK_MEDIUM_AND_ABOVE")]
262    BlockMediumAndAbove,
263    #[serde(rename = "BLOCK_ONLY_HIGH")]
264    BlockOnlyHigh,
265    #[serde(rename = "BLOCK_NONE")]
266    BlockNone,
267}
268
269#[derive(Debug, Serialize, Deserialize)]
270#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
271pub enum HarmProbability {
272    #[serde(rename = "HARM_PROBABILITY_UNSPECIFIED")]
273    Unspecified,
274    Negligible,
275    Low,
276    Medium,
277    High,
278}
279
280#[derive(Debug, Serialize, Deserialize)]
281#[serde(rename_all = "camelCase")]
282pub struct SafetyRating {
283    pub category: HarmCategory,
284    pub probability: HarmProbability,
285}
286
287#[derive(Debug, Serialize, Deserialize)]
288#[serde(rename_all = "camelCase")]
289pub struct CountTokensRequest {
290    pub contents: Vec<Content>,
291}
292
293#[derive(Debug, Serialize, Deserialize)]
294#[serde(rename_all = "camelCase")]
295pub struct CountTokensResponse {
296    pub total_tokens: usize,
297}
298
299#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
300#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq, strum::EnumIter)]
301pub enum Model {
302    #[serde(rename = "gemini-1.5-pro")]
303    Gemini15Pro,
304    #[serde(rename = "gemini-1.5-flash")]
305    Gemini15Flash,
306    #[serde(rename = "custom")]
307    Custom {
308        name: String,
309        /// The name displayed in the UI, such as in the assistant panel model dropdown menu.
310        display_name: Option<String>,
311        max_tokens: usize,
312    },
313}
314
315impl Model {
316    pub fn id(&self) -> &str {
317        match self {
318            Model::Gemini15Pro => "gemini-1.5-pro",
319            Model::Gemini15Flash => "gemini-1.5-flash",
320            Model::Custom { name, .. } => name,
321        }
322    }
323
324    pub fn display_name(&self) -> &str {
325        match self {
326            Model::Gemini15Pro => "Gemini 1.5 Pro",
327            Model::Gemini15Flash => "Gemini 1.5 Flash",
328            Self::Custom {
329                name, display_name, ..
330            } => display_name.as_ref().unwrap_or(name),
331        }
332    }
333
334    pub fn max_token_count(&self) -> usize {
335        match self {
336            Model::Gemini15Pro => 2_000_000,
337            Model::Gemini15Flash => 1_000_000,
338            Model::Custom { max_tokens, .. } => *max_tokens,
339        }
340    }
341}
342
343impl std::fmt::Display for Model {
344    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
345        write!(f, "{}", self.id())
346    }
347}
348
349pub fn extract_text_from_events(
350    events: impl Stream<Item = Result<GenerateContentResponse>>,
351) -> impl Stream<Item = Result<String>> {
352    events.filter_map(|event| async move {
353        match event {
354            Ok(event) => event.candidates.and_then(|candidates| {
355                candidates.into_iter().next().and_then(|candidate| {
356                    candidate.content.parts.into_iter().next().and_then(|part| {
357                        if let Part::TextPart(TextPart { text }) = part {
358                            Some(Ok(text))
359                        } else {
360                            None
361                        }
362                    })
363                })
364            }),
365            Err(error) => Some(Err(error)),
366        }
367    })
368}