Add src/agent.py

This commit is contained in:
2026-06-05 12:37:35 +00:00
parent 7dcadfb251
commit 7410948700
+45
View File
@@ -0,0 +1,45 @@
"""Agent wrapper that uses LangChain's create_agent.
This module demonstrates how to create an agent that can run the
research brief graph. It imports ``create_agent`` from
``langchain.agents`` as required by the task.
"""
from langchain.agents import create_agent
from langchain_openai import ChatOpenAI
from langchain_core.tools import BaseTool
from typing import Dict, Any
# Import the graph builder
from .brief import build_graph
class RunBriefTool(BaseTool):
"""Tool that runs the research brief graph for a given topic."""
name: str = "run_brief"
description: str = "Run the research brief graph for a given topic."
def _run(self, topic: str) -> str:
# Build the graph and run it
graph = build_graph()
state: Dict[str, Any] = {
"topic": topic,
"outline": None,
"step_index": 0,
"notes": [],
"final_brief": None,
}
result = graph.invoke(state)
return result.get("final_brief", "No brief produced.")
# Create the agent using the required create_agent function
llm = ChatOpenAI()
agent = create_agent(model=llm, tools=[RunBriefTool()])
# Expose a simple helper for external use
def run_agent(topic: str) -> str:
"""Invoke the agent with the given topic and return the brief."""
response = agent.invoke({"messages": [{"role": "user", "content": topic}]})
# The agent will return the tool output as a string
return response