Skip to main content

langfuse_client_base/apis/
prompts_api.rs

1/*
2 * langfuse
3 *
4 * ## Authentication  Authenticate with the API using [Basic Auth](https://en.wikipedia.org/wiki/Basic_access_authentication), get API keys in the project settings:  - username: Langfuse Public Key - password: Langfuse Secret Key  ## Exports  - OpenAPI spec: https://cloud.langfuse.com/generated/api/openapi.yml
5 *
6 * The version of the OpenAPI document:
7 *
8 * Generated by: https://openapi-generator.tech
9 */
10
11use super::{configuration, ContentType, Error};
12use crate::{apis::ResponseContent, models};
13use reqwest;
14use serde::{de::Error as _, Deserialize, Serialize};
15
16/// struct for typed errors of method [`prompts_create`]
17#[derive(Debug, Clone, Serialize, Deserialize)]
18#[serde(untagged)]
19pub enum PromptsCreateError {
20    Status400(serde_json::Value),
21    Status401(serde_json::Value),
22    Status403(serde_json::Value),
23    Status404(serde_json::Value),
24    Status405(serde_json::Value),
25    UnknownValue(serde_json::Value),
26}
27
28/// struct for typed errors of method [`prompts_delete`]
29#[derive(Debug, Clone, Serialize, Deserialize)]
30#[serde(untagged)]
31pub enum PromptsDeleteError {
32    Status400(serde_json::Value),
33    Status401(serde_json::Value),
34    Status403(serde_json::Value),
35    Status404(serde_json::Value),
36    Status405(serde_json::Value),
37    UnknownValue(serde_json::Value),
38}
39
40/// struct for typed errors of method [`prompts_get`]
41#[derive(Debug, Clone, Serialize, Deserialize)]
42#[serde(untagged)]
43pub enum PromptsGetError {
44    Status400(serde_json::Value),
45    Status401(serde_json::Value),
46    Status403(serde_json::Value),
47    Status404(serde_json::Value),
48    Status405(serde_json::Value),
49    UnknownValue(serde_json::Value),
50}
51
52/// struct for typed errors of method [`prompts_list`]
53#[derive(Debug, Clone, Serialize, Deserialize)]
54#[serde(untagged)]
55pub enum PromptsListError {
56    Status400(serde_json::Value),
57    Status401(serde_json::Value),
58    Status403(serde_json::Value),
59    Status404(serde_json::Value),
60    Status405(serde_json::Value),
61    UnknownValue(serde_json::Value),
62}
63
64/// Create a new version for the prompt with the given `name`
65#[bon::builder]
66pub async fn prompts_create(
67    configuration: &configuration::Configuration,
68    create_prompt_request: models::CreatePromptRequest,
69) -> Result<models::Prompt, Error<PromptsCreateError>> {
70    // add a prefix to parameters to efficiently prevent name collisions
71    let p_body_create_prompt_request = create_prompt_request;
72
73    let uri_str = format!("{}/api/public/v2/prompts", configuration.base_path);
74    let mut req_builder = configuration
75        .client
76        .request(reqwest::Method::POST, &uri_str);
77
78    if let Some(ref user_agent) = configuration.user_agent {
79        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
80    }
81    if let Some(ref auth_conf) = configuration.basic_auth {
82        req_builder = req_builder.basic_auth(auth_conf.0.to_owned(), auth_conf.1.to_owned());
83    };
84    req_builder = req_builder.json(&p_body_create_prompt_request);
85
86    let req = req_builder.build()?;
87    let resp = configuration.client.execute(req).await?;
88
89    let status = resp.status();
90    let content_type = resp
91        .headers()
92        .get("content-type")
93        .and_then(|v| v.to_str().ok())
94        .unwrap_or("application/octet-stream");
95    let content_type = super::ContentType::from(content_type);
96
97    if !status.is_client_error() && !status.is_server_error() {
98        let content = resp.text().await?;
99        match content_type {
100            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
101            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::Prompt`"))),
102            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::Prompt`")))),
103        }
104    } else {
105        let content = resp.text().await?;
106        let entity: Option<PromptsCreateError> = serde_json::from_str(&content).ok();
107        Err(Error::ResponseError(ResponseContent {
108            status,
109            content,
110            entity,
111        }))
112    }
113}
114
115/// Delete prompt versions. If neither version nor label is specified, all versions of the prompt are deleted.
116#[bon::builder]
117pub async fn prompts_delete(
118    configuration: &configuration::Configuration,
119    prompt_name: &str,
120    label: Option<&str>,
121    version: Option<i32>,
122) -> Result<(), Error<PromptsDeleteError>> {
123    // add a prefix to parameters to efficiently prevent name collisions
124    let p_path_prompt_name = prompt_name;
125    let p_query_label = label;
126    let p_query_version = version;
127
128    let uri_str = format!(
129        "{}/api/public/v2/prompts/{promptName}",
130        configuration.base_path,
131        promptName = crate::apis::urlencode(p_path_prompt_name)
132    );
133    let mut req_builder = configuration
134        .client
135        .request(reqwest::Method::DELETE, &uri_str);
136
137    if let Some(ref param_value) = p_query_label {
138        req_builder = req_builder.query(&[("label", &param_value.to_string())]);
139    }
140    if let Some(ref param_value) = p_query_version {
141        req_builder = req_builder.query(&[("version", &param_value.to_string())]);
142    }
143    if let Some(ref user_agent) = configuration.user_agent {
144        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
145    }
146    if let Some(ref auth_conf) = configuration.basic_auth {
147        req_builder = req_builder.basic_auth(auth_conf.0.to_owned(), auth_conf.1.to_owned());
148    };
149
150    let req = req_builder.build()?;
151    let resp = configuration.client.execute(req).await?;
152
153    let status = resp.status();
154
155    if !status.is_client_error() && !status.is_server_error() {
156        Ok(())
157    } else {
158        let content = resp.text().await?;
159        let entity: Option<PromptsDeleteError> = serde_json::from_str(&content).ok();
160        Err(Error::ResponseError(ResponseContent {
161            status,
162            content,
163            entity,
164        }))
165    }
166}
167
168/// Get a prompt
169#[bon::builder]
170pub async fn prompts_get(
171    configuration: &configuration::Configuration,
172    prompt_name: &str,
173    version: Option<i32>,
174    label: Option<&str>,
175    resolve: Option<bool>,
176) -> Result<models::Prompt, Error<PromptsGetError>> {
177    // add a prefix to parameters to efficiently prevent name collisions
178    let p_path_prompt_name = prompt_name;
179    let p_query_version = version;
180    let p_query_label = label;
181    let p_query_resolve = resolve;
182
183    let uri_str = format!(
184        "{}/api/public/v2/prompts/{promptName}",
185        configuration.base_path,
186        promptName = crate::apis::urlencode(p_path_prompt_name)
187    );
188    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
189
190    if let Some(ref param_value) = p_query_version {
191        req_builder = req_builder.query(&[("version", &param_value.to_string())]);
192    }
193    if let Some(ref param_value) = p_query_label {
194        req_builder = req_builder.query(&[("label", &param_value.to_string())]);
195    }
196    if let Some(ref param_value) = p_query_resolve {
197        req_builder = req_builder.query(&[("resolve", &param_value.to_string())]);
198    }
199    if let Some(ref user_agent) = configuration.user_agent {
200        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
201    }
202    if let Some(ref auth_conf) = configuration.basic_auth {
203        req_builder = req_builder.basic_auth(auth_conf.0.to_owned(), auth_conf.1.to_owned());
204    };
205
206    let req = req_builder.build()?;
207    let resp = configuration.client.execute(req).await?;
208
209    let status = resp.status();
210    let content_type = resp
211        .headers()
212        .get("content-type")
213        .and_then(|v| v.to_str().ok())
214        .unwrap_or("application/octet-stream");
215    let content_type = super::ContentType::from(content_type);
216
217    if !status.is_client_error() && !status.is_server_error() {
218        let content = resp.text().await?;
219        match content_type {
220            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
221            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::Prompt`"))),
222            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::Prompt`")))),
223        }
224    } else {
225        let content = resp.text().await?;
226        let entity: Option<PromptsGetError> = serde_json::from_str(&content).ok();
227        Err(Error::ResponseError(ResponseContent {
228            status,
229            content,
230            entity,
231        }))
232    }
233}
234
235/// Get a list of prompt names with versions and labels
236#[bon::builder]
237pub async fn prompts_list(
238    configuration: &configuration::Configuration,
239    name: Option<&str>,
240    label: Option<&str>,
241    tag: Option<&str>,
242    page: Option<i32>,
243    limit: Option<i32>,
244    from_updated_at: Option<String>,
245    to_updated_at: Option<String>,
246) -> Result<models::PromptMetaListResponse, Error<PromptsListError>> {
247    // add a prefix to parameters to efficiently prevent name collisions
248    let p_query_name = name;
249    let p_query_label = label;
250    let p_query_tag = tag;
251    let p_query_page = page;
252    let p_query_limit = limit;
253    let p_query_from_updated_at = from_updated_at;
254    let p_query_to_updated_at = to_updated_at;
255
256    let uri_str = format!("{}/api/public/v2/prompts", configuration.base_path);
257    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
258
259    if let Some(ref param_value) = p_query_name {
260        req_builder = req_builder.query(&[("name", &param_value.to_string())]);
261    }
262    if let Some(ref param_value) = p_query_label {
263        req_builder = req_builder.query(&[("label", &param_value.to_string())]);
264    }
265    if let Some(ref param_value) = p_query_tag {
266        req_builder = req_builder.query(&[("tag", &param_value.to_string())]);
267    }
268    if let Some(ref param_value) = p_query_page {
269        req_builder = req_builder.query(&[("page", &param_value.to_string())]);
270    }
271    if let Some(ref param_value) = p_query_limit {
272        req_builder = req_builder.query(&[("limit", &param_value.to_string())]);
273    }
274    if let Some(ref param_value) = p_query_from_updated_at {
275        req_builder = req_builder.query(&[("fromUpdatedAt", &param_value.to_string())]);
276    }
277    if let Some(ref param_value) = p_query_to_updated_at {
278        req_builder = req_builder.query(&[("toUpdatedAt", &param_value.to_string())]);
279    }
280    if let Some(ref user_agent) = configuration.user_agent {
281        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
282    }
283    if let Some(ref auth_conf) = configuration.basic_auth {
284        req_builder = req_builder.basic_auth(auth_conf.0.to_owned(), auth_conf.1.to_owned());
285    };
286
287    let req = req_builder.build()?;
288    let resp = configuration.client.execute(req).await?;
289
290    let status = resp.status();
291    let content_type = resp
292        .headers()
293        .get("content-type")
294        .and_then(|v| v.to_str().ok())
295        .unwrap_or("application/octet-stream");
296    let content_type = super::ContentType::from(content_type);
297
298    if !status.is_client_error() && !status.is_server_error() {
299        let content = resp.text().await?;
300        match content_type {
301            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
302            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::PromptMetaListResponse`"))),
303            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::PromptMetaListResponse`")))),
304        }
305    } else {
306        let content = resp.text().await?;
307        let entity: Option<PromptsListError> = serde_json::from_str(&content).ok();
308        Err(Error::ResponseError(ResponseContent {
309            status,
310            content,
311            entity,
312        }))
313    }
314}