diff --git a/src/lib.rs b/src/lib.rs index 8928163..86c01a3 100644 --- a/src/lib.rs +++ b/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, +} + +#[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> { @@ -23,5 +57,15 @@ pub async fn ask(prompt: &str) -> Result> { .body(body)? .send()?; - return Ok(request.text()?); + let result = request.text()?; + + let mut deltas: Vec = 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()); } \ No newline at end of file