{"id":11152,"date":"2024-12-11T13:12:56","date_gmt":"2024-12-11T13:12:56","guid":{"rendered":"https:\/\/metaschool.so\/articles\/?p=11152"},"modified":"2024-12-12T11:07:55","modified_gmt":"2024-12-12T11:07:55","slug":"how-to-create-ai-agents","status":"publish","type":"post","link":"https:\/\/metaschool.so\/articles\/how-to-create-ai-agents\/","title":{"rendered":"How to Create AI Agents? Detailed Guide 2025"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_56_1 ez-toc-wrap-left counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title \" >Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/metaschool.so\/articles\/how-to-create-ai-agents\/#What_is_an_AI_Agent\" title=\"What is an AI Agent?\">What is an AI Agent?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/metaschool.so\/articles\/how-to-create-ai-agents\/#Use_Cases_of_AI_Agents\" title=\"Use Cases of AI Agents\">Use Cases of AI Agents<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/metaschool.so\/articles\/how-to-create-ai-agents\/#How_to_Create_an_AI_Agent_A_Step-by-Step_Guide\" title=\"How to Create an AI Agent: A Step-by-Step Guide\">How to Create an AI Agent: A Step-by-Step Guide<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/metaschool.so\/articles\/how-to-create-ai-agents\/#How_to_Create_an_AI_Agent_Code_Implementation\" title=\"How to Create an AI Agent: Code Implementation\">How to Create an AI Agent: Code Implementation<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/metaschool.so\/articles\/how-to-create-ai-agents\/#Conclusion\" title=\"Conclusion\">Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/metaschool.so\/articles\/how-to-create-ai-agents\/#FAQs\" title=\"FAQs\">FAQs<\/a><\/li><\/ul><\/nav><\/div>\n\n<p>Artificial Intelligence (AI) agents have emerged as transformative tools across industries, capable of performing tasks autonomously and driving efficiency. From customer service chatbots to complex healthcare diagnostic systems, AI agents are reshaping how we interact with technology. As businesses seek to leverage these systems for improved productivity, personalization, and decision-making, understanding their potential and how to create them is becoming increasingly vital.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_is_an_AI_Agent\"><\/span>What is an AI Agent?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>An AI agent is a computer program designed to act autonomously in achieving specific goals. These agents can perceive their environment, gather relevant data, and make decisions to execute predefined tasks. Ranging from simple bots to sophisticated learning systems, AI agents continuously adapt through interaction with their environment, data, and user feedback.<\/p>\n\n\n\n<p>Examples of AI agents include virtual assistants like Siri and Alexa, self-driving cars from Tesla, and robotic process automation tools like <a href=\"https:\/\/www.uipath.com\/\" target=\"_blank\" rel=\"noopener\">UiPath<\/a>. These agents improve productivity, accuracy, and customization, making them integral to various domains.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Importance<\/h3>\n\n\n\n<p>AI agents are pivotal in modern technology for several reasons:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Increased Efficiency<\/strong>: Automating routine tasks reduces human labor and boosts productivity.<\/li>\n\n\n\n<li><strong>Data-Driven Decisions<\/strong>: AI agents analyze vast datasets to provide actionable insights, improving decision-making.<\/li>\n\n\n\n<li><strong>Personalized Experiences<\/strong>: Virtual assistants and chatbots offer tailored interactions, enhancing user satisfaction.<\/li>\n\n\n\n<li><strong>Industry Applications<\/strong>: From healthcare to finance, AI agents enable advanced solutions such as diagnostics, fraud detection, and supply chain optimization.<\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>To learn more about AI Agents, check out this comprehensive <a href=\"https:\/\/metaschool.so\/articles\/ai-agents\">guide<\/a> which explores the architectural componenets of an AI Agents, its types, along with real world examples.<\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Use_Cases_of_AI_Agents\"><\/span>Use Cases of AI Agents<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>AI agents are revolutionizing industries by automating tasks, enhancing decision-making, and personalizing user experiences. Let\u2019s explore some detailed applications of AI Agents across various fields, showcasing their potential.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Customer Service<\/strong><\/h3>\n\n\n\n<p>AI agents in customer service, such as chatbots and virtual assistants, handle inquiries, resolve simple issues, and provide personalized guidance.<\/p>\n\n\n\n<p><strong>Value<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Offer 24\/7 availability and quick responses, improving customer satisfaction.<\/li>\n\n\n\n<li>Automate routine inquiries, reducing operational costs.<\/li>\n\n\n\n<li>Analyze customer interactions to uncover trends and preferences for better decision-making.<\/li>\n<\/ul>\n\n\n\n<p><strong>Examples<\/strong>: Capital One\u2019s <em>Eno<\/em> assists customers with banking tasks, while Alexa supports users in tracking orders and providing product details.<\/p>\n\n\n\n<p><strong>Industries<\/strong>: Banking, retail, travel, and healthcare.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Finance<\/strong><\/h3>\n\n\n\n<p>AI agents facilitate advanced financial tasks, including algorithmic trading, fraud detection, and financial forecasting.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"2000\" height=\"2000\" src=\"https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/12\/rb_2777-1.png\" alt=\"\" class=\"wp-image-11156\" style=\"width:auto;height:400px\" srcset=\"https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/12\/rb_2777-1.png 2000w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/12\/rb_2777-1-300x300.png 300w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/12\/rb_2777-1-1024x1024.png 1024w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/12\/rb_2777-1-150x150.png 150w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/12\/rb_2777-1-768x768.png 768w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/12\/rb_2777-1-1536x1536.png 1536w\" sizes=\"auto, (max-width: 2000px) 100vw, 2000px\" \/><\/figure>\n<\/div>\n\n\n<p><strong>Value<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Improve operational efficiency through real-time market analysis.<\/li>\n\n\n\n<li>Enhance security by identifying and mitigating fraud risks.<\/li>\n\n\n\n<li>Optimize investment strategies with predictive analytics.<\/li>\n<\/ul>\n\n\n\n<p><strong>Examples<\/strong>: AI-based tools like algorithmic trading systems and fraud detection platforms are widely used by financial institutions.<\/p>\n\n\n\n<p><strong>Industries<\/strong>: Banking, insurance, and investment firms.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Healthcare<\/strong><\/h3>\n\n\n\n<p>AI agents assist healthcare professionals by automating diagnostic processes, suggesting treatments, and managing patient records.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"2000\" height=\"2000\" src=\"https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/12\/rb_16745.png\" alt=\"\" class=\"wp-image-11157\" style=\"width:auto;height:400px\" srcset=\"https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/12\/rb_16745.png 2000w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/12\/rb_16745-300x300.png 300w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/12\/rb_16745-1024x1024.png 1024w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/12\/rb_16745-150x150.png 150w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/12\/rb_16745-768x768.png 768w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/12\/rb_16745-1536x1536.png 1536w\" sizes=\"auto, (max-width: 2000px) 100vw, 2000px\" \/><\/figure>\n<\/div>\n\n\n<p><strong>Value<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Enhance diagnostic accuracy through advanced pattern recognition.<\/li>\n\n\n\n<li>Provide personalized treatment recommendations based on patient data.<\/li>\n\n\n\n<li>Reduce administrative workload by summarizing patient records.<\/li>\n<\/ul>\n\n\n\n<p><strong>Examples<\/strong>: AI-driven diagnostic tools and chatbots that help patients book appointments or access health information.<\/p>\n\n\n\n<p><strong>Industries<\/strong>: Hospitals, telemedicine platforms, and research institutions.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>E-Commerce AI Consultant<\/strong><\/h3>\n\n\n\n<p>AI agents analyze trends and customer behavior in e-commerce to optimize operations and improve customer experience.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"2000\" height=\"1333\" src=\"https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/12\/rb_524.png\" alt=\"\" class=\"wp-image-11158\" style=\"width:auto;height:400px\" srcset=\"https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/12\/rb_524.png 2000w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/12\/rb_524-300x200.png 300w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/12\/rb_524-1024x682.png 1024w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/12\/rb_524-768x512.png 768w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/12\/rb_524-1536x1024.png 1536w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/12\/rb_524-930x620.png 930w\" sizes=\"auto, (max-width: 2000px) 100vw, 2000px\" \/><\/figure>\n<\/div>\n\n\n<p><strong>Value<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Track seasonal demand and recommend inventory adjustments.<\/li>\n\n\n\n<li>Provide personalized product recommendations to buyers.<\/li>\n\n\n\n<li>Automate order processing and suggest data-backed marketing strategies.<\/li>\n<\/ul>\n\n\n\n<p><strong>Examples<\/strong>: Amazon\u2019s recommendation engine uses AI to analyze purchase history and suggest products, optimizing sales and customer satisfaction.<\/p>\n\n\n\n<p><strong>Industries<\/strong>: E-commerce, logistics, and wholesale.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Marketing<\/strong><\/h3>\n\n\n\n<p>AI agents transform marketing campaigns by analyzing customer data and delivering personalized strategies.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"2000\" height=\"2000\" src=\"https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/12\/rb_66658.png\" alt=\"\" class=\"wp-image-11159\" style=\"width:auto;height:400px\" srcset=\"https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/12\/rb_66658.png 2000w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/12\/rb_66658-300x300.png 300w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/12\/rb_66658-1024x1024.png 1024w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/12\/rb_66658-150x150.png 150w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/12\/rb_66658-768x768.png 768w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/12\/rb_66658-1536x1536.png 1536w\" sizes=\"auto, (max-width: 2000px) 100vw, 2000px\" \/><\/figure>\n<\/div>\n\n\n<p><strong>Value<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Personalize campaigns to match individual customer preferences.<\/li>\n\n\n\n<li>Predict customer behavior and refine targeting strategies.<\/li>\n\n\n\n<li>Optimize advertising spending for maximum ROI.<\/li>\n<\/ul>\n\n\n\n<p><strong>Examples<\/strong>: AI-powered recommendation systems and campaign optimizers used by platforms like Facebook and Google Ads.<\/p>\n\n\n\n<p><strong>Industries<\/strong>: Retail, digital marketing, and entertainment.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Supply Chain Management<\/strong><\/h3>\n\n\n\n<p>AI agents streamline supply chain operations by forecasting demand and optimizing inventory management.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"2000\" height=\"1333\" src=\"https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/12\/rb_1761.png\" alt=\"\" class=\"wp-image-11160\" style=\"width:auto;height:400px\" srcset=\"https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/12\/rb_1761.png 2000w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/12\/rb_1761-300x200.png 300w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/12\/rb_1761-1024x682.png 1024w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/12\/rb_1761-768x512.png 768w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/12\/rb_1761-1536x1024.png 1536w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/12\/rb_1761-930x620.png 930w\" sizes=\"auto, (max-width: 2000px) 100vw, 2000px\" \/><\/figure>\n<\/div>\n\n\n<p><strong>Value<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Predict demand fluctuations to prevent overstocking or stockouts.<\/li>\n\n\n\n<li>Automate logistics workflows for seamless operations.<\/li>\n\n\n\n<li>Reduce operational costs by improving efficiency.<\/li>\n<\/ul>\n\n\n\n<p><strong>Examples<\/strong>: AI-driven inventory management systems and route optimization tools.<\/p>\n\n\n\n<p><strong>Industries<\/strong>: Manufacturing, logistics, and retail.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_to_Create_an_AI_Agent_A_Step-by-Step_Guide\"><\/span><strong>How to Create an AI Agent: A Step-by-Step Guide<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Creating an AI agent involves a structured process that combines planning, data handling, technical development, and continuous refinement.<\/p>\n\n\n\n<p>Having some foundational understanding of AI and development is important to successfully build an AI agent. Below are the key knowledge areas and skills that are essential for completing this AI project effectively.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Prerequisite Skills for Building AI Agents<\/strong><\/h3>\n\n\n\n<p>To successfully create an AI agent, you need a combination of skills, including:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Machine Learning Expertise<\/strong>: To design and train AI models.<\/li>\n\n\n\n<li><strong>Data Science Skills<\/strong>: For collecting, preprocessing, and analyzing datasets.<\/li>\n\n\n\n<li><strong>Programming Knowledge<\/strong>: Proficiency in languages like Python or Java to develop and integrate the agent.<\/li>\n\n\n\n<li><strong>UI\/UX Design<\/strong>: To create user-friendly interfaces and optimize user interactions.<\/li>\n\n\n\n<li><strong>DevOps Understanding<\/strong>: For deploying, scaling, and maintaining the AI agent efficiently.<\/li>\n<\/ul>\n\n\n\n<p>Ensuring that these skills are present will prepare you to handle the technical challenges of building an AI agent effectively.<\/p>\n\n\n\n<p>Now let\u2019s go through the key steps required to build an AI agent.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 1: Define the Task and Environment<\/strong><\/h3>\n\n\n\n<p>Start by identifying the purpose and scope of your AI agent. Consider the environment where the agent will be deployed, such as an app, website, or enterprise system, and ensure its compatibility with the surrounding technology and workflows. Additionally, define the specific tasks the agent will handle, whether it&#8217;s answering queries, automating workflows, or analyzing data. This foundational step is crucial for aligning the AI agent with your business needs and ensuring seamless integration into its intended environment.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 2: Gather and Prepare Data<\/strong><\/h3>\n\n\n\n<p>Data is the backbone of any AI agent. Collect relevant data from various sources, such as:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Internal Data<\/strong>: Sales records, customer interactions, or operational logs.<\/li>\n\n\n\n<li><strong>External Data<\/strong>: Public datasets, third-party providers, or web scraping.<\/li>\n\n\n\n<li><strong>User-Generated Data<\/strong>: Social media content, product reviews, or surveys.<\/li>\n<\/ul>\n\n\n\n<p>After gathering data, preprocess it by cleaning inconsistencies, handling missing values, and formatting it for machine learning models. For example, when building a chatbot for medical record summarization, preprocess health data to ensure accuracy and relevancy.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 3: Select Your Tech Stack<\/strong><\/h3>\n\n\n\n<p>Selecting the right tech stack is essential for building an efficient, scalable, and maintainable AI agent. Start by choosing programming languages like Python or Java, depending on compatibility with your agent\u2019s objectives. Incorporate AI technologies such as machine learning for pattern recognition, natural language processing (NLP) for understanding and generating human language, computer vision for visual data interpretation, and robotic process automation (RPA) for handling repetitive tasks. To ensure scalability, opt for cloud-based platforms that enable efficient data storage and processing as your AI agent evolves. A well-chosen tech stack lays the foundation for a robust and adaptable AI agent.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: Design the AI Agent<\/h3>\n\n\n\n<p>To design your AI agent, choose the right architecture for efficiency and scalability. A modular design simplifies updates by allowing independent modifications, while concurrent designs enable multitasking for agents handling multiple tasks. Define how the agent will process data, including how it receives inputs and generates responses\u2014e.g., a chatbot handling user queries via text.<\/p>\n\n\n\n<p>Focus on creating an intuitive user experience (UX) with a branded interface and accessibility features like text-to-speech. Add a feedback mechanism to gather user input for continuous improvement. Proper data handling protocols and secure storage are also essential to ensure seamless and compliant operations.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 5: Test and Deploy the AI Agent<\/strong><\/h3>\n\n\n\n<p>To ensure your AI agent performs as expected, rigorous testing is essential. Begin with <strong>unit testing<\/strong> to verify that individual components work independently, followed by <strong>integration testing<\/strong> to ensure smooth interaction between different modules. Conduct <strong>functional testing<\/strong> to check the agent&#8217;s ability to perform its intended tasks, and use <strong>usability testing<\/strong> to observe real users interacting with the agent, identifying areas for improvement. Additionally, perform <strong>edge case testing<\/strong> by feeding extreme or unexpected inputs to assess the agent&#8217;s robustness.<\/p>\n\n\n\n<p>Once testing is complete, deploy the AI agent into its operational environment. This involves integrating it with existing systems or workflows and implementing necessary security measures to safeguard sensitive data. Post-deployment, continuously monitor the agent&#8217;s performance by tracking key metrics such as accuracy, response time, and user satisfaction. Collect user feedback to identify opportunities for refinement and ensure the agent evolves to meet changing requirements. Through thorough testing, deployment, and monitoring, your AI agent can deliver reliable, efficient, and adaptive performance.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_to_Create_an_AI_Agent_Code_Implementation\"><\/span><strong>How to Create an AI Agent: Code Implementation<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>To demonstrate the functionality of an AI Agent, we will build a simple weather informer. The user will set an initial location, and the weather informer will track changes in the user\u2019s location over time. If the user moves by more than 1 degree in latitude or longitude, the agent will fetch real-time weather data for the new location and send alerts. These alerts could include reminders to carry an umbrella if rain is expected or suggestions to wear warmer clothes if the temperature drops. This AI agent helps keep the user informed about relevant weather conditions based on their movements.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Get User&#8217;s Current Location<\/h3>\n\n\n\n<p>To get the latitude and longitude values of your current location, we will use the <a href=\"https:\/\/pypi.org\/project\/geocoder\/\" target=\"_blank\" rel=\"noopener\">geocoder<\/a> library in Python:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"import geocoder\ng = geocoder.ip('me')\nprint(g.latlng)\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #C586C0\">import<\/span><span style=\"color: #D4D4D4\"> geocoder<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">g = geocoder.ip(<\/span><span style=\"color: #CE9178\">&#39;me&#39;<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #DCDCAA\">print<\/span><span style=\"color: #D4D4D4\">(g.latlng)<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>To run this code you first need to install the geocoder library:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"pip install geocoder\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #DCDCAA\">pip<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #CE9178\">install<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #CE9178\">geocoder<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Get Real Time Weather Data<\/h3>\n\n\n\n<p>We will obtain time weather data from <a href=\"https:\/\/openweathermap.org\/\" target=\"_blank\" rel=\"noopener\">OpenWeather<\/a>&#8216;s weather APIs. To access the API key, you first need to sign up on the platform. Don&#8217;t forget to verify your email first, the API key would not work otherwise. Once you have set up your account, navigate to the <a href=\"https:\/\/home.openweathermap.org\/api_keys\" target=\"_blank\" rel=\"noopener\">API keys<\/a> page and copy the Default key. <\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><\/blockquote>\n\n\n\n<p>Replace the placeholder text <code>&lt;your_api_key><\/code> with the Default API key you copied. Feel free to change the latitude and longitude values to your current location (variable <code>lat<\/code> and <code>lon<\/code>).<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"import requests\n\napi_key = &quot;&lt;your_api_key&gt;&quot;\n\n# Latitude and Longitude \nlat = 33.5973\nlon = 73.0479\n\n# Base URL for the OpenWeatherMap API\nbase_url = &quot;https:\/\/api.openweathermap.org\/data\/2.5\/weather&quot;\n\n# Construct the final URL\nurl = f&quot;{base_url}?lat={lat}&amp;lon={lon}&amp;appid={api_key}&amp;units=metric&quot;  # Using metric to get temperature in Celsius\n\n# Make the API call\nresponse = requests.get(url)\n\n# Check if the request was successful (status code 200)\nif response.status_code == 200:\n    weather_data = response.json()\n    \n    # Extract necessary details from the response\n    location_name = weather_data['name']\n    temp_c = weather_data['main']['temp']\n    condition = weather_data['weather'][0]['description']\n    \n    # Print weather details\n    print(f&quot;Temperature: {temp_c}\u00b0C&quot;)\n    print(f&quot;Condition: {condition}&quot;)\nelse:\n    print(f&quot;Error: Unable to fetch data. Status code: {response.status_code}&quot;)\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #C586C0\">import<\/span><span style=\"color: #D4D4D4\"> requests<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">api_key = <\/span><span style=\"color: #CE9178\">&quot;&lt;your_api_key&gt;&quot;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\"># Latitude and Longitude <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">lat = <\/span><span style=\"color: #B5CEA8\">33.5973<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">lon = <\/span><span style=\"color: #B5CEA8\">73.0479<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\"># Base URL for the OpenWeatherMap API<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">base_url = <\/span><span style=\"color: #CE9178\">&quot;https:\/\/api.openweathermap.org\/data\/2.5\/weather&quot;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\"># Construct the final URL<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">url = <\/span><span style=\"color: #569CD6\">f<\/span><span style=\"color: #CE9178\">&quot;<\/span><span style=\"color: #569CD6\">{<\/span><span style=\"color: #D4D4D4\">base_url<\/span><span style=\"color: #569CD6\">}<\/span><span style=\"color: #CE9178\">?lat=<\/span><span style=\"color: #569CD6\">{<\/span><span style=\"color: #D4D4D4\">lat<\/span><span style=\"color: #569CD6\">}<\/span><span style=\"color: #CE9178\">&amp;lon=<\/span><span style=\"color: #569CD6\">{<\/span><span style=\"color: #D4D4D4\">lon<\/span><span style=\"color: #569CD6\">}<\/span><span style=\"color: #CE9178\">&amp;appid=<\/span><span style=\"color: #569CD6\">{<\/span><span style=\"color: #D4D4D4\">api_key<\/span><span style=\"color: #569CD6\">}<\/span><span style=\"color: #CE9178\">&amp;units=metric&quot;<\/span><span style=\"color: #D4D4D4\">  <\/span><span style=\"color: #6A9955\"># Using metric to get temperature in Celsius<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\"># Make the API call<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">response = requests.get(url)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\"># Check if the request was successful (status code 200)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> response.status_code == <\/span><span style=\"color: #B5CEA8\">200<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    weather_data = response.json()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #6A9955\"># Extract necessary details from the response<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    location_name = weather_data[<\/span><span style=\"color: #CE9178\">&#39;name&#39;<\/span><span style=\"color: #D4D4D4\">]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    temp_c = weather_data[<\/span><span style=\"color: #CE9178\">&#39;main&#39;<\/span><span style=\"color: #D4D4D4\">][<\/span><span style=\"color: #CE9178\">&#39;temp&#39;<\/span><span style=\"color: #D4D4D4\">]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    condition = weather_data[<\/span><span style=\"color: #CE9178\">&#39;weather&#39;<\/span><span style=\"color: #D4D4D4\">][<\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">][<\/span><span style=\"color: #CE9178\">&#39;description&#39;<\/span><span style=\"color: #D4D4D4\">]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #6A9955\"># Print weather details<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #DCDCAA\">print<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #569CD6\">f<\/span><span style=\"color: #CE9178\">&quot;Temperature: <\/span><span style=\"color: #569CD6\">{<\/span><span style=\"color: #D4D4D4\">temp_c<\/span><span style=\"color: #569CD6\">}<\/span><span style=\"color: #CE9178\">\u00b0C&quot;<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #DCDCAA\">print<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #569CD6\">f<\/span><span style=\"color: #CE9178\">&quot;Condition: <\/span><span style=\"color: #569CD6\">{<\/span><span style=\"color: #D4D4D4\">condition<\/span><span style=\"color: #569CD6\">}<\/span><span style=\"color: #CE9178\">&quot;<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C586C0\">else<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #DCDCAA\">print<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #569CD6\">f<\/span><span style=\"color: #CE9178\">&quot;Error: Unable to fetch data. Status code: <\/span><span style=\"color: #569CD6\">{<\/span><span style=\"color: #D4D4D4\">response.status_code<\/span><span style=\"color: #569CD6\">}<\/span><span style=\"color: #CE9178\">&quot;<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>If you want to add more functionality or make changes to the API call, <a href=\"https:\/\/openweathermap.org\/current#data\" target=\"_blank\" rel=\"noopener\">this<\/a> documentation will help you.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Putting it together<\/h3>\n\n\n\n<p>Let&#8217;s combine the code we have written so far to complete the functionality of the AI Agent. <\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"import requests\nimport geocoder\nimport time\n\napi_key = &quot;&lt;your_api_key&gt;&quot;\nbase_url = &quot;https:\/\/api.openweathermap.org\/data\/2.5\/weather&quot;\n\n# Function to fetch weather data and send alerts\ndef fetch_weather(lat, lon):\n    url = f&quot;{base_url}?lat={lat}&amp;lon={lon}&amp;appid={api_key}&amp;units=metric&quot; \n\n    # Make the API call\n    response = requests.get(url)\n\n    # Check if the request was successful (status code 200)\n    if response.status_code == 200:\n        weather_data = response.json()\n        \n        # Extract necessary details from the response\n        location_name = weather_data['name']\n        temp_c = weather_data['main']['temp']\n        condition = weather_data['weather'][0]['description']\n        rain_chance = weather_data.get('rain', {}).get('1h', 0)  # Checking if rain is expected in the last 1 hour\n        \n        # Print location and weather details\n        print(f&quot;Location: {location_name}&quot;)\n        print(f&quot;Temperature: {temp_c}\u00b0C&quot;)\n        print(f&quot;Condition: {condition}&quot;)\n        \n        # Send weather alerts based on conditions\n        if rain_chance &gt; 0:\n            print(&quot;Alert: Rain is expected. Don't forget to carry an umbrella!&quot;)\n        if temp_c &lt; 10:\n            print(&quot;Alert: It's cold. Wear warmer clothes!&quot;)\n        elif temp_c &gt; 25:\n            print(&quot;Alert: It's warm. Dress lightly!&quot;)\n        else:\n            print(&quot;The weather is mild. Dress comfortably.&quot;)\n        \n    else:\n        print(f&quot;Error: Unable to fetch data. Status code: {response.status_code}&quot;)\n\n# Function to track user's location and send alerts if they are leaving the house\ndef track_location_and_alert():\n    # Set initial location (Latitude, Longitude)\n    initial_latlng = [33.5973, 73.0479]  # Raw value provided for initial location\n    print(f&quot;Initial location (Latitude, Longitude): {initial_latlng}&quot;)\n\n    # Track the location for changes\n    while True:\n        # Get the current location again\n        g = geocoder.ip('me')\n        current_latlng = g.latlng\n        print(f&quot;Current location (Latitude, Longitude): {current_latlng}&quot;)\n        \n        # Check if the user has moved (more than 1 degree change in either lat or lon)\n        if abs(current_latlng[0] - initial_latlng[0]) &gt; 1 or abs(current_latlng[1] - initial_latlng[1]) &gt; 1:\n            print(&quot;Alert: You seem to be leaving your house. Checking weather...&quot;)\n\n            # Fetch the weather for the new location (use current latitude and longitude)\n            fetch_weather(current_latlng[0], current_latlng[1])\n\n            # Update the initial location to the new location\n            initial_latlng = current_latlng\n\n        # Wait for some time before checking again (e.g., 10 minutes)\n        time.sleep(600)  # 600 seconds = 10 minutes\n\n# Start tracking location and send weather alerts if needed\ntrack_location_and_alert()\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #C586C0\">import<\/span><span style=\"color: #D4D4D4\"> requests<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C586C0\">import<\/span><span style=\"color: #D4D4D4\"> geocoder<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C586C0\">import<\/span><span style=\"color: #D4D4D4\"> time<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">api_key = <\/span><span style=\"color: #CE9178\">&quot;&lt;your_api_key&gt;&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">base_url = <\/span><span style=\"color: #CE9178\">&quot;https:\/\/api.openweathermap.org\/data\/2.5\/weather&quot;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\"># Function to fetch weather data and send alerts<\/span><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">fetch_weather<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">lat<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">lon<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    url = <\/span><span style=\"color: #569CD6\">f<\/span><span style=\"color: #CE9178\">&quot;<\/span><span style=\"color: #569CD6\">{<\/span><span style=\"color: #D4D4D4\">base_url<\/span><span style=\"color: #569CD6\">}<\/span><span style=\"color: #CE9178\">?lat=<\/span><span style=\"color: #569CD6\">{<\/span><span style=\"color: #D4D4D4\">lat<\/span><span style=\"color: #569CD6\">}<\/span><span style=\"color: #CE9178\">&amp;lon=<\/span><span style=\"color: #569CD6\">{<\/span><span style=\"color: #D4D4D4\">lon<\/span><span style=\"color: #569CD6\">}<\/span><span style=\"color: #CE9178\">&amp;appid=<\/span><span style=\"color: #569CD6\">{<\/span><span style=\"color: #D4D4D4\">api_key<\/span><span style=\"color: #569CD6\">}<\/span><span style=\"color: #CE9178\">&amp;units=metric&quot;<\/span><span style=\"color: #D4D4D4\"> <\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #6A9955\"># Make the API call<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    response = requests.get(url)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #6A9955\"># Check if the request was successful (status code 200)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> response.status_code == <\/span><span style=\"color: #B5CEA8\">200<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        weather_data = response.json()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Extract necessary details from the response<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        location_name = weather_data[<\/span><span style=\"color: #CE9178\">&#39;name&#39;<\/span><span style=\"color: #D4D4D4\">]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        temp_c = weather_data[<\/span><span style=\"color: #CE9178\">&#39;main&#39;<\/span><span style=\"color: #D4D4D4\">][<\/span><span style=\"color: #CE9178\">&#39;temp&#39;<\/span><span style=\"color: #D4D4D4\">]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        condition = weather_data[<\/span><span style=\"color: #CE9178\">&#39;weather&#39;<\/span><span style=\"color: #D4D4D4\">][<\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">][<\/span><span style=\"color: #CE9178\">&#39;description&#39;<\/span><span style=\"color: #D4D4D4\">]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        rain_chance = weather_data.get(<\/span><span style=\"color: #CE9178\">&#39;rain&#39;<\/span><span style=\"color: #D4D4D4\">, {}).get(<\/span><span style=\"color: #CE9178\">&#39;1h&#39;<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">)  <\/span><span style=\"color: #6A9955\"># Checking if rain is expected in the last 1 hour<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Print location and weather details<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #DCDCAA\">print<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #569CD6\">f<\/span><span style=\"color: #CE9178\">&quot;Location: <\/span><span style=\"color: #569CD6\">{<\/span><span style=\"color: #D4D4D4\">location_name<\/span><span style=\"color: #569CD6\">}<\/span><span style=\"color: #CE9178\">&quot;<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #DCDCAA\">print<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #569CD6\">f<\/span><span style=\"color: #CE9178\">&quot;Temperature: <\/span><span style=\"color: #569CD6\">{<\/span><span style=\"color: #D4D4D4\">temp_c<\/span><span style=\"color: #569CD6\">}<\/span><span style=\"color: #CE9178\">\u00b0C&quot;<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #DCDCAA\">print<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #569CD6\">f<\/span><span style=\"color: #CE9178\">&quot;Condition: <\/span><span style=\"color: #569CD6\">{<\/span><span style=\"color: #D4D4D4\">condition<\/span><span style=\"color: #569CD6\">}<\/span><span style=\"color: #CE9178\">&quot;<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Send weather alerts based on conditions<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> rain_chance &gt; <\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #DCDCAA\">print<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&quot;Alert: Rain is expected. Don&#39;t forget to carry an umbrella!&quot;<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> temp_c &lt; <\/span><span style=\"color: #B5CEA8\">10<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #DCDCAA\">print<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&quot;Alert: It&#39;s cold. Wear warmer clothes!&quot;<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">elif<\/span><span style=\"color: #D4D4D4\"> temp_c &gt; <\/span><span style=\"color: #B5CEA8\">25<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #DCDCAA\">print<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&quot;Alert: It&#39;s warm. Dress lightly!&quot;<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">else<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #DCDCAA\">print<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&quot;The weather is mild. Dress comfortably.&quot;<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #C586C0\">else<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #DCDCAA\">print<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #569CD6\">f<\/span><span style=\"color: #CE9178\">&quot;Error: Unable to fetch data. Status code: <\/span><span style=\"color: #569CD6\">{<\/span><span style=\"color: #D4D4D4\">response.status_code<\/span><span style=\"color: #569CD6\">}<\/span><span style=\"color: #CE9178\">&quot;<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\"># Function to track user&#39;s location and send alerts if they are leaving the house<\/span><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">track_location_and_alert<\/span><span style=\"color: #D4D4D4\">():<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #6A9955\"># Set initial location (Latitude, Longitude)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    initial_latlng = [<\/span><span style=\"color: #B5CEA8\">33.5973<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">73.0479<\/span><span style=\"color: #D4D4D4\">]  <\/span><span style=\"color: #6A9955\"># Raw value provided for initial location<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #DCDCAA\">print<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #569CD6\">f<\/span><span style=\"color: #CE9178\">&quot;Initial location (Latitude, Longitude): <\/span><span style=\"color: #569CD6\">{<\/span><span style=\"color: #D4D4D4\">initial_latlng<\/span><span style=\"color: #569CD6\">}<\/span><span style=\"color: #CE9178\">&quot;<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #6A9955\"># Track the location for changes<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #C586C0\">while<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">True<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Get the current location again<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        g = geocoder.ip(<\/span><span style=\"color: #CE9178\">&#39;me&#39;<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        current_latlng = g.latlng<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #DCDCAA\">print<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #569CD6\">f<\/span><span style=\"color: #CE9178\">&quot;Current location (Latitude, Longitude): <\/span><span style=\"color: #569CD6\">{<\/span><span style=\"color: #D4D4D4\">current_latlng<\/span><span style=\"color: #569CD6\">}<\/span><span style=\"color: #CE9178\">&quot;<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Check if the user has moved (more than 1 degree change in either lat or lon)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">abs<\/span><span style=\"color: #D4D4D4\">(current_latlng[<\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">] - initial_latlng[<\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">]) &gt; <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">or<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">abs<\/span><span style=\"color: #D4D4D4\">(current_latlng[<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">] - initial_latlng[<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">]) &gt; <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #DCDCAA\">print<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&quot;Alert: You seem to be leaving your house. Checking weather...&quot;<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #6A9955\"># Fetch the weather for the new location (use current latitude and longitude)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            fetch_weather(current_latlng[<\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">], current_latlng[<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">])<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #6A9955\"># Update the initial location to the new location<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            initial_latlng = current_latlng<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Wait for some time before checking again (e.g., 10 minutes)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        time.sleep(<\/span><span style=\"color: #B5CEA8\">600<\/span><span style=\"color: #D4D4D4\">)  <\/span><span style=\"color: #6A9955\"># 600 seconds = 10 minutes<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\"># Start tracking location and send weather alerts if needed<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">track_location_and_alert()<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>The core functionality is divided into two functions. The <code>fetch_weather<\/code> function takes latitude and longitude as inputs and constructs a URL to fetch weather data from the API. It retrieves and processes details such as location name, temperature, weather conditions, and the chance of rain. Depending on the weather conditions, it sends alerts\u2014for instance, reminding the user to carry an umbrella if rain is expected or suggesting warmer clothes in cold weather.<\/p>\n\n\n\n<p>The <code>track_location_and_alert<\/code> function initializes the user\u2019s starting location and continuously checks for changes in their latitude and longitude. If the user\u2019s location changes by more than 1 degree, the script assumes they are leaving their house and fetches weather data for the new location. This function also updates the initial location to the user\u2019s current position and repeats the process every 10 minutes, ensuring timely weather updates and alerts as the user moves.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Designing a Weather Alert Popup<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"import requests\nimport geocoder\nfrom tkinter import messagebox, Tk\n\napi_key = &quot;&lt;your_api_key&gt;&quot;\nbase_url = &quot;https:\/\/api.openweathermap.org\/data\/2.5\/weather&quot;\n\ndef fetch_weather(lat, lon):\n    url = f&quot;{base_url}?lat={lat}&amp;lon={lon}&amp;appid={api_key}&amp;units=metric&quot;  \n    response = requests.get(url)\n\n    if response.status_code == 200:\n        weather_data = response.json()\n        \n        location_name = weather_data['name']\n        temp_c = weather_data['main']['temp']\n        condition = weather_data['weather'][0]['description']\n        \n        weather_message = (\n            f&quot;Temperature: {temp_c}\u00b0C\\n&quot;\n            f&quot;Condition: {condition.capitalize()}\\n&quot;\n        )\n        \n        if temp_c &lt; 10:\n            weather_message += &quot;Alert: It's cold. Wear warmer clothes!&quot;\n        elif temp_c &gt; 25:\n            weather_message += &quot;Alert: It's warm. Dress lightly!&quot;\n        else:\n            weather_message += &quot;The weather is mild. Dress comfortably.&quot;\n\n        return weather_message\n    else:\n        return &quot;Error: Unable to fetch weather data.&quot;\n\ndef get_geolocation():\n    g = geocoder.ip('me')\n    return g.latlng  \n    \ndef show_alert():\n    root = Tk()\n    root.withdraw()  \n\n    latlng = get_geolocation()\n    if latlng:\n        lat, lon = latlng\n        weather_message = fetch_weather(lat, lon)\n    else:\n        weather_message = &quot;Error: Unable to fetch your location.&quot;\n\n    messagebox.showinfo(&quot;Weather Alert&quot;, weather_message)\n    root.destroy()  \n\nshow_alert()\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #C586C0\">import<\/span><span style=\"color: #D4D4D4\"> requests<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C586C0\">import<\/span><span style=\"color: #D4D4D4\"> geocoder<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C586C0\">from<\/span><span style=\"color: #D4D4D4\"> tkinter <\/span><span style=\"color: #C586C0\">import<\/span><span style=\"color: #D4D4D4\"> messagebox, Tk<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">api_key = <\/span><span style=\"color: #CE9178\">&quot;&lt;your_api_key&gt;&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">base_url = <\/span><span style=\"color: #CE9178\">&quot;https:\/\/api.openweathermap.org\/data\/2.5\/weather&quot;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">fetch_weather<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">lat<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">lon<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    url = <\/span><span style=\"color: #569CD6\">f<\/span><span style=\"color: #CE9178\">&quot;<\/span><span style=\"color: #569CD6\">{<\/span><span style=\"color: #D4D4D4\">base_url<\/span><span style=\"color: #569CD6\">}<\/span><span style=\"color: #CE9178\">?lat=<\/span><span style=\"color: #569CD6\">{<\/span><span style=\"color: #D4D4D4\">lat<\/span><span style=\"color: #569CD6\">}<\/span><span style=\"color: #CE9178\">&amp;lon=<\/span><span style=\"color: #569CD6\">{<\/span><span style=\"color: #D4D4D4\">lon<\/span><span style=\"color: #569CD6\">}<\/span><span style=\"color: #CE9178\">&amp;appid=<\/span><span style=\"color: #569CD6\">{<\/span><span style=\"color: #D4D4D4\">api_key<\/span><span style=\"color: #569CD6\">}<\/span><span style=\"color: #CE9178\">&amp;units=metric&quot;<\/span><span style=\"color: #D4D4D4\">  <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    response = requests.get(url)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> response.status_code == <\/span><span style=\"color: #B5CEA8\">200<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        weather_data = response.json()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        location_name = weather_data[<\/span><span style=\"color: #CE9178\">&#39;name&#39;<\/span><span style=\"color: #D4D4D4\">]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        temp_c = weather_data[<\/span><span style=\"color: #CE9178\">&#39;main&#39;<\/span><span style=\"color: #D4D4D4\">][<\/span><span style=\"color: #CE9178\">&#39;temp&#39;<\/span><span style=\"color: #D4D4D4\">]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        condition = weather_data[<\/span><span style=\"color: #CE9178\">&#39;weather&#39;<\/span><span style=\"color: #D4D4D4\">][<\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">][<\/span><span style=\"color: #CE9178\">&#39;description&#39;<\/span><span style=\"color: #D4D4D4\">]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        weather_message = (<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #569CD6\">f<\/span><span style=\"color: #CE9178\">&quot;Temperature: <\/span><span style=\"color: #569CD6\">{<\/span><span style=\"color: #D4D4D4\">temp_c<\/span><span style=\"color: #569CD6\">}<\/span><span style=\"color: #CE9178\">\u00b0C<\/span><span style=\"color: #D7BA7D\">\\n<\/span><span style=\"color: #CE9178\">&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #569CD6\">f<\/span><span style=\"color: #CE9178\">&quot;Condition: <\/span><span style=\"color: #569CD6\">{<\/span><span style=\"color: #D4D4D4\">condition.capitalize()<\/span><span style=\"color: #569CD6\">}<\/span><span style=\"color: #D7BA7D\">\\n<\/span><span style=\"color: #CE9178\">&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        )<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> temp_c &lt; <\/span><span style=\"color: #B5CEA8\">10<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            weather_message += <\/span><span style=\"color: #CE9178\">&quot;Alert: It&#39;s cold. Wear warmer clothes!&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">elif<\/span><span style=\"color: #D4D4D4\"> temp_c &gt; <\/span><span style=\"color: #B5CEA8\">25<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            weather_message += <\/span><span style=\"color: #CE9178\">&quot;Alert: It&#39;s warm. Dress lightly!&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">else<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            weather_message += <\/span><span style=\"color: #CE9178\">&quot;The weather is mild. Dress comfortably.&quot;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> weather_message<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #C586C0\">else<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #CE9178\">&quot;Error: Unable to fetch weather data.&quot;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">get_geolocation<\/span><span style=\"color: #D4D4D4\">():<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    g = geocoder.ip(<\/span><span style=\"color: #CE9178\">&#39;me&#39;<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> g.latlng  <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">show_alert<\/span><span style=\"color: #D4D4D4\">():<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    root = Tk()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    root.withdraw()  <\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    latlng = get_geolocation()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> latlng:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        lat, lon = latlng<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        weather_message = fetch_weather(lat, lon)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #C586C0\">else<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        weather_message = <\/span><span style=\"color: #CE9178\">&quot;Error: Unable to fetch your location.&quot;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    messagebox.showinfo(<\/span><span style=\"color: #CE9178\">&quot;Weather Alert&quot;<\/span><span style=\"color: #D4D4D4\">, weather_message)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    root.destroy()  <\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">show_alert()<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>When you run this code, a popup alert will appear on your screen like the one shown below, giving you weather details according to your current location.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"371\" height=\"203\" src=\"https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/12\/Screenshot-2024-12-12-155728.png\" alt=\"\" class=\"wp-image-11215\" style=\"width:302px;height:auto\" srcset=\"https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/12\/Screenshot-2024-12-12-155728.png 371w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/12\/Screenshot-2024-12-12-155728-300x164.png 300w\" sizes=\"auto, (max-width: 371px) 100vw, 371px\" \/><\/figure>\n<\/div>\n\n\n<p>The purpose of this AI agent is to seamlessly fetch real-time weather information and provide helpful suggestions, such as carrying an umbrella or dressing lighter for warmer weather, as soon as it detects that you may be leaving your house.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>AI agents are transforming the technological landscape by automating tasks, enhancing decision-making, and personalizing user experiences across industries. This guide explored the fundamentals of AI agents, real-world applications, and comprehensive steps to create your own AI Agent.  By understanding and leveraging the potential of AI agents, businesses and developers can unlock new opportunities, drive innovation, and stay competitive in a rapidly evolving digital environment.<\/p>\n\n\n\n<p><strong>Related Reading:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/metaschool.so\/articles\/build-ai-powered-customer-support-chatbot\">How to Build a Customer Support Chatbot with GPT-4<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/metaschool.so\/articles\/ai-project-ideas\">20 AI Project Ideas For Developers to Build On (with Code)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/metaschool.so\/articles\/ai-powered-health-diagnosis-assistant\">How to Build an AI-Powered Health Diagnosis Assistant<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/metaschool.so\/articles\/javascript-chrome-extension-using-chatgpt\">Comprehensive Guide to Build a JavaScript Chrome Extension Using ChatGPT<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"FAQs\"><\/span>FAQs<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1733906334940\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">How Are AI Agents Built?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>AI agents are built using a combination of algorithms, training data, and frameworks. The process involves:<br \/><strong>Defining Objectives<\/strong>: Identify the agent&#8217;s purpose, such as solving problems or interacting with users.<br \/><strong>Data Collection<\/strong>: Gather datasets for training the agent on relevant tasks.<br \/><strong>Training Models<\/strong>: Use machine learning frameworks like TensorFlow or PyTorch to train the agent.<br \/><strong>Integration<\/strong>: Combine the trained model with APIs or platforms to enable real-world interactions.<br \/><strong>Testing and Refinement<\/strong>: Test the agent in simulated or real environments and optimize performance.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1733906346595\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">Can I Create My Own AI?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Yes, you can create your own AI using tools and frameworks like TensorFlow, OpenAI, or IBM watsonx. Beginners can start with pre-built models or low-code platforms like Microsoft Power Automate or ChatGPT\u2019s API, which simplify the AI development process.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1733906416525\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">What Are the 5 Types of Agents in AI?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p><strong>e Reflex Agents<\/strong>: React to current inputs without considering history (e.g., basic chatbots).<br \/><strong>Model-Based Reflex Agents<\/strong>: Maintain internal states to handle more complex situations.<br \/><strong>Goal-Based Agents<\/strong>: Make decisions based on specific objectives (e.g., navigation systems).<br \/><strong>Utility-Based Agents<\/strong>: Consider both goals and optimal performance to maximize outcomes.<br \/><strong>Learning Agents<\/strong>: Adapt and improve over time by learning from interactions and experiences.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1733906429869\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">How to Create an AI Agent Without Code?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>You can create AI agents without coding using no-code platforms such as:<br \/><strong>Microsoft Power Automate<\/strong>: Build process automation workflows.<br \/><strong>Dialogflow<\/strong>: Create conversational agents and chatbots.<br \/><strong>Runway<\/strong>: Design AI-powered tools for creative projects.<br \/><strong>Make (formerly Integromat)<\/strong>: Automate tasks by connecting apps and services.<br \/>These platforms allow users to drag and drop components, set rules, and deploy AI agents quickly and efficiently.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"","protected":false},"author":19,"featured_media":11153,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"","neve_meta_content_width":0,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":"","footnotes":""},"categories":[344],"tags":[],"class_list":["post-11152","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-artificial-intelligence"],"_links":{"self":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/11152","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/users\/19"}],"replies":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/comments?post=11152"}],"version-history":[{"count":11,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/11152\/revisions"}],"predecessor-version":[{"id":11219,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/11152\/revisions\/11219"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/media\/11153"}],"wp:attachment":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/media?parent=11152"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/categories?post=11152"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/tags?post=11152"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}