use futures_core::Stream;
use crate::llm::{input_message, ChatGptClient, LlmError, ResponseStreamEvent};
const SYSTEM_PROMPT: &str = "\
You are a Path of Exile 2 build analysis assistant. The user has uploaded \
their Path of Building export and you have access to their parsed build stats.\n\
\n\
Answer the user's questions about their build. Be specific and reference \
actual numbers from the build data when relevant. If the data doesn't contain \
enough information to answer, say so.\n\
\n\
Here is the parsed build data:\n";
pub struct QueryAgent {
llm: ChatGptClient,
}
impl QueryAgent {
pub fn new(llm: ChatGptClient) -> Self {
Self { llm }
}
pub fn respond(
&self,
build_json: &[u8],
message: &str,
) -> impl Stream<Item = Result<String, LlmError>> + Send {
let instructions = format!(
"{SYSTEM_PROMPT}```json\n{}\n```",
String::from_utf8_lossy(build_json),
);
let input = vec![input_message("user", message)];
let stream = self
.llm
.create_response_stream(&input, Some(&instructions), None, None);
async_stream::stream! {
tokio::pin!(stream);
while let Some(event) = futures_lite::StreamExt::next(&mut stream).await {
match event {
Ok(ResponseStreamEvent::TextDelta(t)) => yield Ok(t),
Err(e) => { yield Err(e); return; }
_ => {} }
}
}
}
}