用Ragas模板快速评估提示词的情感分类效果
该快速入门介绍如何用ragas的prompt_evals模板搭建提示词评估项目,通过情感分析任务比较不同提示词变体,并以二元准确率作为评估指标,同时说明如何自定义提示词和增加数值型指标。
## 即时评价快速启动 那个... `prompt_evals` 模板评价和比较不同的即时变化与情绪分析。 ## 创建工程 ``` ragas quickstart prompt_evals cd prompt_evals ``` ## 安装依赖关系 ``` uv sync ``` ## 设定您的 API 密钥 ``` export OPENAI_API_KEY="your-openai-key" ``` ## 运行评价 ``` uv run python evals.py ``` ## 项目结构 ``` prompt_evals/ ├── README.md # Project documentation ├── pyproject.toml # Project configuration ├── prompt.py # Prompt implementation ├── evals.py # Evaluation workflow ├── __init__.py # Python package marker └── evals/ ├── datasets/ # Test datasets ├── experiments/ # Evaluation results └── logs/ # Execution logs ``` ## 评估什么 该模板评估情绪分类的迅速有效性: - **任务**: 感官分析(阳性/阴性) - **测试案例**: 带有预期情绪标签的电影评论 - **度量衡**: 二进制精度(通过/失败) ## 理解守则 ### 即时电讯( E)`prompt.py`) 执行情绪分析提示 : ``` from prompt import run_prompt sentiment = run_prompt("I loved the movie! It was fantastic.") # Returns: "positive" or "negative" ``` ### 评价`evals.py`) 测试的及时准确性 : ``` @discrete_metric(name="accuracy", allowed_values=["pass", "fail"]) def my_metric(prediction: str, actual: str): return ( MetricResult(value="pass", reason="") if prediction == actual else MetricResult(value="fail", reason="") ) ``` ## 测试数据 该数据集包括电影评论: ``` dataset_dict = [ {"text": "I loved the movie! It was fantastic.", "label": "positive"}, {"text": "The movie was terrible and boring.", "label": "negative"}, # More examples... ] ``` ## 自定义 ### 测试不同的提示 修改 `prompt.py` 测试变量 : ``` # Version 1: Simple prompt = f"Is this positive or negative: {text}" # Version 2: With examples prompt = f"""Classify sentiment: Examples: - "Great movie" -> positive - "Boring film" -> negative Text: {text} Sentiment:""" # Compare results across versions ``` ### 添加更多度量衡 评估其他方面: ``` from ragas.metrics import NumericalMetric confidence = NumericalMetric( name="confidence", prompt="Rate confidence 1-5 in this classification: {prediction}", allowed_values=(1, 5), ) ``` ## 下一个步骤 - [法官对齐](<https://docs.ragas.io/en/stable/howtos/cli/judge_alignment/>) - 衡量 LLM 即时裁判的校正 - [LLM 基准制定](<https://docs.ragas.io/en/stable/howtos/cli/benchmark_llm/>) - 比较不同的模型 1月 2, 2026 1月 2, 2026 GitHub
查看原始来源