> ## 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.

# Gemini

> 将 Firecrawl 与 Google 的 Gemini AI 集成，用于网页抓取与 AI 工作流

将 Firecrawl 与 Google 的 Gemini 集成，以构建由网页数据驱动的 AI 应用。

<div id="setup">
  ## 安装与配置
</div>

```bash theme={null}
npm install firecrawl @google/genai
```

创建“.env”文件：

```bash theme={null}
FIRECRAWL_API_KEY=your_firecrawl_key
GEMINI_API_KEY=your_gemini_key
```

> **注意：** 如果使用 Node 版本低于 20，请安装 `dotenv`，并在代码中添加 `import 'dotenv/config'`。

<div id="scrape-summarize">
  ## 抓取 + 摘要
</div>

此示例演示了一个简单的工作流：抓取网站并使用 Gemini 对内容生成摘要。

```typescript theme={null}
import { Firecrawl } from 'firecrawl';
import { GoogleGenAI } from '@google/genai';

const firecrawl = new Firecrawl({ apiKey: process.env.FIRECRAWL_API_KEY });
const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });

const scrapeResult = await firecrawl.scrape('https://firecrawl.dev', {
    formats: ['markdown']
});

console.log('Scraped content length:', scrapeResult.markdown?.length);

const response = await ai.models.generateContent({
    model: 'gemini-2.5-flash',
    contents: `Summarize: ${scrapeResult.markdown}`,
});

console.log('Summary:', response.text);
```

<div id="content-analysis">
  ## 内容分析
</div>

此示例展示了如何使用 Gemini 的多轮对话功能来分析网页内容。

```typescript theme={null}
import { Firecrawl } from 'firecrawl';
import { GoogleGenAI } from '@google/genai';

const firecrawl = new Firecrawl({ apiKey: process.env.FIRECRAWL_API_KEY });
const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });

const scrapeResult = await firecrawl.scrape('https://news.ycombinator.com/', {
    formats: ['markdown']
});

console.log('Scraped content length:', scrapeResult.markdown?.length);

const chat = ai.chats.create({
    model: 'gemini-2.5-flash'
});

// 获取 Hacker News 上的前 3 条热门内容
const result1 = await chat.sendMessage({
    message: `Based on this website content from Hacker News, what are the top 3 stories right now?\n\n${scrapeResult.markdown}`
});
console.log('Top 3 Stories:', result1.text);

// 获取 Hacker News 上的第 4 和第 5 条热门内容
const result2 = await chat.sendMessage({
    message: `Now, what are the 4th and 5th top stories on Hacker News from the same content?`
});
console.log('4th and 5th Stories:', result2.text);
```

<div id="structured-extraction">
  ## 结构化数据提取
</div>

此示例演示了如何使用 Gemini 的 JSON 模式，从已抓取的网站内容中提取结构化数据。

```typescript theme={null}
import { Firecrawl } from 'firecrawl';
import { GoogleGenAI, Type } from '@google/genai';

const firecrawl = new Firecrawl({ apiKey: process.env.FIRECRAWL_API_KEY });
const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });

const scrapeResult = await firecrawl.scrape('https://stripe.com', {
    formats: ['markdown']
});

console.log('抓取的内容长度:', scrapeResult.markdown?.length);

const response = await ai.models.generateContent({
    model: 'gemini-2.5-flash',
    contents: `提取公司信息: ${scrapeResult.markdown}`,
    config: {
        responseMimeType: 'application/json',
        responseSchema: {
            type: Type.OBJECT,
            properties: {
                name: { type: Type.STRING },
                industry: { type: Type.STRING },
                description: { type: Type.STRING },
                products: {
                    type: Type.ARRAY,
                    items: { type: Type.STRING }
                }
            },
            propertyOrdering: ['name', 'industry', 'description', 'products']
        }
    }
});

console.log('已提取的公司信息:', response?.text);
```

更多示例请参阅 [Gemini 文档](https://ai.google.dev/docs)。
