diff --git a/.gitignore b/.gitignore index 2a23442f1..d1bac5ec8 100644 --- a/.gitignore +++ b/.gitignore @@ -67,3 +67,4 @@ tmp/ temp/ .langgraph_api +final_report.md diff --git a/README.md b/README.md index 1447167d5..ca1ed7ab2 100644 --- a/README.md +++ b/README.md @@ -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). diff --git a/run_research.py b/run_research.py new file mode 100644 index 000000000..7886754b6 --- /dev/null +++ b/run_research.py @@ -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())