Skip to content
LlamaParse 文档版本 跟随上游 源快照 4dab0148 更新于 2026-10-16

文件操作可以让你处理索引里的单个文件。你能按名字查找文件、用正则在其解析后的内容里 grep、也能读取整个文件的文本 —— 全程不必接触原始文档。

这些端点在 SDK 里位于 client.retrieval.* 之下。

提示

兼容 MCP 的智能体可以通过 LlamaParse MCP 的 findFilesInIndex、grepFileFromIndex 与 readFileFromIndex 三个工具获得同样的能力。

查找文件

按完整文件名或子串匹配查找文件。

  • Python
  • TypeScript
  • Go
  • Java
  • CLI
# Exact name match
results = await client.retrieval.find(
    index_id="<your-index-id>",
    file_name="quarterly-report.pdf",
)

# Substring match (case-insensitive)
results = await client.retrieval.find(
    index_id="<your-index-id>",
    file_name_contains="quarterly",
)

for f in results.items:
    print(f"{f.file_id}: {f.file_name}")
// Substring match
const results = await client.retrieval.find({
  index_id: "<your-index-id>",
  file_name_contains: "quarterly",
});

for (const f of results.items) {
  console.log(`${f.file_id}: ${f.file_name}`);
}
// Exact name match
page, err := client.Retrieval.Find(ctx, llamacloud.RetrievalFindParams{
  IndexID:  "<your-index-id>",
  FileName: llamacloud.String("quarterly-report.pdf"),
})
if err != nil {
  log.Fatal(err)
}

// Substring match (case-insensitive)
page, err = client.Retrieval.Find(ctx, llamacloud.RetrievalFindParams{
  IndexID:          "<your-index-id>",
  FileNameContains: llamacloud.String("quarterly"),
})
if err != nil {
  log.Fatal(err)
}

for _, f := range page.Items {
  fmt.Printf("%s: %s\n", f.FileID, f.FileName)
}
import ai.llamaindex.llamacloud.models.retrieval.RetrievalFindPage;
import ai.llamaindex.llamacloud.models.retrieval.RetrievalFindParams;
import ai.llamaindex.llamacloud.models.retrieval.RetrievalFindResponse;

// Substring match (case-insensitive)
RetrievalFindPage results = client.retrieval().find(
    RetrievalFindParams.builder()
        .indexId("<your-index-id>")
        .fileNameContains("quarterly")
        .build());

for (RetrievalFindResponse f : results.items()) {
    System.out.println(f.fileId() + ": " + f.fileName());
}

终端窗口

# Exact name match
llp beta:retrieval find \
  --index-id "<your-index-id>" \
  --file-name "quarterly-report.pdf"

# Substring match (case-insensitive)
llp beta:retrieval find \
  --index-id "<your-index-id>" \
  --file-name-contains "quarterly" \
  | jq -r '"\(.file_id): \(.file_name)"'

在文件内 grep

在文件的解析文本里搜索一个正则模式。适合在文档里定位特定词语、模式或章节。

  • Python
  • TypeScript
  • Go
  • Java
  • CLI
found = await client.retrieval.find(
    index_id="<your-index-id>",
    file_name="quarterly-report.pdf",
)
if not found.items:
    raise SystemExit("quarterly-report.pdf is not in the index")
file_id = found.items[0].file_id

results = await client.retrieval.grep(
    index_id="<your-index-id>",
    file_id=file_id,
    pattern="revenue|profit",
    context_chars=100,  # characters of context around each match
)

for match in results.items:
    print(f"Offset {match.start_char}-{match.end_char}: {match.content}")
const found = await client.retrieval.find({
  index_id: "<your-index-id>",
  file_name: "quarterly-report.pdf",
});
if (found.items.length === 0) {
  throw new Error("quarterly-report.pdf is not in the index");
}
const fileId = found.items[0].file_id;

