feat: add serliaize

This commit is contained in:
0xMRTT 2023-06-17 15:31:54 +02:00
parent f0fa28e0a9
commit a15619b8c7
Signed by: 0xMRTT
GPG Key ID: 910B287304120902
1 changed files with 45 additions and 1 deletions

View File

@ -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>> {
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)?;
println!("{:?}", deltas);
return Ok(deltas[-1].detail.choices[0].delta.content.clone());