RAG 命令行界面
一个常见的用例是与大型语言模型讨论您保存在计算机本地的文件。
我们编写了一个CLI工具来帮助您实现这一目标!您可以将rag CLI工具指向您本地保存的一组文件,它会将这些文件摄取到本地向量数据库中,然后用于您终端内的Chat问答交互环境。
默认情况下,该工具使用OpenAI进行嵌入和大型语言模型处理,并搭配本地Chroma向量数据库实例。警告:这意味着默认情况下,您使用此工具摄入的本地数据将会被发送至OpenAI的API。
不过,您确实有能力自定义此工具中使用的模型和数据库。这包括可以在本地运行所有模型执行!请参阅下方的自定义部分。
要设置CLI工具,请确保您已安装该库:
$ pip install -U llama-index
您还需要安装 Chroma:
$ pip install -U chromadb
之后,您就可以开始使用该工具:
$ llamaindex-cli rag -husage: 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.以下是一些帮助你入门的高级步骤:
- Set the
OPENAI_API_KEYenvironment variable: By default, this tool uses OpenAI’s API. As such, you’ll need to ensure the OpenAI API Key is set under theOPENAI_API_KEYenvironment variable whenever you use the tool.Terminal window $ export OPENAI_API_KEY=<api_key> - Ingest some files: Now, you need to point the tool at some local files that it can ingest into the local vector database. For this example, we’ll ingest the LlamaIndex
README.mdfile:You can also specify a file glob pattern such as:Terminal window $ llamaindex-cli rag --files "./README.md"Terminal window $ llamaindex-cli rag --files "./docs/**/*.rst" - Ask a Question: You can now start asking questions about any of the documents you’d ingested in the prior step:
Terminal window $ llamaindex-cli rag --question "What is LlamaIndex?"LlamaIndex is a data framework that helps in ingesting, structuring, and accessing private or domain-specific data for LLM-based applications. It provides tools such as data connectors to ingest data from various sources, data indexes to structure the data, and engines for natural language access to the data. LlamaIndex follows a Retrieval-Augmented Generation (RAG) approach, where it retrieves information from data sources, adds it to the question as context, and then asks the LLM to generate an answer based on the enriched prompt. This approach overcomes the limitations of fine-tuning LLMs and provides a more cost-effective, up-to-date, and trustworthy solution for data augmentation. LlamaIndex is designed for both beginner and advanced users, with a high-level API for easy usage and lower-level APIs for customization and extension. - 打开聊天REPL: 你甚至可以在终端内打开聊天界面!只需运行
$ llamaindex-cli rag --chat并开始询问关于已摄取文件的问题。
创建一个LlamaIndex聊天应用
Section titled “Create a LlamaIndex chat application”您也可以基于所选文件创建一个包含 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 工具具有高度可定制性!该工具通过将IngestionPipeline模块与RagCLI模块相结合来实现功能。
要创建您自己的自定义RAG CLI工具,只需创建一个脚本,使用您自己配置的IngestionPipeline来实例化RagCLI类。然后,您只需在脚本中运行rag_cli_instance.cli(),即可针对您选择的嵌入模型、LLMs、向量数据库等执行相同的摄取和问答命令。
以下是一些高级代码,展示基本设置:
#!/path/to/your/virtualenv/bin/pythonimport osfrom llama_index.core.ingestion import IngestionPipeline, IngestionCachefrom llama_index.core.storage.docstore import SimpleDocumentStorefrom 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 instancellm = ... # 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(),)
# 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 file_extractor=file_extractor, # optional)
if __name__ == "__main__": rag_cli_instance.cli()从那里开始,您只需几个步骤就能使用自定义的CLI脚本:
-
请确保将顶部的python路径替换为您的虚拟环境正在使用的路径(在您的虚拟环境激活状态下运行
$ which python) -
假设你将文件保存在
/path/to/your/script/my_rag_cli.py。然后,你可以简单地修改你的 shell 配置文件(例如.bashrc或.zshrc),添加一行类似$ export PATH="/path/to/your/script:$PATH"的内容。 -
之后执行
$ chmod +x my_rag_cli.py来赋予文件可执行权限。 -
就这样!你现在只需打开一个新的终端会话并运行
$ my_rag_cli.py -h。现在你可以使用相同的参数运行脚本,但使用你的自定义代码配置!- 注意:如果您只想以
$ my_rag_cli --chat方式运行命令,可以从my_rag_cli.py文件中移除.py文件扩展名
- 注意:如果您只想以