use async_trait::async_trait;
use crate::{BaseChatModel, Message};
use super::{EvalError, Evaluator, Score};
pub struct Faithfulness<M: BaseChatModel> {
judge: M,
llm_split: bool,
empty_score: f64,
}
fn split_claims(prediction: &str) -> Vec<String> {
prediction
.split(['。', '.', '!', '?', ';', ';', '\n'])
.map(|s| s.trim().to_string())
.filter(|s| !s.is_empty())
.collect()
}
impl<M: BaseChatModel> Faithfulness<M> {
pub fn new(judge: M) -> Self {
Self {
judge,
llm_split: false,
empty_score: 1.0,
}
}
pub fn with_llm_split(mut self, v: bool) -> Self {
self.llm_split = v;
self
}
pub fn with_empty_score(mut self, score: f64) -> Self {
self.empty_score = score;
self
}
async fn verify_claim(&self, context: &str, claim: &str) -> Result<bool, EvalError> {
let system =
"你是事实核查员。判断给定的陈述能否从参考上下文中推导出来。只输出\"是\"或\"否\"。"
.to_string();
let user = format!(
"参考上下文:\n{context}\n\n陈述:\n{claim}\n\n这条陈述能从上下文推导出来吗?"
);
let result = self
.judge
.chat_with_system(system, vec![Message::human(user)])
.await
.map_err(|e| EvalError::PredictorError(e.to_string()))?;
Ok(parse_yes_no(&result.content))
}
async fn split_claims_llm(&self, prediction: &str) -> Result<Vec<String>, EvalError> {
let system = "你是文本分析助手。把回答拆成原子陈述,每条一行,只输出陈述本身,不要编号不要解释。".to_string();
let user = format!("回答:\n{prediction}\n\n把它拆成原子陈述,每行一条:");
let result = self
.judge
.chat_with_system(system, vec![Message::human(user)])
.await
.map_err(|e| EvalError::PredictorError(e.to_string()))?;
Ok(result
.content
.lines()
.map(|s| s.trim().to_string())
.filter(|s| !s.is_empty())
.collect())
}
}
#[async_trait]
impl<M: BaseChatModel> Evaluator for Faithfulness<M> {
async fn eval(
&self,
_input: &str,
prediction: &str,
reference: &str,
) -> Result<Score, EvalError> {
let claims = if self.llm_split {
self.split_claims_llm(prediction).await?
} else {
split_claims(prediction)
};
if claims.is_empty() {
return Ok(Score::new(self.empty_score).with_label("no_claims"));
}
let futs = claims.iter().map(|claim| self.verify_claim(reference, claim));
let results = futures_util::future::join_all(futs).await;
let mut supported = 0usize;
for r in results {
if r? {
supported += 1;
}
}
let value = supported as f64 / claims.len() as f64;
Ok(Score::new(value).with_label("faithfulness"))
}
fn name(&self) -> &str {
"faithfulness"
}
}
fn parse_yes_no(raw: &str) -> bool {
let lower = raw.to_lowercase();
if lower.contains("否")
|| lower.contains("no")
|| lower.contains("不能")
|| lower.contains("不是")
|| lower.contains("false")
{
return false;
}
lower.contains("是") || lower.contains("yes") || lower.contains("能") || lower.contains("true")
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{BaseLanguageModel, LLMResult, Runnable, RunnableConfig};
use futures_util::Stream;
use std::pin::Pin;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
#[derive(Debug)]
struct JudgeError(String);
impl std::fmt::Display for JudgeError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl std::error::Error for JudgeError {}
struct SeqMockJudge {
replies: Vec<String>,
call: Arc<AtomicUsize>,
}
impl SeqMockJudge {
fn new(replies: Vec<String>) -> Self {
Self {
replies,
call: Arc::new(AtomicUsize::new(0)),
}
}
}
#[async_trait]
impl Runnable<Vec<Message>, LLMResult> for SeqMockJudge {
type Error = JudgeError;
async fn invoke(
&self,
_input: Vec<Message>,
_config: Option<RunnableConfig>,
) -> Result<LLMResult, Self::Error> {
Err(JudgeError("use chat".into()))
}
}
#[async_trait]
impl BaseLanguageModel<Vec<Message>, LLMResult> for SeqMockJudge {
fn model_name(&self) -> &str {
"seq-mock"
}
fn get_num_tokens(&self, t: &str) -> usize {
t.len()
}
fn with_temperature(self, _: f32) -> Self {
self
}
fn with_max_tokens(self, _: usize) -> Self {
self
}
}
#[async_trait]
impl BaseChatModel for SeqMockJudge {
async fn chat(
&self,
_messages: Vec<Message>,
_config: Option<RunnableConfig>,
) -> Result<LLMResult, Self::Error> {
let idx = self.call.fetch_add(1, Ordering::SeqCst);
let reply = self.replies.get(idx).cloned().unwrap_or_default();
Ok(LLMResult {
content: reply,
model: "seq-mock".to_string(),
token_usage: None,
tool_calls: None,
})
}
async fn stream_chat(
&self,
_messages: Vec<Message>,
_config: Option<RunnableConfig>,
) -> Result<Pin<Box<dyn Stream<Item = Result<String, Self::Error>> + Send>>, Self::Error>
{
Err(JudgeError("not supported".into()))
}
}
#[test]
fn test_split_claims() {
let claims = split_claims("巴黎是法国首都。伦敦是英国首都。");
assert_eq!(claims.len(), 2);
assert_eq!(claims[0], "巴黎是法国首都");
assert_eq!(claims[1], "伦敦是英国首都");
}
#[test]
fn test_split_claims_empty() {
assert!(split_claims("").is_empty());
assert!(split_claims("。。。").is_empty());
}
#[tokio::test]
async fn test_faithfulness_all_supported() {
let judge = Faithfulness::new(SeqMockJudge::new(vec!["是".into(), "是".into()]));
let s = judge
.eval("", "巴黎是法国首都。伦敦是英国首都。", "ctx")
.await
.unwrap();
assert!((s.value - 1.0).abs() < 1e-9);
}
#[tokio::test]
async fn test_faithfulness_half_supported() {
let judge = Faithfulness::new(SeqMockJudge::new(vec!["是".into(), "否".into()]));
let s = judge
.eval("", "巴黎是法国首都。伦敦是英国首都。", "ctx")
.await
.unwrap();
assert!((s.value - 0.5).abs() < 1e-9);
}
#[tokio::test]
async fn test_faithfulness_none_supported() {
let judge = Faithfulness::new(SeqMockJudge::new(vec!["否".into(), "否".into()]));
let s = judge
.eval("", "巴黎是法国首都。伦敦是英国首都。", "ctx")
.await
.unwrap();
assert!((s.value - 0.0).abs() < 1e-9);
}
#[tokio::test]
async fn test_faithfulness_empty_prediction() {
let judge = Faithfulness::new(SeqMockJudge::new(vec![]));
let s = judge.eval("", "", "ctx").await.unwrap();
assert!((s.value - 1.0).abs() < 1e-9); }
#[tokio::test]
async fn test_faithfulness_empty_score_configurable() {
let judge = Faithfulness::new(SeqMockJudge::new(vec![])).with_empty_score(0.0);
let s = judge.eval("", "", "ctx").await.unwrap();
assert!((s.value - 0.0).abs() < 1e-9);
}
#[tokio::test]
async fn test_faithfulness_llm_split() {
let judge = Faithfulness::new(SeqMockJudge::new(vec![
"巴黎是法国首都\n伦敦是英国首都".into(),
"是".into(),
"是".into(),
]))
.with_llm_split(true);
let s = judge
.eval("", "巴黎是法国首都,伦敦是英国首都。", "ctx")
.await
.unwrap();
assert!((s.value - 1.0).abs() < 1e-9);
}
#[test]
fn test_parse_yes_no() {
assert!(parse_yes_no("是"));
assert!(parse_yes_no("yes"));
assert!(!parse_yes_no("否"));
assert!(!parse_yes_no("no"));
assert!(!parse_yes_no("不是"));
assert!(!parse_yes_no("不能"));
}
}