Building A Database-Driven Chatbot With LangChain and OpenAI - A Practical Approach (Part 1, Warm-Up) - by Mathews Pious - Aug, 2024 - GoPenAI
Building A Database-Driven Chatbot With LangChain and OpenAI - A Practical Approach (Part 1, Warm-Up) - by Mathews Pious - Aug, 2024 - GoPenAI
Open in app
38
Search
Get unlimited access to the best of Medium for less than $1/week. Become a member
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.
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…
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
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…
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 llm
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model=OPENAI_MODEL,openai_api_key=OPENAI_API_KEY,temperature=0
llm
Key components:
#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…
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.
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")
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
#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";'
##output
'[(20,)]'
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 .
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
## 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!
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…
Link to part 2
https://medium.com/@mathewspious/building-a-database-driven-chatbot-with-
langchain-and-openai-a-practical-approach-part-2-6a279622f0fc
Reference
Follow
Driven by the ambition to evolve into a distinguished Technology Architect, focusing on cloud infrastructure,
data engineering and AI.
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…
Sep 3
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
Oct 3 9 2
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…
Sep 6 50
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
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…
Oct 14 890 10
Lists
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…
Oct 15 62
The ability to query databases using natural language continues to revolutionize data
interaction. Check out our expanded evaluation.
Sep 5 201
Devvrat Rana
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
Sep 20
https://blog.gopenai.com/building-a-database-driven-chatbot-with-langchain-and-openai-a-practical-approach-part-1-fd11cba82fca 17/17