0% found this document useful (0 votes)
45 views17 pages

Building A Database-Driven Chatbot With LangChain and OpenAI - A Practical Approach (Part 1, Warm-Up) - by Mathews Pious - Aug, 2024 - GoPenAI

Uploaded by

陳賢明
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views17 pages

Building A Database-Driven Chatbot With LangChain and OpenAI - A Practical Approach (Part 1, Warm-Up) - by Mathews Pious - Aug, 2024 - GoPenAI

Uploaded by

陳賢明
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

2024/10/25 晚上9:41 Building a Database-Driven Chatbot with LangChain and OpenAI: A Practical Approach (Part 1, Warm-up) | by Mathews Pi…

Open in app

38
Search

Get unlimited access to the best of Medium for less than $1/week. Become a member

Building a Database-Driven Chatbot with


LangChain and OpenAI: A Practical Approach
(Part 1, Warm-up)
Mathews Pious · Follow
Published in GoPenAI
5 min read · Aug 28, 2024

Listen Share More

Natural Language Processing (NLP) has become a fundamental aspect of modern


software, powering everything from chatbots and virtual assistants to smart search
engines and recommendation systems. As the demand for intelligent and
responsive applications continues to grow, so does the need for robust frameworks
that seamlessly integrate language models into complex workflows.

LangChain is one such powerful framework designed to streamline the


development of applications leveraging large language models (LLMs). It simplifies
the process of chaining multiple NLP tasks, dynamically generating prompts, and
managing memory, making it an ideal solution for building sophisticated,
production-ready applications.

In this blog, we’ll adopt a hands-on, step-by-step approach to unlock the full
potential of LangChain. Throughout this series, we’ll cover:

LangChain Chains: Learn how to create task sequences that interact with LLMs
effectively.

Dynamic Query Example and Table Selection: Understand how to dynamically


choose examples and metadata to retrieve relevant data from databases.

https://blog.gopenai.com/building-a-database-driven-chatbot-with-langchain-and-openai-a-practical-approach-part-1-fd11cba82fca 1/17
2024/10/25 晚上9:41 Building a Database-Driven Chatbot with LangChain and OpenAI: A Practical Approach (Part 1, Warm-up) | by Mathews Pi…

LangChain Memory using DynamoDB: Utilize DynamoDB to store


conversational context, enhancing the fluidity and intelligence of interactions.

Deploying Applications with FastAPI: Discover how to deploy your LangChain-


based applications for real-world scenarios.

By the end of this series, you’ll have built a production-ready chatbot that retrieves
data from an SQLite database. This project will familiarize you with LangChain’s
core features and demonstrate how to apply them in your NLP solutions.

Whether you’re new to coding or beginning with NLP, this guide will equip you with
the practical skills needed to create powerful, real-world applications using
LangChain.

Prerequisites
Basic knowledge of Python

Familiarity with LangChain fundamentals

OpenAI API key

LangChain API key (LangSmith can be used to deepen your understanding of


LangChain)

Architecture diagram

https://blog.gopenai.com/building-a-database-driven-chatbot-with-langchain-and-openai-a-practical-approach-part-1-fd11cba82fca 2/17
2024/10/25 晚上9:41 Building a Database-Driven Chatbot with LangChain and OpenAI: A Practical Approach (Part 1, Warm-up) | by Mathews Pi…

Test Data
We’ll be working with a simulated dataset from an imaginary airline service,
containing information about flights, baggage, and passengers. This data is stored in
an SQLite database, and our objective is to develop a chatbot that aids airline ground
staff in tracking the status of passenger baggage.

The following Python script will generate the test database, baggage_tracking.db .

You don't need to worry about the logic behind the code, as it was generated by AI.
You can find the script at the link below:

chatbot_sql/test_data/baggage_details_db.py at main ·
mathewspious/chatbot_sql
Contribute to mathewspious/chatbot_sql development by creating
an account on GitHub.
github.com

Project Setup
We’ll set up the project in stages, starting with the essentials. First, ensure you have
Python installed, along with API keys for both OpenAI and LangChain.
https://blog.gopenai.com/building-a-database-driven-chatbot-with-langchain-and-openai-a-practical-approach-part-1-fd11cba82fca 3/17
2024/10/25 晚上9:41 Building a Database-Driven Chatbot with LangChain and OpenAI: A Practical Approach (Part 1, Warm-up) | by Mathews Pi…

Python Virtual Environment


It’s recommended to use a virtual environment in Python to manage dependencies.
A virtual environment ensures that all necessary packages are installed in an
isolated workspace, avoiding conflicts with other projects.

# Creating virtual environment


