跳到内容

🕵🏻‍♀️ 创建一个 GitHub 仓库的 RAG 系统专家,并在 Argilla 中记录您的预测

在本教程中,我们将向您展示如何创建一个 RAG 系统,该系统可以回答有关特定 GitHub 仓库的问题。例如,我们将以 Argilla 仓库为目标。这个 RAG 系统将以仓库的文档为目标,因为这是关于仓库的大部分自然语言信息所在之处。

本教程包括以下步骤

  • 为 LlamaIndex 设置 Argilla 回调处理程序。
  • 初始化 GitHub 客户端
  • 使用我们选择的 GitHub 仓库中的一组特定文件创建索引。
  • 从 Argilla 仓库创建一个 RAG 系统,提出问题,并自动将答案记录到 Argilla。

本教程基于 LlamaIndex 制作的 Github 仓库阅读器

开始入门

部署 Argilla 服务器¶

如果您已经部署了 Argilla,则可以跳过此步骤。否则,您可以按照本指南快速部署 Argilla。

设置环境¶

要完成本教程,您需要通过 pip 安装此集成和一个第三方库。

注意

查看集成 GitHub 仓库 此处

!pip install "argilla-llama-index"
!pip install "llama-index-readers-github==0.1.9"

让我们进行所需的导入

from llama_index.core import (
    Settings,
    VectorStoreIndex,
)
from llama_index.core.instrumentation import get_dispatcher
from llama_index.llms.openai import OpenAI
from llama_index.readers.github import (
    GithubClient,
    GithubRepositoryReader,
)

from argilla_llama_index import ArgillaHandler

我们需要设置 OpenAI API 密钥和 GitHub 令牌。OpenAI API 密钥是使用 GPT 模型运行查询所必需的,而 GitHub 令牌确保您有权访问您正在使用的仓库。虽然 GitHub 令牌对于公共仓库可能不是必需的,但仍然建议使用。

import os

os.environ["OPENAI_API_KEY"] = "sk-..."
openai_api_key = os.getenv("OPENAI_API_KEY")

os.environ["GITHUB_TOKEN"] = "ghp_..."
github_token = os.getenv("GITHUB_TOKEN")

设置 Argilla 的 LlamaIndex 处理程序

为了在您的 LlamaIndex 工作流程中轻松地将数据记录到 Argilla 中,您只需要初始化 Argilla 处理程序并将其附加到 Llama Index 分发器以处理 span 和事件。这确保了使用 Llama Index 获得的预测会连同有用的元数据一起自动记录到 Argilla 实例中。

  • dataset_name:数据集的名称。如果数据集不存在,将使用指定的名称创建它。否则,它将被更新。
  • api_url:连接到 Argilla 实例的 URL。
  • api_key:用于向 Argilla 实例进行身份验证的 API 密钥。
  • number_of_retrievals:要记录的检索文档数量。默认为 0。
  • workspace_name:用于记录数据的工作区名称。默认情况下,为第一个可用的工作区。

> 有关凭据的更多信息,请查看 用户工作区 的文档。

argilla_handler = ArgillaHandler(
    dataset_name="github_query_llama_index",
    api_url="http://localhost:6900",
    api_key="argilla.apikey",
    number_of_retrievals=2,
)
root_dispatcher = get_dispatcher()
root_dispatcher.add_span_handler(argilla_handler)
root_dispatcher.add_event_handler(argilla_handler)

从 GitHub 检索数据

首先,我们需要初始化 GitHub 客户端,其中将包括用于仓库访问的 GitHub 令牌。

github_client = GithubClient(github_token=github_token, verbose=True)

在创建我们的 GithubRepositoryReader 实例之前,我们需要调整嵌套。由于 Jupyter 内核在事件循环上运行,我们必须防止此循环在仓库完全读取之前完成。

import nest_asyncio

nest_asyncio.apply()

现在,让我们使用必要的仓库详细信息创建一个 GithubRepositoryReader 实例。在本例中,我们将以 argilla 仓库的 main 分支为目标。由于我们将专注于文档,我们将专注于 argilla/docs/ 文件夹,排除图像、json 文件和 ipynb 文件。

documents = GithubRepositoryReader(
    github_client=github_client,
    owner="argilla-io",
    repo="argilla",
    use_parser=False,
    verbose=False,
    filter_directories=(
        ["argilla/docs/"],
        GithubRepositoryReader.FilterType.INCLUDE,
    ),
    filter_file_extensions=(
        [
            ".png",
            ".jpg",
            ".jpeg",
            ".gif",
            ".svg",
            ".ico",
            ".json",
            ".ipynb",   # Erase this line if you want to include notebooks

        ],
        GithubRepositoryReader.FilterType.EXCLUDE,
    ),
).load_data(branch="main")

创建索引并进行一些查询

现在,让我们从此文档创建一个 LlamaIndex 索引,我们可以开始查询 RAG 系统了。

# LLM settings
Settings.llm = OpenAI(
    model="gpt-3.5-turbo", temperature=0.8, openai_api_key=openai_api_key
)

# Load the data and create the index
index = VectorStoreIndex.from_documents(documents)

# Create the query engine
query_engine = index.as_query_engine()
response = query_engine.query("How do I create a Dataset in Argilla?")
response

生成的响应将自动记录在我们的 Argilla 实例中。查看一下!从 Argilla 中,您可以快速查看您的预测并对其进行标注,这样您就可以结合合成数据和人工反馈。

Argilla UI

让我们再问几个问题,看看 RAG 聊天机器人的整体行为。请记住,答案会自动记录到您的 Argilla 实例中。

questions = [
    "How can I list the available datasets?",
    "Which are the user credentials?",
    "Can I use markdown in Argilla?",
    "Could you explain how to annotate datasets in Argilla?",
]

answers = []

for question in questions:
    answers.append(query_engine.query(question))

for question, answer in zip(questions, answers):
    print(f"Question: {question}")
    print(f"Answer: {answer}")
    print("----------------------------")
Question: How can I list the available datasets?
Answer: You can list all the datasets available in a workspace by utilizing the `datasets` attribute of the `Workspace` class. Additionally, you can determine the number of datasets in a workspace by using `len(workspace.datasets)`. To list the datasets, you can iterate over them and print out each dataset. Remember that dataset settings are not preloaded when listing datasets, and if you need to work with settings, you must load them explicitly for each dataset.
----------------------------
Question: Which are the user credentials?
Answer: The user credentials in Argilla consist of a username, password, and API key.
----------------------------
Question: Can I use markdown in Argilla?
Answer: Yes, you can use Markdown in Argilla.
----------------------------
Question: Could you explain how to annotate datasets in Argilla?
Answer: To annotate datasets in Argilla, users can manage their data annotation projects by setting up `Users`, `Workspaces`, `Datasets`, and `Records`. By deploying Argilla on the Hugging Face Hub or with `Docker`, installing the Python SDK with `pip`, and creating the first project, users can get started in just 5 minutes. The tool allows for interacting with data in a more engaging way through features like quick labeling with filters, AI feedback suggestions, and semantic search, enabling users to focus on training models and monitoring their performance effectively.
----------------------------