baichat-rs/src/lib.rs
2023-06-17 16:37:32 +02:00

76 lines
2.0 KiB
Rust

use ratmom::{prelude::*, Request};
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize, Debug)]
pub struct Delta {
pub role: String,
pub id: String,
#[serde(rename(serialize = "parentMessageId", deserialize = "parentMessageId"))]
pub parent_message_id: String,
pub text: String,
pub delta: String,
pub detail: Detail,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Detail {
pub id: String,
pub object: String,
pub created: i64,
pub model: String,
pub choices: Vec<Choice>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Choice {
pub delta: DeltaChoice,
pub index: i64,
pub finish_reason: Option<String>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct DeltaChoice {
pub content: String,
}
pub async fn ask(prompt: &str) -> Result<Vec<Delta>, Box<dyn std::error::Error>> {
let mut body = String::new();
body.push_str(r#"{
"prompt": ""#);
body.push_str(&prompt);
body.push_str(r#"",
"options": {
"parentMessageId": "8c00bd29-75b0-42c7-9d4f-05a94ac8b2de"
}
}"#);
let mut request = Request::builder()
.method("POST")
.uri("https://chatbot.theb.ai/api/chat-process")
.header("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/112.0")
.header("Accept-Language", "en-US,en;q=0.5")
.header("Content-Type", "application/json")
.header("Host", "chatbot.theb.ai")
.header("Referer", "https://chatbot.theb.ai")
.header("Origin", "https://chatbot.theb.ai")
.body(body)?
.send()?;
let result = request.text()?;
let mut deltas: Vec<Delta> = Vec::new();
for line in result.lines() {
if line == "" {
continue;
}
println!("{}", line);
println!("------------------");
let delta: Delta = serde_json::from_str(line).unwrap();
deltas.push(delta);
}
println!("{:?}", deltas);
return Ok(deltas);
}