LlamaIndexTS学习笔记

LlamaIndex 简介

https://www.cnblogs.com/chengnan113/p/17436377.html
LLamaIndex 的任务是通过查询、检索的方式挖掘外部数据的信息,并将其传递给大模型,因此其主要由 x 部分组成:

  • 数据连接。首先将数据能读取进来,这样才能挖掘。
  • 索引构建。要查询外部数据,就必须先构建可以查询的索引,llamdaIndex 将数据存储在 Node 中,并基于 Node 构建索引。索引类型包括向量索引、列表索引、树形索引等;
  • 查询接口。有了索引,就必须提供查询索引的接口。通过这些接口用户可以与不同的 大模型进行对话,也能自定义需要的 Prompt 组合方式。查询接口会完成 检索+对话的功能,即先基于索引进行检索,再将检索结果和之前的输入 Prompt 进行(自定义)组合形成新的扩充 Prompt,对话大模型并拿到结果进行解析。

(TODO,待写成技术分享)LlamaIndex VS. LangChain

LlamaIndex 和 LangChain 是构建 LLM 应用程序的两个框架。LlamaIndex 专注于 RAG 用例,LangChain 得到了更广泛的应用。我们可以看到,如果是和 RAG 相关的用例,LlamaIndex 会方便很多,可以说是首选。

但是如果你的应用需要一些非 RAG 的功能,可能 LangChain 是一个更好的选择。

https://cloud.tencent.com/developer/article/2384394

对话机器人

DSL 生成

代码生成

Agent

有多种方式可以为 LLM 提供代理。最具模型泛型的方法是 ReAct 范式。

LlamaIndex 的改造

概念:
https://legacy.ts.llamaindex.ai/getting_started/concepts

OpenAI 调用示例:
https://github.com/run-llama/LlamaIndexTS/blob/main/examples/agent/large_toolcall_with_gpt4o.ts#L45

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
const llm = new OpenAI(init);
const response = await llm.chat({
tools: [interpreterTool],
messages: [
{ role: "system", content: systemPrompt },
{
role: "user",
content: [
{
type: "text",
text: userQuestion,
},
{
type: "text",
text: `Use data from following CSV raw contents:\n${csvData}`,
},
],
},
],
});

OpenAI.chat():
https://github.com/run-llama/LlamaIndexTS/blob/main/packages/providers/openai/src/llm.ts#L146
继承自 ToolCallLLM-BaseLLM

(我们用的)BaseLLM:
https://github.com/run-llama/LlamaIndexTS/blob/main/packages/core/src/llms/base.ts#L15

OpenAIAgent:
https://github.com/run-llama/LlamaIndexTS/blob/main/packages/providers/openai/src/agent.ts#L20
继承自 LLMAgent

LLMAgent:
https://github.com/run-llama/LlamaIndexTS/blob/main/packages/core/src/agent/llm.ts#L65

OpenAI 的 Embedding 代理:
https://github.com/run-llama/LlamaIndexTS/blob/main/packages/providers/openai/src/embedding.ts#L19
继承自 BaseEmbedding

(我们用的)BaseEmbedding:
https://github.com/run-llama/LlamaIndexTS/blob/main/packages/core/src/embeddings/base.ts#L22

使用 LlamaIndex 的优点:
结构化数据的 index,生成 3 种不同 size 的 chunk,增加召回的质量
我们可以只引用这部分包

分割生成 chunk

SentenceSplitter:
https://github.com/run-llama/LlamaIndexTS/blob/main/packages/core/src/node-parser/sentence-splitter.ts#L24

这个更好-MarkdownNodeParser:
https://legacy.ts.llamaindex.ai/modules/node_parser

召回-AutoMergingRetriever

https://docs.llamaindex.ai/en/stable/examples/retrievers/auto_merging_retriever/

使用本地模型

如何使用本地模型:
How can I connect an embedding model (e.g m3e-base) at local directory by using TS embedding configuration?
https://github.com/run-llama/LlamaIndexTS/issues/1106

设置本地模型路径:
/Users/leozhou/git/test/LlamaIndexTS/examples/node_modules/@huggingface/transformers/src/env.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
* @file Module used to configure Transformers.js.
*
* **Example:** Disable remote models.
* ```javascript
* import { env } from '@huggingface/transformers';
* env.allowRemoteModels = false;
* ```
*
* **Example:** Set local model path.
* ```javascript
* import { env } from '@huggingface/transformers';
* env.localModelPath = '/path/to/local/models/';
* ```
*
* **Example:** Set cache directory.
* ```javascript
* import { env } from '@huggingface/transformers';
* env.cacheDir = '/path/to/cache/directory/';
* ```
*
* @module env
*/

参数设置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
async function pipeline(
task,
model = null,
{
progress_callback = null,
config = null,
cache_dir = null,
local_files_only = false,
revision = 'main',
device = null,
dtype = null,
model_file_name = null,
session_options = {},
} = {}
) {}

// getModelFile方法:
if (_env_js__WEBPACK_IMPORTED_MODULE_2__.env.allowLocalModels) {
}

部署知识库(老版本)

下载模型

1
git clone <https://huggingface.co/moka-ai/>

设置模型路径

1
model = SentenceTransformer("../models/moka-ai_m3e-small")

安装依赖

1
2
cd datav_wiki/scripts
pip install pipenv flask sentence_transformers

启动

1
2
npm run serve
npm run start-chat

模型:

moka-ai/m3e-small

模型存放位置:

/data/models/m3e-small/

里面是几个 json 文件和一个 92M 的 pytorch_model.bin 文件。

资料

一文看懂 LlamaIndex 用法,为 LLMs 学习私有知识
https://www.cnblogs.com/chengnan113/p/17436377.html

LlamaIndex 官方年度巨献:高清大图纵览高级 RAG 技术
https://developer.volcengine.com/articles/7382252504068653066

使用 Llama-Index、Llama 3 和 Qdrant 构建高级重排-RAG 系统
https://blog.csdn.net/wjjc1017/article/details/138517803