-
Integrates AI models with PostgreSQL for enhanced database capabilities:
- OpenAI's GPT models
- LLAMA-3.1 (via OpenVLLM for local deployment)
-
Key Components:
- pgVector: Enables efficient vector operations in PostgreSQL
- sentence-transformers: Facilitates text embedding and semantic analysis
-
Core Functionalities:
- AI-powered responses directly within PostgreSQL database
- Semantic clustering capabilities on PostgreSQL
-
Optional Features:
- pRESTd integration for direct REST API access
-
Advanced Use Cases:
- Templating support for customized AI interactions
- Chained Conversations demonstrated for complex query handling
- Custom PostgreSQL database with Python (plpython3u) extension
- Integration with OpenAI API for generating AI responses
- Docker-based setup for easy deployment and scaling
- Environment variable management for secure API key handling
- Docker
- Docker Compose
- OpenAI API Key
You can install this project in two ways:
-
Pull the image from the command line:
docker pull ghcr.io/data2json/postresql-openai:v0.0.1
-
Use as base image in your Dockerfile:
FROM ghcr.io/data2json/postresql-openai:v0.0.1
-
Clone the repository:
git clone https://github.com/your-username/postgres-openai.git cd postgres-openai
-
Create a
.env
file in the project root with the following content:POSTGRES_PASSWORD=your_postgres_password OPENAI_API_KEY=your_openai_api_key OPENAI_API_BASE=http://your-api-base-url:port/v1
Replace the values with your actual PostgreSQL password, OpenAI API key, and API base URL.
-
Build and start the Docker containers:
docker-compose up -d --build
Once the container is up and running, you can connect to the PostgreSQL database and use the AI-powered functions.
-
Connect to the database:
docker-compose exec db pgcli
-
Insert a question into the
ai_responses
table:INSERT INTO ai_responses (input_text) VALUES ('Why is the sky blue?');
-
Retrieve the AI-generated response:
SELECT * FROM ai_responses ORDER BY id DESC LIMIT 1;
curl -X POST "http://192.168.2.2:3000/mydb/public/ai_responses" -H "Content-Type: application/json" -d '{"input_text": "Hi mom!"}' | jq
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 109 100 84 100 25 131 39 --:--:-- --:--:-- --:--:-- 172
{
"id": 3,
"input_text": "Hi mom!",
"ai_response": "Hello sweetie! How's your day going?"
}
Dockerfile
: Defines the PostgreSQL image with Python and OpenAI integrationdocker-compose.yml
: Orchestrates the Docker setupinit.sql
: Initializes the database schema and functions
You can customize the conversation templates by modifying the upsert_conversation_template
function calls in the init.sql
file.
If you encounter any issues:
- Check that your
.env
file is correctly set up with valid credentials. - Ensure Docker and Docker Compose are up to date.
- Check the Docker logs for any error messages:
docker-compose logs db
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the BSD License - see the LICENSE file for details.