const results = await client.retrieval.grep({
  index_id: "<your-index-id>",
  file_id: fileId,
  pattern: "revenue|profit",
  context_chars: 100,
});

for (const match of results.items) {
  console.log(`Offset ${match.start_char}-${match.end_char}: ${match.content}`);
}
found, err := client.Retrieval.Find(ctx, llamacloud.RetrievalFindParams{
  IndexID:  "<your-index-id>",
  FileName: llamacloud.String("quarterly-report.pdf"),
})
if err != nil {
  log.Fatal(err)
}
if len(found.Items) == 0 {
  log.Fatal("quarterly-report.pdf is not in the index")
}
fileID := found.Items[0].FileID

page, err := client.Retrieval.Grep(ctx, llamacloud.RetrievalGrepParams{
  IndexID:      "<your-index-id>",
  FileID:       fileID,
  Pattern:      "revenue|profit",
  ContextChars: llamacloud.Int(100), // characters of context around each match
})
if err != nil {
  log.Fatal(err)
}

for _, match := range page.Items {
  fmt.Printf("Offset %d-%d: %s\n", match.StartChar, match.EndChar, match.Content)
}
import ai.llamaindex.llamacloud.models.retrieval.RetrievalFindPage;
import ai.llamaindex.llamacloud.models.retrieval.RetrievalFindParams;
import ai.llamaindex.llamacloud.models.retrieval.RetrievalGrepPage;
import ai.llamaindex.llamacloud.models.retrieval.RetrievalGrepParams;
import ai.llamaindex.llamacloud.models.retrieval.RetrievalGrepResponse;

RetrievalFindPage found = client.retrieval().find(
    RetrievalFindParams.builder()
        .indexId("<your-index-id>")
        .fileName("quarterly-report.pdf")
        .build());
if (found.items().isEmpty()) {
    throw new IllegalStateException("quarterly-report.pdf is not in the index");
}
String fileId = found.items().get(0).fileId();

RetrievalGrepPage results = client.retrieval().grep(
    RetrievalGrepParams.builder()
        .indexId("<your-index-id>")
        .fileId(fileId)
        .pattern("revenue|profit")
        .contextChars(100) // characters of context around each match
        .build());

for (RetrievalGrepResponse match : results.items()) {
    System.out.println("Offset " + match.startChar() + "-" + match.endChar() + ": " + match.content());
}

终端窗口

FILE_ID=$(llp beta:retrieval find \
  --index-id "<your-index-id>" \
  --file-name "quarterly-report.pdf" \
  --max-items 1 \
  | jq -r '.file_id // empty')
if [ -z "$FILE_ID" ]; then
  echo "could not find quarterly-report.pdf in the index" >&2
else
  llp beta:retrieval grep \
    --index-id "<your-index-id>" \
    --file-id "$FILE_ID" \
    --pattern "revenue|profit" \
    --context-chars 100 \
    | jq -r '"Offset \(.start_char)-\(.end_char): \(.content)"'
fi

Grep 参数

参数类型默认值说明
index_idstring必填索引 ID。
file_idstring必填要搜索的文件 ID。
patternstring必填要匹配的正则模式。
context_charsintnull每个匹配前后各带多少字符的上下文。

读取文件

读取文件已解析的文本内容。对大文档支持用 offset 与 max_length 分页。

  • Python
  • TypeScript
  • Go
  • Java
  • CLI
found = await client.retrieval.find(
    index_id="<your-index-id>",
    file_name="quarterly-report.pdf",
)
if not found.items:
    raise SystemExit("quarterly-report.pdf is not in the index")
file_id = found.items[0].file_id

# Read the full file
result = await client.retrieval.read(
    index_id="<your-index-id>",
    file_id=file_id,
)
print(result.content)

