Spaces:
Sleeping
Sleeping
| from agents import build_reader_agent, build_search_agent, writer_chain, critic_chain | |
| import time | |
| def extract_text(content) -> str: | |
| if isinstance(content, str): | |
| return content | |
| elif isinstance(content, list): | |
| return "".join(item.get("text", "") for item in content if isinstance(item, dict) and "text" in item) | |
| return str(content) | |
| def run_research_pipeline(topic: str) -> dict: | |
| state={} | |
| #Search Agent working | |
| print("\n" + "="*50 ) | |
| print("step 1 - search agent is wokring ...") | |
| print("=" *50) | |
| search_agent= build_search_agent() | |
| search_result= search_agent.invoke({ | |
| "messages": [("user", f"Find recent, reliable and detailed information about: {topic}")] | |
| }) | |
| state["search_results"]= extract_text(search_result['messages'][-1].content) | |
| print("\n search result", state['search_results']) | |
| # Introduce delay to prevent rate limits | |
| time.sleep(5) | |
| #Step 2 - reader agent | |
| print("\n" + "="*50) | |
| print("step 2- reader agent is scrapping top respurces ...") | |
| print("="*50) | |
| reader_agent= build_reader_agent() | |
| reader_result = reader_agent.invoke({ | |
| "messages": [("user", | |
| f"Based on the following search results about '{topic}'," | |
| f"pick the most relevant URL and scrape it for deeper content.\n\n" | |
| f"Search Results: \n{state['search_results'][:800]}" | |
| )] | |
| }) | |
| state['scraped_content']= extract_text(reader_result['messages'][-1].content) | |
| print("\nScraped content\n", state['scraped_content']) | |
| # Introduce delay to prevent rate limits | |
| time.sleep(5) | |
| #Step 3- writer chain | |
| print("\n" + "="*50) | |
| print("step 3- Writer is drafting the report ...") | |
| print("="*50) | |
| research_combined= ( | |
| f"Search Results: \n {state['search_results']}\n\n" | |
| f"Detailed Scraped Content: \n {state['scraped_content']}" | |
| ) | |
| state['report']= writer_chain.invoke({ | |
| "topic":topic, | |
| "research": research_combined | |
| }) | |
| print("\n final report\n", state['report']) | |
| #Critic Report | |
| print("\n" + "="*50) | |
| print("step 3- Critic is reviewing the report ...") | |
| print("="*50) | |
| # Introduce delay to prevent rate limits | |
| time.sleep(5) | |
| state['feedback']=critic_chain.invoke({ | |
| "report": state['report'] | |
| }) | |
| print("\n critic report \n", state['feedback']) | |
| return state | |
| if __name__ == "__main__": | |
| topic= input("\n Enter a research topic: " ) | |
| run_research_pipeline(topic) | |