chatbot_sql % python3.12 -m venv venv
# Activating virtula environment
chatbot_sql % source venv/bin/activate
# Validating python env
(venv) chatbot_sql % python3 -V
Python 3.12.3

First Notebook: Creating a Simple LangChain Application


In this first notebook, we’ll develop a basic LangChain application that generates an
SQL query based on user input. To keep things simple, we’ll utilize a built-in chain
with minimal parameters, avoiding unnecessary complexities.

Handling API Keys


Since API usage incurs costs, we’ll securely load the API keys each time the
application runs, storing them in memory for efficient use.

#Reading OpenAI Key


OPENAI_API_KEY=input("Enter OpenAI API Key")
#Reading Langchain API Key
LANGCHAIN_API_KEY=input("Enter Langchain API Key")

Enabling LangChain Trace


In the code below, we enable LangChain tracing and configure the OpenAI model.
LangChain tracing helps visualize the internal processes within the framework, and
the results can be analyzed in the LangSmith dashboard for better insights.

#Enabiling Langchain Tracing


LANGCHAIN_TRACING_V2= "true"
#OpenAI Model
OPENAI_MODEL="gpt-4o-mini"

https://blog.gopenai.com/building-a-database-driven-chatbot-with-langchain-and-openai-a-practical-approach-part-1-fd11cba82fca 4/17
2024/10/25 晚上9:41 Building a Database-Driven Chatbot with LangChain and OpenAI: A Practical Approach (Part 1, Warm-up) | by Mathews Pi…

Initializing the Language Model


We’ll initialize the language model with the following configuration:

model: Specifies the OpenAI model to use.

openai_api_key: Used for authenticating with OpenAI.

temperature: Controls response randomness, with 0.5 balancing creativity and


consistency.

# initializing llm
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model=OPENAI_MODEL,openai_api_key=OPENAI_API_KEY,temperature=0
llm

Initializing the Database Object


We’ll connect to the SQLite database using the SQLDatabase class from
langchain_community.utilities.sql_database .

#initializing Database object


from langchain_community.utilities.sql_database import SQLDatabase
db = SQLDatabase.from_uri("sqlite:///baggage_tracking.db")
db

Invoking the Language Model (LLM)


Next, we’ll create a query-generation chain that uses the LLM and connects it to the
database. This chain translates natural language questions into SQL queries.

Key components:

create_sql_query_chain(llm, db): Combines the LLM with the SQL database.

generate_query.invoke({…}): Generates an SQL query based on the user’s input.

#creating Chain
from langchain.chains import create_sql_query_chain

https://blog.gopenai.com/building-a-database-driven-chatbot-with-langchain-and-openai-a-practical-approach-part-1-fd11cba82fca 5/17
2024/10/25 晚上9:41 Building a Database-Driven Chatbot with LangChain and OpenAI: A Practical Approach (Part 1, Warm-up) | by Mathews Pi…

generate_query = create_sql_query_chain(llm, db)


