fix: Face processor now saves status and total_faces to face.json
This commit is contained in:
@@ -73,6 +73,14 @@ struct ChatRequest {
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct ChatResponse {
|
||||
pub choices: Vec<Choice>,
|
||||
pub usage: Option<Usage>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct Usage {
|
||||
pub prompt_tokens: u32,
|
||||
pub completion_tokens: u32,
|
||||
pub total_tokens: u32,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
@@ -87,6 +95,20 @@ pub enum LlmResponse {
|
||||
ToolCalls(Vec<ToolCall>),
|
||||
}
|
||||
|
||||
impl LlmResponse {
|
||||
pub fn usage(&self) -> Option<(u32, u32, u32)> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Full LLM response with usage
|
||||
pub struct LlmResponseWithUsage {
|
||||
pub response: LlmResponse,
|
||||
pub input_tokens: u32,
|
||||
pub output_tokens: u32,
|
||||
pub total_tokens: u32,
|
||||
}
|
||||
|
||||
/// Get the LLM chat URL from centralized config
|
||||
pub fn llm_chat_url() -> String {
|
||||
config::llm::CHAT_URL.clone()
|
||||
@@ -241,6 +263,76 @@ pub async fn call_llm(
|
||||
}
|
||||
}
|
||||
|
||||
/// Call Gemma4 with messages and optional tools. Returns response with usage.
|
||||
pub async fn call_llm_with_usage(
|
||||
messages: Vec<ChatMessage>,
|
||||
tools: Option<Vec<ToolDef>>,
|
||||
max_tokens: u32,
|
||||
timeout_secs: u64,
|
||||
) -> anyhow::Result<LlmResponseWithUsage> {
|
||||
let timeout = if timeout_secs > 0 {
|
||||
timeout_secs
|
||||
} else {
|
||||
*config::llm::CHAT_TIMEOUT_SECS
|
||||
};
|
||||
|
||||
let start = std::time::Instant::now();
|
||||
|
||||
let req = ChatRequest {
|
||||
model: llm_model(),
|
||||
messages,
|
||||
temperature: 0.1,
|
||||
max_tokens,
|
||||
stream: false,
|
||||
tools,
|
||||
};
|
||||
|
||||
let res = LLM_CLIENT
|
||||
.post(&llm_chat_url())
|
||||
.json(&req)
|
||||
.timeout(std::time::Duration::from_secs(timeout))
|
||||
.send()
|
||||
.await?;
|
||||
|
||||
if !res.status().is_success() {
|
||||
let text = res.text().await.unwrap_or_default();
|
||||
anyhow::bail!("LLM API error: {}", text);
|
||||
}
|
||||
|
||||
let chat_res: ChatResponse = res.json().await?;
|
||||
let choice = chat_res
|
||||
.choices
|
||||
.into_iter()
|
||||
.next()
|
||||
.ok_or_else(|| anyhow::anyhow!("Empty LLM response"))?;
|
||||
|
||||
let usage = chat_res.usage.unwrap_or(Usage {
|
||||
prompt_tokens: 0,
|
||||
completion_tokens: 0,
|
||||
total_tokens: 0,
|
||||
});
|
||||
|
||||
let 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")
|
||||
})?;
|
||||
LlmResponse::ToolCalls(calls)
|
||||
}
|
||||
_ => {
|
||||
let content = choice.message.content.unwrap_or_default();
|
||||
LlmResponse::Text(content.trim().to_string())
|
||||
}
|
||||
};
|
||||
|
||||
Ok(LlmResponseWithUsage {
|
||||
response,
|
||||
input_tokens: usage.prompt_tokens,
|
||||
output_tokens: usage.completion_tokens,
|
||||
total_tokens: usage.total_tokens,
|
||||
})
|
||||
}
|
||||
|
||||
/// Helper to build the system prompt + user messages
|
||||
pub fn build_conversation(
|
||||
system_prompt: &str,
|
||||
|
||||
@@ -172,7 +172,7 @@ Include every chunk number exactly once. Only respond with the JSON."#,
|
||||
}
|
||||
}
|
||||
|
||||
warn!(
|
||||
warn!(
|
||||
"LLM rerank: could not parse response — content: {}",
|
||||
&content[..content.len().min(200)]
|
||||
);
|
||||
@@ -187,14 +187,14 @@ mod tests {
|
||||
fn test_utf8_truncation_safe() {
|
||||
let long_chinese = "這是一段很長的中文文字,用來測試字元截斷是否正確運作,避免在多位元組字元中間切割造成恐慌。".repeat(3);
|
||||
let text = long_chinese.as_str();
|
||||
|
||||
|
||||
let display = if text.chars().count() > 100 {
|
||||
let truncated: String = text.chars().take(100).collect();
|
||||
format!("{}...", truncated)
|
||||
} else {
|
||||
text.to_string()
|
||||
};
|
||||
|
||||
|
||||
assert!(display.chars().count() <= 103);
|
||||
assert!(display.starts_with("這是一段很長"));
|
||||
assert!(display.ends_with("..."));
|
||||
@@ -209,7 +209,7 @@ mod tests {
|
||||
} else {
|
||||
short_text.to_string()
|
||||
};
|
||||
|
||||
|
||||
assert_eq!(display, "短文字");
|
||||
assert!(!display.ends_with("..."));
|
||||
}
|
||||
@@ -223,7 +223,7 @@ mod tests {
|
||||
} else {
|
||||
mixed.to_string()
|
||||
};
|
||||
|
||||
|
||||
assert!(display.chars().count() <= 103);
|
||||
assert!(display.ends_with("..."));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user