Let's build an AI agent using Laravel | Laravel AI SDK

Let's build an AI agent using Laravel | Laravel AI SDK

Introduction to Laravel AI SDK

Overview of the Video

  • The presenter, Aritya, introduces the video focused on using the newly released Laravel AI SDK to create a sophisticated sales agent.
  • The main goal is to implement a chatbot that can respond to user queries about products and orders.

Project Setup

  • A Laravel project with React and Inertia is used as a starter kit for simplicity, emphasizing the implementation of the Laravel AI SDK.
  • The presenter mentions having models for categories, orders, and products, with seeded values in the database for demonstration.

Installing and Configuring the SDK

Installation Steps

  • The installation process begins by copying a command to install the SDK via terminal.
  • After installation, files including stubs and migrations need to be published using another command.

Database Migration

  • Two tables are created: agent_conversations for storing conversation history and agent_conversation_messages for individual messages within those conversations.
  • The presenter runs migration commands in terminal to set up these tables in their database.

Configuring API Keys and Models

Configuration Details

  • Default settings use OpenAI APIs for text generation; other models like Gemini (for images) and Coher (for reranking) are also mentioned.
  • Users can change default models or specify different ones based on specific tasks by updating their environment variables.

Setting Up API Keys

  • The presenter explains how to add API keys into the .env file, specifically mentioning an OpenAI key setup.

Creating the Sales Assistant Agent

Defining Agent Characteristics

  • An agent named "sales assistant" is created with capabilities to provide structured outputs based on user queries regarding products and orders.

Instructions for Agent Functionality

  • Boundaries are defined so that the agent has access only to relevant data such as products, categories, and order information.

Implementing Tools for Enhanced Functionality

Tool Creation Process

  • Instructions are provided on defining what tasks or tools will assist the agent in responding accurately to user inquiries about orders or product listings.

Utilizing Built-in Tools

  • Discussion includes built-in tools available from various models like web search functionalities which can enhance responses based on user input.

Implementing User-Specific Tools in a Product Management System

Overview of Tool Integration

  • The discussion begins with the introduction of various tools: product tool, list category tool, and list order tool for users. These tools are designed to cater to specific user needs.
  • The speaker outlines the process of adding these tools into the system, emphasizing the importance of passing details to each tool for better functionality.

Setting Up Routes and Controllers

  • A route is created in web.php to facilitate communication with an agent through a POST request named invoke agent.
  • The speaker demonstrates how to create an AgentController using PHP Artisan command, which will handle requests related to invoking agents.

Validating Requests

  • Before invoking the agent, it’s crucial to validate that a prompt has been provided by the user. This involves setting validation rules for incoming requests.
  • The method used can either pass user information directly or utilize a prompt function; flexibility is highlighted as key in this design.

Handling Agent Responses

  • A response from the sales assistant is generated based on validated input. If structured output is needed later, adjustments may be required.
  • The constructor within the sales assistant captures user data, ensuring that interactions are personalized and relevant.

Developing Product Tool Functionality

  • Focus shifts towards creating functionality for listing products. Options include displaying all products or filtering them based on category ID.
  • Validation checks are implemented for category inputs; if no category ID is provided, all products will be listed by default.

Querying Products Based on Categories

  • A query mechanism is established where products can be filtered based on their associated categories using Laravel's Eloquent ORM capabilities.
  • The implementation includes checking relationships between products and categories before returning results back to users.

Frontend Interaction with React

  • Transitioning to frontend development using React, state management for messages from users is discussed alongside handling input values effectively.
  • An event handler (handleSend) is set up for sending messages via fetch calls; this includes preparing JSON data for transmission.

Testing API Calls and Handling CSRF Tokens

  • Initial testing reveals issues due to missing CSRF tokens in fetch requests; solutions involve adding meta tags and configuring headers correctly.

Product Management and User Interaction in Development

Parsing JSON and Displaying Products

  • The initial error encountered was related to parsing data as JSON. The solution involved changing the parsing method to text, allowing for a successful display of all products.
  • A state management approach using useState is introduced to handle chat messages, with an optional ID for each message since it's not always necessary.
  • Responses are updated by appending new user messages along with an incremented ID, ensuring that the assistant's responses are tracked effectively.

User Message Handling and UI Improvements

  • Before sending a message, it’s added to the responses list as a user message, enhancing interaction tracking.
  • After sending a request (e.g., "show me all products"), there’s a need to clear the input field and potentially show a loading state while fetching data from the database.

Category Filtering and Product Relations

  • Users can filter products by category (e.g., men's tops), demonstrating how specific queries yield relevant product results based on category relations in the database.
  • The implementation of a markdown library improves visual presentation by converting markdown text into HTML safely using DOM Purify to prevent XSS attacks.

Order Management and User-Specific Tools

  • New tools have been added for listing categories based on provided names, alongside an order tool that retrieves orders linked to specific users via their IDs.
  • Emphasis is placed on maintaining clean input schemas when passing user IDs directly into functions or classes for order retrieval.

Conversation Tracking and Database Integration

  • The system allows saving conversations automatically through Laravel AI SDK integration, eliminating manual message handling.
  • When creating or interacting with an assistant instance, either a new conversation ID can be generated or an existing one can be passed to retrieve past messages efficiently.

Testing Functionality and Final Demonstration

  • A demonstration shows how user interactions (like asking for product lists or orders associated with them) are processed correctly within the system.
  • Conversations are stored in the database after interactions; this enables retrieval of chat history based on conversation IDs for future reference.
  • The final test involves querying both product listings and personal orders successfully, showcasing comprehensive functionality within the application.
Video description

Hello everyone, In this video, we will take the first look at Laravel AI SDK and create a simple agent. Git repo: https://github.com/adityar15/laravelagentdemo Follow me on Twitter: https://twitter.com/adir1521 Follow me on LinkedIn https://www.linkedin.com/in/adityakadam2194/ Follow me on GitHub https://github.com/adityar15