#invoking generate query chain to generate the SQL query
query = generate_query.invoke({"question":"How many passengers are there?","dia
query

'SQLQuery: SELECT COUNT("passenger_id") AS "total_passengers" FROM "passenger_d

You’ll notice that the query contains an extra string, SQLQuery: , which we need to
remove before execution. We can fix this with a custom parser.

Custom SQL Output Parser


We’ll create a simple parser to remove the extra string. In LangChain, the
BaseGenerationOutputParser class is used to process model outputs. We’ll implement
a custom parser to extract the valid SQL query.

from langchain_core.output_parsers import BaseGenerationOutputParser


from typing import List
from langchain_core.exceptions import OutputParserException
from langchain_core.outputs import Generation
class SQlExtractParser(BaseGenerationOutputParser[str]):
"""
A parser for SQL output.
"""

def parse_result(self, result: List[Generation], *, partial: bool = False)


"""
Parse the SQL query input and return the extracted SQL query.

Args:
sql_query_input (str): The input SQL query string.
partial (bool, optional): Whether the input is a partial query. Def

Returns:
str: The extracted SQL query.
"""
if not result:
raise OutputParserException("Input SQL query is empty")

print(f"Incoming query {result[0]}")

if 'SQLQuery:' in result[0].text:
sql_query = result[0].text.split('SQLQuery:', 1)[1].strip()
print(f"Extracted SQL query: {sql_query}")
else:
sql_query = result[0].text

https://blog.gopenai.com/building-a-database-driven-chatbot-with-langchain-and-openai-a-practical-approach-part-1-fd11cba82fca 6/17
2024/10/25 晚上9:41 Building a Database-Driven Chatbot with LangChain and OpenAI: A Practical Approach (Part 1, Warm-up) | by Mathews Pi…

return sql_query

Running the SQL Query


We’ll now chain the output of the query generation to the SQLExtractParser .

#creating Chain
from langchain.chains import create_sql_query_chain
generate_query = create_sql_query_chain(llm, db)
# creating basic chain, passing the output of generate_query to SQLExtractParse
chain = generate_query | SQlExtractParser()
#invoking generate query chain to generate the SQL query and part the query str
query = chain.invoke({"question":"How many passengers are there?","dialect":"sq
query

### Output###
Incoming query text='SQLQuery: SELECT COUNT("passenger_id") AS "passenger_count
Extracted SQL query: SELECT COUNT("passenger_id") AS "passenger_count" FROM "pa
'SELECT COUNT("passenger_id") AS "passenger_count" FROM "passenger_details";'

Executing the SQL Query


Once the query is generated, we’ll execute it against the SQLite database using
LangChain’s QuerySQLDataBaseTool .

from langchain_community.tools.sql_database.tool import QuerySQLDataBaseTool


execute_query = QuerySQLDataBaseTool(db=db)
query_result = execute_query.invoke(query)
query_result

##output
'[(20,)]'

Generating a Natural Language Response


We’ll create a function to convert the user question, SQL query, and result into a
natural language response. We’ll use LangChain’s PromptTemplate and

https://blog.gopenai.com/building-a-database-driven-chatbot-with-langchain-and-openai-a-practical-approach-part-1-fd11cba82fca 7/17
2024/10/25 晚上9:41 Building a Database-Driven Chatbot with LangChain and OpenAI: A Practical Approach (Part 1, Warm-up) | by Mathews Pi…

StrOutputParser .

from langchain_core.prompts import PromptTemplate


from langchain_core.output_parsers import StrOutputParser
def rephrase(user_query:str,query:str, query_result:str):
answer_prompt = PromptTemplate.from_template(
"""Given the following user question, corresponding SQL query, and SQL res

Question: {question}
SQL Query: {query}
SQL Result: {result}
Answer: """
)
rephrase_answer = answer_prompt | llm | StrOutputParser()
response=rephrase_answer.invoke({"question":user_query,"query":query,"resul
print(f"Rephrased Response : %s", response)
return response

Now, we can invoke the rephrase() function to generate a natural language


response.

rephrase("How many passengers are there?", query, query_result)

## output
'There are 20 passengers.'

This forms the basic version of our project. In the next section, we’ll enhance the
solution by adding chaining, memory management, dynamic selection of few-shot
examples, dynamic table schema selection, and more. Stay tuned!

The complete code is available in the below git repo.

chatbot_sql/langchain/langchain_1.ipynb at main ·
mathewspious/chatbot_sql

https://blog.gopenai.com/building-a-database-driven-chatbot-with-langchain-and-openai-a-practical-approach-part-1-fd11cba82fca 8/17
2024/10/25 晚上9:41 Building a Database-Driven Chatbot with LangChain and OpenAI: A Practical Approach (Part 1, Warm-up) | by Mathews Pi…

Contribute to mathewspious/chatbot_sql development by creating


an account on GitHub.
github.com

Link to part 2

https://medium.com/@mathewspious/building-a-database-driven-chatbot-with-
langchain-and-openai-a-practical-approach-part-2-6a279622f0fc

Reference

langchain.chains.sql_database.query.create_sql_query_chain - 🦜🔗 LangChain 0.2.13


Edit description
api.python.langchain.com

Langchain OpenAI Chatbots Nlp To Sql Python

Follow

Written by Mathews Pious


3 Followers · Writer for GoPenAI

Driven by the ambition to evolve into a distinguished Technology Architect, focusing on cloud infrastructure,
data engineering and AI.

More from Mathews Pious and GoPenAI

https://blog.gopenai.com/building-a-database-driven-chatbot-with-langchain-and-openai-a-practical-approach-part-1-fd11cba82fca 9/17
2024/10/25 晚上9:41 Building a Database-Driven Chatbot with LangChain and OpenAI: A Practical Approach (Part 1, Warm-up) | by Mathews Pi…

Mathews Pious in GoPenAI

Building a Database-Driven Chatbot with LangChain and OpenAI: A


Practical Approach (Part 3…
Welcome to the third chapter of our series. In the previous parts, we built a basic SQL-driven
chatbot. Today, we will enhance it by…

Sep 3

Abhay Dodiya in GoPenAI

Anomaly Detection in Time Series using Autoencoder.

https://blog.gopenai.com/building-a-database-driven-chatbot-with-langchain-and-openai-a-practical-approach-part-1-fd11cba82fca 10/17
2024/10/25 晚上9:41 Building a Database-Driven Chatbot with LangChain and OpenAI: A Practical Approach (Part 1, Warm-up) | by Mathews Pi…

Anomaly Detection in Time Series with the help of Autoencoder will help us to decode anomaly.
We are going to look at real life example of…

Oct 3 85

Sofien Kaabar, CFA in GoPenAI

Time Series Forecasting With Bayesian Regression


From Theory to Application: Bayesian Ridge Regression in Action

Oct 3 9 2

Mathews Pious in GoPenAI

https://blog.gopenai.com/building-a-database-driven-chatbot-with-langchain-and-openai-a-practical-approach-part-1-fd11cba82fca 11/17
2024/10/25 晚上9:41 Building a Database-Driven Chatbot with LangChain and OpenAI: A Practical Approach (Part 1, Warm-up) | by Mathews Pi…

Building a Database-Driven Chatbot with LangChain and OpenAI: A


Practical Approach (Part 4…
Welcome to the fourth part of our series on building a SQL-based chatbot with LangChain and
OpenAI. In previous sections, we developed a…

Sep 6 50

See all from Mathews Pious

See all from GoPenAI

Recommended from Medium

https://blog.gopenai.com/building-a-database-driven-chatbot-with-langchain-and-openai-a-practical-approach-part-1-fd11cba82fca 12/17
2024/10/25 晚上9:41 Building a Database-Driven Chatbot with LangChain and OpenAI: A Practical Approach (Part 1, Warm-up) | by Mathews Pi…

Deepak Chaudhari

OpenAI Launches Realtime API


Transforming Conversational AI with Low-Latency Voice Interaction

Oct 2 139 3

https://blog.gopenai.com/building-a-database-driven-chatbot-with-langchain-and-openai-a-practical-approach-part-1-fd11cba82fca 13/17
2024/10/25 晚上9:41 Building a Database-Driven Chatbot with LangChain and OpenAI: A Practical Approach (Part 1, Warm-up) | by Mathews Pi…

Vishal Rajput in AIGuys

The Prompt Report: Prompt Engineering Techniques


Prompting Techniques Survey

Oct 14 890 10

Lists

Coding & Development


11 stories · 863 saves

Predictive Modeling w/ Python


20 stories · 1614 saves

Practical Guides to Machine Learning


10 stories · 1968 saves

Natural Language Processing


1772 stories · 1374 saves

https://blog.gopenai.com/building-a-database-driven-chatbot-with-langchain-and-openai-a-practical-approach-part-1-fd11cba82fca 14/17
2024/10/25 晚上9:41 Building a Database-Driven Chatbot with LangChain and OpenAI: A Practical Approach (Part 1, Warm-up) | by Mathews Pi…

Tom Martin in Artificial Intelligence in Plain English

From RAG to TAG: Exploring the Power of Table-Augmented Generation


(TAG): A Leap Beyond…
An In-Depth Look at TAG and Its Advantages Over RAG, with some theoretical examples using
LOTUS

Oct 15 62

Rachana Aluri in Waii

Text-to-SQL’s Power Players: Comparing Claude 3.5 Sonnet, GPT-4o,


Mistral Large 2, Llama 3.1
https://blog.gopenai.com/building-a-database-driven-chatbot-with-langchain-and-openai-a-practical-approach-part-1-fd11cba82fca 15/17
2024/10/25 晚上9:41 Building a Database-Driven Chatbot with LangChain and OpenAI: A Practical Approach (Part 1, Warm-up) | by Mathews Pi…

The ability to query databases using natural language continues to revolutionize data
interaction. Check out our expanded evaluation.

Sep 5 201

Devvrat Rana

Understanding LangChain Agents: A Beginner’s Guide to How LangChain


Agents Work
Introduction:

Jun 2 66

https://blog.gopenai.com/building-a-database-driven-chatbot-with-langchain-and-openai-a-practical-approach-part-1-fd11cba82fca 16/17
2024/10/25 晚上9:41 Building a Database-Driven Chatbot with LangChain and OpenAI: A Practical Approach (Part 1, Warm-up) | by Mathews Pi…

Hamna Arif

Advanced Text-to-SQL with llama-index: Handling Multiple Tables with


Natural Language Queries
Welcome to the third and final article in my Text-to-SQL series. In this part, I’ll focus on how you
can enable natural language…

Sep 20

See more recommendations

https://blog.gopenai.com/building-a-database-driven-chatbot-with-langchain-and-openai-a-practical-approach-part-1-fd11cba82fca 17/17

You might also like