aiapi/
lib.rs

1#![feature(doc_auto_cfg)]
2#![feature(doc_cfg)]
3
4use reqwest::RequestBuilder;
5use serde::{Deserialize, Serialize};
6use thiserror::Error;
7
8#[derive(Error, Debug)]
9pub enum Error {
10  #[error("JSON: {0}")]
11  Json(#[from] sonic_rs::Error),
12
13  #[error("Request: {0}")]
14  Request(#[from] reqwest::Error),
15
16  #[cfg(feature = "from_yml")]
17  #[error("Yml: {0}")]
18  Yml(#[from] saphyr::ScanError),
19
20  #[cfg(feature = "from_yml")]
21  #[error("{path}: {error}")]
22  File { error: std::io::Error, path: String },
23
24  #[error("API: {status}\n{text}")]
25  Api {
26    status: reqwest::StatusCode,
27    text: String,
28  },
29
30  #[error("RateLimit: {text}")]
31  RateLimit { text: String },
32
33  #[error("Timeout: {text}")]
34  Timeout { text: String },
35
36  #[error("ApiKeyInvalid: {text}")]
37  ApiKeyInvalid { text: String },
38
39  #[error("EmptyResponse: {text}")]
40  EmptyResponse { text: String },
41
42  #[cfg(feature = "from_yml")]
43  #[error("ConfTraitError: {0}")]
44  ConfTrait(String),
45}
46
47pub type Result<T> = std::result::Result<T, Error>;
48
49#[derive(Serialize, Deserialize, Debug, Clone)]
50pub struct Usage {
51  pub prompt_tokens: u64,
52  pub completion_tokens: u64,
53  #[serde(default)]
54  pub think_tokens: u64,
55}
56
57#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
58#[serde(rename_all = "snake_case")]
59pub enum FinishReason {
60  Stop,
61  Length,
62  ToolCalls,
63  ContentFilter,
64  #[serde(other)]
65  Unknown,
66}
67
68#[derive(Debug)]
69pub struct ChatResult {
70  pub id: String,
71  pub content: String,
72  pub usage: Usage,
73  pub finish_reason: FinishReason,
74}
75
76pub trait AiApi {
77  fn url(&self) -> &str;
78  fn req(
79    &self,
80    conf: &impl ConfTrait,
81    model: &str,
82    content: impl Into<String>,
83  ) -> Result<RequestBuilder>;
84
85  fn chat(
86    &self,
87    token: &str,
88    req: &RequestBuilder,
89  ) -> impl std::future::Future<Output = Result<ChatResult>> + Send;
90}
91
92pub mod conf;
93pub use conf::{Conf, ConfQroq, ConfTrait, ReasoningEffort};
94
95pub mod openai;
96pub mod response_to_error;
97pub use openai::OpenAI;
98
99pub mod token_li;
100pub use token_li::TokenLi;
101
102pub mod gemini;
103pub use gemini::Gemini;
104
105#[cfg(feature = "from_yml")]
106pub mod from_yml;