Building an End-to-End Q&A Chatbot with Langchain and OpenAI/Ollama

Sayan Mandal
3 min readJun 12, 2024

--

This article outlines the development of a production-ready, interactive Q&A chatbot utilizing Langchain, a powerful framework for constructing modular Natural Language Processing (NLP) workflows. The project leverages the capabilities of OpenAI’s Large Language Model (LLM) and explores the potential integration of open-source alternatives like Ollama.

Project Scope:

Project Title: End-to-End Q&A Chatbot Utilizing Langchain, OpenAI, and Open-Source LLMs.

Objective: Construct a user-centric chatbot capable of responding to various questions through a combination of paid and open-source LLMs.

Technical Stack:

1. Langchain: A modular NLP development framework.

2. Paid LLM: OpenAI (demonstrated with gpt-3.5-turbo model).

3. Open-Source LLM Option: Ollama (seamless integration process).

4. Streamlit: Enables the creation of interactive web UIs.

Leveraging Essential Libraries:

1. langchain_openai: Provides seamless interaction with OpenAI’s API within Langchain.

2. langchain_core: Offers core functionalities for constructing NLP pipelines within Langchain.

3. python-dotenv: Enables secure management of environment variables (API keys).

4. Streamlit: Facilitates the development of user-friendly web applications for deploying the chatbot.

5. Langchain_community (Optional): Extends Langchain’s functionality with additional community-developed features.

langchain_openai 
langchain_core
python-dotenv
streamlit
langchain_community

## These all added in the requirements.txt file.

Step-by-Step Implementation:

1. Environment Preparation:

  • Establish a virtual environment (venv) to isolate project dependencies.
  • Install required libraries using pip install.
  • Utilize python-dotenv to manage API keys securely (stored in a separate .env file).

2. Langchain Configuration:

  • Set environment variables for OPENAI_API_KEY (obtained from your OpenAI account) and optionally LANGCHAIN_PROJECT for managing Langchain projects.

3. Enabling Langchain Tracing:

  • Activate Langchain tracing by setting LANGCHAIN_TRACING_V2=true for comprehensive monitoring and debugging capabilities.

4. Chatbot Development:

  • Define a ChatPromptTemplate in Langchain to establish the conversation flow:

A. System message introducing the chatbot’s capabilities.

  • Utilize ChatOpenAI from langchain_openai to specify the chosen LLM model (gpt-3.5-turbo for OpenAI).
  • Employ StrOutputParser from langchain_core.output_parsers to handle the LLM's response as a string.

5. Streamlit Integration:

  • Import streamlit for building the chatbot's interactive user interface.
  • Create a Streamlit app with a title and a text input field for users to enter their questions.

6. LLM Call and Output Processing:

  • Instantiate the ChatOpenAI object specifying the chosen LLM model.
  • Create an instance of StrOutputParser for parsing the LLM's response.
  • Construct a Langchain chain by combining the ChatPromptTemplate, ChatOpenAI, and StrOutputParser elements in a modular fashion.

7. Interactive Q&A Functionality:

  • Implement a conditional statement using user input (if input_text).
  • If the user enters a question, invoke the Langchain chain with the question as input (chain.invoke({'question': input_text})).
  • Display the LLM’s response on the Streamlit app using st.write.

In the text above, you can see that I have used open-source language models like LLama2 with Ollama framework.

LangSmith helps us track and assess our language model applications and intelligent agents to assist in transitioning from prototype to production. (token, cost, call and where failure assesses everything)

--

--