Zenn
👩‍💻

LangChainのOpen Deep Research を動かしてみた(内部の動き)

2025/03/23に公開

本記事では、LangChainのOpen Deep Researchのレポート作成プロセスにおいて、ステップごとにどのような設計がされていてプロンプトが使われているかを解説します。
動作そのものは、LangChainのOpen Deep Research を動かしてみたを参照してください。
検索Queryの生成から、セクションのPlanning、レポートのまとめなど細かいプロンプトテクニックが利用されていることがわかります。これらのプロセスを段階的に示すことで、LLMの出力を効率的に活用し、ビジネスや学術分野で通用する充実したレポートを作り上げるための具体的なアプローチを理解できるのではと思います。

レポート作成の流れ

以下の流れは、LangChainのOpen Deep Research を動かしてみたで解説した流れです。

  1. トピックの入力
    Compare LangSmith to LangGraph to LangChain「LangSmithとLangGraphとLangChainを比較して」というように、レポートで扱うトピックを指定します。

  2. 構成案(Report Plan)の生成
    LLMがレポートのセクション案を提案してきます。

  3. Human in the loop でのフィードバック
    必要に応じてProvide 1 Section Demand in the world「世界の需要のセクションを入れて」などの指示を与えて再生成。

    問題なければ、Trueを返して先に進めます。

  4. 検索クエリの生成
    各セクションごとに最適化されたクエリ(例: “LangChain’s prompt engineering best practices” など)をLLMが提案し、指定した検索APIに送信します。

  5. セクションごとの原稿作成
    検索結果や既存のコンテキストを踏まえて、各セクションをLLMが文章化。

  6. 結論・導入文の作成
    最後にIntroductionとConclusionを生成します。

  7. レポートの統合・完成
    すべてのセクションを統合して最終版のレポートを出力します。


内部の動き

各Flowで内部的に実行されているプロンプトを追っていきます。

2.構成案(Report Plan)の生成

最初に関連する情報を収集するための検索クエリを2つ提示し、それをツールに渡す想定でプロンプトが作られています。
INPUT

You are performing research for a report.  
<Report topic>
Compare LangSmith to LangGraph to LangChain.
</Report topic>
<Report organization>
Use this structure to create a report on the user-provided topic:
1. Introduction (no research needed)
  - Brief overview of the topic area
2. Main Body Sections:
  - Each section should focus on a sub-topic of the user-provided topic
3. Conclusion
  - Aim for 1 structural element (either a list of table) that distills the main body sections 
  - Provide a concise summary of the report
</Report organization>
<Task>
Your goal is to generate 2 web search queries that will help gather information for planning the report sections. 
The queries should:
1. Be related to the Report topic
2. Help satisfy the requirements specified in the report organization
Make the queries specific enough to find high-quality, relevant sources while covering the breadth needed for the report structure.
</Task>
<Format>
Call the Queries tool
</Format>

OUTPUT

{
  "queries": [
    {
      "search_query": "Comparative analysis of LangSmith, LangGraph, and LangChain: features, architecture, and design differences"
    },
    {
      "search_query": "Performance, use cases, and integration differences among LangSmith, LangGraph, and LangChain in modern LLM pipelines"
    }
  ]
}

レポートの構成(Report organization)を「Introduction → Main Body Sections → Conclusion」の流れで作成するよう明示し、各セクションにどのような情報を盛り込むべきかという要件を詳しく指示します。また、検索クエリを生成するタスク(<Task>)として、2つの具体的なクエリを返す形式(JSON)を求めています。

次に検索結果からレポートの構成をPlanしています。
INPUT

I want a plan for a report that is concise and focused.

<Report topic>
The topic of the report is:
Compare LangSmith to LangGraph to LangChain.
</Report topic>

<Report organization>
The report should follow this organization: 
Use this structure to create a report on the user-provided topic:

1. Introduction (no research needed)
   - Brief overview of the topic area

2. Main Body Sections:
   - Each section should focus on a sub-topic of the user-provided topic
   
3. Conclusion
   - Aim for 1 structural element (either a list of table) that distills the main body sections 
   - Provide a concise summary of the report
</Report organization>

<Context>
 ここに検索結果が挿入される。省略。
</Context>

<Task>
Generate a list of sections for the report. Your plan should be tight and focused with NO overlapping sections or unnecessary filler. 

For example, a good report structure might look like:
1/ intro
2/ overview of topic A
3/ overview of topic B
4/ comparison between A and B
5/ conclusion

