Agnes 2.0-flash是推理模型,推理token占大量预算。 800仍不够→JSON截断(parse error: unexpected end of JSON input)。 1200确保推理+正文都够用。
80 lines
2.2 KiB
Rust
80 lines
2.2 KiB
Rust
// Rerank 重排管线 — 外部 API (模力方舟 bge-reranker-v2-m3)
|
|
// 牧尘指定:重排用外部模力方舟,不走本地推理
|
|
|
|
use reqwest::blocking::Client;
|
|
use serde::{Deserialize, Serialize};
|
|
use std::time::Duration;
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
pub struct RerankResult {
|
|
pub index: usize,
|
|
pub text: String,
|
|
pub relevance_score: f64,
|
|
}
|
|
|
|
pub struct Reranker {
|
|
endpoint: String,
|
|
model: String,
|
|
client: Client,
|
|
}
|
|
|
|
impl Reranker {
|
|
pub fn new(endpoint: &str, model: &str) -> Self {
|
|
Self {
|
|
endpoint: endpoint.to_string(),
|
|
model: model.to_string(),
|
|
client: Client::builder()
|
|
.timeout(Duration::from_secs(10))
|
|
.build()
|
|
.unwrap_or_default(),
|
|
}
|
|
}
|
|
|
|
pub fn rerank(
|
|
&self,
|
|
query: &str,
|
|
documents: &[String],
|
|
top_n: usize,
|
|
) -> Result<Vec<RerankResult>, Box<dyn std::error::Error>> {
|
|
if documents.is_empty() {
|
|
return Ok(vec![]);
|
|
}
|
|
|
|
let body = serde_json::json!({
|
|
"model": self.model,
|
|
"query": query,
|
|
"documents": documents,
|
|
"top_n": top_n,
|
|
});
|
|
|
|
let resp = self
|
|
.client
|
|
.post(&self.endpoint)
|
|
.header("Content-Type", "application/json")
|
|
.body(serde_json::to_string(&body)?)
|
|
.send()?
|
|
.text()?;
|
|
|
|
let parsed: serde_json::Value = serde_json::from_str(&resp)?;
|
|
let results: Vec<RerankResult> = parsed["results"]
|
|
.as_array()
|
|
.unwrap_or(&vec![])
|
|
.iter()
|
|
.enumerate()
|
|
.map(|(i, r)| RerankResult {
|
|
index: r["index"].as_u64().unwrap_or(i as u64) as usize,
|
|
text: r["document"]["text"].as_str().unwrap_or("").to_string(),
|
|
relevance_score: r["relevance_score"].as_f64().unwrap_or(0.0),
|
|
})
|
|
.collect();
|
|
|
|
eprintln!(
|
|
"[rerank] reranked {} docs → top {} for query: {}",
|
|
documents.len(),
|
|
results.len(),
|
|
&query[..query.len().min(40)]
|
|
);
|
|
Ok(results)
|
|
}
|
|
}
|