Ragas Ragas
stable · 中文译文
中文译文 · 原文:https://docs.ragas.io/en/stable/concepts/components/eval_sample/ · 许可证 Apache-2.0

Evaluation Sample(评测样本)

评测样本是一个结构化的数据实例,用于在特定场景中评估和衡量你的 LLM 应用性能。它代表一次交互单元,或 AI 应用预期要处理的一个具体用例。在 Ragas 中,评测样本用 SingleTurnSample 和 MultiTurnSample 类表示。

SingleTurnSample

SingleTurnSample 表示用户、LLM 与评测期望结果之间的单轮交互。它适合涉及单个问答对的评测,可能还带有额外的上下文或参考信息。

示例

以下示例演示如何创建 SingleTurnSample 实例,用于评测基于 RAG 的应用中的单轮交互。在此场景中,用户提出一个问题,AI 给出答案。我们将创建一个 SingleTurnSample 实例来表示这次交互,包括检索到的上下文、参考答案和评测评分量表。

from ragas import SingleTurnSample

# User's question
user_input = "What is the capital of France?"

# Retrieved contexts (e.g., from a knowledge base or search engine)
retrieved_contexts = ["Paris is the capital and most populous city of France."]

# AI's response
response = "The capital of France is Paris."

# Reference answer (ground truth)
reference = "Paris"

# Evaluation rubric
rubric = {
    "accuracy": "Correct",
    "completeness": "High",
    "fluency": "Excellent"
}

# Create the SingleTurnSample instance
sample = SingleTurnSample(
    user_input=user_input,
    retrieved_contexts=retrieved_contexts,
    response=response,
    reference=reference,
    rubric=rubric
)

MultiTurnSample

MultiTurnSample 表示 Human、AI 以及可选 Tool 与评测期望结果之间的多轮交互。它适合在更复杂的交互中表示对话式智能体以进行评测。在 MultiTurnSample 中,user_input 属性表示共同构成人类用户与 AI 系统多轮对话的一组消息。这些消息是 HumanMessage、AIMessage 和 ToolMessage 类的实例。

示例

以下示例演示如何创建 MultiTurnSample 实例以评测多轮交互。在此场景中,用户想知道纽约市当前天气。AI 助手将使用天气 API 工具获取信息并回复用户。

from ragas.messages import HumanMessage, AIMessage, ToolMessage, ToolCall

# User asks about the weather in New York City
user_message = HumanMessage(content="What's the weather like in New York City today?")

# AI decides to use a weather API tool to fetch the information
ai_initial_response = AIMessage(
    content="Let me check the current weather in New York City for you.",
    tool_calls=[ToolCall(name="WeatherAPI", args={"location": "New York City"})]
)

# Tool provides the weather information
tool_response = ToolMessage(content="It's sunny with a temperature of 75°F in New York City.")

# AI delivers the final response to the user
ai_final_response = AIMessage(content="It's sunny and 75 degrees Fahrenheit in New York City today.")

# Combine all messages into a list to represent the conversation
conversation = [
    user_message,
    ai_initial_response,
    tool_response,
    ai_final_response
]

现在,用这段对话创建 MultiTurnSample 对象,包括任何参考回复和评测评分量表。

from ragas import MultiTurnSample
# Reference response for evaluation purposes
reference_response = "Provide the current weather in New York City to the user."


# Create the MultiTurnSample instance
sample = MultiTurnSample(
    user_input=conversation,
    reference=reference_response,
)