Each section should have the fields:

- Name - Name for this section of the report.
- Description - Brief overview of the main topics covered in this section.
- Research - Whether to perform web research for this section of the report.
- Content - The content of the section, which you will leave blank for now.

Integration guidelines:
- Include examples and implementation details within main topic sections, not as separate sections
- Ensure each section has a distinct purpose with no content overlap
- Combine related concepts rather than separating them

Before submitting, review your structure to ensure it has no redundant sections and follows a logical flow.
</Task>

<Feedback>
Here is feedback on the report structure from review (if any):
None
</Feedback>

<Format>
Call the Sections tool 
</Format>

OUTPUT

{
  "sections": [
    {
      "name": "Introduction",
      "description": "Provides a brief overview of language model frameworks and introduces the three key players: LangSmith, LangGraph, and LangChain.",
      "research": false,
      "content": ""
    },
    {
      "name": "Framework Overview and Context",
      "description": "Explains the primary focus and use cases for each framework. This section covers LangChain's modular pipeline approach, LangGraph's node-based customizations and graph visualizations, and LangSmith's role in monitoring and testing, referencing real-world application insights.",
      "research": false,
      "content": ""
    },,, 省略
  ]
}

3.Human in the loop でのフィードバック

Human in the loop でユーザーからのプラン変更のフィードバックを受け取ります。trueを入力すると次のステップに進みます。
INPUT

{
  "topic": "Compare LangSmith to LangGraph to LangChain.",
  "feedback_on_report_plan": "Provide 1 Section Demand in the world",
  "sections": [
    XXXX 現在のSection案
  ],
  "completed_sections": []
}

レポート構成案を受け取り、人間が「世界の需要のセクションを入れて」など追加要望を出す例がこのフェーズです。これに対して、再度LLMがセクション案を更新・再生成する形になります。Human in the loopのプロセスでは、人間によるフィードバックを繰り返し行い、より正確で目的に合ったセクション構成へ洗練させます。

4.検索クエリの生成

実際のセクション執筆に必要な情報を収集するために、各セクションに対応する検索クエリを生成するプロンプト例です。<Section topic>タグにどんな内容のセクションかを指定し、それに基づいてLLMが具体的な検索クエリを提案します。これによって、執筆に必要な外部ソースや関連するドキュメントを探すことが可能になります。
INPUT

You are an expert technical writer crafting targeted web search queries that will gather comprehensive information for writing a technical report section.  

<Report topic>
Compare LangSmith to LangGraph to LangChain.
</Report topic>

<Section topic>
Explores LangChain’s features, its role as a foundational toolbox for LLM application development, and its strengths in prompt engineering and modular pipeline creation.
</Section topic>

<Task>
Your goal is to generate 2 search queries that will help gather comprehensive information above the section topic. 

The queries should:
1. Be related to the topic 
2. Examine different aspects of the topic

Make the queries specific enough to find high-quality, relevant sources.
</Task>

<Format>
Call the Queries tool
</Format>

OUTPUT

{
  "queries": [
    {
      "search_query": "LangChain features as a foundational toolbox for LLM application development: examine its modular pipeline design and key role in prompt engineering"
    },
    {
      "search_query": "In-depth analysis of LangChain’s prompt engineering capabilities and modular architecture for LLM applications compared to tools like LangGraph and LangSmith"
    }
  ]
}

5.セクションごとの原稿作成

この段階では、検索結果などのソースを踏まえて、実際のセクション本文を書き上げるプロンプトを使用します。書き方のガイドライン(字数や言語の明瞭さ、Markdown形式、引用ルールなど)が具体的に示されているのが特徴です。執筆時に利用した情報源を明確に示す必要があり、引用(Citation)を番号付きで併記する形式を指示しています。
INPUT

Write one section of a research report.  

<Task>
1. Review the report topic, section name, and section topic carefully.
2. If present, review any existing section content.
3. Then, look at the provided Source material.
4. Decide the sources that you will use it to write a report section.
5. Write the report section and list your sources.
</Task>

<Writing Guidelines>
- If existing section content is not populated, write from scratch
- If existing section content is populated, synthesize it with the source material
- Strict 150-200 word limit
- Use simple, clear language
- Use short paragraphs (2-3 sentences max)
- Use ## for section title (Markdown format)
</Writing Guidelines>

