use crate::SingleLineMatch;
use chatgpt::prelude::*;
use chatgpt::types::CompletionResponse;
pub const MAX_TOKENS: usize = 4096;
pub const INITIAL_PROMPT: &str =
"Which line in the log file below is the clearest explanation of a problem:\n\n";
pub async fn analyze(
chatgpt_key: String,
lines: Vec<&str>,
) -> std::result::Result<Option<SingleLineMatch>, Box<dyn std::error::Error + Send + Sync>> {
let client = ChatGPT::new(chatgpt_key)?;
let mut truncated: Vec<&str> = lines
.iter()
.rev()
.take_while(|line| line.len() < MAX_TOKENS - INITIAL_PROMPT.len())
.copied()
.collect();
truncated.reverse();
let prompt = format!("{}{}", INITIAL_PROMPT, truncated.join("\n"));
let response: CompletionResponse = client.send_message(&prompt).await?;
let text = &response.message().content;
for (i, line) in lines.iter().enumerate().rev() {
if line.starts_with(text) {
return Ok(Some(SingleLineMatch::from_lines(
&lines,
i,
Some("chatgpt"),
)));
}
}
log::debug!("Unable to find chatgpt answer in lines: {:?}", text);
Ok(None)
}