# Read a specific range
result = await client.retrieval.read(
    index_id="<your-index-id>",
    file_id=file_id,
    offset=1000,
    max_length=5000,
)
print(result.content)
const found = await client.retrieval.find({
  index_id: "<your-index-id>",
  file_name: "quarterly-report.pdf",
});
if (found.items.length === 0) {
  throw new Error("quarterly-report.pdf is not in the index");
}
const fileId = found.items[0].file_id;

// Read the full file
const result = await client.retrieval.read({
  index_id: "<your-index-id>",
  file_id: fileId,
});
console.log(result.content);

// Read a specific range
const partial = await client.retrieval.read({
  index_id: "<your-index-id>",
  file_id: fileId,
  offset: 1000,
  max_length: 5000,
});
console.log(partial.content);
found, err := client.Retrieval.Find(ctx, llamacloud.RetrievalFindParams{
  IndexID:  "<your-index-id>",
  FileName: llamacloud.String("quarterly-report.pdf"),
})
if err != nil {
  log.Fatal(err)
}
if len(found.Items) == 0 {
  log.Fatal("quarterly-report.pdf is not in the index")
}
fileID := found.Items[0].FileID

// Read the full file
result, err := client.Retrieval.Read(ctx, llamacloud.RetrievalReadParams{
  IndexID: "<your-index-id>",
  FileID:  fileID,
})
if err != nil {
  log.Fatal(err)
}
fmt.Println(result.Content)

// Read a specific range
partial, err := client.Retrieval.Read(ctx, llamacloud.RetrievalReadParams{
  IndexID:   "<your-index-id>",
  FileID:    fileID,
  Offset:    llamacloud.Int(1000),
  MaxLength: llamacloud.Int(5000),
})
if err != nil {
  log.Fatal(err)
}
fmt.Println(partial.Content)
import ai.llamaindex.llamacloud.models.retrieval.RetrievalFindPage;
import ai.llamaindex.llamacloud.models.retrieval.RetrievalFindParams;
import ai.llamaindex.llamacloud.models.retrieval.RetrievalReadParams;
import ai.llamaindex.llamacloud.models.retrieval.RetrievalReadResponse;

RetrievalFindPage found = client.retrieval().find(
    RetrievalFindParams.builder()
        .indexId("<your-index-id>")
        .fileName("quarterly-report.pdf")
        .build());
if (found.items().isEmpty()) {
    throw new IllegalStateException("quarterly-report.pdf is not in the index");
}
String fileId = found.items().get(0).fileId();

// Read the full file
RetrievalReadResponse result = client.retrieval().read(
    RetrievalReadParams.builder()
        .indexId("<your-index-id>")
        .fileId(fileId)
        .build());
System.out.println(result.content());

// Read a specific range
RetrievalReadResponse partial = client.retrieval().read(
    RetrievalReadParams.builder()
        .indexId("<your-index-id>")
        .fileId(fileId)
        .offset(1000)
        .maxLength(5000)
        .build());
System.out.println(partial.content());

终端窗口

FILE_ID=$(llp beta:retrieval find \
  --index-id "<your-index-id>" \
  --file-name "quarterly-report.pdf" \
  --max-items 1 \
  | jq -r '.file_id // empty')
if [ -z "$FILE_ID" ]; then
  echo "could not find quarterly-report.pdf in the index" >&2
else
  # Read the full file
  llp beta:retrieval read \
    --index-id "<your-index-id>" \
    --file-id "$FILE_ID" \
    | jq -r '.content'

  # Read a specific range
  llp beta:retrieval read \
    --index-id "<your-index-id>" \
    --file-id "$FILE_ID" \
    --offset 1000 \
    --max-length 5000 \
    | jq -r '.content'
fi

读取参数

参数类型默认值说明
index_idstring必填索引 ID。
file_idstring必填要读取的文件 ID。
offsetint0起始字符偏移。
max_lengthintnull从偏移起最多返回多少字符。省略则返回整个文件。

BytesRouter 是社区翻译站点,所有译文仅供参考,一切以官方英文原文为准。