openai_interface/chat/
update.rs1pub mod request {
7 use std::collections::HashMap;
8
9 use serde::Serialize;
10
11 use crate::rest::post::{Post, PostNoStream};
12
13 #[derive(Debug, Serialize, Default, Clone)]
19 pub struct ChatUpdate<'a> {
20 #[serde(skip_serializing)]
22 pub completion_id: &'a str,
23 #[serde(skip_serializing_if = "Option::is_none")]
25 pub extra_body_map: Option<serde_json::Map<String, serde_json::Value>>,
26 #[serde(skip_serializing)]
28 pub extra_query: HashMap<&'a str, &'a str>,
29 pub metadata: Option<HashMap<&'a str, &'a str>>,
36 }
37
38 impl ChatUpdate<'_> {
39 pub fn build_url(base_url: impl AsRef<str>, completion_id: impl AsRef<str>) -> String {
40 let mut url = url::Url::parse(base_url.as_ref()).expect("Invalid base URL");
41 url.path_segments_mut()
42 .expect("Cannot modify URL path")
43 .push("chat")
44 .push("completions")
45 .push(completion_id.as_ref());
46 url.to_string()
47 }
48 }
49
50 impl Post for ChatUpdate<'_> {
51 fn is_streaming(&self) -> bool {
52 false
53 }
54
55 fn build_url(&self, base_url: &str) -> Result<String, crate::errors::OapiError> {
59 let mut url = url::Url::parse(base_url.trim_end_matches('/'))
60 .map_err(crate::errors::OapiError::UrlError)?;
61 url.path_segments_mut()
62 .map_err(|_| crate::errors::OapiError::UrlCannotBeBase(base_url.to_string()))?
63 .push("chat")
64 .push("completions")
65 .push(self.completion_id);
66
67 for (key, value) in &self.extra_query {
68 url.query_pairs_mut().append_pair(key, value);
69 }
70
71 Ok(url.to_string())
72 }
73 }
74
75 impl PostNoStream for ChatUpdate<'_> {
76 type Response = crate::chat::ChatCompletion;
77 }
78}