This article introduces AI Agents, which are smart programs that can perform tasks, make decisions, and communicate effectively. Unlike basic chatbots, AI Agents use tools for precise answers when they lack information. The tutorial shows how to build different types of AI Agents from scratch using Ollama, a library that allows running large language models (LLMs) locally for better data control. Readers will learn how to set up their own Agents to perform web searches and even execute Python code. The tutorial is beginner-friendly, offering clear Python code examples. Stay tuned for Part 2, where more advanced applications will be explored. Visit GitHub for the full code and get ready to create your own AI Agents!
Intro to AI Agents: Building Intelligent Systems from Scratch
AI Agents are computer programs that can operate by themselves to accomplish tasks, decide what to do, and communicate with users. These agents utilize various tools to perform their functions efficiently. In Generative AI applications, AI Agents are designed to follow logical steps and can access outside resources, such as web searches or databases, when their existing knowledge falls short. Unlike basic chatbots that may provide random responses when unsure, AI Agents harness tools to deliver more precise and relevant answers.
As we advance towards Agentic AI, we envision systems capable of making independent decisions without needing constant human control. Current AI Agents primarily react to prompts given by users, while future Agentic AIs will proactively tackle problems and adapt their actions based on the context.
Creating an AI Agent from scratch is becoming increasingly user-friendly—similar to how developing simple machine learning models was enhanced a decade ago with tools like Scikit-Learn. In this article, we will break down the process of building different types of AI Agents using easy-to-follow Python code, allowing anyone to replicate the examples at home.
Getting Started with AI Agents
To launch your AI Agent, you won’t need expensive GPUs or API keys. The only software required is Ollama, which allows you to run large language models locally, giving you greater control over data privacy without the necessity of cloud services.
Begin by downloading Ollama from its official website and use the prompt command to download a chosen model. For this guide, we’ll use Alibaba’s Qwen model due to its efficiency and light resource requirements.
Let’s test our AI Agent now by writing some code.
First, we will initialize the model in Python:
import ollama
llm = "qwen2.5"
Then, let’s see if it can respond to a simple prompt:
stream = ollama.generate(model=llm, prompt="what time is it?", stream=True)
for chunk in stream:
print(chunk['response'], end='', flush=True)
While this might be a basic interaction, we want our AI Agent to have more capabilities. Therefore, we need to activate tools that allow it to perform tasks beyond just chatting.
One common tool is the ability to search the Internet. We can utilize the DuckDuckGo search tool, which is straightforward to implement in Python.
With Ollama, we can define the tools in a format that allows our Agent to understand their functions.
From simple web searches to specific queries in finance, such as:
def search_yf(query: str) -> str:
engine = DuckDuckGoSearchResults(backend="news")
return engine.run(f"site:finance.yahoo.com {query}")
Creating a Simple Web-Searching Agent
At its core, even a simple Agent should effectively use one or more tools to generate meaningful outputs for user inquiries.
We need to start by crafting prompt instructions for our agent and maintain an active chat loop with user input. Here’s a basic structure:
prompt="""
You are an assistant with access to tools; decide when to use tools to respond to user messages.
"""
messages = [{"role": "system", "content": prompt}]
As we develop this conversational loop, we can introduce conditions to handle user inputs and trigger the AI responses, ultimately creating an engaging and responsive AI Agent.
Conclusion
In this article, we’ve explored the foundational aspects of creating an AI Agent using Ollama. With these basic tools and methods, you’re on your way to developing your own customized Agents for various applications.
Make sure to look out for our upcoming posts, where we delve even deeper into more sophisticated examples and functionalities.
For a detailed walkthrough, you can check the full code on GitHub.
Thank you for reading! If you have questions, need assistance, or want to share your projects, feel free to connect with me. Let’s explore the world of AI together!
What is “AI Agents from Zero to Hero – Part 1”?
“AI Agents from Zero to Hero – Part 1” is a guide designed to help beginners understand AI agents. It covers the basics of AI technology, how these agents work, and their applications in everyday life.
Who can benefit from this guide?
Anyone interested in learning about AI can benefit from this guide. Whether you are a student, a professional in another field, or just a curious person, this guide will provide valuable insights into AI agents.
Do I need any prior knowledge to start?
No prior knowledge is needed. This guide is aimed at beginners, so it explains concepts in simple terms. You can start from scratch and build your understanding step by step.
What will I learn from Part 1?
In Part 1, you will learn the fundamental concepts of AI agents, their functions, and how they impact various industries. You will also see real-world examples of AI in action.
Are there any practical exercises included?
Yes, the guide includes practical exercises to help you apply what you learn. These exercises are designed to reinforce your understanding and make the material more engaging.