Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,39 @@ result = agent.invoke({"messages": [{"role": "user", "content": "what is langgra

See [examples/research/research_agent.py](examples/research/research_agent.py) for a more complex example.

For scientific research tasks, we provide a specialized agent that searches across multiple academic databases:

```python
from examples.scientific_research.scientific_agent import scientific_agent

# Example: Research a complex chemical synthesis
research_question = """
Design a multi-step synthesis route for Taxol (Paclitaxel),
including retrosynthetic analysis, literature review of key reactions,
and detailed experimental procedures. Consider the challenges of stereochemistry,
protecting group strategies, and scalability.
"""

result = scientific_agent.invoke({
"messages": [{"role": "user", "content": research_question}]
})
```

The scientific research agent includes:
- **Multi-database search**: PubMed, arXiv, Semantic Scholar
- **Anti-hallucination measures**: Only uses information from search results
- **Professional output**: Scientific citation format and comprehensive reports
- **Parameterized queries**: Control results count and database selection

See [examples/scientific_research/scientific_agent.py](examples/scientific_research/scientific_agent.py) for the full implementation.

## Examples
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i like this section, but lets remove line 69 then

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok


The repository includes several examples of deep agents:

- [Research Agent](examples/research/research_agent.py) - A comprehensive research agent that can conduct deep research on any topic
- [Scientific Research Agent](examples/scientific_research/scientific_agent.py) - A specialized agent for scientific research using academic databases (PubMed, arXiv, Semantic Scholar)

The agent created with `create_deep_agent` is just a LangGraph graph - so you can interact with it (streaming, human-in-the-loop, memory, studio)
in the same way you would any LangGraph agent.

Expand Down Expand Up @@ -185,5 +218,5 @@ as well as custom instructions.
- [ ] Code cleanliness (type hinting, docstrings, formating)
- [ ] Allow for more of a robust virtual filesystem
- [ ] Create an example of a deep coding agent built on top of this
- [ ] Benchmark the example of [deep research agent](examples/research/research_agent.py)
- [ ] Benchmark the examples of [deep research agent](examples/research/research_agent.py)
- [ ] Add human-in-the-loop support for tools
7 changes: 7 additions & 0 deletions examples/scientific_research/langgraph.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"dependencies": ["."],
"graphs": {
"scientific_research": "./scientific_agent.py:scientific_agent"
},
"env": ".env"
}
7 changes: 7 additions & 0 deletions examples/scientific_research/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
deepagents
langgraph-cli[inmem]
langchain-community
arxiv
semanticscholar
biopython
xmltodict
227 changes: 227 additions & 0 deletions examples/scientific_research/scientific_agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
from typing import List
from langchain_community.agent_toolkits.load_tools import load_tools
from langchain_community.tools.semanticscholar.tool import SemanticScholarQueryRun
from langchain_community.tools.pubmed.tool import PubmedQueryRun

from deepagents import create_deep_agent


# Scientific literature search tool
def scientific_literature_search(
query: str,
max_results: int = 10,
databases: List[str] = ["pubmed", "arxiv", "semantic_scholar"],
):
"""
Search scientific literature across multiple databases

Args:
query: Search query for scientific literature
max_results: Maximum number of results per database (used by all databases)
databases: List of databases to search ("pubmed", "arxiv", "semantic_scholar")

Returns:
Dictionary with results from each specified database
"""
results = {}

if "pubmed" in databases:
try:
pubmed_tool = PubmedQueryRun()
# PubMed supports max_results parameter
pubmed_results = pubmed_tool.run(query, max_results=max_results)
results["pubmed"] = pubmed_results
except Exception as e:
results["pubmed"] = f"Error searching PubMed: {str(e)}"

if "arxiv" in databases:
try:
arxiv_tools = load_tools(["arxiv"])
# arXiv tool uses different parameter names
arxiv_results = arxiv_tools[0].run(query, max_results=max_results)
results["arxiv"] = arxiv_results
except Exception as e:
results["arxiv"] = f"Error searching arXiv: {str(e)}"

if "semantic_scholar" in databases:
try:
semantic_scholar_tool = SemanticScholarQueryRun()
# Semantic Scholar supports max_results parameter
semantic_results = semantic_scholar_tool.run(query, max_results=max_results)
results["semantic_scholar"] = semantic_results
except Exception as e:
results["semantic_scholar"] = f"Error searching Semantic Scholar: {str(e)}"

return results


sub_scientific_research_prompt = """You are a dedicated scientific researcher. Your job is to conduct scientific research based on the user's questions.

CRITICAL ANTI-HALLUCINATION RULES:
- ONLY use information that comes directly from the scientific literature search tools
- NEVER make up facts, data, or conclusions that are not supported by the search results
- If the search results don't contain enough information to answer the question, clearly state this limitation
- Always cite specific sources from the search results when making claims
- If you're unsure about something, say "Based on the available literature..." or "The search results indicate..."
- Do not extrapolate beyond what the search results explicitly state

Conduct thorough scientific literature research and then reply to the user with a detailed scientific answer to their question.

Only your FINAL answer will be passed on to the user. They will have NO knowledge of anything except your final message, so your final report should be your final message!"""

scientific_research_sub_agent = {
"name": "scientific-research-agent",
"description": "Used to research scientific questions in depth. Only give this researcher one topic at a time. Do not pass multiple sub questions to this researcher. Instead, you should break down a large topic into the necessary components, and then call multiple scientific research agents in parallel, one for each sub question.",
"prompt": sub_scientific_research_prompt,
"tools": ["scientific_literature_search"],
}

sub_scientific_critique_prompt = """You are a dedicated scientific editor. You are being tasked to critique a scientific report.

You can find the report at `scientific_report.md`.

You can find the question/topic for this report at `research_question.txt`.

CRITICAL ANTI-HALLUCINATION RULES:
- ONLY critique based on information from the search tools or the actual content of the report
- NEVER make up facts or suggest improvements that aren't grounded in the search results
- If you suggest additional research, only recommend searching for specific, well-defined topics
- Always base your critiques on concrete evidence from the literature or the report itself

The user may ask for specific areas to critique the report in. Respond to the user with a detailed critique of the scientific report. Things that could be improved.

You can use the search tool to search for information, if that will help you critique the report.

Do not write to the `scientific_report.md` yourself.

Things to check:
- Check that each section is appropriately named and follows scientific conventions
- Check that the report is written as you would find in a scientific journal - it should be technical and detailed
- Check that the report is comprehensive. If any paragraphs or sections are short, or missing important details, point it out.
- Check that the article covers key scientific concepts, ensures overall understanding, and does not omit important parts.
- Check that the article deeply analyzes mechanisms, experimental procedures, and provides valuable scientific insights
- Check that the article closely follows the research topic and directly answers scientific questions
- Check that the article has a clear structure, uses proper scientific terminology, and is easy to understand for the target audience.
- Check that chemical structures, reaction schemes, and experimental procedures are properly described
- Check that citations follow proper scientific format
- CRITICAL: Verify that all claims are supported by the cited sources and search results
"""

scientific_critique_sub_agent = {
"name": "scientific-critique-agent",
"description": "Used to critique the final scientific report. Give this agent some information about how you want it to critique the report.",
"prompt": sub_scientific_critique_prompt,
}


# Prompt prefix to steer the agent to be an expert scientific researcher
scientific_instructions = """You are an expert scientific researcher. Your job is to conduct thorough scientific research, and then write a polished scientific report.

CRITICAL ANTI-HALLUCINATION RULES:
- ONLY use information that comes directly from the scientific literature search tools
- NEVER make up facts, data, or conclusions that are not supported by the search results
- If the search results don't contain enough information to answer the question, clearly state this limitation
- Always cite specific sources from the search results when making claims
- If you're unsure about something, say "Based on the available literature..." or "The search results indicate..."
- Do not extrapolate beyond what the search results explicitly state
- When writing the report, only include information that has been verified through the search tools

The first thing you should do is to write the original user question to `research_question.txt` so you have a record of it.

Use the scientific-research-agent to conduct deep scientific research. It will respond to your questions/topics with a detailed scientific answer.

When you think you have enough information to write a final scientific report, write it to `scientific_report.md`

You can call the scientific-critique-agent to get a critique of the final report. After that (if needed) you can do more research and edit the `scientific_report.md`
You can do this however many times you want until you are satisfied with the result.

Only edit the file once at a time (if you call this tool in parallel, there may be conflicts).

Here are instructions for writing the final scientific report:

<scientific_report_instructions>

CRITICAL: Make sure the answer is written in the same language as the human messages! If you make a todo plan - you should note in the plan what language the report should be in so you don't forget!
Note: the language the report should be in is the language the QUESTION is in, not the language/country that the question is ABOUT.

Please create a detailed scientific answer to the overall research brief that:
1. Is well-organized with proper headings (# for title, ## for sections, ### for subsections)
2. Includes specific scientific facts and insights from the research
3. References relevant scientific papers using proper citation format
4. Provides a balanced, thorough scientific analysis. Be as comprehensive as possible, and include all information that is relevant to the overall research question. People are using you for deep scientific research and will expect detailed, comprehensive answers.
5. Includes a "References" section at the end with all cited papers
6. Uses proper scientific terminology and conventions

You can structure your scientific report in a number of different ways. Here are some examples:

For synthesis planning:
1. Introduction and target molecule analysis
2. Retrosynthetic analysis
3. Literature review of key reactions
4. Proposed synthesis route
5. Experimental procedures
6. Discussion and optimization strategies
7. Conclusion and future directions

For reaction mechanism analysis:
1. Introduction and reaction overview
2. Literature review of similar reactions
3. Mechanistic analysis
4. Experimental considerations
5. Optimization strategies
6. Conclusion

For materials research:
1. Introduction and materials overview
2. Literature review of synthesis methods
3. Structure-property relationships
4. Characterization techniques
5. Applications and performance
6. Future research directions

For each section of the report, do the following:
- Use clear, technical scientific language
- Use ## for section title (Markdown format) for each section of the report
- Do NOT ever refer to yourself as the writer of the report. This should be a professional scientific report without any self-referential language.
- Do not say what you are doing in the report. Just write the report without any commentary from yourself.
- Each section should be as long as necessary to deeply answer the question with the information you have gathered. It is expected that sections will be fairly long and verbose. You are writing a deep scientific research report, and users will expect a thorough answer.
- Use bullet points to list out information when appropriate, but by default, write in paragraph form.
- Include chemical structures using SMILES notation or descriptive text where appropriate
- Provide detailed experimental procedures with specific conditions where relevant
- Include relevant data, yields, and characterization results where applicable
- CRITICAL: Only include information that comes directly from the search results - do not add speculative or unsupported claims

REMEMBER:
The brief and research may be in English, but you need to translate this information to the right language when writing the final answer.
Make sure the final answer report is in the SAME language as the human messages in the message history.

Format the report in clear markdown with proper structure and include source references where appropriate.

<Citation Rules>
- Use proper scientific citation format (e.g., Author et al., Journal, Year)
- Assign each unique reference a citation number in your text
- End with ### References that lists each source with corresponding numbers
- Include DOI links where available
- Example format:
[1] Author, A. et al. Journal Name 2023, Volume, Pages. DOI: 10.xxxx/xxxxx
- Citations are extremely important in scientific writing
- CRITICAL: Only cite sources that actually appear in the search results - do not make up citations
- If you cannot find specific information in the search results, clearly state this limitation
</Citation Rules>
</scientific_report_instructions>

You have access to scientific research tools.

## `scientific_literature_search`

Use this to search across multiple scientific databases (PubMed, arXiv, Semantic Scholar) for scientific literature. You can specify the number of results, whether to include abstracts, and which databases to search.

IMPORTANT: This is your ONLY source of information. Do not use any knowledge outside of what this tool returns. If the search results don't contain enough information to fully answer the question, clearly state what information is missing and what additional searches would be needed.
"""

# Create the scientific agent
scientific_agent = create_deep_agent(
[scientific_literature_search],
scientific_instructions,
subagents=[scientific_critique_sub_agent, scientific_research_sub_agent],
).with_config({"recursion_limit": 1000})