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_get`]
29#[derive(Debug, Clone, Serialize, Deserialize)]
30#[serde(untagged)]
31pub enum PromptsGetError {
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_list`]
41#[derive(Debug, Clone, Serialize, Deserialize)]
42#[serde(untagged)]
43pub enum PromptsListError {
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/// Create a new version for the prompt with the given `name`
53#[bon::builder]
54pub async fn prompts_create(
55    configuration: &configuration::Configuration,
56    create_prompt_request: models::CreatePromptRequest,
57) -> Result<models::Prompt, Error<PromptsCreateError>> {
58    // add a prefix to parameters to efficiently prevent name collisions
59    let p_body_create_prompt_request = create_prompt_request;
60
61    let uri_str = format!("{}/api/public/v2/prompts", configuration.base_path);
62    let mut req_builder = configuration
63        .client
64        .request(reqwest::Method::POST, &uri_str);
65
66    if let Some(ref user_agent) = configuration.user_agent {
67        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
68    }
69    if let Some(ref auth_conf) = configuration.basic_auth {
70        req_builder = req_builder.basic_auth(auth_conf.0.to_owned(), auth_conf.1.to_owned());
71    };
72    req_builder = req_builder.json(&p_body_create_prompt_request);
73
74    let req = req_builder.build()?;
75    let resp = configuration.client.execute(req).await?;
76
77    let status = resp.status();
78    let content_type = resp
79        .headers()
80        .get("content-type")
81        .and_then(|v| v.to_str().ok())
82        .unwrap_or("application/octet-stream");
83    let content_type = super::ContentType::from(content_type);
84
85    if !status.is_client_error() && !status.is_server_error() {
86        let content = resp.text().await?;
87        match content_type {
88            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
89            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::Prompt`"))),
90            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`")))),
91        }
92    } else {
93        let content = resp.text().await?;
94        let entity: Option<PromptsCreateError> = serde_json::from_str(&content).ok();
95        Err(Error::ResponseError(ResponseContent {
96            status,
97            content,
98            entity,
99        }))
100    }
101}
102
103/// Get a prompt
104#[bon::builder]
105pub async fn prompts_get(
106    configuration: &configuration::Configuration,
107    prompt_name: &str,
108    version: Option<i32>,
109    label: Option<&str>,
110) -> Result<models::Prompt, Error<PromptsGetError>> {
111    // add a prefix to parameters to efficiently prevent name collisions
112    let p_path_prompt_name = prompt_name;
113    let p_query_version = version;
114    let p_query_label = label;
115
116    let uri_str = format!(
117        "{}/api/public/v2/prompts/{promptName}",
118        configuration.base_path,
119        promptName = crate::apis::urlencode(p_path_prompt_name)
120    );
121    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
122
123    if let Some(ref param_value) = p_query_version {
124        req_builder = req_builder.query(&[("version", &param_value.to_string())]);
125    }
126    if let Some(ref param_value) = p_query_label {
127        req_builder = req_builder.query(&[("label", &param_value.to_string())]);
128    }
129    if let Some(ref user_agent) = configuration.user_agent {
130        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
131    }
132    if let Some(ref auth_conf) = configuration.basic_auth {
133        req_builder = req_builder.basic_auth(auth_conf.0.to_owned(), auth_conf.1.to_owned());
134    };
135
136    let req = req_builder.build()?;
137    let resp = configuration.client.execute(req).await?;
138
139    let status = resp.status();
140    let content_type = resp
141        .headers()
142        .get("content-type")
143        .and_then(|v| v.to_str().ok())
144        .unwrap_or("application/octet-stream");
145    let content_type = super::ContentType::from(content_type);
146
147    if !status.is_client_error() && !status.is_server_error() {
148        let content = resp.text().await?;
149        match content_type {
150            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
151            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::Prompt`"))),
152            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`")))),
153        }
154    } else {
155        let content = resp.text().await?;
156        let entity: Option<PromptsGetError> = serde_json::from_str(&content).ok();
157        Err(Error::ResponseError(ResponseContent {
158            status,
159            content,
160            entity,
161        }))
162    }
163}
164
165/// Get a list of prompt names with versions and labels
166#[bon::builder]
167pub async fn prompts_list(
168    configuration: &configuration::Configuration,
169    name: Option<&str>,
170    label: Option<&str>,
171    tag: Option<&str>,
172    page: Option<i32>,
173    limit: Option<i32>,
174    from_updated_at: Option<String>,
175    to_updated_at: Option<String>,
176) -> Result<models::PromptMetaListResponse, Error<PromptsListError>> {
177    // add a prefix to parameters to efficiently prevent name collisions
178    let p_query_name = name;
179    let p_query_label = label;
180    let p_query_tag = tag;
181    let p_query_page = page;
182    let p_query_limit = limit;
183    let p_query_from_updated_at = from_updated_at;
184    let p_query_to_updated_at = to_updated_at;
185
186    let uri_str = format!("{}/api/public/v2/prompts", configuration.base_path);
187    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
188
189    if let Some(ref param_value) = p_query_name {
190        req_builder = req_builder.query(&[("name", &param_value.to_string())]);
191    }
192    if let Some(ref param_value) = p_query_label {
193        req_builder = req_builder.query(&[("label", &param_value.to_string())]);
194    }
195    if let Some(ref param_value) = p_query_tag {
196        req_builder = req_builder.query(&[("tag", &param_value.to_string())]);
197    }
198    if let Some(ref param_value) = p_query_page {
199        req_builder = req_builder.query(&[("page", &param_value.to_string())]);
200    }
201    if let Some(ref param_value) = p_query_limit {
202        req_builder = req_builder.query(&[("limit", &param_value.to_string())]);
203    }
204    if let Some(ref param_value) = p_query_from_updated_at {
205        req_builder = req_builder.query(&[("fromUpdatedAt", &param_value.to_string())]);
206    }
207    if let Some(ref param_value) = p_query_to_updated_at {
208        req_builder = req_builder.query(&[("toUpdatedAt", &param_value.to_string())]);
209    }
210    if let Some(ref user_agent) = configuration.user_agent {
211        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
212    }
213    if let Some(ref auth_conf) = configuration.basic_auth {
214        req_builder = req_builder.basic_auth(auth_conf.0.to_owned(), auth_conf.1.to_owned());
215    };
216
217    let req = req_builder.build()?;
218    let resp = configuration.client.execute(req).await?;
219
220    let status = resp.status();
221    let content_type = resp
222        .headers()
223        .get("content-type")
224        .and_then(|v| v.to_str().ok())
225        .unwrap_or("application/octet-stream");
226    let content_type = super::ContentType::from(content_type);
227
228    if !status.is_client_error() && !status.is_server_error() {
229        let content = resp.text().await?;
230        match content_type {
231            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
232            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::PromptMetaListResponse`"))),
233            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`")))),
234        }
235    } else {
236        let content = resp.text().await?;
237        let entity: Option<PromptsListError> = serde_json::from_str(&content).ok();
238        Err(Error::ResponseError(ResponseContent {
239            status,
240            content,
241            entity,
242        }))
243    }
244}