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

# AutoGen

> Microsoft AutoGen のマルチエージェント会話で Firecrawl をツールとして使用します。

AIエージェント向けのコンテキスト API である Firecrawl を [Microsoft AutoGen](https://github.com/microsoft/autogen) と統合すると、マルチエージェント会話でリアルタイムのWeb検索ツールとスクレイピングツールを利用できるようになります。AutoGen は Microsoft によってアーカイブされているため、新規プロジェクトは [Microsoft Agent Framework](https://learn.microsoft.com/en-us/agent-framework/migration-guide/from-autogen/) で開始してください。

> [!NOTE]
>
> AutoGen は Microsoft によってアーカイブされており、現在はメンテナンスされていません。Microsoft は新規プロジェクトに [Microsoft Agent Framework](https://github.com/microsoft/agent-framework) を推奨しています。既存のプロジェクトは [移行ガイド](https://learn.microsoft.com/en-us/agent-framework/migration-guide/from-autogen/) に沿って移行できます。以下の Firecrawl 関数は通常の Python で書かれているため、移行先でもそのままツールとして再利用できます。

## セットアップ

```bash theme={null}
pip install -U "autogen-agentchat" "autogen-ext[openai]" firecrawl
```

キーを設定します：

```bash theme={null}
export FIRECRAWL_API_KEY=fc-YOUR-API-KEY
export OPENAI_API_KEY=sk-YOUR-OPENAI-KEY
```

## AutoGen ツールとしての Firecrawl

この例では、Firecrawl の `scrape` と `search` を AutoGen の関数ツールとして扱い、1 つの `AssistantAgent` がそれらを使って質問に回答できるようにします。

```python theme={null}
import asyncio
from firecrawl import Firecrawl
from autogen_agentchat.agents import AssistantAgent
from autogen_agentchat.ui import Console
from autogen_ext.models.openai import OpenAIChatCompletionClient

firecrawl = Firecrawl()


def scrape_url(url: str) -> str:
    """URLをスクレイピングしてクリーンなmarkdownを返す。"""
    result = firecrawl.scrape(url, formats=["markdown"])
    return result.markdown or ""


def web_search(query: str, limit: int = 5) -> list[dict]:
    """ウェブを検索して上位の結果を返す。"""
    result = firecrawl.search(query, limit=limit)
    return [
        {"title": r.title, "url": r.url, "snippet": r.description}
        for r in result.web or []
    ]


async def main() -> None:
    model = OpenAIChatCompletionClient(model="gpt-4o-mini")

    researcher = AssistantAgent(
        name="researcher",
        model_client=model,
        tools=[scrape_url, web_search],
        system_message=(
            "You are a web researcher. Use web_search to find candidate sources, "
            "then scrape_url to read the most relevant ones. Cite URLs in your answer."
        ),
    )

    await Console(
        researcher.run_stream(
            task="What does Firecrawl's /agent endpoint do? Cite the docs."
        )
    )


if __name__ == "__main__":
    asyncio.run(main())
```

次を実行します：

```bash theme={null}
python researcher.py
```

## マルチエージェント: リサーチャー + ライター

リサーチャーエージェントの Firecrawl の出力を、ラウンドロビン方式のチーム内のライターエージェントに渡します。

```python theme={null}
import asyncio
from firecrawl import Firecrawl
from autogen_agentchat.agents import AssistantAgent
from autogen_agentchat.conditions import MaxMessageTermination
from autogen_agentchat.teams import RoundRobinGroupChat
from autogen_agentchat.ui import Console
from autogen_ext.models.openai import OpenAIChatCompletionClient

firecrawl = Firecrawl()


def scrape_url(url: str) -> str:
    result = firecrawl.scrape(url, formats=["markdown"])
    return result.markdown or ""


def web_search(query: str, limit: int = 5) -> list[dict]:
    result = firecrawl.search(query, limit=limit)
    return [
        {"title": r.title, "url": r.url, "snippet": r.description}
        for r in result.web or []
    ]


async def main() -> None:
    model = OpenAIChatCompletionClient(model="gpt-4o-mini")

    researcher = AssistantAgent(
        name="researcher",
        model_client=model,
        tools=[scrape_url, web_search],
        system_message="Gather sources with web_search + scrape_url. Reply with bullet-point findings and URLs.",
    )

    writer = AssistantAgent(
        name="writer",
        model_client=model,
        system_message="Turn the researcher's findings into a 200-word briefing with inline citations.",
    )

    team = RoundRobinGroupChat(
        [researcher, writer],
        termination_condition=MaxMessageTermination(max_messages=6),
    )

    await Console(team.run_stream(task="Write a briefing on Firecrawl's crawl endpoint."))


if __name__ == "__main__":
    asyncio.run(main())
```

## 注意事項

* これらの例では同期型の `Firecrawl` クライアントを使用しています。小規模なワークロードであれば、AutoGen のイベントループ内でも問題なく動作します。高い並行性でスクレイピングする場合は、`AsyncFirecrawl` を `async def` ツールと組み合わせて使用するか、[バッチスクレイプ](https://docs.firecrawl.dev/ja/features/batch-scrape.md) を使用してください。
* `OpenAIChatCompletionClient` は、AutoGen がサポートする任意のモデルクライアント (Azure OpenAI、`autogen-ext` 経由の Anthropic、Ollama など) に置き換え可能です。Firecrawl は特定のモデルに依存しません。
* ラウンドロビン以外のエージェントパターン (selector、swarm、nested teams) については、[AutoGen docs](https://microsoft.github.io/autogen/) を参照してください。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.