检索增强生成 (RAG)
LlamaIndex最常见的用例之一是检索增强生成(RAG),在该过程中,您的数据被索引并选择性检索,作为LLM响应查询的源材料。您可以了解更多关于RAG背后的概念。
在新文件夹中,运行:
npm initnpm i -D typescript @types/nodenpm i llamaindex然后,查看安装步骤以安装LlamaIndex.TS并准备OpenAI密钥。
您可以通过API使用其他大语言模型;如果您更倾向于使用本地模型,请查看我们的本地大语言模型示例。
创建文件 example.ts。该代码将
- 加载示例文件
- 将其转换为文档对象
- 为其建立索引(使用 OpenAI 创建嵌入向量)
- 创建一个查询引擎来回答关于数据的问题
在同一文件夹中创建一个 tsconfig.json 文件:
现在你可以运行以下代码
npx tsx example.ts您应该期望输出类似:
In college, the author studied subjects like linear algebra and physics, but did not find them particularly interesting. They started slacking off, skipping lectures, and eventually stopped attending classes altogether. They also had a negative experience with their English classes, where they were required to pay for catch-up training despite getting verbal approval to skip most of the classes. Ultimately, the author lost motivation for college due to their job as a software developer and stopped attending classes, only returning years later to pick up their papers.
0: Score: 0.8305309270895813 - I started this decade as a first-year college stud...
1: Score: 0.8286388215713089 - A short digression. I’m not saying colleges are wo...一旦你掌握了基础的RAG技术,可能就会想要考虑与你的数据对话。