information-retrieval

Exploration of information retrieval topics
git clone git://git.laack.co/information-retrieval.git
Log | Files | Refs

search.py (2819B)


      1 import pandas as pd
      2 import sys
      3 from datetime import datetime
      4 import os
      5 from smolagents import CodeAgent, OpenAIModel
      6 from web_search import WebSearchTool
      7 from web_search import WebVisitTool
      8 import os
      9 
     10 from smolagents import (
     11     CodeAgent,
     12     ToolCallingAgent,
     13 )
     14 
     15 
     16 custom_role_conversions = {"tool-call": "assistant", "tool-response": "user"}
     17 user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0"
     18 
     19 def create_agent(model):
     20 
     21     tools = [WebSearchTool(), WebVisitTool()]
     22 
     23     text_webbrowser_agent = ToolCallingAgent(
     24         model=model,
     25         tools=tools,
     26         max_steps=200,
     27         verbosity_level=2,
     28         planning_interval=4,
     29         name="search_agent",
     30         description=f"""A team member that will search the internet to answer your question.
     31     Ask him for all your questions that require browsing the web.
     32     Provide him as much context as possible, in particular if you need to search on a specific timeframe!
     33     And don't hesitate to provide him with a complex search task, like finding a difference between two webpages.
     34     Your request must be a real sentence, not a google search! Like "Find me this information (...)" rather than a few keywords. Note that the date today is {datetime.now().strftime('%B %d, %Y')}
     35     """,
     36         provide_run_summary=True,
     37     )
     38     text_webbrowser_agent.prompt_templates["managed_agent"]["task"] += """You can navigate to .txt online files.
     39     If a non-html page is in another format, especially .pdf or a Youtube video, use tool 'inspect_file_as_text' to inspect it.
     40     Additionally, if after some searching you find out that you need more information to answer the question, you can use `final_answer` with your request for clarification as argument to request for more information."""
     41 
     42     manager_agent = CodeAgent(
     43         model=model,
     44         tools=tools,
     45         max_steps=12,
     46         verbosity_level=2,
     47         additional_authorized_imports=["*"],
     48         planning_interval=4,
     49         managed_agents=[text_webbrowser_agent],
     50     )
     51 
     52     return manager_agent
     53 
     54 
     55 if __name__ == "__main__":
     56     model = OpenAIModel(
     57         model_id="claude-opus-4-5-20251101",
     58         api_base="https://api.anthropic.com/v1/",
     59         api_key=os.environ["ANTHROPIC_API_KEY"],
     60     )
     61 
     62     agent = create_agent(model)
     63 
     64     #answer = agent.run(sys.argv[1] + f"Note that the date today is {datetime.now().strftime('%B %d, %Y')}")
     65 
     66     df = pd.read_csv('questions.csv', quotechar='"', on_bad_lines='skip')
     67     itr = 0
     68     for index, row in df.iterrows():
     69         print(row['question'])
     70         answer = agent.run(row['question'] + f" Note that the date today is {datetime.now().strftime('%B %d, %Y')}")
     71         with open(str(itr) + '.txt', 'w') as f:
     72             f.write(str(answer))