blackman_client/apis/
semantic_cache_api.rs

1/*
2 * Blackman AI API
3 *
4 * A transparent AI API proxy that optimizes token usage to reduce costs.  ## Authentication  Blackman AI supports two authentication methods:  ### 1. API Key (Recommended for integrations)  Use the API key created from your dashboard:  ```bash curl -X POST https://app.useblackman.ai/v1/completions \\   -H \"Authorization: Bearer sk_your_api_key_here\" \\   -H \"Content-Type: application/json\" \\   -d '{\"provider\": \"OpenAI\", \"model\": \"gpt-4\", \"messages\": [{\"role\": \"user\", \"content\": \"Hello!\"}]}' ```  ### 2. JWT Token (For web UI)  Obtain a JWT token by logging in:  ```bash curl -X POST https://app.useblackman.ai/v1/auth/login \\   -H \"Content-Type: application/json\" \\   -d '{\"email\": \"user@example.com\", \"password\": \"yourpassword\"}' ```  Then use the token:  ```bash curl -X POST https://app.useblackman.ai/v1/completions \\   -H \"Authorization: Bearer your_jwt_token\" \\   -H \"Content-Type: application/json\" \\   -d '{...}' ```  ### Provider API Keys (Optional)  You can optionally provide your own LLM provider API key via the `X-Provider-Api-Key` header, or store it in your account settings.  ## Client SDKs  Auto-generated SDKs are available for 10 languages:  - **TypeScript**: [View Docs](https://github.com/blackman-ai/typescript-sdk) - **Python**: [View Docs](https://github.com/blackman-ai/python-sdk) - **Go**: [View Docs](https://github.com/blackman-ai/go-sdk) - **Java**: [View Docs](https://github.com/blackman-ai/java-sdk) - **Ruby**: [View Docs](https://github.com/blackman-ai/ruby-sdk) - **PHP**: [View Docs](https://github.com/blackman-ai/php-sdk) - **C#**: [View Docs](https://github.com/blackman-ai/csharp-sdk) - **Rust**: [View Docs](https://github.com/blackman-ai/rust-sdk) - **Swift**: [View Docs](https://github.com/blackman-ai/swift-sdk) - **Kotlin**: [View Docs](https://github.com/blackman-ai/kotlin-sdk)  All SDKs are generated from this OpenAPI spec using [openapi-generator](https://openapi-generator.tech).  ## Quick Start  ```python # Python example with API key import blackman_client from blackman_client import CompletionRequest  configuration = blackman_client.Configuration(     host=\"http://localhost:8080\",     access_token=\"sk_your_api_key_here\"  # Your Blackman API key )  with blackman_client.ApiClient(configuration) as api_client:     api = blackman_client.CompletionsApi(api_client)     response = api.completions(         CompletionRequest(             provider=\"OpenAI\",             model=\"gpt-4o\",             messages=[{\"role\": \"user\", \"content\": \"Hello!\"}]         )     ) ```
5 *
6 * The version of the OpenAPI document: 0.1.0
7 * 
8 * Generated by: https://openapi-generator.tech
9 */
10
11
12use reqwest;
13use serde::{Deserialize, Serialize, de::Error as _};
14use crate::{apis::ResponseContent, models};
15use super::{Error, configuration, ContentType};
16
17
18/// struct for typed errors of method [`get_config`]
19#[derive(Debug, Clone, Serialize, Deserialize)]
20#[serde(untagged)]
21pub enum GetConfigError {
22    Status401(),
23    Status500(),
24    UnknownValue(serde_json::Value),
25}
26
27/// struct for typed errors of method [`get_stats`]
28#[derive(Debug, Clone, Serialize, Deserialize)]
29#[serde(untagged)]
30pub enum GetStatsError {
31    Status401(),
32    Status500(),
33    UnknownValue(serde_json::Value),
34}
35
36/// struct for typed errors of method [`invalidate_all`]
37#[derive(Debug, Clone, Serialize, Deserialize)]
38#[serde(untagged)]
39pub enum InvalidateAllError {
40    Status401(),
41    Status500(),
42    Status503(),
43    UnknownValue(serde_json::Value),
44}
45
46/// struct for typed errors of method [`update_config`]
47#[derive(Debug, Clone, Serialize, Deserialize)]
48#[serde(untagged)]
49pub enum UpdateConfigError {
50    Status400(),
51    Status401(),
52    Status500(),
53    UnknownValue(serde_json::Value),
54}
55
56
57pub async fn get_config(configuration: &configuration::Configuration, ) -> Result<models::SemanticCacheConfig, Error<GetConfigError>> {
58
59    let uri_str = format!("{}/v1/semantic-cache/config", configuration.base_path);
60    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
61
62    if let Some(ref user_agent) = configuration.user_agent {
63        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
64    }
65    if let Some(ref token) = configuration.bearer_access_token {
66        req_builder = req_builder.bearer_auth(token.to_owned());
67    };
68
69    let req = req_builder.build()?;
70    let resp = configuration.client.execute(req).await?;
71
72    let status = resp.status();
73    let content_type = resp
74        .headers()
75        .get("content-type")
76        .and_then(|v| v.to_str().ok())
77        .unwrap_or("application/octet-stream");
78    let content_type = super::ContentType::from(content_type);
79
80    if !status.is_client_error() && !status.is_server_error() {
81        let content = resp.text().await?;
82        match content_type {
83            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
84            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::SemanticCacheConfig`"))),
85            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::SemanticCacheConfig`")))),
86        }
87    } else {
88        let content = resp.text().await?;
89        let entity: Option<GetConfigError> = serde_json::from_str(&content).ok();
90        Err(Error::ResponseError(ResponseContent { status, content, entity }))
91    }
92}
93
94pub async fn get_stats(configuration: &configuration::Configuration, ) -> Result<models::SemanticCacheStats, Error<GetStatsError>> {
95
96    let uri_str = format!("{}/v1/semantic-cache/stats", configuration.base_path);
97    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
98
99    if let Some(ref user_agent) = configuration.user_agent {
100        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
101    }
102    if let Some(ref token) = configuration.bearer_access_token {
103        req_builder = req_builder.bearer_auth(token.to_owned());
104    };
105
106    let req = req_builder.build()?;
107    let resp = configuration.client.execute(req).await?;
108
109    let status = resp.status();
110    let content_type = resp
111        .headers()
112        .get("content-type")
113        .and_then(|v| v.to_str().ok())
114        .unwrap_or("application/octet-stream");
115    let content_type = super::ContentType::from(content_type);
116
117    if !status.is_client_error() && !status.is_server_error() {
118        let content = resp.text().await?;
119        match content_type {
120            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
121            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::SemanticCacheStats`"))),
122            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::SemanticCacheStats`")))),
123        }
124    } else {
125        let content = resp.text().await?;
126        let entity: Option<GetStatsError> = serde_json::from_str(&content).ok();
127        Err(Error::ResponseError(ResponseContent { status, content, entity }))
128    }
129}
130
131pub async fn invalidate_all(configuration: &configuration::Configuration, ) -> Result<models::InvalidateResponse, Error<InvalidateAllError>> {
132
133    let uri_str = format!("{}/v1/semantic-cache/invalidate", configuration.base_path);
134    let mut req_builder = configuration.client.request(reqwest::Method::DELETE, &uri_str);
135
136    if let Some(ref user_agent) = configuration.user_agent {
137        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
138    }
139    if let Some(ref token) = configuration.bearer_access_token {
140        req_builder = req_builder.bearer_auth(token.to_owned());
141    };
142
143    let req = req_builder.build()?;
144    let resp = configuration.client.execute(req).await?;
145
146    let status = resp.status();
147    let content_type = resp
148        .headers()
149        .get("content-type")
150        .and_then(|v| v.to_str().ok())
151        .unwrap_or("application/octet-stream");
152    let content_type = super::ContentType::from(content_type);
153
154    if !status.is_client_error() && !status.is_server_error() {
155        let content = resp.text().await?;
156        match content_type {
157            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
158            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::InvalidateResponse`"))),
159            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::InvalidateResponse`")))),
160        }
161    } else {
162        let content = resp.text().await?;
163        let entity: Option<InvalidateAllError> = serde_json::from_str(&content).ok();
164        Err(Error::ResponseError(ResponseContent { status, content, entity }))
165    }
166}
167
168pub async fn update_config(configuration: &configuration::Configuration, semantic_cache_config: models::SemanticCacheConfig) -> Result<(), Error<UpdateConfigError>> {
169    // add a prefix to parameters to efficiently prevent name collisions
170    let p_semantic_cache_config = semantic_cache_config;
171
172    let uri_str = format!("{}/v1/semantic-cache/config", configuration.base_path);
173    let mut req_builder = configuration.client.request(reqwest::Method::PUT, &uri_str);
174
175    if let Some(ref user_agent) = configuration.user_agent {
176        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
177    }
178    if let Some(ref token) = configuration.bearer_access_token {
179        req_builder = req_builder.bearer_auth(token.to_owned());
180    };
181    req_builder = req_builder.json(&p_semantic_cache_config);
182
183    let req = req_builder.build()?;
184    let resp = configuration.client.execute(req).await?;
185
186    let status = resp.status();
187
188    if !status.is_client_error() && !status.is_server_error() {
189        Ok(())
190    } else {
191        let content = resp.text().await?;
192        let entity: Option<UpdateConfigError> = serde_json::from_str(&content).ok();
193        Err(Error::ResponseError(ResponseContent { status, content, entity }))
194    }
195}
196