跳至内容

RAG 命令行界面#

一个常见的用例是与LLM聊天,讨论您保存在计算机本地的文件。

我们编写了一个CLI工具来帮助您实现这一目标!您可以将rag CLI工具指向本地保存的一组文件,它会将这些文件导入到本地向量数据库中,然后在终端内用于聊天问答交互式环境。

默认情况下,该工具使用OpenAI进行嵌入和LLM处理,同时搭配本地Chroma向量数据库实例。警告:这意味着默认情况下,您通过该工具摄入的本地数据将会被发送至OpenAI的API接口。

不过,您确实可以自定义此工具中使用的模型和数据库。这包括完全在本地运行所有模型执行的可能性!请参阅下方的自定义部分。

设置#

要设置CLI工具,请确保已安装该库:

$ pip install -U llama-index

您还需要安装 Chroma

$ pip install -U chromadb

之后,你就可以开始使用这个工具了:

$ llamaindex-cli rag -h
usage: llamaindex-cli rag [-h] [-q QUESTION] [-f FILES [FILES ...]] [-c] [-v] [--clear] [--create-llama]

options:
  -h, --help            show this help message and exit
  -q QUESTION, --question QUESTION
                        The question you want to ask.
  -f, --files FILES [FILES ...]
                        The name of the file(s) or directory you want to ask a question about,such
                        as "file.pdf". Supports globs like "*.py".
  -c, --chat            If flag is present, opens a chat REPL.
  -v, --verbose         Whether to print out verbose information during execution.
  --clear               Clears out all currently embedded data.
  --create-llama        Create a LlamaIndex application based on the selected files.

用法#

以下是一些帮助你入门的高级步骤:

  1. 设置OPENAI_API_KEY环境变量: 默认情况下,该工具使用OpenAI的API。因此,在使用工具时,您需要确保OpenAI API密钥已设置为OPENAI_API_KEY环境变量。
    $ export OPENAI_API_KEY=
    
  2. 导入一些文件: 现在,你需要让工具指向一些本地文件,以便将其导入本地向量数据库。在这个示例中,我们将导入LlamaIndex的README.md文件:
    $ llamaindex-cli rag --files "./README.md"
    
    你也可以指定一个文件通配符模式,例如:
    $ llamaindex-cli rag --files "./docs/**/*.rst"
    
  3. 提问: 您现在可以开始就之前步骤中摄入的任何文档提出问题:
    $ llamaindex-cli rag --question "What is LlamaIndex?"
    LlamaIndex 是一个数据框架,可帮助在基于LLM的应用中摄入、构建和访问私有或特定领域数据。它提供诸如数据连接器(用于从各种来源摄入数据)、数据索引(用于结构化数据)以及自然语言数据访问引擎等工具。LlamaIndex采用检索增强生成(RAG)方法,即从数据源检索信息,将其作为上下文添加到问题中,然后要求LLM基于增强后的提示生成答案。这种方法克服了微调LLM的局限性,为数据增强提供了更具成本效益、最新且可靠的解决方案。LlamaIndex设计兼顾初学者和高级用户,提供高级API便于使用,以及低级API用于定制和扩展。
    
  4. 打开聊天REPL: 您甚至可以在终端内打开一个聊天界面!只需运行 $ llamaindex-cli rag --chat 并开始询问有关您已摄入文件的问题。

创建一个LlamaIndex聊天应用#

您还可以基于所选文件,使用FastAPI后端和NextJS前端创建一个全栈聊天应用。

要启动应用程序,请确保您的机器上已安装NodeJS和npx。如果尚未安装,请参考LlamaIndex.TS文档获取安装说明。

完成所有设置后,创建新应用非常简单。只需运行以下命令:

$ llamaindex-cli rag --create-llama

它将调用我们的create-llama工具,因此您需要提供一些信息来创建应用程序。您可以在npmjs - create-llama上找到关于create-llama的更多信息

 llamaindex-cli rag --create-llama

Calling create-llama using data from /tmp/rag-data/...

