当前位置: 首页 > news >正文

ragas官方文档中文版(六十八)

操作指南

本节中的每个指南都针对您作为有经验的用户在使用 Ragas 时可能遇到的实际问题提供了专注的解决方案。这些指南设计得简洁直接,为您的问题提供快速解决方案。我们假设您对 Ragas 的概念有基本了解且能够熟练使用。如果不是,请先浏览 快速入门 (Get Started)部分。

R2R 集成

R2R 是一款用于 AI 检索增强生成(RAG)的一体化解决方案,具备生产级功能,包括多模态内容导入、混合搜索功能、用户/文档管理等。

概述

在本教程中,我们将:

  1. 利用 R2R 的 /rag 端点对小型数据集执行检索增强生成(RAG)。
  2. 评估生成的响应。
  3. 分析评估的追踪信息。

R2R 设置

安装依赖

首先,安装必要的包:

%pipinstallr2r-q
设置本地环境

配置 R2R_API_KEY 、 OPENAI_API_KEY 和 RAGAS_APP_TOKEN (可选)。

%pip install r2r-q
获取数据
dataset=["OpenAI is one of the most recognized names in the large language model space, known for its GPT series of models. These models excel at generating human-like text and performing tasks like creative writing, answering questions, and summarizing content. GPT-4, their latest release, has set benchmarks in understanding context and delivering detailed responses.","Anthropic is well-known for its Claude series of language models, designed with a strong focus on safety and ethical AI behavior. Claude is particularly praised for its ability to follow complex instructions and generate text that aligns closely with user intent.","DeepMind, a division of Google, is recognized for its cutting-edge Gemini models, which are integrated into various Google products like Bard and Workspace tools. These models are renowned for their conversational abilities and their capacity to handle complex, multi-turn dialogues.","Meta AI is best known for its LLaMA (Large Language Model Meta AI) series, which has been made open-source for researchers and developers. LLaMA models are praised for their ability to support innovation and experimentation due to their accessibility and strong performance.","Meta AI with it's LLaMA models aims to democratize AI development by making high-quality models available for free, fostering collaboration across industries. Their open-source approach has been a game-changer for researchers without access to expensive resources.","Microsoft’s Azure AI platform is famous for integrating OpenAI’s GPT models, enabling businesses to use these advanced models in a scalable and secure cloud environment. Azure AI powers applications like Copilot in Office 365, helping users draft emails, generate summaries, and more.","Amazon’s Bedrock platform is recognized for providing access to various language models, including its own models and third-party ones like Anthropic’s Claude and AI21’s Jurassic. Bedrock is especially valued for its flexibility, allowing users to choose models based on their specific needs.","Cohere is well-known for its language models tailored for business use, excelling in tasks like search, summarization, and customer support. Their models are recognized for being efficient, cost-effective, and easy to integrate into workflows.","AI21 Labs is famous for its Jurassic series of language models, which are highly versatile and capable of handling tasks like content creation and code generation. The Jurassic models stand out for their natural language understanding and ability to generate detailed and coherent responses.","In the rapidly advancing field of artificial intelligence, several companies have made significant contributions with their large language models. Notable players include OpenAI, known for its GPT Series (including GPT-4); Anthropic, which offers the Claude Series; Google DeepMind with its Gemini Models; Meta AI, recognized for its LLaMA Series; Microsoft Azure AI, which integrates OpenAI’s GPT Models; Amazon AWS (Bedrock), providing access to various models including Claude (Anthropic) and Jurassic (AI21 Labs); Cohere, which offers its own models tailored for business use; and AI21 Labs, known for its Jurassic Series. These companies are shaping the landscape of AI by providing powerful models with diverse capabilities.",]
设置 R2R 客户端
fromr2rimportR2RClient client=R2RClient()
导入数据
ingest_response=client.documents.create(chunks=dataset,)
使用 /rag 端点

/rag 端点通过将搜索结果与语言模型输出相结合来实现检索增强生成。生成过程可以使用 rag_generation_config 参数进行自定义,而检索过程可以使用 search_settings 进行配置。

query="What makes Meta AI’s LLaMA models stand out?"search_settings={"limit":2,"graph_settings":{"enabled":False,"limit":2},}response=client.retrieval.rag(query=query,search_settings=search_settings)print(response.results.generated_answer)

输出

Meta AI’s LLaMA models stand out due to theiropen-source nature,which supports innovationandexperimentation by making high-quality models accessible to researchersanddevelopers[1].This approach democratizes AI development,fostering collaboration across industriesandenabling researchers without access to expensive resources to workwithadvanced AI models[2].

评估

使用 Ragas 评估 R2R 客户端

有了 R2R 客户端后,我们可以使用 Ragas 的 R2R 集成进行评估。此过程涉及以下关键组件:

  1. R2R 客户端和配置 :指定 RAG 设置的 R2RClient 和 /rag 配置。
  2. 评估数据集 :您需要一个包含 Ragas 指标所需所有必要输入的 Ragas EvaluationDataset 。
  3. Ragas 指标 :Ragas 提供了多种评估指标来评估 RAG 的不同方面,如忠实度(faithfulness)、答案相关性(answer relevance)和上下文召回率(context recall)。您可以在 Ragas 文档中查看所有可用指标的完整列表。
构建 Ragas 评估数据集

EvaluationDataset 是 Ragas 中用于表示评估样本的数据类型。您可以在核心概念部分找到关于其结构和用法的更多详细信息。

我们将使用 Ragas 中的 transform_to_ragas_dataset 函数为我们的数据获取 EvaluationDataset 。

