mirror of
https://github.com/vxcontrol/langchaingo.git
synced 2026-08-24 21:31:28 -04:00
d44ea4436a
* examples: Add READMEs to each example
36 lines
1.7 KiB
Markdown
36 lines
1.7 KiB
Markdown
# PostgreSQL Database Chain Example
|
|
|
|
This example demonstrates how to use the LangChain Go library to interact with a PostgreSQL database using natural language queries. It showcases the power of combining language models with database operations.
|
|
|
|
## What This Example Does
|
|
|
|
1. **Database Setup**:
|
|
- Creates two tables: `foo` and `foo1`.
|
|
- Populates `foo` with 100 rows and `foo1` with 200 rows of sample data.
|
|
|
|
2. **LangChain Integration**:
|
|
- Initializes an OpenAI language model.
|
|
- Sets up a SQL database chain using the PostgreSQL database.
|
|
|
|
3. **Natural Language Queries**:
|
|
- Executes three different queries using natural language:
|
|
a. Retrieves rows from the `foo` table where the ID is less than 23.
|
|
b. Repeats the first query but specifies the table name explicitly.
|
|
c. Compares the data volume in `foo` and `foo1` tables.
|
|
|
|
## Key Features
|
|
|
|
- **Natural Language to SQL**: Converts human-readable questions into SQL queries.
|
|
- **Database Interaction**: Executes SQL queries and returns results.
|
|
- **Flexible Querying**: Demonstrates querying with and without specifying table names.
|
|
- **Data Comparison**: Shows the ability to analyze and compare data across tables.
|
|
|
|
## How to Run
|
|
|
|
1. Ensure you have a PostgreSQL database set up and accessible.
|
|
2. Set the `LANGCHAINGO_POSTGRESQL` environment variable with your database connection string.
|
|
3. Make sure you have an OpenAI API key set up (the example uses default configuration).
|
|
4. Run the example using `go run postgresql_database_chain.go`.
|
|
|
|
This example is perfect for developers looking to integrate natural language processing with database operations, enabling more intuitive data querying and analysis.
|