Skip to content

feat: Add script for programmatic research execution #144

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
wants to merge 1 commit into
base: main
Choose a base branch
from
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,4 @@ tmp/
temp/

.langgraph_api
final_report.md
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,16 @@ python tests/run_evaluate.py

Follow the [quickstart](#-quickstart) to start LangGraph server locally and test the agent out on LangGraph Studio.

#### Command-Line Usage

To run the research process programmatically and save the final report to a file, you can use the `run_research.py` script.

```bash
python run_research.py "Your research query here"
```

This will create a `final_report.md` file in the root directory with the output.

#### Hosted deployment

You can easily deploy to [LangGraph Platform](https://langchain-ai.github.io/langgraph/concepts/#deployment-options).
Expand Down
37 changes: 37 additions & 0 deletions run_research.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import sys
import os
import asyncio
import argparse

# Add the src directory to the Python path before other imports
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), 'src')))

from langchain_core.messages import HumanMessage
from open_deep_research.deep_researcher import deep_researcher

async def main():
parser = argparse.ArgumentParser(description="Run the deep research agent.")
parser.add_argument("query", type=str, help="The research query.")
args = parser.parse_args()

config = {"configurable": {}}
inputs = {"messages": [HumanMessage(content=args.query)]}
final_report = ""

async for event in deep_researcher.astream_events(inputs, config=config, version="v2"):
kind = event["event"]
if kind == "on_chain_end":
if event["name"] == "final_report_generation":
final_report = event["data"]["output"]["final_report"]
print("Final report generated.")
break

if final_report:
with open("final_report.md", "w") as f:
f.write(final_report)
print("Final report saved to final_report.md")
else:
print("Could not generate final report.")

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