feat: add parent message id

This commit is contained in:
0xMRTT 2023-06-17 17:08:59 +02:00
parent 80eabb9f9f
commit bf5fb5df49
Signed by: 0xMRTT
GPG Key ID: 910B287304120902

View File

@ -34,17 +34,46 @@ pub struct DeltaChoice {
pub content: String,
}
pub async fn ask(prompt: &str) -> Result<Vec<Delta>, Box<dyn std::error::Error>> {
struct ThebAI {
pub parent_message_id: Option<String>,
}
impl ThebAI {
pub fn new(parent_message_id: Option<&str>) -> ThebAI {
if let Some(parent_message_id) = parent_message_id {
return ThebAI {
parent_message_id: Some(parent_message_id.to_string()),
}
} else {
return ThebAI {
parent_message_id: String::from("8c00bd29-75b0-42c7-9d4f-05a94ac8b2de"),
}
}
}
pub async fn ask(&self, prompt: &str, parent_message_id: Option<String>) -> Result<Vec<Delta>, Box<dyn std::error::Error>> {
let mut body = String::new();
body.push_str(r#"{
"prompt": ""#);
body.push_str(&prompt);
if let Some(parent_message_id) = parent_message_id {
body.push_str(r#"",
"options": {
"parentMessageId": "8c00bd29-75b0-42c7-9d4f-05a94ac8b2de"
"parentMessageId": ""#);
body.push_str(parent_message_id.as_str());
body.push_str(r#""
}
}"#);
} else {
body.push_str(r#"",
"options": {
"parentMessageId": ""#);
body.push_str(self.parent_message_id.as_ref().unwrap().as_str());
body.push_str(r#""
}
}"#);
}
let mut request = Request::builder()
.method("POST")
.uri("https://chatbot.theb.ai/api/chat-process")
@ -64,13 +93,16 @@ pub async fn ask(prompt: &str) -> Result<Vec<Delta>, Box<dyn std::error::Error>>
if line == "" {
continue;
}
println!("{}", line);
println!("------------------");
let delta: Delta = serde_json::from_str(line).unwrap();
deltas.push(delta);
}
self.parent_message_id = Some(deltas.last().unwrap().id.clone());
println!("{:?}", deltas);
return Ok(deltas);
}
}