Skip to content

hkint/go-postgres-agent

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PostgreSQL Database AI Agent Demo

An intelligent database agent demo that can interact with PostgreSQL databases using natural language queries powered by LLM models.

Features

  • Natural Language Interface: Ask questions about your database in nature language
  • SQL Query Execution: Automatically generates and executes SQL queries based on your requests
  • Conversation Memory: Maintains context across multiple interactions
  • Error Handling: Robust error handling for database operations and API calls
  • Data Serialization: Properly handles various PostgreSQL data types including arrays, timestamps, and custom types

Prerequisites

  • Go 1.23+
  • PostgreSQL database
  • LLM API key

Installation

  1. Clone or download the source code

  2. Install dependencies:

    go mod tidy
  3. Copy the environment configuration:

    cp .env.example .env
  4. Edit .env file with your actual configuration

Usage

  1. Run the application:

    go run .
  2. Start asking questions about your database:

    You: Show me all tables in the database
    You: How many users do we have?
    You: What are the top 5 products by sales?
    
  3. Use special commands:

    • clear: Clear conversation history
    • exit or quit: Exit the application

Project Structure

  • main.go: Main application entry point and CLI interface
  • db_agent.go: Core database agent implementation
  • config.go: Configuration management
  • go.mod: Go module dependencies
  • .env.example: Environment variables template

Dependencies

  • github.com/joho/godotenv: Environment variable loading
  • github.com/lib/pq: PostgreSQL driver
  • github.com/sashabaranov/go-openai: OpenAI API client

Key Features

Data Type Handling

The agent properly serializes various PostgreSQL data types:

  • Timestamps (converted to RFC3339 format)
  • Arrays (both integer and string arrays)
  • Binary data (converted to strings)
  • Custom types (handled via reflection)

Conversation Context

The agent maintains conversation history, allowing for:

  • Follow-up questions
  • Contextual queries
  • Reference to previous results

Error Management

Comprehensive error handling for:

  • Database connection issues
  • SQL execution errors
  • API communication problems
  • Data serialization issues

Example Interactions

You: What tables do we have?
Agent: I'll check what tables are available in your database.

You: Show me the structure of the users table
Agent: Here's the structure of the users table with all columns and their data types.

You: How many active users do we have?
Agent: Based on the users table, there are 1,247 active users.

Security Notes

  • Never commit your .env file with actual credentials
  • Use environment variables in production
  • Consider implementing connection pooling for production use
  • Review and sanitize any user inputs if extending this tool

Contributing

Feel free to submit issues and pull requests to improve the functionality and add new features.

License

This project is provided as-is for educational and development purposes.

About

A demo AI Agent for PostgreSQL database

Resources

Stars

Watchers

Forks

Languages