如何使用 UpTrain 和 LlamaIndex进行大型语言模型评估

概述

在本文中,我们将探讨如何利用 UpTrain 和 LlamaIndex 来评估和改善生成性AI应用程序的性能。UpTrain 是一个开源平台,可以对生成的响应进行评估,提供20多个预配置检查,帮助进行根本原因分析以及提供解决方案的见解。而 LlamaIndex 则允许我们基于自有数据进行检索增强生成(RAG),解决数据分布不匹配的问题。

安装 UpTrain 和 LlamaIndex

首先,您需要安装 UpTrain 和 LlamaIndex:

%pip install -q uptrain llama-index

注意:安装后可能需要重启内核以便使用更新的包。

导入所需库

import httpx
import os
import openai
import pandas as pd

from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, Settings
from uptrain import Evals, EvalLlamaIndex, Settings as UpTrainSettings

创建数据集文件夹

我们可以使用任何文档来执行此操作。为了本教程,我们将使用来自维基百科的纽约市数据。

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)  # //中转API

创建查询列表

在生成响应之前,我们需要创建一个与纽约市相关的查询列表。

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?"},
]

创建查询引擎

接下来,我们使用 LlamaIndex 创建一个向量存储索引,并将其作为查询引擎,以检索相关文档。

openai.api_key = "sk-************************"  # your OpenAI API key

# 创建向量索引
documents = SimpleDirectoryReader("./nyc_wikipedia/").load_data()
vector_index = VectorStoreIndex.from_documents(documents)
query_engine = vector_index.as_query_engine()

运行评估

使用 EvalLlamaIndex 对象生成响应并进行评估。我们选择了两个最相关的评估:

  1. 上下文相关性:检查检索到的上下文是否与查询相关。
  2. 响应简洁性:检查响应是否简洁,不包含不必要的信息。
settings = UpTrainSettings(openai_api_key=openai.api_key)

llamaindex_object = EvalLlamaIndex(settings=settings, query_engine=query_engine)

results = llamaindex_object.evaluate(data=data, checks=[Evals.CONTEXT_RELEVANCE, Evals.RESPONSE_CONCISENESS])
pd.DataFrame(results)

可能遇到的错误

  1. API密钥错误:如果您的 OpenAI API 密钥无效,您将遇到身份验证错误。请确保输入正确的密钥,并保持其私密性。

  2. 网络请求失败:若数据集文件下载失败,检查您的网络连接以及指定的URL是否有效。

  3. 包未找到:如果您在运行代码时遇到导入错误,请确保您已正确安装所需库。

如果你觉得这篇文章对你有帮助, 请点赞, 关注我的博客, 谢谢!

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值