02. SQL

This tutorial covers how to create SQL queries using a chain created using create_sql_query_chain, execute them, and retrieve answers. We will also look at the differences in how it works compared to SQL Agent.

Copy

# Configuration file for managing API KEY as environment variable
from dotenv import load_dotenv

# Load API KEY information
load_dotenv()

Copy

True

Copy

# Set up LangSmith tracking. https://smith.langchain.com
# !pip install langchain-teddynote
from langchain_teddynote import logging

# Enter a project name.
logging.langsmith("SQL")

Copy

Start tracking LangSmith.
[Project Name]
SQL

Retrieve SQL Database information.

Copy

Copy

Create an LLM object and pass LLM and DB as parameters to create a chain.

This tutorial will proceed with gpt-3.5-turbo as changing the model here may not work smoothly.

Copy

(Optional) You can specify the Prompt directly in the following way.

When writing directly, you can add a descriptive column description along with table_info.

Copy

Running chain generates queries based on the DB.

Copy

Copy

Next, it's time to check if the query we created works correctly.

Copy

Copy

Copy

Copy

Copy

Copy

답변을 LLM 으로 증강-생성

Using the chain created in the previous step, the answer is output in a short answer format. You can adjust this to a chain in LCEL grammar to get a more natural answer.

Copy

Copy

Copy

Agent

You can use Agent to create SQL queries and output the execution results as answers.

Copy

Copy

Copy

Copy

Last updated