questions=["Who are the major players in the large language model space?","What is Microsoft’s Azure AI platform known for?","What kind of models does Cohere provide?",]references=["The major players include OpenAI (GPT Series), Anthropic (Claude Series), Google DeepMind (Gemini Models), Meta AI (LLaMA Series), Microsoft Azure AI (integrating GPT Models), Amazon AWS (Bedrock with Claude and Jurassic), Cohere (business-focused models), and AI21 Labs (Jurassic Series).","Microsoft’s Azure AI platform is known for integrating OpenAI’s GPT models, enabling businesses to use these models in a scalable and secure cloud environment.","Cohere provides language models tailored for business use, excelling in tasks like search, summarization, and customer support.",]r2r_responses=[]search_settings={"limit":2,"graph_settings":{"enabled":False,"limit":2},}forqueinquestions:response=client.retrieval.rag(query=que,search_settings=search_settings)r2r_responses.append(response)
fromragas.integrations.r2rimporttransform_to_ragas_dataset ragas_eval_dataset=transform_to_ragas_dataset(user_inputs=questions,r2r_responses=r2r_responses,references=references)

输出

EvaluationDataset(features=['user_input','retrieved_contexts','response','reference'],len=3)
选择指标

为了评估我们的 RAG 端点,我们将使用以下指标:

  • 答案相关性(Response Relevancy) :衡量响应对用户输入(查询)的相关程度。
  • 上下文精确度(Context Precision) :衡量成功检索到的相关文档(或信息片段)的数量。
  • 忠实度(Faithfulness) :衡量响应对检索到的上下文在事实层面的一致性。
fromragas.metricsimportAnswerRelevancy,ContextPrecision,Faithfulnessfromragasimportevaluatefromlangchain_openaiimportChatOpenAIfromragas.llmsimportLangchainLLMWrapper llm=ChatOpenAI(model="gpt-4o-mini")evaluator_llm=LangchainLLMWrapper(llm)ragas_metrics=[AnswerRelevancy(llm=evaluator_llm),ContextPrecision(llm=evaluator_llm),Faithfulness(llm=evaluator_llm)]results=evaluate(dataset=ragas_eval_dataset,metrics=ragas_metrics)

输出

Querying Client:100%|██████████|3/3[00:00<?,?it/s]Evaluating:100%|██████████|9/9[00:00<?,?it/s]
user_inputretrieved_contextsresponsereferenceanswer_relevancycontext_precisionfaithfulness
0Who are the major players in the large languag…[In the rapidly advancing field of artificial …The major players in the large language model …The major players include OpenAI (GPT Series),…1.0000001.01.000000
1What is Microsoft’s Azure AI platform known for?[Microsoft’s Azure AI platform is famous for i…Microsoft’s Azure AI platform is known for int…Microsoft’s Azure AI platform is known for int…0.9489081.00.833333
2What kind of models does Cohere provide?[Cohere is well-known for its language models …Cohere provides language models tailored for b…Cohere provides language models tailored for b…0.9037651.01.000000
追踪评估

为了更好地理解评估得分,我们可以使用以下代码获取评估结果的追踪信息和判定理由。

results.upload()

快乐编码!

http://www.cnnetsun.cn/news/3343354.html

相关文章:

  • 免费解锁Wand高级功能:Wand-Enhancer终极使用指南
  • 终极指南:如何用免费软件Fan Control精准控制Windows风扇转速与噪音平衡
  • HCIP-BGP实验
  • 5分钟玩转猫抓扩展:让网页视频下载变得像点外卖一样简单
  • UnityFigmaBridge实战:打通Figma与Unity的UI设计开发全流程
  • 怪物猎人:世界风灵月影修改器下载67项修改器(带汉化)
  • PIC18LF47K42驱动CMT-8540S-SMT蜂鸣器实现智能硬件音频反馈
  • AI Agent 多智能体协作系统:从单 Agent 到多 Agent 架构的工程实践
  • AI自动化科研突破:弱监督强假说验证与成本效益分析
  • 从零搭建AI开发环境:Windows下CUDA、cuDNN、Anaconda与GPU版PyTorch一站式配置指南
  • Docker Compose编排极狐GitLab:一键部署与配置实战
  • Ornith-1.0-35B-6bit配置文件详解:如何优化模型性能与显存占用
  • Illustrator脚本深度解析:矢量图形自动化处理的技术实现
  • HSTracker:macOS炉石传说智能辅助工具的终极完整指南
  • GitHub汉化插件:3分钟让英文界面变中文的终极指南
  • RAG系统优化与效果评估实践指南
  • TCN-Transformer-LSTM混合模型在时序预测中的应用与优化
  • 3步掌握智能配置工具:开源项目的快速上手指南
  • ERB Lint 多格式输出指南:GitLab、JUnit、JSON 报告生成终极教程 [特殊字符]
  • LinkSwift:2025年最全面的网盘直链下载助手终极指南
  • D3KeyHelper深度解析:5个实战技巧打造暗黑破坏神3极致自动化体验
  • MATLAB R2024a Simscape 电路仿真:6 类常见电路模型库与 2 种建模方法对比
  • Qwen-2.5_1.5B_Instruct_rai_1.7.1_npu_16K模型架构解析:从ONNX文件到NPU推理的全流程
  • AI编码助手真实力测评(Copilot代码质量红蓝对抗实录)
  • 如何快速搭建个人B站视频库:完整指南解锁大会员4K和充电专属内容
  • 【AI】----人人都在说 Agent,90% 的人根本没搞懂,一篇讲透底层逻辑
  • Ubuntu GNOME桌面美化进阶:从基础定制到个性化工作流
  • Linux 普通用户无特权安装指定版本 CUDA 实战指南
  • 【工业质检实战】铝型材表面缺陷数据集构建与YOLO模型训练全流程解析
  • 两层神经网络在图像分类中的优化与应用