Difference Between a Transformer and an AI Agent

Materials available at: http://forejune.co/cuda/

AI Agent

  • A system using a model, often a Transformer

  • An LLM + tools + memory + planning + an execution loop:
    • Perceive
    • Reason
    • Act
    • Observe the result
    • Act again

  • Typical components of an agent:
    • Perception / input
    • Reasoning / planning
    • Tools / actions
    • Memory
    • Loop

    
    | Traditional Transformer             | AI Agent                         |
    | ----------------------------------- | -------------------------------- |
    | Processes input                     | Observes environment             |
    | Generates output                    | Takes actions                    |
    | Usually one inference task          | Repeated inference loop          |
    | Context is primarily input sequence | Maintains state/memory           |
    | No inherent tools                   | Can use tools                    |
    | No inherent planning                | Can plan multiple steps          |
    | Output is usually text/tokens       | Output can be an action          |
    | Model-centric                       | System-centric                   |
    | Transformer is the main component   | Transformer may be one component |
    	
  • A Tic-Tac-Toe Agent Class in C/C++

    See also ../tictrans/tictrans.html
    (A Simple Training of an AI Transformer to Play Tic-Tac-Toe in C/C++)

    	
    const int SOS = 9;    // start of sequence
    const int EOS = 10;   // end of sequence
    
    enum Player {EMPTY=0, X, O};
    
    // helper functions
    bool hasWon(const vector<Player>& board, Player player)
    {
        if (board.size() != 9)
            throw invalid_argument("Invalid tic-tac-toe board size.");
    
        if (player != X && player != O)
            throw invalid_argument("Invalid player.");
    
        static const int w[8][3] = {
            {0,1,2}, {3,4,5}, {6,7,8},  // rows
            {0,3,6}, {1,4,7}, {2,5,8},  // columns
            {0,4,8}, {2,4,6}             // diagonals
        };
    
        for (int i = 0; i < 8; i++) {
            if (board[w[i][0]] == player &&
                board[w[i][1]] == player &&
                board[w[i][2]] == player)
                return true;
        }
    
        return false;
    }
    
    bool gameOver(const vector<Player>& board)
    {
        if (board.size() != 9)
            throw invalid_argument("Invalid tic-tac-toe board size.");
    
        // Either X or O has won.
        if (hasWon(board, X) || hasWon(board, O))
            return true;
    
        // No winner: game ends when the board is full
        for (int i = 0; i < 9; i++) {
            if (board[i] == EMPTY)
                return false;
        }
    
        return true;  // Draw
    }
    
    // predict next move from history
    int predictMove( MiniGPT& model, const vector<int>& history, 
    		const vector<Player>& board)
    {
        if (board.size() != 9)
            throw invalid_argument("Invalid tic-tac-toe board size.");
    
        // Prepend SOS to current move history
        vector<int> prompt;
        prompt.push_back(SOS);
    
        for (int i = 0; i < history.size(); i++)
           prompt.push_back( history[i] );
    
        matrixd P = model.forward(prompt);
    
        if (P.empty() || P.back().size() < 9)
            return -1;
    
        const vector<double>& scores = P.back();
    
        int bestMove = -1;
        double bestScore = -999;
    
        // Choose the highest-scoring legal move.
        for (int move = 0; move < 9; move++) {
            if (board[move] == EMPTY &&
                scores[move] > bestScore) {
                bestScore = scores[move];
                bestMove = move;
            }
        }
    
        return bestMove;
    }
    		

    class TicTacToeAgent
    {
    protected:
        Player agentPlayer;
        string goalDescription;  // Goal
        vector<int> history;     // Current game's sequence of moves (memory)
        MiniGPT model;           // Decoder-only Transformer
    public:
        explicit TicTacToeAgent(Player player);
        bool goalAchieved(const vector<Player>& board);
        void recordMove(int action);
        vector<int> planner(const vector<Player>& board);
        int actionSelection(const vector<Player>& board);
        void reset();
    };
    
    // "TicTacToeAgent agent = X;" not allowed
    TicTacToeAgent::TicTacToeAgent(Player player): agentPlayer(player),
          goalDescription(
              "Align three of my marks in a row, column, or diagonal."),
          model(20, 12, 32, 4, 64, 2)
    {
        model.readWeights( (char *) "weights.txt" );
        if (agentPlayer != X && agentPlayer != O)
            throw invalid_argument("Agent must play X or O.");
    }
      
    bool TicTacToeAgent::goalAchieved(const vector<Player>& board)
    {
        return hasWon(board, agentPlayer);
    }
       
    // Record every move made in the current game.
    void TicTacToeAgent::recordMove(int action)
    {
        if (action < 0 || action >= 9)
            throw invalid_argument("Invalid move index.");
    
        history.push_back(action);
    }
    
    // returns legal moves
    vector<int> TicTacToeAgent::planner(const vector<Player>& board)
    {
        if (board.size() != 9)
            throw invalid_argument("Invalid tic-tac-toe board size.");
    
        vector<int> legalMoves;
    
        if (gameOver(board))
            return legalMoves;
    
        for (int i = 0; i < 9; i++) {
            if (board[i] == EMPTY)
                legalMoves.push_back(i);
        }
    
        return legalMoves;
    }
    
    int TicTacToeAgent::actionSelection(
        const vector<Player>& board)
    {
        vector<int> candidates = planner(board);
    
        int n = candidates.size();
        if ( n == 0 )	// no legal moves
            return -1;
        else if ( n == 1 )  // only one legal move
    	return candidates[0];
    
        Player opponent = (agentPlayer == X) ? O : X;
    
        // Strategy works with transformer advice 
        // First check whether the agent can win immediately
        for (int move : candidates) {
            vector<Player> testBoard = board;
    
            testBoard[move] = agentPlayer;
    
            if (hasWon(testBoard, agentPlayer))
                return move;
        }
    
        // Check whether the opponent can win immediately.
        //    If so, block that winning move.
        for (int move : candidates) {
            vector<Player> testBoard = board;
    
            testBoard[move] = opponent;
    
            if (hasWon(testBoard, opponent))
                return move;
        }
    
        // Ask the Transformer to predict the next move.
        int move = predictMove(model, history, board);
    
        if (find(candidates.begin(), candidates.end(), move)
                != candidates.end()) {
            return move;
        }
    
        // If transformer prediction failed, use random legal move
        srand(time(NULL));
    
        int r = rand() % n;
    
        return candidates[r];
    }
    
    void TicTacToeAgent::reset()
    {
      history.clear();
    }