{"repo": "bahathabet/agentic-search", "files": {"Agentic_Web_Search.ipynb": {"f8fde8a072928c279e18d526f7467c3c1f4b33b1": {"t": 1785745468.0910683, "n": 10050, "h": "5cf9dd6eecdda8b1", "c": "{\n \"cells\": [\n {\n \"cell_type\": \"code\",\n \"execution_count\": 2,\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"from dotenv import load_dotenv\\n\",\n \"from openai import OpenAI, AsyncOpenAI\\n\",\n \"from agents import Agent, Runner, trace, function_tool, OpenAIChatCompletionsModel, input_guardrail, GuardrailFunctionOutput, gen_trace_id\\n\",\n \"from typing import Dict\\n\",\n \"import os\\n\",\n \"import asyncio\\n\",\n \"from bs4 import BeautifulSoup\\n\",\n \"import requests\\n\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"metadata\": {},\n \"outputs\": [],\n \"source\": []\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": 3,\n \"metadata\": {},\n \"outputs\": [\n {\n \"name\": \"stdout\",\n \"output_type\": \"stream\",\n \"text\": [\n \"Openai_Api_Key exists and begins sk-p\\n\",\n \"Google_Api_Key exists and begins AIza\\n\",\n \"Deepseek_Api_Key exists and begins sk-3\\n\",\n \"Anthropic_Api_Key exists and begins sk-a\\n\",\n \"Gsearch_Api_Key exists and begins AIza\\n\",\n \"Search_Engine_Id exists and begins b5a9\\n\"\n ]\n }\n ],\n \"source\": [\n \"load_dotenv(override=True)\\n\",\n \"\\n\",\n \"# Explicit variable assignments\\n\",\n \"openai_api_key = os.getenv('OPENAI_API_KEY')\\n\",\n \"google_api_key = os.getenv('GOOGLE_API_KEY')\\n\",\n \"deepseek_api_key = os.getenv('DEEPSEEK_API_KEY')\\n\",\n \"anthropic_api_key = os.getenv('ANTHROPIC_API_KEY')\\n\",\n \"gsearch_api_key = os.getenv('GSEARCH_API_KEY')\\n\",\n \"search_engine_id = os.getenv('SEARCH_ENGINE_ID')\\n\",\n \"\\n\",\n \"# List of variable names (as strings)\\n\",\n \"var_names = [\\n\",\n \" \\\"openai_api_key\\\", \\\"google_api_key\\\", \\\"deepseek_api_key\\\", \\\"anthropic_api_key\\\",\\n\",\n \" \\\"gsearch_api_key\\\", \\\"search_engine_id\\\",\\n\",\n \"]\\n\",\n \"\\n\",\n \"\\n\",\n \"for var in var_names:\\n\",\n \" value = locals()[var]\\n\",\n \" label = var.title()\\n\",\n \" if value:\\n\",\n \" print(f\\\"{label} exists and begins {value[:4]}\\\")\\n\",\n \" else:\\n\",\n \" print(f\\\"{label} not set\\\")\\n\",\n \" \"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": 3,\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"def fetch_search_links(query: str) -> list[str]:\\n\",\n \" \\\"\\\"\\\"\\n\",\n \" Queries the Google Custom Search API and returns a list of formatted URLs.\\n\",\n \" \\n\",\n \" Returns:\\n\",\n \" list[str]: A list of formatted URLs returned by the search engine.\\n\",\n \" \\n\",\n \" Raises:\\n\",\n \" RuntimeError: If the request fails or JSON response is invalid.\\n\",\n \" \\\"\\\"\\\"\\n\",\n \" url = 'https://www.googleapis.com/customsearch/v1'\\n\",\n \" max_results = 5\\n\",\n \" params = {\\n\",\n \" 'q': query,\\n\",\n \" 'key': gsearch_api_key,\\n\",\n \" 'cx': search_engine_id,\\n\",\n \" 'num': max_results\\n\",\n \" }\\n\",\n \"\\n\",\n \" try:\\n\",\n \" response = requests.get(url, params=params)\\n\",\n \" response.raise_for_status()\\n\",\n \" results = response.json()\\n\",\n \"\\n\",\n \" items = results.get('items', [])\\n\",\n \" links = [item.get('formattedUrl') for item in items if item.get('formattedUrl')]\\n\",\n \"\\n\",\n \" return links\\n\",\n \"\\n\",\n \" except requests.exceptions.RequestException as e:\\n\",\n \" raise RuntimeError(f\\\"Request failed: {e}\\\")\\n\",\n \" except ValueError:\\n\",\n \" raise RuntimeError(\\\"Error parsing JSON response.\\\")\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": 4,\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"def fetch_website_contents(url: str) -> str:\\n\",\n \" headers = {\\n\",\n \" \\\"User-Agent\\\": (\\n\",\n \" \\\"Mozilla/5.0 (Windows NT 10.0; Win64; x64) \\\"\\n\",\n \" \\\"AppleWebKit/537.36 (KHTML, like Gecko) \\\"\\n\",\n \" \\\"Chrome/117.0.0.0 Safari/537.36\\\"\\n\",\n \" )\\n\",\n \" }\\n\",\n \"\\n\",\n \" try:\\n\",\n \" response = requests.get(url, headers=headers, timeout=10)\\n\",\n \" response.raise_for_status()\\n\",\n \" soup = BeautifulSoup(response.content, 'html.parser')\\n\",\n \"\\n\",\n \" title = soup.title.get_text(strip=True) if soup.title else \\\"No title found\\\"\\n\",\n \"\\n\",\n \" if soup.body:\\n\",\n \" for tag in soup.body([\\\"script\\\", \\\"style\\\", \\\"img\\\", \\\"input\\\", \\\"noscript\\\"]):\\n\",\n \" tag.decompose()\\n\",\n \" text = soup.body.get_text(separator=\\\"\\\\n\\\", strip=True)\\n\",\n \" text = remove_short_lines(text)\\n\",\n \" else:\\n\",\n \" text = \\\"\\\"\\n\",\n \"\\n\",\n \" return f\\\"Webpage Title:\\\\n{title}\\\\n\\\\nWebpage Contents:\\\\n{text}\\\\n\\\"\\n\",\n \"\\n\",\n \" except requests.RequestException as e:\\n\",\n \" return f\\\"[ERROR] Failed to fetch {url}: {e}\\\"\\n\",\n \" except Exception as e:\\n\",\n \" return f\\\"[ERROR] Unexpected error: {e}\\\"\\n\",\n \" \\n\",\n \"def remove_short_lines(text: str)-> str:\\n\",\n \" min_words=3\\n\",\n \" lines = text.splitlines()\\n\",\n \" filtered = [line for line in lines if len(line.split()) >= min_words]\\n\",\n \" return \\\"\\\\n\\\".join(filtered)\\n\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": 8,\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"@function_tool\\n\",\n \"def deep_search_tool(query: str) -> str:\\n\",\n \" links = fetch_search_links(query)\\n\",\n \" content = \\\"\\\"\\n\",\n \" content = f\\\"{content} \\\\nSource URLs:\\\\n{\\\"\\\\n\\\".join(links)}\\\"\\n\",\n \" for link in links:\\n\",\n \" content = content + f\\\"\\\\n\\\\n## New Search Source:\\\\nSource Content:\\\\n\\\" + fetch_website_contents(link)\\n\",\n \" \\n\",\n \" return content\\n\",\n \" \\n\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": 9,\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"\\n\",\n \"GEMINI_BASE_URL = \\\"https://generativelanguage.googleapis.com/v1beta/openai/\\\"\\n\",\n \"DEEPSEEK_BASE_URL = \\\"https://api.deepseek.com/v1\\\"\\n\",\n \"CLAUDE_BASE_URL = \\\"https://api.anthropic.com/v1/\\\"\\n\",\n \"\\n\",\n \"claude_client = AsyncOpenAI(base_url=CLAUDE_BASE_URL, api_key=anthropic_api_key)\\n\",\n \"deepseek_client = AsyncOpenAI(base_url=DEEPSEEK_BASE_URL, api_key=deepseek_api_key)\\n\",\n \"lama_client = AsyncOpenAI(base_url='http://localhost:11434/v1', api_key='ollama')\\n\",\n \"# gemini_client = AsyncOpenAI(base_url=GEMINI_BASE_URL, api_key=google_api_key)\\n\",\n \"\\n\",\n \"deepseek_model = OpenAIChatCompletionsModel(model=\\\"deepseek-chat\\\", openai_client=deepseek_client)\\n\",\n \"claude_model=OpenAIChatCompletionsModel(model=\\\"claude-3-7-sonnet-20250219\\\", openai_client=claude_client)\\n\",\n \"llama_model = OpenAIChatCompletionsModel(model=\\\"llama3.2:latest\\\", openai_client=lama_client)\\n\",\n \"# Gemini model does not work with OpenAi Agents SDK\\n\",\n \"# gemini_model = OpenAIChatCompletionsModel(model=\\\"gemini-2.0-flash\\\", openai_client=gemini_client)\\n\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": 17,\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"sys_text = \\\"\\\"\\\" You are an assistant agent who can answer questions for users. \\\\\\n\",\n \" if the user asks you any question, then do not answer directly but \\\\\\n\",\n \" use the provided deep search tools only to get more recent information \\\\\\n\",\n \" related to the question. \\\\nYou should use the deep_search_tool tool \\\\\\n\",\n \" to get a context text which you can use to answer questions. \\\\nBe professional and use \\\\\\n\",\n \" the given tool and strictly answer questions using the context given by the tool.\\\\n\\\\\\n\",\n \" ##\\\\n\\\\n You should provide at the end of your response all the source urls, for example: 'https://www.website.com/pages'\\\\\\n\",\n \" \\\"\\\"\\\"\\n\",\n \"\\n\",\n \"# text = \\\"what are the stock market summary in Germany for May 2025?\\\"\\n\",\n \"text = \\\"what are the Agentic AI trends in 2025?\\\"\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": 11,\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"llama_agent = Agent(name=\\\"llama_agent\\\", instructions=sys_text, model=llama_model, tools=[deep_search_tool])\\n\",\n \"claudeAgent = Agent(name=\\\"claude_agent\\\", instructions=sys_text, model=claude_model, tools=[deep_search_tool])\\n\",\n \"deepseek_agent = Agent(name=\\\"deepseek_agent: deepseek_model\\\", model=deepseek_model, instructions=sys_text, tools=[deep_search_tool])\\n\",\n \"# for openai models, you need to define the model name in the model parameter, no need to define the client and model\\n\",\n \"gpt_agent = Agent(name=\\\"chat_agent: gpt-4o-mini\\\", model=\\\"gpt-4o-mini\\\", instructions=sys_text, tools=[deep_search_tool])\\n\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"with trace(\\\"searcher_agent_\\\"):\\n\",\n \" result = await Runner.run(gpt_agent, text)\\n\",\n \" print(result.final_output)\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"with trace(\\\"searcher_agents\\\"):\\n\",\n \" results = await asyncio.gather(\\n\",\n \" Runner.run(deepseek_agent, text),\\n\",\n \" Runner.run(claudeAgent, text),\\n\",\n \" Runner.run(gpt_agent, text),\\n\",\n \" Runner.run(llama_agent, text)\\n\",\n \" )\\n\",\n \"\\n\",\n \"outputs = [result.final_output for result in results]\\n\",\n \"\\n\",\n \"for output in outputs:\\n\",\n \" print(output + \\\"\\\\n\\\\n\\\")\"\n ]\n }\n ],\n \"metadata\": {\n \"kernelspec\": {\n \"display_name\": \".venv\",\n \"language\": \"python\",\n \"name\": \"python3\"\n },\n \"language_info\": {\n \"codemirror_mode\": {\n \"name\": \"ipython\",\n \"version\": 3\n },\n \"file_extension\": \".py\",\n \"mimetype\": \"text/x-python\",\n \"name\": \"python\",\n \"nbconvert_exporter\": \"python\",\n \"pygments_lexer\": \"ipython3\",\n \"version\": \"3.12.10\"\n }\n },\n \"nbformat\": 4,\n \"nbformat_minor\": 2\n}\n"}, "7161d8654cc284cf0d86edc5d82cb3579d664d09": {"t": 1785753551.9507868, "n": 10050, "h": "5cf9dd6eecdda8b1", "c": "{\n \"cells\": [\n {\n \"cell_type\": \"code\",\n \"execution_count\": 2,\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"from dotenv import load_dotenv\\n\",\n \"from openai import OpenAI, AsyncOpenAI\\n\",\n \"from agents import Agent, Runner, trace, function_tool, OpenAIChatCompletionsModel, input_guardrail, GuardrailFunctionOutput, gen_trace_id\\n\",\n \"from typing import Dict\\n\",\n \"import os\\n\",\n \"import asyncio\\n\",\n \"from bs4 import BeautifulSoup\\n\",\n \"import requests\\n\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"metadata\": {},\n \"outputs\": [],\n \"source\": []\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": 3,\n \"metadata\": {},\n \"outputs\": [\n {\n \"name\": \"stdout\",\n \"output_type\": \"stream\",\n \"text\": [\n \"Openai_Api_Key exists and begins sk-p\\n\",\n \"Google_Api_Key exists and begins AIza\\n\",\n \"Deepseek_Api_Key exists and begins sk-3\\n\",\n \"Anthropic_Api_Key exists and begins sk-a\\n\",\n \"Gsearch_Api_Key exists and begins AIza\\n\",\n \"Search_Engine_Id exists and begins b5a9\\n\"\n ]\n }\n ],\n \"source\": [\n \"load_dotenv(override=True)\\n\",\n \"\\n\",\n \"# Explicit variable assignments\\n\",\n \"openai_api_key = os.getenv('OPENAI_API_KEY')\\n\",\n \"google_api_key = os.getenv('GOOGLE_API_KEY')\\n\",\n \"deepseek_api_key = os.getenv('DEEPSEEK_API_KEY')\\n\",\n \"anthropic_api_key = os.getenv('ANTHROPIC_API_KEY')\\n\",\n \"gsearch_api_key = os.getenv('GSEARCH_API_KEY')\\n\",\n \"search_engine_id = os.getenv('SEARCH_ENGINE_ID')\\n\",\n \"\\n\",\n \"# List of variable names (as strings)\\n\",\n \"var_names = [\\n\",\n \" \\\"openai_api_key\\\", \\\"google_api_key\\\", \\\"deepseek_api_key\\\", \\\"anthropic_api_key\\\",\\n\",\n \" \\\"gsearch_api_key\\\", \\\"search_engine_id\\\",\\n\",\n \"]\\n\",\n \"\\n\",\n \"\\n\",\n \"for var in var_names:\\n\",\n \" value = locals()[var]\\n\",\n \" label = var.title()\\n\",\n \" if value:\\n\",\n \" print(f\\\"{label} exists and begins {value[:4]}\\\")\\n\",\n \" else:\\n\",\n \" print(f\\\"{label} not set\\\")\\n\",\n \" \"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": 3,\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"def fetch_search_links(query: str) -> list[str]:\\n\",\n \" \\\"\\\"\\\"\\n\",\n \" Queries the Google Custom Search API and returns a list of formatted URLs.\\n\",\n \" \\n\",\n \" Returns:\\n\",\n \" list[str]: A list of formatted URLs returned by the search engine.\\n\",\n \" \\n\",\n \" Raises:\\n\",\n \" RuntimeError: If the request fails or JSON response is invalid.\\n\",\n \" \\\"\\\"\\\"\\n\",\n \" url = 'https://www.googleapis.com/customsearch/v1'\\n\",\n \" max_results = 5\\n\",\n \" params = {\\n\",\n \" 'q': query,\\n\",\n \" 'key': gsearch_api_key,\\n\",\n \" 'cx': search_engine_id,\\n\",\n \" 'num': max_results\\n\",\n \" }\\n\",\n \"\\n\",\n \" try:\\n\",\n \" response = requests.get(url, params=params)\\n\",\n \" response.raise_for_status()\\n\",\n \" results = response.json()\\n\",\n \"\\n\",\n \" items = results.get('items', [])\\n\",\n \" links = [item.get('formattedUrl') for item in items if item.get('formattedUrl')]\\n\",\n \"\\n\",\n \" return links\\n\",\n \"\\n\",\n \" except requests.exceptions.RequestException as e:\\n\",\n \" raise RuntimeError(f\\\"Request failed: {e}\\\")\\n\",\n \" except ValueError:\\n\",\n \" raise RuntimeError(\\\"Error parsing JSON response.\\\")\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": 4,\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"def fetch_website_contents(url: str) -> str:\\n\",\n \" headers = {\\n\",\n \" \\\"User-Agent\\\": (\\n\",\n \" \\\"Mozilla/5.0 (Windows NT 10.0; Win64; x64) \\\"\\n\",\n \" \\\"AppleWebKit/537.36 (KHTML, like Gecko) \\\"\\n\",\n \" \\\"Chrome/117.0.0.0 Safari/537.36\\\"\\n\",\n \" )\\n\",\n \" }\\n\",\n \"\\n\",\n \" try:\\n\",\n \" response = requests.get(url, headers=headers, timeout=10)\\n\",\n \" response.raise_for_status()\\n\",\n \" soup = BeautifulSoup(response.content, 'html.parser')\\n\",\n \"\\n\",\n \" title = soup.title.get_text(strip=True) if soup.title else \\\"No title found\\\"\\n\",\n \"\\n\",\n \" if soup.body:\\n\",\n \" for tag in soup.body([\\\"script\\\", \\\"style\\\", \\\"img\\\", \\\"input\\\", \\\"noscript\\\"]):\\n\",\n \" tag.decompose()\\n\",\n \" text = soup.body.get_text(separator=\\\"\\\\n\\\", strip=True)\\n\",\n \" text = remove_short_lines(text)\\n\",\n \" else:\\n\",\n \" text = \\\"\\\"\\n\",\n \"\\n\",\n \" return f\\\"Webpage Title:\\\\n{title}\\\\n\\\\nWebpage Contents:\\\\n{text}\\\\n\\\"\\n\",\n \"\\n\",\n \" except requests.RequestException as e:\\n\",\n \" return f\\\"[ERROR] Failed to fetch {url}: {e}\\\"\\n\",\n \" except Exception as e:\\n\",\n \" return f\\\"[ERROR] Unexpected error: {e}\\\"\\n\",\n \" \\n\",\n \"def remove_short_lines(text: str)-> str:\\n\",\n \" min_words=3\\n\",\n \" lines = text.splitlines()\\n\",\n \" filtered = [line for line in lines if len(line.split()) >= min_words]\\n\",\n \" return \\\"\\\\n\\\".join(filtered)\\n\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": 8,\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"@function_tool\\n\",\n \"def deep_search_tool(query: str) -> str:\\n\",\n \" links = fetch_search_links(query)\\n\",\n \" content = \\\"\\\"\\n\",\n \" content = f\\\"{content} \\\\nSource URLs:\\\\n{\\\"\\\\n\\\".join(links)}\\\"\\n\",\n \" for link in links:\\n\",\n \" content = content + f\\\"\\\\n\\\\n## New Search Source:\\\\nSource Content:\\\\n\\\" + fetch_website_contents(link)\\n\",\n \" \\n\",\n \" return content\\n\",\n \" \\n\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": 9,\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"\\n\",\n \"GEMINI_BASE_URL = \\\"https://generativelanguage.googleapis.com/v1beta/openai/\\\"\\n\",\n \"DEEPSEEK_BASE_URL = \\\"https://api.deepseek.com/v1\\\"\\n\",\n \"CLAUDE_BASE_URL = \\\"https://api.anthropic.com/v1/\\\"\\n\",\n \"\\n\",\n \"claude_client = AsyncOpenAI(base_url=CLAUDE_BASE_URL, api_key=anthropic_api_key)\\n\",\n \"deepseek_client = AsyncOpenAI(base_url=DEEPSEEK_BASE_URL, api_key=deepseek_api_key)\\n\",\n \"lama_client = AsyncOpenAI(base_url='http://localhost:11434/v1', api_key='ollama')\\n\",\n \"# gemini_client = AsyncOpenAI(base_url=GEMINI_BASE_URL, api_key=google_api_key)\\n\",\n \"\\n\",\n \"deepseek_model = OpenAIChatCompletionsModel(model=\\\"deepseek-chat\\\", openai_client=deepseek_client)\\n\",\n \"claude_model=OpenAIChatCompletionsModel(model=\\\"claude-3-7-sonnet-20250219\\\", openai_client=claude_client)\\n\",\n \"llama_model = OpenAIChatCompletionsModel(model=\\\"llama3.2:latest\\\", openai_client=lama_client)\\n\",\n \"# Gemini model does not work with OpenAi Agents SDK\\n\",\n \"# gemini_model = OpenAIChatCompletionsModel(model=\\\"gemini-2.0-flash\\\", openai_client=gemini_client)\\n\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": 17,\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"sys_text = \\\"\\\"\\\" You are an assistant agent who can answer questions for users. \\\\\\n\",\n \" if the user asks you any question, then do not answer directly but \\\\\\n\",\n \" use the provided deep search tools only to get more recent information \\\\\\n\",\n \" related to the question. \\\\nYou should use the deep_search_tool tool \\\\\\n\",\n \" to get a context text which you can use to answer questions. \\\\nBe professional and use \\\\\\n\",\n \" the given tool and strictly answer questions using the context given by the tool.\\\\n\\\\\\n\",\n \" ##\\\\n\\\\n You should provide at the end of your response all the source urls, for example: 'https://www.website.com/pages'\\\\\\n\",\n \" \\\"\\\"\\\"\\n\",\n \"\\n\",\n \"# text = \\\"what are the stock market summary in Germany for May 2025?\\\"\\n\",\n \"text = \\\"what are the Agentic AI trends in 2025?\\\"\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": 11,\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"llama_agent = Agent(name=\\\"llama_agent\\\", instructions=sys_text, model=llama_model, tools=[deep_search_tool])\\n\",\n \"claudeAgent = Agent(name=\\\"claude_agent\\\", instructions=sys_text, model=claude_model, tools=[deep_search_tool])\\n\",\n \"deepseek_agent = Agent(name=\\\"deepseek_agent: deepseek_model\\\", model=deepseek_model, instructions=sys_text, tools=[deep_search_tool])\\n\",\n \"# for openai models, you need to define the model name in the model parameter, no need to define the client and model\\n\",\n \"gpt_agent = Agent(name=\\\"chat_agent: gpt-4o-mini\\\", model=\\\"gpt-4o-mini\\\", instructions=sys_text, tools=[deep_search_tool])\\n\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"with trace(\\\"searcher_agent_\\\"):\\n\",\n \" result = await Runner.run(gpt_agent, text)\\n\",\n \" print(result.final_output)\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"with trace(\\\"searcher_agents\\\"):\\n\",\n \" results = await asyncio.gather(\\n\",\n \" Runner.run(deepseek_agent, text),\\n\",\n \" Runner.run(claudeAgent, text),\\n\",\n \" Runner.run(gpt_agent, text),\\n\",\n \" Runner.run(llama_agent, text)\\n\",\n \" )\\n\",\n \"\\n\",\n \"outputs = [result.final_output for result in results]\\n\",\n \"\\n\",\n \"for output in outputs:\\n\",\n \" print(output + \\\"\\\\n\\\\n\\\")\"\n ]\n }\n ],\n \"metadata\": {\n \"kernelspec\": {\n \"display_name\": \".venv\",\n \"language\": \"python\",\n \"name\": \"python3\"\n },\n \"language_info\": {\n \"codemirror_mode\": {\n \"name\": \"ipython\",\n \"version\": 3\n },\n \"file_extension\": \".py\",\n \"mimetype\": \"text/x-python\",\n \"name\": \"python\",\n \"nbconvert_exporter\": \"python\",\n \"pygments_lexer\": \"ipython3\",\n \"version\": \"3.12.10\"\n }\n },\n \"nbformat\": 4,\n \"nbformat_minor\": 2\n}\n"}, "3895cca6cff04dda2b31625a51713f5508023489": {"t": 1785753553.6433134, "n": 9936, "h": "f47e3907706ca070", "c": "{\n \"cells\": [\n {\n \"cell_type\": \"code\",\n \"execution_count\": 1,\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"from dotenv import load_dotenv\\n\",\n \"from openai import OpenAI, AsyncOpenAI\\n\",\n \"from agents import Agent, Runner, trace, function_tool, OpenAIChatCompletionsModel, input_guardrail, GuardrailFunctionOutput, gen_trace_id\\n\",\n \"from typing import Dict\\n\",\n \"import os\\n\",\n \"import asyncio\\n\",\n \"from bs4 import BeautifulSoup\\n\",\n \"import requests\\n\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": 2,\n \"metadata\": {},\n \"outputs\": [\n {\n \"name\": \"stdout\",\n \"output_type\": \"stream\",\n \"text\": [\n \"Openai_Api_Key exists and begins sk-p\\n\",\n \"Google_Api_Key exists and begins AIza\\n\",\n \"Deepseek_Api_Key exists and begins sk-3\\n\",\n \"Anthropic_Api_Key exists and begins sk-a\\n\",\n \"Gsearch_Api_Key exists and begins AIza\\n\",\n \"Search_Engine_Id exists and begins b5a9\\n\"\n ]\n }\n ],\n \"source\": [\n \"load_dotenv(override=True)\\n\",\n \"\\n\",\n \"# Explicit variable assignments\\n\",\n \"openai_api_key = os.getenv('OPENAI_API_KEY')\\n\",\n \"google_api_key = os.getenv('GOOGLE_API_KEY')\\n\",\n \"deepseek_api_key = os.getenv('DEEPSEEK_API_KEY')\\n\",\n \"anthropic_api_key = os.getenv('ANTHROPIC_API_KEY')\\n\",\n \"gsearch_api_key = os.getenv('GSEARCH_API_KEY')\\n\",\n \"search_engine_id = os.getenv('SEARCH_ENGINE_ID')\\n\",\n \"\\n\",\n \"# List of variable names (as strings)\\n\",\n \"var_names = [\\n\",\n \" \\\"openai_api_key\\\", \\\"google_api_key\\\", \\\"deepseek_api_key\\\", \\\"anthropic_api_key\\\",\\n\",\n \" \\\"gsearch_api_key\\\", \\\"search_engine_id\\\",\\n\",\n \"]\\n\",\n \"\\n\",\n \"\\n\",\n \"for var in var_names:\\n\",\n \" value = locals()[var]\\n\",\n \" label = var.title()\\n\",\n \" if value:\\n\",\n \" print(f\\\"{label} exists and begins {value[:4]}\\\")\\n\",\n \" else:\\n\",\n \" print(f\\\"{label} not set\\\")\\n\",\n \" \"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": 3,\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"def fetch_search_links(query: str) -> list[str]:\\n\",\n \" \\\"\\\"\\\"\\n\",\n \" Queries the Google Custom Search API and returns a list of formatted URLs.\\n\",\n \" \\n\",\n \" Returns:\\n\",\n \" list[str]: A list of formatted URLs returned by the search engine.\\n\",\n \" \\n\",\n \" Raises:\\n\",\n \" RuntimeError: If the request fails or JSON response is invalid.\\n\",\n \" \\\"\\\"\\\"\\n\",\n \" url = 'https://www.googleapis.com/customsearch/v1'\\n\",\n \" max_results = 5\\n\",\n \" params = {\\n\",\n \" 'q': query,\\n\",\n \" 'key': gsearch_api_key,\\n\",\n \" 'cx': search_engine_id,\\n\",\n \" 'num': max_results\\n\",\n \" }\\n\",\n \"\\n\",\n \" try:\\n\",\n \" response = requests.get(url, params=params)\\n\",\n \" response.raise_for_status()\\n\",\n \" results = response.json()\\n\",\n \"\\n\",\n \" items = results.get('items', [])\\n\",\n \" links = [item.get('formattedUrl') for item in items if item.get('formattedUrl')]\\n\",\n \"\\n\",\n \" return links\\n\",\n \"\\n\",\n \" except requests.exceptions.RequestException as e:\\n\",\n \" raise RuntimeError(f\\\"Request failed: {e}\\\")\\n\",\n \" except ValueError:\\n\",\n \" raise RuntimeError(\\\"Error parsing JSON response.\\\")\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": 4,\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"def fetch_website_contents(url: str) -> str:\\n\",\n \" headers = {\\n\",\n \" \\\"User-Agent\\\": (\\n\",\n \" \\\"Mozilla/5.0 (Windows NT 10.0; Win64; x64) \\\"\\n\",\n \" \\\"AppleWebKit/537.36 (KHTML, like Gecko) \\\"\\n\",\n \" \\\"Chrome/117.0.0.0 Safari/537.36\\\"\\n\",\n \" )\\n\",\n \" }\\n\",\n \"\\n\",\n \" try:\\n\",\n \" response = requests.get(url, headers=headers, timeout=10)\\n\",\n \" response.raise_for_status()\\n\",\n \" soup = BeautifulSoup(response.content, 'html.parser')\\n\",\n \"\\n\",\n \" title = soup.title.get_text(strip=True) if soup.title else \\\"No title found\\\"\\n\",\n \"\\n\",\n \" if soup.body:\\n\",\n \" for tag in soup.body([\\\"script\\\", \\\"style\\\", \\\"img\\\", \\\"input\\\", \\\"noscript\\\"]):\\n\",\n \" tag.decompose()\\n\",\n \" text = soup.body.get_text(separator=\\\"\\\\n\\\", strip=True)\\n\",\n \" text = remove_short_lines(text)\\n\",\n \" else:\\n\",\n \" text = \\\"\\\"\\n\",\n \"\\n\",\n \" return f\\\"Webpage Title:\\\\n{title}\\\\n\\\\nWebpage Contents:\\\\n{text}\\\\n\\\"\\n\",\n \"\\n\",\n \" except requests.RequestException as e:\\n\",\n \" return f\\\"[ERROR] Failed to fetch {url}: {e}\\\"\\n\",\n \" except Exception as e:\\n\",\n \" return f\\\"[ERROR] Unexpected error: {e}\\\"\\n\",\n \" \\n\",\n \"def remove_short_lines(text: str)-> str:\\n\",\n \" min_words=3\\n\",\n \" lines = text.splitlines()\\n\",\n \" filtered = [line for line in lines if len(line.split()) >= min_words]\\n\",\n \" return \\\"\\\\n\\\".join(filtered)\\n\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": 8,\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"@function_tool\\n\",\n \"def deep_search_tool(query: str) -> str:\\n\",\n \" links = fetch_search_links(query)\\n\",\n \" content = \\\"\\\"\\n\",\n \" content = f\\\"{content} \\\\nSource URLs:\\\\n{\\\"\\\\n\\\".join(links)}\\\"\\n\",\n \" for link in links:\\n\",\n \" content = content + f\\\"\\\\n\\\\n## New Search Source:\\\\nSource Content:\\\\n\\\" + fetch_website_contents(link)\\n\",\n \" \\n\",\n \" return content\\n\",\n \" \\n\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": 9,\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"\\n\",\n \"GEMINI_BASE_URL = \\\"https://generativelanguage.googleapis.com/v1beta/openai/\\\"\\n\",\n \"DEEPSEEK_BASE_URL = \\\"https://api.deepseek.com/v1\\\"\\n\",\n \"CLAUDE_BASE_URL = \\\"https://api.anthropic.com/v1/\\\"\\n\",\n \"\\n\",\n \"claude_client = AsyncOpenAI(base_url=CLAUDE_BASE_URL, api_key=anthropic_api_key)\\n\",\n \"deepseek_client = AsyncOpenAI(base_url=DEEPSEEK_BASE_URL, api_key=deepseek_api_key)\\n\",\n \"lama_client = AsyncOpenAI(base_url='http://localhost:11434/v1', api_key='ollama')\\n\",\n \"# gemini_client = AsyncOpenAI(base_url=GEMINI_BASE_URL, api_key=google_api_key)\\n\",\n \"\\n\",\n \"deepseek_model = OpenAIChatCompletionsModel(model=\\\"deepseek-chat\\\", openai_client=deepseek_client)\\n\",\n \"claude_model=OpenAIChatCompletionsModel(model=\\\"claude-3-7-sonnet-20250219\\\", openai_client=claude_client)\\n\",\n \"llama_model = OpenAIChatCompletionsModel(model=\\\"llama3.2:latest\\\", openai_client=lama_client)\\n\",\n \"# Gemini model does not work with OpenAi Agents SDK\\n\",\n \"# gemini_model = OpenAIChatCompletionsModel(model=\\\"gemini-2.0-flash\\\", openai_client=gemini_client)\\n\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": 17,\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"sys_text = \\\"\\\"\\\" You are an assistant agent who can answer questions for users. \\\\\\n\",\n \" if the user asks you any question, then do not answer directly but \\\\\\n\",\n \" use the provided deep search tools only to get more recent information \\\\\\n\",\n \" related to the question. \\\\nYou should use the deep_search_tool tool \\\\\\n\",\n \" to get a context text which you can use to answer questions. \\\\nBe professional and use \\\\\\n\",\n \" the given tool and strictly answer questions using the context given by the tool.\\\\n\\\\\\n\",\n \" ##\\\\n\\\\n You should provide at the end of your response all the source urls, for example: 'https://www.website.com/pages'\\\\\\n\",\n \" \\\"\\\"\\\"\\n\",\n \"\\n\",\n \"# text = \\\"what are the stock market summary in Germany for May 2025?\\\"\\n\",\n \"text = \\\"what are the Agentic AI trends in 2025?\\\"\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": 11,\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"llama_agent = Agent(name=\\\"llama_agent\\\", instructions=sys_text, model=llama_model, tools=[deep_search_tool])\\n\",\n \"claudeAgent = Agent(name=\\\"claude_agent\\\", instructions=sys_text, model=claude_model, tools=[deep_search_tool])\\n\",\n \"deepseek_agent = Agent(name=\\\"deepseek_agent: deepseek_model\\\", model=deepseek_model, instructions=sys_text, tools=[deep_search_tool])\\n\",\n \"# for openai models, you need to define the model name in the model parameter, no need to define the client and model\\n\",\n \"gpt_agent = Agent(name=\\\"chat_agent: gpt-4o-mini\\\", model=\\\"gpt-4o-mini\\\", instructions=sys_text, tools=[deep_search_tool])\\n\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"with trace(\\\"searcher_agent_\\\"):\\n\",\n \" result = await Runner.run(gpt_agent, text)\\n\",\n \" print(result.final_output)\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"metadata\": {},\n \"outputs\": [],\n \"source\": [\n \"with trace(\\\"searcher_agents\\\"):\\n\",\n \" results = await asyncio.gather(\\n\",\n \" Runner.run(deepseek_agent, text),\\n\",\n \" Runner.run(claudeAgent, text),\\n\",\n \" Runner.run(gpt_agent, text),\\n\",\n \" Runner.run(llama_agent, text)\\n\",\n \" )\\n\",\n \"\\n\",\n \"outputs = [result.final_output for result in results]\\n\",\n \"\\n\",\n \"for output in outputs:\\n\",\n \" print(output + \\\"\\\\n\\\\n\\\")\"\n ]\n }\n ],\n \"metadata\": {\n \"kernelspec\": {\n \"display_name\": \".venv\",\n \"language\": \"python\",\n \"name\": \"python3\"\n },\n \"language_info\": {\n \"codemirror_mode\": {\n \"name\": \"ipython\",\n \"version\": 3\n },\n \"file_extension\": \".py\",\n \"mimetype\": \"text/x-python\",\n \"name\": \"python\",\n \"nbconvert_exporter\": \"python\",\n \"pygments_lexer\": \"ipython3\",\n \"version\": \"3.12.10\"\n }\n },\n \"nbformat\": 4,\n \"nbformat_minor\": 2\n}\n"}}}}