> ## Documentation Index
> Fetch the complete documentation index at: https://docs.firecrawl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# LlamaIndex

> RAGアプリでFirecrawlとLlamaIndexを活用する

Webコンテンツを基にしたベクトル検索と埋め込みを用いて、FirecrawlをLlamaIndexと統合し、AIアプリケーションを構築します。

<div id="setup">
  ## セットアップ
</div>

```bash theme={null}
npm install llamaindex @llamaindex/openai firecrawl
```

「.env」ファイルを作成:

```bash theme={null}
FIRECRAWL_API_KEY=your_firecrawl_key
OPENAI_API_KEY=your_openai_key
```

> **注意:** Node \< 20 を使用する場合は、`dotenv` をインストールし、コードに `import 'dotenv/config'` を追加してください。

<div id="rag-with-vector-search">
  ## ベクトル検索を用いたRAG
</div>

この例では、LlamaIndex と Firecrawl を組み合わせてウェブサイトをクロールし、埋め込みを作成し、RAG を用いてコンテンツにクエリを実行する方法を示します。

```typescript theme={null}
import { Firecrawl } from 'firecrawl';
import { Document, VectorStoreIndex, Settings } from 'llamaindex';
import { OpenAI, OpenAIEmbedding } from '@llamaindex/openai';

Settings.llm = new OpenAI({ model: "gpt-4o" });
Settings.embedModel = new OpenAIEmbedding({ model: "text-embedding-3-small" });

const firecrawl = new Firecrawl({ apiKey: process.env.FIRECRAWL_API_KEY });
const crawlResult = await firecrawl.crawl('https://firecrawl.dev', {
  limit: 10,
  scrapeOptions: { formats: ['markdown'] }
});
console.log(`Crawled ${crawlResult.data.length } pages`);

const documents = crawlResult.data.map((page: any, i: number) =>
  new Document({
    text: page.markdown,
    id_: `page-${i}`,
    metadata: { url: page.metadata?.sourceURL }
  })
);

const index = await VectorStoreIndex.fromDocuments(documents);
console.log('Vector index created with embeddings');

const queryEngine = index.asQueryEngine();
const response = await queryEngine.query({ query: 'What is Firecrawl and how does it work?' });

console.log('\nAnswer:', response.toString());
```

より多くの例については、[LlamaIndex のドキュメント](https://ts.llamaindex.ai/)を参照してください。
