Skip to content

imkjangid/PyAgentHub

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PyAgentHub

🤖
Welcome to PyAgentHub

Code smarter. Build agents.

🧠 Built for developers, researchers, and curious minds
🧩 Focused on modular, scalable agent design

This repository provides a comprehensive guide to building an AI agent using Python. It covers the fundamental concepts, essential libraries, and practical steps involved in creating an intelligent agent capable of interacting with its environment and achieving specific goals. We will explore different types of agents, their architectures, and implementation techniques, equipping you with the knowledge to develop your own AI agents for various applications.

Introduction to AI Agents

An AI agent is an autonomous system that senses its environment through inputs and influences it through outputs. Its core purpose is to accomplish one or more defined goals. These agents can range from simple rule-based responders to advanced learning systems capable of adapting to dynamic conditions.

AI Agent Architecture

Key Components of an AI Agent

Perception: The ability to sense and interpret the environment.
Reasoning: The ability to process information and make decisions.
Action: The ability to execute actions that affect the environment.
Learning: The ability to improve performance over time through experience.

Types of AI Agents

Simple Reflex Agents: These agents react directly to percepts based on predefined rules. They have no memory of past states.
Model-Based Reflex Agents: These agents maintain an internal model of the environment to make decisions based on past experiences and current percepts.
Goal-Based Agents: These agents have a specific goal in mind and make decisions to achieve that goal.
Utility-Based Agents: These agents consider multiple goals and choose actions that maximize their overall utility or happiness.
Learning Agents: These agents can learn from experience and improve their performance over time.

Setting Up the Environment

This involves installing Python and the necessary libraries.

1. Installing Python

Don’t have Python yet? Download and install the latest Python release from https://www.python.org ↗, following the OS-specific installation steps.

Tip: On Windows, during installation, check Add Python to PATH to make it accessible from the command line.

2. Verify Installation

Open a terminal or command prompt and run:

Bash or Command Prompt

python --version

You should see the installed Python version (e.g., Python 3.xx.x).

3. Create a Project Directory

Organize your work by creating a dedicated folder:

Bash or Command Prompt

mkdir ai-agent
cd ai-agent

4. Set Up a Virtual Environment

A virtual environment keeps your project dependencies isolated.

Bash or Command Prompt

python -m venv <env_name>

5. Activate the Virtual Environment

A virtual environment keeps your project dependencies isolated.

Windows Command Prompt

env_name/Scripts/activate

Bash: Windows

source env_name/Scripts/activate

Bash: macOS/Linux

source env_name/bin/activate

6. Upgrade pip

Ensure you have the latest package manager:

Bash or Command Prompt

pip install --upgrade pip

7. Install Required Packages

For a basic AI agent setup:

Bash or Command Prompt

pip install openai langchain python-dotenv
  • openai → Talk to OpenAI’s models.
  • langchain → Build structured, multi-step AI workflows.
  • python-dotenv → Securely manage API keys and config

Optional but useful::

Bash or Command Prompt

pip install faiss-cpu duckduckgo-search requests
  • faiss-cpu → Fast vector search for knowledge retrieval.
  • duckduckgo-search → Live web search tool.
  • requests → General-purpose HTTP client for API calls.

8. Configure Environment Variables

Create a .env file in your project root to store API keys securely:

OPENAI_API_KEY=your_openai_key_here

Load it in your Python code:

from dotenv import load_dotenv
load_dotenv()

9. Test the Setup

Run a quick Python check:

print("Environment is ready!")

If no errors appear, your environment is successfully configured.

10. 🚀 Run a Starter AI Agent

Once your environment is ready, you can test it by creating agent.py with a minimal AI agent that uses LangChain and the OpenAI API.

📂 Project Structure
ai-agent/ │ ├── env_name/ # Virtual environment folder ├── .env # API keys and secrets ├── agent.py # Starter AI agent script └── requirements.txt # Optional: freeze dependencies

STEP 1: Import required libraries:

import os
from dotenv import load_dotenv
from langchain.chat_models import ChatOpenAI
from langchain.agents import initialize_agent, AgentType
from langchain.tools import Tool
from langchain.utilities import DuckDuckGoSearchAPIWrapper

STEP 2: Load environment variables from .env

load_dotenv()

STEP 3: Get your API key

openai_api_key = os.getenv("OPENAI_API_KEY")
if not openai_api_key:
    raise ValueError("OPENAI_API_KEY not found in .env file")

STEP 4: Initialize the LLM

llm = ChatOpenAI(
    temperature=0,
    model="gpt-3.5-turbo",  # Change to gpt-4 if available
    openai_api_key=openai_api_key
)

STEP 5: Add a simple search tool

search = DuckDuckGoSearchAPIWrapper()
tools = [
    Tool(
        name="Web Search",
        func=search.run,
        description="Search the web for current information"
    )
]

STEP 6: Create the agent

agent = initialize_agent(
    tools,
    llm,
    agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
    verbose=True
)

STEP 7: Run the agent

if __name__ == "__main__":
    query = "What's the latest news about AI agents?"
    print(f"🤖 Asking agent: {query}")
    response = agent.run(query)
    print("\nAgent Response:\n", response)

Test the Agent

Activate your virtual environment:

python agent.py

🔍 Fresh code drops ahead. Keep an eye on PyAgentHub! 🚀

Thank You!

About

A centralized platform offering Python scripts, implementation examples, and reference materials for AI agent design and testing.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages