如何在LlamaIndex中使用UpTrain
概述:在本示例中,我们将了解如何将 UpTrain 与 LlamaIndex 结合使用。UpTrain(GitHub || 官网 || 文档)是一个用于评估和改进生成式AI应用的开源平台。它提供20多项预配置检查的评分(涵盖语言、代码、嵌入用例),对失败案例进行根本原因分析,并提供解决方案的见解。有关UpTrain评估的更多详细信息可在此处查看。
问题:存在两个主要问题:
- 大多数大型语言模型训练所用的数据并不能代表它们实际应用的数据。这导致训练分布与测试分布之间存在不匹配,从而可能导致性能不佳。
- 大型语言模型生成的结果并不总是可靠的。响应可能与提示无关,不符合期望的语气或上下文,或者可能具有冒犯性等。
解决方案: 上述两个问题由两个不同的工具解决,我们将向您展示如何将它们结合使用:
- LlamaIndex通过允许您使用在您自己的数据上微调的检索器来执行检索增强生成(RAG),从而解决了第一个问题。这使您能够使用自己的数据微调检索器,然后使用该检索器执行RAG。
- UpTrain 通过允许您对生成的响应执行评估来解决第二个问题。这有助于您确保响应与提示相关,符合期望的语气或上下文,并且不具冒犯性等。
安装 UpTrain 和 LlamaIndex
Section titled “Install UpTrain and LlamaIndex”%pip install -qU uptrain llama-indexNote: you may need to restart the kernel to use updated packages.import httpximport osimport openaiimport pandas as pd
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, Settingsfrom uptrain import Evals, EvalLlamaIndex, Settings as UpTrainSettings/Users/dhruvchawla/Work/llama_index/venv/lib/python3.11/site-packages/lazy_loader/__init__.py:185: RuntimeWarning: subpackages can technically be lazily loaded, but it causes the package to be eagerly loaded even if it is already lazily loaded.So, you probably shouldn't use subpackages with this lazy feature. warnings.warn(msg, RuntimeWarning)您可以使用任何您拥有的文档来完成此操作。在本教程中,我们将使用从维基百科提取的纽约市数据。我们只会向文件夹添加一个文档,但您可以根据需要添加任意数量的文档。
url = "https://uptrain-assets.s3.ap-south-1.amazonaws.com/data/nyc_text.txt"if not os.path.exists("nyc_wikipedia"): os.makedirs("nyc_wikipedia")dataset_path = os.path.join("./nyc_wikipedia", "nyc_text.txt")
if not os.path.exists(dataset_path): r = httpx.get(url) with open(dataset_path, "wb") as f: f.write(r.content)在生成回复之前,我们需要创建一个查询列表。由于查询引擎是基于纽约市训练的,我们将创建一个与纽约市相关的查询列表。
data = [ {"question": "What is the population of New York City?"}, {"question": "What is the area of New York City?"}, {"question": "What is the largest borough in New York City?"}, {"question": "What is the average temperature in New York City?"}, {"question": "What is the main airport in New York City?"}, {"question": "What is the famous landmark in New York City?"}, {"question": "What is the official language of New York City?"}, {"question": "What is the currency used in New York City?"}, {"question": "What is the time zone of New York City?"}, {"question": "What is the famous sports team in New York City?"},]本笔记本使用 OpenAI API 为提示生成文本,并创建向量存储索引。因此,请将 openai.api_key 设置为您的 OpenAI API 密钥。
openai.api_key = "sk-************************" # your OpenAI API key使用LlamaIndex创建查询引擎
Section titled “Create a query engine using LlamaIndex”让我们使用LLamaIndex创建一个向量存储索引,然后将其用作查询引擎从文档中检索相关部分。
Settings.chunk_size = 512
documents = SimpleDirectoryReader("./nyc_wikipedia/").load_data()
vector_index = VectorStoreIndex.from_documents( documents,)
query_engine = vector_index.as_query_engine()UpTrain 为您提供:
- 带有高级钻取和筛选选项的仪表板
- 失败案例中的洞察与常见主题
- 生产数据的可观测性与实时监控
- 通过与您的CI/CD流水线无缝集成进行回归测试
您可以选择以下两种替代方案之一来使用 UpTrain 进行评估:
替代方案1:使用UpTrain的开源软件(OSS)进行评估
Section titled “Alternative 1: Evaluate using UpTrain’s Open-Source Software (OSS)”You can use the open-source evaluation service to evaluate your model. In this case, you will need to provide an OpenAI API key. You can get yours here.
为了在UpTrain仪表板中查看您的评估结果,您需要通过终端运行以下命令进行设置:
git clone https://github.com/uptrain-ai/uptraincd uptrainbash run_uptrain.shThis will start the UpTrain dashboard on your local machine. You can access it at http://localhost:3000/dashboard.
Note: The project_name will be the project name under which the evaluations performed will be shown in the UpTrain dashboard.
settings = UpTrainSettings( openai_api_key=openai.api_key,)创建 EvalLlamaIndex 对象
Section titled “Create the EvalLlamaIndex object”现在我们已经创建了查询引擎,可以使用它来创建一个EvalLlamaIndex对象。该对象将用于生成查询的响应。
llamaindex_object = EvalLlamaIndex( settings=settings, query_engine=query_engine)现在我们有了查询列表,我们可以使用 EvalLlamaIndex 对象为查询生成响应,然后对响应执行评估。您可以在此处找到 UpTrain 提供的完整评估列表。我们选择了两个在本教程中最相关的评估:
-
上下文相关性:此评估检查检索到的上下文是否与查询相关。这一点很重要,因为检索到的上下文用于生成响应。如果检索到的上下文与查询不相关,那么响应也将与查询不相关。
-
响应简洁性:此评估检查响应是否简洁。这一点很重要,因为响应应当简明扼要,不应包含任何不必要的信息。
results = llamaindex_object.evaluate( project_name="uptrain-llama-index", evaluation_name="nyc_wikipedia", # adding project and evaluation names allow you to track the results in the UpTrain dashboard data=data, checks=[Evals.CONTEXT_RELEVANCE, Evals.RESPONSE_CONCISENESS],)100%|██████████| 10/10 [00:02<00:00, 3.94it/s]100%|██████████| 10/10 [00:03<00:00, 3.12it/s]pd.DataFrame(results).dataframe tbody tr th { vertical-align: top;}
.dataframe thead th { text-align: right;}| 问题 | 响应 | 上下文 | score_context_relevance | explanation_context_relevance | score_response_conciseness | explanation_response_conciseness | |
|---|---|---|---|---|---|---|---|
| 0 | 纽约市的人口是多少? | 纽约市的人口为8,804,190... | === 人口密度 ===\n\n2020年,该城市... | 无 | 无 | 无 | 无 |
| 1 | 纽约市的面积是多少? | 纽约市总面积为468.484平方... | 地形中的一些自然起伏已被... | 无 | 无 | 无 | 无 |
| 2 | 纽约市最大的行政区是哪个? | 皇后区是纽约市最大的行政区。 | ==== 布鲁克林 ====\n布鲁克林(国王县),o... | 无 | 无 | 无 | 无 |
| 3 | 纽约市的平均气温是多少? | 纽约市的平均气温为33... | 同样地,0°F(−18°C)的读数也... | 无 | 无 | 无 | 无 |
| 4 | 纽约市的主要机场是什么? | 约翰·F·肯尼迪国际机场 | 沿东北走廊及长途... | 无 | 无 | 无 | 无 |
| 5 | 纽约市的著名地标是什么? | 纽约市的著名地标是圣... | 该定居点被命名为新阿姆斯特丹(荷兰语:... | 无 | 无 | 无 | 无 |
| 6 | 纽约市的官方语言是什么? | 纽约有多达800种语言被使用... | === 口音与方言 ===\n\n纽约地区... | 无 | 无 | 无 | 无 |
| 7 | 纽约市使用的货币是什么? | 纽约市使用的货币是美元... | === 房地产 ===\n\n房地产是一个主要... | 无 | 无 | 无 | 无 |
| 8 | 纽约市的时区是什么? | 东部标准时间 (EST) | 根据纽约市审计长,wo... | 无 | 无 | 无 | 无 |
| 9 | 纽约市著名的运动队是什么? | 纽约市著名的运动队是... | ==== 足球 ====\n在足球方面,纽约市是... | 无 | 无 | 无 | 无 |
替代方案2:使用UpTrain托管服务和仪表板进行评估
Section titled “Alternative 2: Evaluate using UpTrain’s Managed Service and Dashboards”Alternatively, you can use UpTrain’s managed service to evaluate your model. You can create a free UpTrain account here and get free trial credits. If you want more trial credits, book a call with the maintainers of UpTrain here.
使用托管服务的优势包括:
- 无需在本地机器上设置 UpTrain 仪表板。
- 无需其API密钥即可访问多种大型语言模型。
执行评估后,您可以在UpTrain仪表板的https://dashboard.uptrain.ai/dashboard处查看它们https://dashboard.uptrain.ai/dashboard
Note: The project_name will be the project name under which the evaluations performed will be shown in the UpTrain dashboard.
UPTRAIN_API_KEY = "up-**********************" # your UpTrain API key
# We use `uptrain_access_token` parameter instead of 'openai_api_key' in settings in this casesettings = UpTrainSettings( uptrain_access_token=UPTRAIN_API_KEY,)创建 EvalLlamaIndex 对象
Section titled “Create the EvalLlamaIndex object”现在我们已经创建了查询引擎,可以使用它来创建一个EvalLlamaIndex对象。该对象将用于生成查询的响应。
llamaindex_object = EvalLlamaIndex( settings=settings, query_engine=query_engine)现在我们有了查询列表,我们可以使用 EvalLlamaIndex 对象为查询生成响应,然后对响应执行评估。您可以在此处找到 UpTrain 提供的完整评估列表。我们选择了两个在本教程中最相关的评估:
-
上下文相关性:此评估检查检索到的上下文是否与查询相关。这一点很重要,因为检索到的上下文用于生成响应。如果检索到的上下文与查询不相关,那么响应也将与查询不相关。
-
响应简洁性: 该评估检查响应是否简洁。这一点很重要,因为响应应当简明扼要,不应包含任何不必要的信息。
results = llamaindex_object.evaluate( project_name="uptrain-llama-index", evaluation_name="nyc_wikipedia", # adding project and evaluation names allow you to track the results in the UpTrain dashboard data=data, checks=[Evals.CONTEXT_RELEVANCE, Evals.RESPONSE_CONCISENESS],)[32m2024-01-23 18:36:57.815[0m | [1mINFO [0m | [36muptrain.framework.remote[0m:[36mlog_and_evaluate[0m:[36m507[0m - [1mSending evaluation request for rows 0 to <50 to the Uptrain server[0mpd.DataFrame(results).dataframe tbody tr th { vertical-align: top;}
.dataframe thead th { text-align: right;}| 问题 | 响应 | 上下文 | score_context_relevance | explanation_context_relevance | score_response_conciseness | explanation_response_conciseness | |
|---|---|---|---|---|---|---|---|
| 0 | 纽约市的人口是多少? | 纽约市的人口为8,804,190... | 纽约,通常被称为纽约市或NYC,是... | 1.0 | 该问题询问纽约的人口... | 1.0 | 该问题询问纽约的人口... |
| 1 | 纽约市的面积是多少? | 纽约市的面积为468.484平方英里... | 纽约,通常被称为纽约市或NYC,是... | 1.0 | 步骤1:问题要求计算新...的面积 | 1.0 | 问题询问纽约市的面积... |
| 2 | 纽约市最大的行政区是哪个? | 皇后区是纽约市最大的行政区。 | ==== 布鲁克林 ====\n布鲁克林(国王县),o... | 0.5 | 步骤1:问题要求找出最大的... | 1.0 | 问题询问纽约市最大的行政区... |
| 3 | 纽约市的平均气温是多少? | 纽约市的平均气温为57... | 同样地,0°F(−18°C)的读数也... | 0.5 | 该问题要求计算平均温度... | 1.0 | 该问题要求计算平均温度... |
| 4 | 纽约市的主要机场是什么? | 纽约市的主要机场是约翰·F·肯尼迪... | 沿东北走廊及长途... | 1.0 | 问题是“N...的主要机场是什么” | 1.0 | 问题询问的是纽约的主要机场... |
| 5 | 纽约市的著名地标是什么? | 纽约市的著名地标是帝国大厦... | 创纪录的6660万游客到访纽约... | 1.0 | 问题询问的是位于N...的著名地标... | 1.0 | 问题询问的是位于N...的著名地标... |
| 6 | 纽约市的官方语言是什么? | 纽约市的官方语言并非... | === 口音与方言 ===\n\n纽约地区... | 0.0 | 该问题询问的是官方语言... | 0.0 | 该问题询问...的官方语言 |
| 7 | 纽约市使用的货币是什么? | 纽约市使用的货币是美元... | === 房地产 ===\n\n房地产是一个主要... | 0.0 | 问题是“在...使用的货币是什么” | 1.0 | 该问题特别询问了当前... |
| 8 | 纽约市的时区是什么? | 东部标准时间 (EST) | 根据纽约市审计长,wo... | 0.0 | 问题是“纽约的时区是什么……” | 1.0 | 问题询问纽约的时区... |
| 9 | 纽约市著名的运动队是什么? | 纽约市著名的运动队是... | ==== 棒球 ====\n纽约被描述为... | 1.0 | 问题询问的是著名的体育队伍... | 1.0 | 问题询问的是著名的体育队伍... |
得分与对应得分案例数量的直方图

您可以筛选失败案例并生成其中的共同主题。这有助于识别核心问题并帮助修复它