✔ What is your project named?  my-app
✔ Which model would you like to use?  gpt-3.5-turbo
✔ Please provide your OpenAI API key (leave blank to skip): …
? How would you like to proceed?  - Use arrow-keys. Return to submit.
   Just generate code (~1 sec)
   Generate code and install dependencies (~2 min)  Generate code, install dependencies, and run the app (~2 min)
...

如果选择选项Generate code, install dependencies, and run the app (~2 min),所有依赖项将被安装且应用会自动运行。然后您可以通过访问此地址来使用应用程序:http://localhost:3000

支持的文件类型#

在内部,rag CLI工具使用SimpleDirectoryReader将本地文件系统中的原始文件解析为字符串。

该模块包含针对多种文件类型的自定义读取器。其中某些可能需要您pip install另一个模块才能解析特定文件类型。

如果遇到SimpleDirectoryReader没有自定义读取器的文件扩展名类型,它将仅以纯文本文件的形式读取该文件。

请参阅下一节了解如何添加自定义文件阅读器以及自定义CLI工具的其他方面!

自定义#

rag CLI工具具有高度可定制性!该工具通过结合IngestionPipelineQueryPipeline模块在RagCLI模块内实现功能。

要创建您自己的自定义rag CLI工具,您只需创建一个脚本,该脚本使用您自己配置的IngestionPipelineQueryPipeline来实例化RagCLI类。然后,您只需在脚本中运行rag_cli_instance.cli(),即可针对您选择的嵌入模型、LLMs、向量数据库等运行相同的摄取和问答命令。

以下是一些高级代码,展示基本设置:

#!/path/to/your/virtualenv/bin/python
import os
from llama_index.core.ingestion import IngestionPipeline, IngestionCache
from llama_index.core.query_pipeline import QueryPipeline
from llama_index.core.storage.docstore import SimpleDocumentStore
from llama_index.cli.rag import RagCLI


# optional, set any API keys your script may need (perhaps using python-dotenv library instead)
os.environ["OPENAI_API_KEY"] = "sk-xxx"

docstore = SimpleDocumentStore()

vec_store = ...  # your vector store instance
llm = ...  # your LLM instance - optional, will default to OpenAI gpt-3.5-turbo

custom_ingestion_pipeline = IngestionPipeline(
    transformations=[...],
    vector_store=vec_store,
    docstore=docstore,
    cache=IngestionCache(),
)

# Setting up the custom QueryPipeline is optional!
# You can still customize the vector store, LLM, and ingestion transformations without
# having to customize the QueryPipeline
custom_query_pipeline = QueryPipeline()
custom_query_pipeline.add_modules(...)
custom_query_pipeline.add_link(...)

# you can optionally specify your own custom readers to support additional file types.
file_extractor = {".html": ...}

rag_cli_instance = RagCLI(
    ingestion_pipeline=custom_ingestion_pipeline,
    llm=llm,  # optional
    query_pipeline=custom_query_pipeline,  # optional
    file_extractor=file_extractor,  # optional
)

if __name__ == "__main__":
    rag_cli_instance.cli()

从这里开始,只需几个步骤,您就能使用自定义的CLI脚本:

  1. 请确保将顶部的python路径替换为您虚拟环境正在使用的路径(在虚拟环境激活状态下运行$ which python)

  2. 假设你将文件保存在/path/to/your/script/my_rag_cli.py路径下。之后,你只需修改shell的配置文件(如.bashrc.zshrc),添加一行类似$ export PATH="/path/to/your/script:$PATH"的内容。

  3. 之后执行 $ chmod +x my_rag_cli.py 给文件赋予可执行权限。
  4. 搞定!现在你只需打开一个新的终端会话并运行$ my_rag_cli.py -h。现在你可以使用相同的参数运行脚本,但使用的是你的自定义代码配置!
  5. 注意:如果您只想以$ my_rag_cli --chat方式运行命令,可以从my_rag_cli.py文件中移除.py文件扩展名
优云智算