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