This commit is contained in:
@@ -1,14 +1,20 @@
|
||||
use std::{
|
||||
io::{BufRead, BufReader},
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::editors::context_editor::ProjectContext;
|
||||
|
||||
pub fn continue_content(
|
||||
context: &str,
|
||||
previous_content: &str,
|
||||
instruction: &str,
|
||||
context: String,
|
||||
previous_content: String,
|
||||
instruction: String,
|
||||
max_tokens: usize,
|
||||
project: &ProjectContext,
|
||||
) -> Result<String, Box<dyn std::error::Error>> {
|
||||
project: ProjectContext,
|
||||
result: Arc<Mutex<String>>,
|
||||
) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let client = reqwest::blocking::Client::new();
|
||||
|
||||
let messages = vec![
|
||||
@@ -39,7 +45,7 @@ pub fn continue_content(
|
||||
messages,
|
||||
temperature: 0.7,
|
||||
max_tokens,
|
||||
stream: false,
|
||||
stream: true,
|
||||
};
|
||||
|
||||
let response = client
|
||||
@@ -47,17 +53,30 @@ pub fn continue_content(
|
||||
.json(&request)
|
||||
.send()?;
|
||||
|
||||
if !response.status().is_success() {
|
||||
return Err(format!("Request failed: {}", response.text()?).into());
|
||||
let reader = BufReader::new(response);
|
||||
|
||||
for line in reader.lines() {
|
||||
let line = line?;
|
||||
if line == "data: [DONE]" {
|
||||
break;
|
||||
}
|
||||
if let Some(json) = line.strip_prefix("data: ") {
|
||||
if let Ok(chunk) = serde_json::from_str::<StreamingChatResponse>(json) {
|
||||
if let Some(content) = chunk.choices[0].delta.content.as_ref() {
|
||||
result.lock().unwrap().push_str(content);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let response: ChatResponse = response.json()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
if let Some(choice) = response.choices.into_iter().next() {
|
||||
Ok(choice.message.content)
|
||||
} else {
|
||||
Err("No response from model".into())
|
||||
}
|
||||
#[derive(Debug, PartialEq, Clone, Copy)]
|
||||
pub enum ReadyState {
|
||||
Idle,
|
||||
Generating,
|
||||
Ready,
|
||||
}
|
||||
|
||||
// Simple request structure
|
||||
@@ -69,6 +88,27 @@ struct ChatRequest {
|
||||
stream: bool,
|
||||
}
|
||||
|
||||
// Streaming response structures
|
||||
#[derive(Deserialize, Debug)]
|
||||
struct StreamingChatResponse {
|
||||
choices: Vec<StreamingChoice>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
struct StreamingChoice {
|
||||
delta: Delta,
|
||||
#[serde(default)]
|
||||
finish_reason: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
struct Delta {
|
||||
#[serde(default)]
|
||||
role: Option<String>,
|
||||
#[serde(default)]
|
||||
content: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
struct Message {
|
||||
role: String,
|
||||
|
||||
Reference in New Issue
Block a user