Goodguygregory93/ai-agents-certification-code
0
1from tools import Tools2from retriever import Retriever3from llama_index.core.agent.workflow import AgentWorkflow4from llama_index.llms.ollama import Ollama5from dotenv import load_dotenv6import gradio as gr7import colorama8import os9 10 11async def test_duck_duck_go_search_tool(search_query: str):12 '''13 basic model test to determine if the duck_duck_go_search_tool is functional14 15 Args:16 search_query (str): query for searching in duck duck go.17 Returns:18 response (str): returns a result from duck duck go search tools.19 20 '''21 prompt = f"use duck_duck_go_search to find the following information: {search_query}"22 23 response = await general_agent.run(prompt)24 25 return response26 27async def test_wikipedia_search_tool(wiki_query: str):28 '''29 basic model test to determine if the wikipedia_search_tool is functional30 and works for returning relevant wikipedia articles from the supplied wiki_query 31 32 Args:33 wiki_query (str):34 Returns:35 response (str):36 '''37 prompt = f"use wikipedia_search_tool to find the following information: {wiki_query}"38 39 response = await general_agent.run(prompt)40 41 return response42 43async def test_youtube_transcription_tool(yt_url: str, yt_prompt: str):44 '''45 basic model test to determine if the wikipedia_search_tool is functional46 and works for returning relevant wikipedia articles from the supplied wiki_query 47 48 [Package Documentation](https://pypi.org/project/youtube-transcript-api/)49 50 Args:51 yt_url (str): the youtube url of the transcription required to search52 yt_query (str): the information needed from the provided youtube video53 Returns:54 response (str): the information from the transcription that answers the yt_query55 '''56 prompt = f"""use the youtube_transcription_tool to return the transcript from this youtube video:57 {yt_url}58 59 then use it to answer this question: 60 {yt_prompt}61 """62 63 response = await general_agent.run(prompt)64 65 return response 66 67def test_top_wired_articles_tool():68 '''69 70 Args:71 None72 Returns:73 top_wired_result (str):74 '''75 76 tools = Tools()77 78 return tools.top_wired_articles_tool()79 80async def test_retrieval_question_tool(agent_question: str):81 '''82 basic model test to determine if the retrieval tool for `similar_question_tool` is functional83 and works for returning relevant answers based on the supplied question84 85 Args:86 agent_question (str): question supplied to the Agent87 Returns:88 response (str): found FINAL ANSWER to the supplied question89 '''90 prompt = f"use similar_question_tool to find the FINAL ANSWER to the following question: {agent_question}"91 92 response = await general_agent.run(prompt)93 94 return response95 96 97 98 99# Build the Gradio Interface using Blocks as this is a complex application100# allowing for multiple tool call tests 101with gr.Blocks(fill_width=True) as demo:102 gr.Markdown("""103 104 # ๐ค GAIA Agent Tools Test App105 106 **Description:**107 108 This project hosts all of the tools our GAIA Agent will leverage109 to accomplish the Level 1 questions, in various forms the tools will be available110 above for us to test the accuracy and functionality of the tools. Ideally this could be mocked111 however LLMs are very unpredictable.112 113 To save yourself the ๐ธ and tokens from HuggingFaceInference you can select a model from the default providers114 to execute the tools and then ensure their output is valid. just ensure `Ollama` is running and serving the model you need115 116 **Reference Documentation**117 118 - [Ollama with LlamaIndex ReactAgent](https://datavizandai.github.io/2024/10/18/ollama-agent.html)119 120 - [Ollama LlamaIndex Documentation](https://llamahub.ai/l/llms/llama-index-llms-ollama?from=)121 122 ### ๐ฆ Choose Local LLM Provider123 124 """)125 126 model_selection = gr.Dropdown(["qwen3:8b", "llama3.2:latest", "mistral:7b" ], label="๐ง Tool Capable LLMs Used", multiselect=False, interactive=True)127 128 129 gr.Markdown("""130 ## ๐ ๏ธ Agent Tools131 --------------------132 """)133 with gr.Row():134 # Duck Duck Go Search135 gr.Markdown("### ๐ฆ DuckDuckGo Search Tool")136 137 with gr.Row():138 # DuckDuckGo Search Tool:139 with gr.Column(scale=0, min_width=400):140 duck_duck_query = gr.Textbox(label="Search Query")141 duck_duck_btn = gr.Button("Run Tool")142 143 with gr.Column(scale=1):144 duck_duck_result = gr.Textbox(label="Duck Duck Go Search Result", lines=3)145 146 duck_duck_btn.click(147 fn=test_duck_duck_go_search_tool,148 inputs=duck_duck_query,149 outputs=duck_duck_result150 )151 152 with gr.Row():153 # Wikipedia Search Tool154 gr.Markdown("### ๐ ๐ Wikipedia Search Tool")155 156 with gr.Row():157 with gr.Column(scale=0, min_width=400):158 wiki_query = gr.Textbox(label="Wikipedia Query")159 wiki_btn = gr.Button("Run Tool")160 161 with gr.Column(scale=1):162 wiki_result = gr.Textbox(label="Wikipedia Search Result", lines=3)163 164 wiki_btn.click(165 fn=test_wikipedia_search_tool,166 inputs=wiki_query,167 outputs=wiki_result168 )169 170 with gr.Row():171 # Youtube Transcription Search Tool172 gr.Markdown("### ๐น Youtube Transcription Search Tool")173 174 with gr.Row():175 with gr.Column(scale=0, min_width=400):176 yt_url = gr.Textbox(label="Youtube URL")177 yt_query = gr.Textbox(label="Video Question")178 179 yt_transcription_btn = gr.Button("Run Tool")180 181 with gr.Column(scale=1):182 yt_query_result = gr.Textbox(label="Youtube Transcription Response", lines=3)183 184 yt_transcription_btn.click(185 fn=test_youtube_transcription_tool,186 inputs=[yt_url, yt_query ],187 outputs=yt_query_result188 )189 190 with gr.Row():191 # Youtube Transcription Search Tool192 gr.Markdown("### ๐ป ๐ฐ Most Popular Wired Articles Tool")193 194 with gr.Row():195 with gr.Column(scale=0, min_width=400):196 gr.Markdown("""This tool scrapes Wired for the most popular published articles, listed from their RSS Feed. 197 198 Reading the articles will require a paid subscription, because I am not evil.199 Supporting Wired is responsible and ethical200 """)201 wired_btn = gr.Button("Run Tool")202 203 with gr.Column(scale=1):204 top_wired_result = gr.Textbox(label="Most Popular Articles", lines=3)205 206 wired_btn.click(207 fn=test_top_wired_articles_tool,208 outputs=top_wired_result209 )210 211 with gr.Row():212 # Youtube Transcription Search Tool213 gr.Markdown("### ๐ค Similar Question Query Tool")214 215 with gr.Row():216 with gr.Column(scale=0, min_width=400):217 gr.Markdown("""This tool supports Retrieval Augmented Generation to query for similar questions based on the supplied218 query. The Agent will call the tool and hopefully use the BM25 Retriever to find and access the question219 that correspond to the correct answer.220 """)221 agent_question = gr.Textbox(label="Agent Question")222 agent_btn = gr.Button("Run Tool")223 224 with gr.Column(scale=1):225 agents_found_answer = gr.Textbox(label="Agent's Found Answer", lines=3)226 227 agent_btn.click(228 fn=test_retrieval_question_tool,229 inputs=agent_question,230 outputs=agents_found_answer231 )232 233if __name__ == "__main__":234 load_dotenv()235 236 print(colorama.Fore.YELLOW + "๐ง initializing an LLM for the Agent")237 print('-'*15)238 239 llm = Ollama(240 model="qwen3:8b",241 request_timeout=120.0,242 temperature=0243 )244 245 246 print(colorama.Fore.YELLOW + "establishing Agent's tools... ")247 print('-'*15)248 249 found_tools = []250 251 # tools = Tools(status_updates=True).tool_belt252 retriever = Retriever(confirm_load=False)253 254 found_tools.append(retriever.similar_question_tool)255 # print(colorama.Fore.YELLOW + "โ
๐ Wikipedia Search tool has been created")256 # for tool in tools:257 # found_tools.append(tool)258 259 print(colorama.Fore.YELLOW + "๐ ๏ธ initialized the Agent's tools...")260 print('-'*15)261 262 263 print(colorama.Fore.YELLOW + "๐ค building Agent Workflow for prompting")264 print('-'*15)265 266 general_agent = AgentWorkflow.from_tools_or_functions(267 found_tools,268 llm=llm269 )270 271 272 print(colorama.Fore.GREEN + "๐ฐ๏ธ starting gradio server.. ")273 274 demo.launch()275 print(colorama.Fore.CYAN)276 