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 - Postman collection: https://cloud.langfuse.com/generated/postman/collection.json
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) -> Result<models::Prompt, Error<PromptsGetError>> {
176    // add a prefix to parameters to efficiently prevent name collisions
177    let p_path_prompt_name = prompt_name;
178    let p_query_version = version;
179    let p_query_label = label;
180
181    let uri_str = format!(
182        "{}/api/public/v2/prompts/{promptName}",
183        configuration.base_path,
184        promptName = crate::apis::urlencode(p_path_prompt_name)
185    );
186    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
187
188    if let Some(ref param_value) = p_query_version {
189        req_builder = req_builder.query(&[("version", &param_value.to_string())]);
190    }
191    if let Some(ref param_value) = p_query_label {
192        req_builder = req_builder.query(&[("label", &param_value.to_string())]);
193    }
194    if let Some(ref user_agent) = configuration.user_agent {
195        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
196    }
197    if let Some(ref auth_conf) = configuration.basic_auth {
198        req_builder = req_builder.basic_auth(auth_conf.0.to_owned(), auth_conf.1.to_owned());
199    };
200
201    let req = req_builder.build()?;
202    let resp = configuration.client.execute(req).await?;
203
204    let status = resp.status();
205    let content_type = resp
206        .headers()
207        .get("content-type")
208        .and_then(|v| v.to_str().ok())
209        .unwrap_or("application/octet-stream");
210    let content_type = super::ContentType::from(content_type);
211
212    if !status.is_client_error() && !status.is_server_error() {
213        let content = resp.text().await?;
214        match content_type {
215            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
216            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::Prompt`"))),
217            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`")))),
218        }
219    } else {
220        let content = resp.text().await?;
221        let entity: Option<PromptsGetError> = serde_json::from_str(&content).ok();
222        Err(Error::ResponseError(ResponseContent {
223            status,
224            content,
225            entity,
226        }))
227    }
228}
229
230/// Get a list of prompt names with versions and labels
231#[bon::builder]
232pub async fn prompts_list(
233    configuration: &configuration::Configuration,
234    name: Option<&str>,
235    label: Option<&str>,
236    tag: Option<&str>,
237    page: Option<i32>,
238    limit: Option<i32>,
239    from_updated_at: Option<String>,
240    to_updated_at: Option<String>,
241) -> Result<models::PromptMetaListResponse, Error<PromptsListError>> {
242    // add a prefix to parameters to efficiently prevent name collisions
243    let p_query_name = name;
244    let p_query_label = label;
245    let p_query_tag = tag;
246    let p_query_page = page;
247    let p_query_limit = limit;
248    let p_query_from_updated_at = from_updated_at;
249    let p_query_to_updated_at = to_updated_at;
250
251    let uri_str = format!("{}/api/public/v2/prompts", configuration.base_path);
252    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
253
254    if let Some(ref param_value) = p_query_name {
255        req_builder = req_builder.query(&[("name", &param_value.to_string())]);
256    }
257    if let Some(ref param_value) = p_query_label {
258        req_builder = req_builder.query(&[("label", &param_value.to_string())]);
259    }
260    if let Some(ref param_value) = p_query_tag {
261        req_builder = req_builder.query(&[("tag", &param_value.to_string())]);
262    }
263    if let Some(ref param_value) = p_query_page {
264        req_builder = req_builder.query(&[("page", &param_value.to_string())]);
265    }
266    if let Some(ref param_value) = p_query_limit {
267        req_builder = req_builder.query(&[("limit", &param_value.to_string())]);
268    }
269    if let Some(ref param_value) = p_query_from_updated_at {
270        req_builder = req_builder.query(&[("fromUpdatedAt", &param_value.to_string())]);
271    }
272    if let Some(ref param_value) = p_query_to_updated_at {
273        req_builder = req_builder.query(&[("toUpdatedAt", &param_value.to_string())]);
274    }
275    if let Some(ref user_agent) = configuration.user_agent {
276        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
277    }
278    if let Some(ref auth_conf) = configuration.basic_auth {
279        req_builder = req_builder.basic_auth(auth_conf.0.to_owned(), auth_conf.1.to_owned());
280    };
281
282    let req = req_builder.build()?;
283    let resp = configuration.client.execute(req).await?;
284
285    let status = resp.status();
286    let content_type = resp
287        .headers()
288        .get("content-type")
289        .and_then(|v| v.to_str().ok())
290        .unwrap_or("application/octet-stream");
291    let content_type = super::ContentType::from(content_type);
292
293    if !status.is_client_error() && !status.is_server_error() {
294        let content = resp.text().await?;
295        match content_type {
296            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
297            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::PromptMetaListResponse`"))),
298            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`")))),
299        }
300    } else {
301        let content = resp.text().await?;
302        let entity: Option<PromptsListError> = serde_json::from_str(&content).ok();
303        Err(Error::ResponseError(ResponseContent {
304            status,
305            content,
306            entity,
307        }))
308    }
309}