Documentation Index
Fetch the complete documentation index at: https://langchain-zh.cn/llms.txt
Use this file to discover all available pages before exploring further.
本笔记本提供了快速入门 CSVLoader 文档加载器 的概述。有关 CSVLoader 所有功能和配置的详细文档,请参阅 API 参考。
本示例介绍了如何从 CSV 文件加载数据。第二个参数是要从 CSV 文件中提取的 column 列名。CSV 文件中的每一行将创建一个文档。当未指定 column 时,每一行将转换为键/值对,每个键/值对输出到文档 pageContent 的新行中。当指定 column 时,每一行创建一个文档,并使用指定列的值作为文档的 pageContent。
集成详情
要使用 CSVLoader 文档加载器,您需要安装 @langchain/community 集成包以及 d3-dsv@2 对等依赖项。
LangChain CSVLoader 集成位于 @langchain/community 集成包中。
npm install @langchain/community @langchain/core d3-dsv@2
实例化
现在我们可以实例化模型对象并加载文档:
import { CSVLoader } from "@langchain/community/document_loaders/fs/csv"
const exampleCsvPath = "../../../../../../langchain/src/document_loaders/tests/example_data/example_separator.csv";
const loader = new CSVLoader(exampleCsvPath)
const docs = await loader.load()
docs[0]
Document {
pageContent: 'id|html: 1|"<i>Corruption discovered at the core of the Banking Clan!</i>"',
metadata: {
source: '../../../../../../langchain/src/document_loaders/tests/example_data/example_separator.csv',
line: 1
},
id: undefined
}
console.log(docs[0].metadata)
{
source: '../../../../../../langchain/src/document_loaders/tests/example_data/example_separator.csv',
line: 1
}
用法:提取单列
示例 CSV 文件:
id|html
1|"<i>Corruption discovered at the core of the Banking Clan!</i>"
2|"<i>Reunited, Rush Clovis and Senator Amidala</i>"
3|"<i>discover the full extent of the deception.</i>"
4|"<i>Anakin Skywalker is sent to the rescue!</i>"
import { CSVLoader } from "@langchain/community/document_loaders/fs/csv";
const singleColumnLoader = new CSVLoader(
exampleCsvPath,
{
column: "html",
separator:"|"
}
);
const singleColumnDocs = await singleColumnLoader.load();
console.log(singleColumnDocs[0]);
Document {
pageContent: '<i>Corruption discovered at the core of the Banking Clan!</i>',
metadata: {
source: '../../../../../../langchain/src/document_loaders/tests/example_data/example_separator.csv',
line: 1
},
id: undefined
}
API 参考
有关 CSVLoader 所有功能和配置的详细文档,请参阅 API 参考。