feat: add serliaize
This commit is contained in:
parent
f0fa28e0a9
commit
a15619b8c7
46
src/lib.rs
46
src/lib.rs
@ -1,4 +1,38 @@
|
||||
use ratmom::{prelude::*, Request};
|
||||
use serde::{Serialize, Deserialize};
|
||||
|
||||
// {"role":"assistant","id":"chatcmpl-7SQEOvjjNeoy4D4IZnohylUEbnkyJ","parentMessageId":"c57a0ed4-2a1a-40b4-b57d-050fa167287a","text":"Hello","delta":"Hello","detail":{"id":"chatcmpl-7SQEOvjjNeoy4D4IZnohylUEbnkyJ","object":"chat.completion.chunk","created":1687008372,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":"Hello"},"index":0,"finish_reason":null}]}}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
struct Delta {
|
||||
pub role: String,
|
||||
pub id: String,
|
||||
pub parent_message_id: String,
|
||||
pub text: String,
|
||||
pub delta: String,
|
||||
pub detail: Detail,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
struct Detail {
|
||||
pub id: String,
|
||||
pub object: String,
|
||||
pub created: i64,
|
||||
pub model: String,
|
||||
pub choices: Vec<Choice>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
struct Choice {
|
||||
pub delta: DeltaChoice,
|
||||
pub index: i64,
|
||||
pub finish_reason: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
struct DeltaChoice {
|
||||
pub content: String,
|
||||
}
|
||||
|
||||
pub async fn ask(prompt: &str) -> Result<String, Box<dyn std::error::Error>> {
|
||||
|
||||
@ -23,5 +57,15 @@ pub async fn ask(prompt: &str) -> Result<String, Box<dyn std::error::Error>> {
|
||||
.body(body)?
|
||||
.send()?;
|
||||
|
||||
return Ok(request.text()?);
|
||||
let result = request.text()?;
|
||||
|
||||
let mut deltas: Vec<Delta> = Vec::new();
|
||||
for line in result.lines() {
|
||||
let delta: Delta = serde_json::from_str(&line)?;
|
||||
deltas.push(delta);
|
||||
}
|
||||
|
||||
println!("{:?}", deltas);
|
||||
|
||||
return Ok(deltas[-1].detail.choices[0].delta.content.clone());
|
||||
}
|
Loading…
Reference in New Issue
Block a user