Building a simple Console ChatGPT Bot with Langchain
In this blog post, we’ll walk through creating a simple console-based ChatGPT chat bot using Langchain. We’ll cover setting up your development environment and guide you through the code step-by-step with running samples.
Disclaimer: This blog post has been enhanced with the assistance of AI. The code, text structure, and technical explanations are entirely my own, with AI helping to expand and refine the content.
Responsible use of AI for Productivity
I’m a strong believer in responsibly embracing AI-assisted technology to boost productivity. As a husband and a parent, my life is a 24/7 commitment that never stops. I’m also passionate about staying active, enjoying activities like hiking, running, boxing, skiing, motorcycle driving, learning and trying out new technologies, sharing my knowledge trough blog posts and tutorials, tackling DIY projects around the house, and of course, playing hide and seek with my daughter!
These pursuits keep my schedule packed and without the assistance of AI to enhance and enrich the quality of my blog posts, it would be challenging to share my knowledge as frequently as I’d like.
I value honesty and transparency, which is why I want to clearly explain how and why I use AI in creating the content for this blog post.
Now, let’s roll up our sleeves and dive into the content of this blog post! Let’s go !
Setting Up PyCharm IDE
Before diving into the code, let’s set up your development environment using PyCharm, one of the most popular Python IDEs.
Download and Install PyCharm:
- Go to the PyCharm download page and choose the Community edition (free). Optionally you can use the Professional edition (paid) but the paid version is not required for this tutorial.
- Follow the installation instructions for your operating system.
2. Create a New Project:
- Open PyCharm and click on “New Project”.
- Choose a location for your project and select “Pure Python” as the project type.
- Click “Create” to set up your new project.
3. Configure the Python Interpreter:
- Go to
File
->Settings
(orPreferences
on macOS) ->Project: <your_project_name>
->Python Interpreter
. - Click the gear icon and select “Add”.
- Choose “Virtual Environment” and click “OK” to set up a new virtual environment for your project.
Creating a simple script to test the environment
Create simple_chatgbt_bot.py python file by right click on the root directory
-> New ->
Python File
Name the file “simple_chatgpt_chatbot”. The file should have .py at the end of the filename.
Add the following line in the script to check if the environment is set up correctly:
print("Hello!")
Running the app
- Run the Script from the IDE:
- In PyCharm, right-click the Python file containing your code and select “Run ‘simple_chatgpt_chatbot.py”.
- Run the Script from Terminal:
- Open Terminal and navigate to the “simple_chatgpt_chatbot.py” file. Run the following command "
python simple_chatgpt_chatbot.py"
You should see an output “Hello!” in the Terminal window.
Note: Running the app via Terminal requires to have python installed on your machine. Installing python on your local machine is outside of the scope of this tutorial and there are plenty resources available online.
Obtaining an OpenAI API Key
To use the OpenAI API, you’ll need an API key. Here’s how you can obtain one:
1. Create an OpenAI Account:
- Visit OpenAI’s platform and sign up for an account if you don’t already have one.
2. Generate an API Key:
- After logging in, navigate to the API Keys page.
- Click on “Create new key”.
- Give your key a name and click “Create Key”.
- Copy the API key and store it securely, as you’ll need it for your project.
3. Add the API Key to Your .env
File:
- Create a file named
.env
in your project directory. - Add the following line to the
.env
file:
OPENAI_API_KEY=your_api_key_here
Note: Files with “.” prefix are system fules and are hidden by default. You need to enable viewing hidden files to be able to edit it
Step-by-Step Creation of the Console Chat Bot
Now, let’s break down the code for creating a simple console chat bot.
1. Installing pip
On Windows:
- Download and install Python from the official Python website.
pip
comes bundled with Python installations 3.4 and later. Ensure you check the box to add Python to your PATH during installation.
On macOS:
- Python 2.x is pre-installed on macOS, but you should install Python 3.x which includes
pip
. Install it using Homebrew by running:
brew install python
- Alternatively, download and install Python from the official Python website.
2. Install Required Packages:
- Open the terminal in PyCharm and run the following commands to install the necessary packages:
pip install python-dotenv langchain-core langchain-openai
3. Import Necessary Libraries
First, import the required libraries and set up environment variables.
import os
from dotenv import load_dotenv, find_dotenv
load_dotenv(find_dotenv(), override=True
This code loads environment variables from a .env
file. You should have a .env
file in your project directory with your OpenAI API key. To check if the key exists use the following command to print out the OpenAI API key
print(os.environ.get("OPENAI_API_KEY")) #print the OPENAI_API_KEY
If by any chance the OpenAI API key is not loaded add the following line to set the api key as an evironment variable.
os.environ["OPENAI_API_KEY"] = constants.APIKEY
Setting the variable from the code is not recommended for production. The api key is a secret that should exists in the environment variables.
4. Set Up Langchain Components
Import Langchain components and configure the language model.
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_openai import ChatOpenAI
Now we can set up the ChatOpenAI
model with the desired parameters.
llm = ChatOpenAI(model_name='gpt-3.5-turbo', temperature=1)
5. Define the Prompt Template
Create a prompt template for the chat bot.
prompt = ChatPromptTemplate.from_template("You are a chatbot having a conversation with a human about the following {content}")
This template defines how the chat bot will structure its responses.
6. Set Up the Chain
Combine the prompt template, language model, and output parser.
chain = prompt | llm | StrOutputParser()
This chain processes the input through the prompt, the language model, and the output parser.
7. Implement a Chat Loop
Add a loop to interact with the chat bot through the console.
while True:
content = input('Your prompt ')
if content.lower() in ['quit', 'exit', 'bye']:
print('Goodbye!')
break
response = chain.invoke({"content": content})
print(response)
print('-' * 50)
This loop continuously takes user input, processes it with the chat bot, and prints the response. The loop exits if the user types “quit”, “exit”, or “bye”.
8. Interact with the Chat Bot
Enter different prompts and observe how the chat bot responds. Type “quit”, “exit”, or “bye” to end the session.
For example you might enter: “What is the capital of Germany?”
You should get an answer similar to this one: “Berlin is the capital of Germany.”
9. Modify the ChatPromptTemplate
You can modify the the template to return the answers in JSON format
prompt = ChatPromptTemplate.from_template("You are a chatbot having a conversation with a human about the following {content}. Return the answers in JSON format")
If you save the changes, run the script again and ask the same question “What is the capital of Germany” you should get the answer in JSON format:
“{
“question”: “What is the capital of Germany?”,
“answer”: “Berlin”
}”
You can also modify the template to return all answers in Spanish.
prompt = ChatPromptTemplate.from_template("You are a chatbot having a conversation with a human about the following {content. Return the answers in Spanish")
If you save the changes, run the script again and ask the same question “What is the capital of Germany” you should get the answer in Spanish: “La capital de Alemania es Berlín.”
Interesting right! Use your imagination and experiment with different templates and see which results you will get.
Conclusion
In this tutorial, we’ve built a simple console-based chat bot using Langchain. We set up PyCharm and walked through the code step-by-step. With this foundation, you can further enhance the chat bot or integrate it into different applications.
You can find the full code by following this link
Stay tuned for the next blog post, where we’ll extend on the app that we have covered in this tutorial add we will add the ability to save and restore the chat history.
If you think this article was helpful to you, be sure to give it 👏👏👏.
Also, you can follow me on Medium