DEV Community

Josmel Noel
Josmel Noel

Posted on

1 1

How to build an AI agent using Python

# Building an AI Agent Using Python

Welcome, fellow developers! Today we're going to dive into creating an AI agent using Python. This tutorial assumes you have a basic understanding of Python and some familiarity with AI tools. Let's get started!

Prerequisites

To follow along, ensure you have the following:

  1. Python 3.x installed (you can check your version by running python --version in your terminal)
  2. A text editor or IDE of your choice (e.g., Visual Studio Code, PyCharm, etc.)
  3. The TensorFlow library for machine learning (pip install tensorflow)
  4. Scikit-learn library for machine learning (pip install scikit-learn)
  5. Numpy library for numerical computations (usually included by default with Python)
  6. A dataset to train our AI agent

Creating Our AI Agent

For this tutorial, we'll create a simple reinforcement learning agent that plays the cartpole game as an example of an AI problem.

First, let's import the necessary libraries:

import numpy as np
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from cartpole_env import CartPoleEnv
Enter fullscreen mode Exit fullscreen mode

Here we're using TensorFlow for creating our neural network and cartpole_env for our environment. The CartPoleEnv is a built-in environment from the OpenAI Gym library that simulates balancing a pole on a cart.

Defining the Neural Network

Next, we'll define our neural network architecture:

def create_model():
    model = Sequential()
    model.add(Dense(16, input_dim=4, activation='relu'))  # Input layer
    model.add(Dense(16, activation='relu'))            # Hidden layer
    model.add(Dense(2))                                # Output layer
    return model
Enter fullscreen mode Exit fullscreen mode

Interacting with the Environment

Now let's write a function to interact with our environment and learn from its experiences:

def play_cartpole(model, env, num_episodes=100):
    scores = []
    for episode in range(num_episodes):
        state = env.reset()
        score = 0
        while True:
            action = model.predict([state])[0]
            next_state, reward, done, _ = env.step(action)
            score += reward
            state = next_state
            if done or episode == num_episodes - 1:
                break
        scores.append(score)
    return np.mean(scores), scores
Enter fullscreen mode Exit fullscreen mode

Training the AI Agent

Finally, let's train our agent and play some games:

if __name__ == "__main__":
    env = CartPoleEnv()
    model = create_model()

    for layer in model.layers:
        layer.trainable = True  # Set all layers to be trainable

    num_episodes = 500
    best_score, best_scores = play_cartpole(model, env, num_episodes)
    print("Average score:", best_score)
Enter fullscreen mode Exit fullscreen mode

Conclusion

There you have it! A simple AI agent built using Python and TensorFlow. You can tweak the parameters, change the environment, or even try other reinforcement learning algorithms to enhance this project further.

Keep learning and experimenting with different AI problems and techniques – the field of AI is constantly evolving! Happy coding!

tutorial image

Next.js Tutorial 2025 - Build a Full Stack Social App

In this 4-hour hands-on tutorial, Codesistency walks you through the process of building a social platform from scratch with Next.js (App Router), React, Prisma ORM, Clerk for authentication, Neon for PostgreSQL hosting, Tailwind CSS, Shadcn UI, and UploadThing for image uploads.

Watch the video →

Top comments (0)

Dev Diairies image

User Feedback & The Pivot That Saved The Project ↪️

We’re following the journey of a dev team building on the Stellar Network as they go from hackathon idea to funded startup, testing their product in the real world and adapting as they go.

Watch full video 🎥

👋 Kindness is contagious

Explore this insightful write-up, celebrated by our thriving DEV Community. Developers everywhere are invited to contribute and elevate our shared expertise.

A simple "thank you" can brighten someone’s day—leave your appreciation in the comments!

On DEV, knowledge-sharing fuels our progress and strengthens our community ties. Found this useful? A quick thank you to the author makes all the difference.

Okay