feat: ASRX hybrid pipeline, identity history, worker fixes, checkpoint system
This commit is contained in:
@@ -97,6 +97,68 @@ pub fn llm_vision_model() -> String {
|
||||
config::llm::VISION_MODEL.clone()
|
||||
}
|
||||
|
||||
/// Call the vision LLM with text + base64 images. Returns the generated text.
|
||||
pub async fn call_llm_vision(
|
||||
system_prompt: &str,
|
||||
user_text: &str,
|
||||
base64_images: Vec<String>,
|
||||
max_tokens: u32,
|
||||
timeout_secs: u64,
|
||||
) -> anyhow::Result<String> {
|
||||
let mut content_parts: Vec<Value> = vec![json!({"type": "text", "text": user_text})];
|
||||
for img in &base64_images {
|
||||
content_parts.push(json!({
|
||||
"type": "image_url",
|
||||
"image_url": {"url": format!("data:image/jpeg;base64,{}", img)}
|
||||
}));
|
||||
}
|
||||
|
||||
let messages = json!([
|
||||
{"role": "system", "content": system_prompt},
|
||||
{"role": "user", "content": content_parts}
|
||||
]);
|
||||
|
||||
let req = json!({
|
||||
"model": llm_vision_model(),
|
||||
"messages": messages,
|
||||
"temperature": 0.1,
|
||||
"max_tokens": max_tokens,
|
||||
"stream": false,
|
||||
});
|
||||
|
||||
let client = reqwest::Client::builder()
|
||||
.timeout(std::time::Duration::from_secs(timeout_secs))
|
||||
.build()?;
|
||||
|
||||
let res = client.post(&llm_vision_url()).json(&req).send().await?;
|
||||
if !res.status().is_success() {
|
||||
let text = res.text().await.unwrap_or_default();
|
||||
anyhow::bail!("Vision LLM API error: {}", text);
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct VisionResponse {
|
||||
choices: Vec<VisionChoice>,
|
||||
}
|
||||
#[derive(Deserialize)]
|
||||
struct VisionChoice {
|
||||
message: VisionMessage,
|
||||
}
|
||||
#[derive(Deserialize)]
|
||||
struct VisionMessage {
|
||||
content: Option<String>,
|
||||
}
|
||||
|
||||
let vision_res: VisionResponse = res.json().await?;
|
||||
let content = vision_res
|
||||
.choices
|
||||
.into_iter()
|
||||
.next()
|
||||
.and_then(|c| c.message.content)
|
||||
.unwrap_or_default();
|
||||
Ok(content.trim().to_string())
|
||||
}
|
||||
|
||||
/// Build a tool definition JSON for function calling
|
||||
pub fn make_tool(name: &str, description: &str, properties: Value, required: Vec<&str>) -> ToolDef {
|
||||
ToolDef {
|
||||
@@ -121,9 +183,11 @@ pub async fn call_llm(
|
||||
timeout_secs: u64,
|
||||
) -> anyhow::Result<LlmResponse> {
|
||||
let client = reqwest::Client::builder()
|
||||
.timeout(std::time::Duration::from_secs(
|
||||
if timeout_secs > 0 { timeout_secs } else { *config::llm::CHAT_TIMEOUT_SECS },
|
||||
))
|
||||
.timeout(std::time::Duration::from_secs(if timeout_secs > 0 {
|
||||
timeout_secs
|
||||
} else {
|
||||
*config::llm::CHAT_TIMEOUT_SECS
|
||||
}))
|
||||
.build()?;
|
||||
|
||||
let req = ChatRequest {
|
||||
@@ -135,11 +199,7 @@ pub async fn call_llm(
|
||||
tools,
|
||||
};
|
||||
|
||||
let res = client
|
||||
.post(&llm_chat_url())
|
||||
.json(&req)
|
||||
.send()
|
||||
.await?;
|
||||
let res = client.post(&llm_chat_url()).json(&req).send().await?;
|
||||
|
||||
if !res.status().is_success() {
|
||||
let text = res.text().await.unwrap_or_default();
|
||||
@@ -147,13 +207,17 @@ pub async fn call_llm(
|
||||
}
|
||||
|
||||
let chat_res: ChatResponse = res.json().await?;
|
||||
let choice = chat_res.choices.into_iter().next()
|
||||
let choice = chat_res
|
||||
.choices
|
||||
.into_iter()
|
||||
.next()
|
||||
.ok_or_else(|| anyhow::anyhow!("Empty LLM response"))?;
|
||||
|
||||
match choice.finish_reason.as_deref() {
|
||||
Some("tool_calls") => {
|
||||
let calls = choice.message.tool_calls
|
||||
.ok_or_else(|| anyhow::anyhow!("finish_reason=tool_calls but no tool_calls in message"))?;
|
||||
let calls = choice.message.tool_calls.ok_or_else(|| {
|
||||
anyhow::anyhow!("finish_reason=tool_calls but no tool_calls in message")
|
||||
})?;
|
||||
Ok(LlmResponse::ToolCalls(calls))
|
||||
}
|
||||
_ => {
|
||||
@@ -164,16 +228,18 @@ pub async fn call_llm(
|
||||
}
|
||||
|
||||
/// Helper to build the system prompt + user messages
|
||||
pub fn build_conversation(system_prompt: &str, user_query: &str, history: Vec<ChatMessage>) -> Vec<ChatMessage> {
|
||||
let mut messages = vec![
|
||||
ChatMessage {
|
||||
role: "system".to_string(),
|
||||
content: Some(system_prompt.to_string()),
|
||||
tool_calls: None,
|
||||
tool_call_id: None,
|
||||
name: None,
|
||||
},
|
||||
];
|
||||
pub fn build_conversation(
|
||||
system_prompt: &str,
|
||||
user_query: &str,
|
||||
history: Vec<ChatMessage>,
|
||||
) -> Vec<ChatMessage> {
|
||||
let mut messages = vec![ChatMessage {
|
||||
role: "system".to_string(),
|
||||
content: Some(system_prompt.to_string()),
|
||||
tool_calls: None,
|
||||
tool_call_id: None,
|
||||
name: None,
|
||||
}];
|
||||
// Add history (user + assistant exchanges)
|
||||
messages.extend(history);
|
||||
// Add current user query
|
||||
|
||||
Reference in New Issue
Block a user