> For the complete documentation index, see [llms.txt](https://docs.neuron-ai.dev/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.neuron-ai.dev/neuron-v3-zh/rag/retrieval.md).

# 检索

### 介绍

RAG 模块有一个独立的检索组件，允许你实现不同策略来从外部数据源完成上下文检索。默认情况下，RAG 使用 `SimilarityRetrieval` 它只是查询向量存储以检索文档：

```php
namespace App\Neuron;

use NeuronAI\Providers\AIProviderInterface;
use NeuronAI\RAG\Embeddings\EmbeddingsProviderInterface;
use NeuronAI\RAG\RAG;
use NeuronAI\RAG\RAG\Retrieval\RetrievalInterface;
use NeuronAI\RAG\RAG\Retrieval\SimilarityRetrieval;
use NeuronAI\RAG\VectorStore\VectorStoreInterface;

class WorkoutTipsAgent extends RAG
{
    protected function retrieval(): RetrievalInterface
    {
        return new SimilarityRetrieval(
            $this->resolveVectorStore(),
            $this->resolveEmbeddingsProvider()
        );
    }
    
    protected function provider(): AIProviderInterface
    {
        // 返回一个 AI 提供者实例...
    }
    
    protected function embeddings(): EmbeddingsProviderInterface
    {
        // 返回一个嵌入提供者实例...
    }
    
    protected function vectorStore(): VectorStoreInterface
    {
        // 返回一个向量存储实例...
    }
}
```

实现 `RetrievalInterface` 你可以自由为你的 RAG 创建任何自定义检索行为。

```php
interface RetrievalInterface
{
    /**
     * 检索与给定查询相关的文档。
     *
     * @return Document[]
     */
    public function retrieve(Message $query): array;
}
```

如果你正在实现自定义工作流，你可以将检索作为一个独立组件，用于动态检索上下文数据，以供你的代理式系统使用。

### 作为工具的检索

Neuron 为你提供了一个内置的 `RetrievalTool` 该工具使 AI 代理能够在模型认为需要更多上下文来回答当前用户问题时，从向量存储中执行上下文检索。它建立在 `RetrievalInterface` 之上，从而可以构建具有按需 RAG（Retrieval Augmented Generation，检索增强生成）能力的代理，而不是由 RAG 组件提供的自动上下文注入。

下面是一个使用内置 `SimilarityRetrieval`:

```php
use NeuronAI\Tools\Toolkits\RetrievalTool;
use NeuronAI\RAG\Retrieval\SimilarityRetrieval;

class AgenticRAG extends Agent
{
    protected function provider(): AIProviderInterface
    {...}
    
    protected function instructions(): string
    {...}
    
    protected function tools(): array
    {
        return [
            RetrievalTool::make(
                new SimilarityRetrieval(
                    $this->vectorStore(), 
                    $this->embeddings()
                )
            ),
        ];
    }
    
    protected function vectorStore(): VectorStoreInterface
    {
        return new FileVectorStore(__DIR__);
    }
    
    protected function embeddings(): EmbeddingsProviderInterface
    {
        return new OllamaEmbeddingsProvider(
            model: 'OLLAMA_EMBEDDINGS_MODEL'
        );
    }
}
```

正如你在这个示例中看到的，我们并不扩展 RAG，而是使用基础的 Agent。在这个实现中，我们让模型决定是否需要搜索外部来源来回答用户问题。

你始终可以使用所有工具和代理方法来自定义描述、指令以及提示词，以便让模型按照你的使用场景进行表现。

### RAPTOR 检索模块

大多数检索增强模型通过将文档拆分为小块并仅检索最相关的部分来工作。然而，这种方法有一些局限：

* **上下文丢失**：仅检索小而孤立的片段可能会错过整体情况，尤其是对于上下文很长的文档。
* **多步推理困难**：有些问题需要来自文档多个部分的信息。

**在以下情况使用 RAPTOR：**

* 用户提出需要全面覆盖的开放式问题
* 你的领域涉及复杂主题，其中上下文和事实同样重要
* 你需要处理关于跨文档主题、趋势或关系的查询

**在以下情况下坚持使用传统 RAG：**

* 用户主要需要快速、具体的事实检索
* 处理速度和 token 效率是关键限制

在专门的仓库中了解更多关于 RAPTOR 的信息：

{% embed url="<https://github.com/neuron-core/raptor-retrieval>" %}
