|
| 1 | +import httpx |
| 2 | +from langgraph.graph import StateGraph |
| 3 | +from langgraph.types import Send |
| 4 | + |
| 5 | +from template_langgraph.agents.news_summarizer_agent.models import AgentState, Article, StructuredArticle |
| 6 | +from template_langgraph.llms.azure_openais import AzureOpenAiWrapper |
| 7 | +from template_langgraph.loggers import get_logger |
| 8 | + |
| 9 | +logger = get_logger(__name__) |
| 10 | + |
| 11 | + |
| 12 | +class MockNotifier: |
| 13 | + def notify(self, request_id: str, body: dict) -> None: |
| 14 | + """Simulate sending a notification to the user.""" |
| 15 | + logger.info(f"Notification sent for request {request_id}: {body}") |
| 16 | + |
| 17 | + |
| 18 | +class MockScraper: |
| 19 | + def scrape(self, url: str) -> str: |
| 20 | + """Simulate scraping a web page.""" |
| 21 | + return "<html><body><h1>Mocked web content</h1></body></html>" |
| 22 | + |
| 23 | + |
| 24 | +class HttpxScraper: |
| 25 | + def scrape(self, url: str) -> str: |
| 26 | + """Retrieve the HTML content of a web page.""" |
| 27 | + with httpx.Client() as client: |
| 28 | + response = client.get(url) |
| 29 | + response.raise_for_status() |
| 30 | + return response.text |
| 31 | + |
| 32 | + |
| 33 | +class MockSummarizer: |
| 34 | + def summarize( |
| 35 | + self, |
| 36 | + prompt: str, |
| 37 | + content: str, |
| 38 | + ) -> StructuredArticle: |
| 39 | + """Simulate summarizing the input.""" |
| 40 | + return StructuredArticle( |
| 41 | + title="Mocked Title", |
| 42 | + date="2023-01-01", |
| 43 | + summary=f"Mocked summary of the content: {content}, prompt: {prompt}", |
| 44 | + keywords=["mock", "summary"], |
| 45 | + score=75, |
| 46 | + ) |
| 47 | + |
| 48 | + |
| 49 | +class LlmSummarizer: |
| 50 | + def __init__(self, llm=AzureOpenAiWrapper().chat_model): |
| 51 | + self.llm = llm |
| 52 | + |
| 53 | + def summarize( |
| 54 | + self, |
| 55 | + prompt: str, |
| 56 | + content: str, |
| 57 | + ) -> StructuredArticle: |
| 58 | + """Use the LLM to summarize the input.""" |
| 59 | + logger.info(f"Summarizing input with LLM: {prompt}") |
| 60 | + return self.llm.with_structured_output(StructuredArticle).invoke( |
| 61 | + input=[ |
| 62 | + {"role": "system", "content": prompt}, |
| 63 | + {"role": "user", "content": content}, |
| 64 | + ] |
| 65 | + ) |
| 66 | + |
| 67 | + |
| 68 | +class NewsSummarizerAgent: |
| 69 | + def __init__( |
| 70 | + self, |
| 71 | + llm=AzureOpenAiWrapper().chat_model, |
| 72 | + notifier=MockNotifier(), |
| 73 | + scraper=MockScraper(), |
| 74 | + summarizer=MockSummarizer(), |
| 75 | + ): |
| 76 | + self.llm = llm |
| 77 | + self.notifier = notifier |
| 78 | + self.scraper = scraper |
| 79 | + self.summarizer = summarizer |
| 80 | + |
| 81 | + def create_graph(self): |
| 82 | + """Create the main graph for the agent.""" |
| 83 | + # Create the workflow state graph |
| 84 | + workflow = StateGraph(AgentState) |
| 85 | + |
| 86 | + # Create nodes |
| 87 | + workflow.add_node("initialize", self.initialize) |
| 88 | + workflow.add_node("fetch_web_content", self.fetch_web_content) |
| 89 | + workflow.add_node("notify", self.notify) |
| 90 | + |
| 91 | + # Create edges |
| 92 | + workflow.set_entry_point("initialize") |
| 93 | + workflow.add_conditional_edges( |
| 94 | + source="initialize", |
| 95 | + path=self.run_subtasks, |
| 96 | + ) |
| 97 | + workflow.add_edge("fetch_web_content", "notify") |
| 98 | + workflow.set_finish_point("notify") |
| 99 | + return workflow.compile( |
| 100 | + name=NewsSummarizerAgent.__name__, |
| 101 | + ) |
| 102 | + |
| 103 | + def initialize(self, state: AgentState) -> AgentState: |
| 104 | + """Initialize the agent state.""" |
| 105 | + logger.info(f"Initializing state: {state}") |
| 106 | + # FIXME: retrieve urls from user request |
| 107 | + return state |
| 108 | + |
| 109 | + def run_subtasks(self, state: AgentState) -> list[Send]: |
| 110 | + """Run the subtasks for the agent.""" |
| 111 | + logger.info(f"Running subtasks with state: {state}") |
| 112 | + return [ |
| 113 | + Send( |
| 114 | + node="fetch_web_content", |
| 115 | + arg=AgentState( |
| 116 | + input=state.input, |
| 117 | + output=state.output, |
| 118 | + target_url_index=idx, |
| 119 | + ), |
| 120 | + ) |
| 121 | + for idx, _ in enumerate(state.input.urls) |
| 122 | + ] |
| 123 | + |
| 124 | + def fetch_web_content(self, state: AgentState): |
| 125 | + url: str = state.input.urls[state.target_url_index] |
| 126 | + is_valid_url = url.startswith("http") |
| 127 | + is_valid_content = False |
| 128 | + content = "" |
| 129 | + |
| 130 | + # Check if the URL is valid |
| 131 | + if not is_valid_url: |
| 132 | + logger.error(f"Invalid URL: {url}") |
| 133 | + is_valid_content = False |
| 134 | + else: |
| 135 | + # Scrape the web content |
| 136 | + try: |
| 137 | + logger.info(f"Scraping URL: {url}") |
| 138 | + content = self.scraper.scrape(url) |
| 139 | + is_valid_content = True |
| 140 | + except httpx.RequestError as e: |
| 141 | + logger.error(f"Error fetching web content: {e}") |
| 142 | + |
| 143 | + if is_valid_content: |
| 144 | + logger.info(f"Summarizing content with LLM @ {state.target_url_index}: {url}") |
| 145 | + structured_article: StructuredArticle = self.summarizer.summarize( |
| 146 | + prompt=state.input.request, |
| 147 | + content=content, |
| 148 | + ) |
| 149 | + state.output.articles.append( |
| 150 | + Article( |
| 151 | + is_valid_url=is_valid_url, |
| 152 | + is_valid_content=is_valid_content, |
| 153 | + content=content, |
| 154 | + url=url, |
| 155 | + structured_article=structured_article, |
| 156 | + ), |
| 157 | + ) |
| 158 | + |
| 159 | + def notify(self, state: AgentState) -> AgentState: |
| 160 | + """Send notifications to the user.""" |
| 161 | + logger.info(f"Sending notifications with state: {state}") |
| 162 | + # Simulate sending notifications |
| 163 | + # convert list of articles to a dictionary for notification |
| 164 | + summary = {} |
| 165 | + for i, article in enumerate(state.output.articles): |
| 166 | + summary[i] = article.model_dump() |
| 167 | + self.notifier.notify( |
| 168 | + request_id=state.input.request_id, |
| 169 | + body=summary, |
| 170 | + ) |
| 171 | + return state |
| 172 | + |
| 173 | + |
| 174 | +# For testing |
| 175 | +# graph = NewsSummarizerAgent().create_graph() |
| 176 | + |
| 177 | +graph = NewsSummarizerAgent( |
| 178 | + notifier=MockNotifier(), |
| 179 | + scraper=HttpxScraper(), |
| 180 | + summarizer=LlmSummarizer(), |
| 181 | +).create_graph() |
0 commit comments