<Citation Rules>
- Assign each unique URL a single citation number in your text
- End with ### Sources that lists each source with corresponding numbers
- IMPORTANT: Number sources sequentially without gaps (1,2,3,4...) in the final list regardless of which sources you choose
- Example format:
  [1] Source Title: URL
  [2] Source Title: URL
</Citation Rules>

<Final Check>
1. Verify that EVERY claim is grounded in the provided Source material
2. Confirm each URL appears ONLY ONCE in the Source list
3. Verify that sources are numbered sequentially (1,2,3...) without any gaps
</Final Check>

<Report topic>
Compare LangSmith to LangGraph to LangChain.
</Report topic>

<Section name>
LangChain Overview
</Section name>

<Section topic>
Explores LangChain’s features, its role as a foundational toolbox for LLM application development, and its strengths in prompt engineering and modular pipeline creation.
</Section topic>

<Existing section content (if populated)>

</Existing section content>

<Source material>
Content from sources: 省略
</Source material>

OUTPUT

## LangChain Overview

LangChain is a foundational toolbox for building applications powered by large language models. It provides robust features for prompt engineering, where developers can create dynamic, customizable prompt templates and chain them to achieve specific outputs [1]. This approach enables efficient and scalable language processing, handling intricate workflows such as text summarization and question generation.
...省略

### Sources
[1] LangChain: Powerful Prompt Template Frameworks for LLMs: https://rumeysakara.medium.com/langchain-powerful-prompt-template-frameworks-for-llms-47d07fbd231b  
...省略

6.結論・導入文の作成

ここでは、IntroductionとConclusionの書き方を指示するプロンプトの例が示されています。導入では「# 見出しのみ」「50~100語以内」などの厳密なフォーマットが課されているのがポイントです。結論(Conclusion)では比較表をMarkdownで書くなど、細かい指定があります。

You are an expert technical writer crafting a section that synthesizes information from the rest of the report.

<Report topic>
Compare LangSmith to LangGraph to LangChain.
</Report topic>

<Section name>
Introduction
</Section name>

<Section topic>
Provides a brief overview of language model frameworks and introduces the three main tools: LangSmith, LangGraph, and LangChain.
</Section topic>

<Available report content>
XXXX 省略
</Available report content>

<Task>
1. Section-Specific Approach:

For Introduction:
- Use # for report title (Markdown format)
- 50-100 word limit
- Write in simple and clear language
- Focus on the core motivation for the report in 1-2 paragraphs
- Use a clear narrative arc to introduce the report
- Include NO structural elements (no lists or tables)
- No sources section needed

For Conclusion/Summary:
- Use ## for section title (Markdown format)
- 100-150 word limit
- For comparative reports:
   * Must include a focused comparison table using Markdown table syntax
   * Table should distill insights from the report
   * Keep table entries clear and concise
- For non-comparative reports:
   * Only use ONE structural element IF it helps distill the points made in the report:
   * Either a focused table comparing items present in the report (using Markdown table syntax)
   * Or a short list using proper Markdown list syntax:
     - Use `*` or `-` for unordered lists
     - Use `1.` for ordered lists
     - Ensure proper indentation and spacing
- End with specific next steps or implications
- No sources section needed

3. Writing Approach:
- Use concrete details over general statements
- Make every word count
- Focus on your single most important point
</Task>

<Quality Checks>
- For introduction: 50-100 word limit, # for report title, no structural elements, no sources section
- For conclusion: 100-150 word limit, ## for section title, only ONE structural element at most, no sources section
- Markdown format
- Do not include word count or any preamble in your response
</Quality Checks>

7.レポートの統合・完成

最後に、すべてのセクションを統合して最終的なレポートを完成させます。各セクションで作成されたテキスト(Introduction、メインのセクション、結論など)を順番に結合し、全体として自然な流れになるよう調整します。


このように、レポート作成を目的とした一連のプロンプトチェーン(Prompt Engineering)は、細かな役割分担とステップごとの入念な指示によって構成されています。最終的には、ユーザーが求める構成と品質基準に沿ったレポートが生成されるように、複数のプロンプトとHuman in the loopでのフィードバック・再生成プロセスを繰り返しながら完成形に近づけていきます。

参考

当記事の内容は以下の参考文書を元にしています。
open_deep_research (LangChain) https://github.com/langchain-ai/open_deep_research
LangChainのOpen Deep Research を動かしてみた https://zenn.dev/dxclablinkage/articles/0a8d22db4d2048

Discussion

ログインするとコメントできます