-
Couldn't load subscription status.
- Fork 691
Adding an example of a scientific deep research #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
IvanReznikov
wants to merge
2
commits into
langchain-ai:master
Choose a base branch
from
IvanReznikov:scientific_agent
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| deepagents | ||
| langgraph-cli[inmem] | ||
| langchain-community | ||
| arxiv | ||
| semanticscholar | ||
| biopython | ||
| xmltodict |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok