✨ Today, I implemented and tested a simple Chatbot using Python + ChatGPT + LangChain! 🤖
Building chatbots is one of the most exciting applications of Machine Learning (ML) and Large Language Models (LLMs). These technologies allow us to create systems that can understand, learn, and respond like humans 💬.
🔑 Why is this important?
💡 With Python, LangChain, and ChatGPT, I created a chatbot that remembers conversation history, understands context, and gives intelligent responses.
👉 Sample Code (Simple Chatbot with Memory):
import os
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_core.output_parsers import StrOutputParser
from langchain_core.messages import HumanMessage, AIMessage, SystemMessage
# Set OpenAI API key
os.environ["OPENAI_API_KEY"] = "your_api_key"
# Initialize model
llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)
parser = StrOutputParser()
# Create a prompt with history
prompt = ChatPromptTemplate.from_messages([
SystemMessage(content="You are an intelligent chatbot. Answer the following question."),
MessagesPlaceholder(variable_name="history"),
MessagesPlaceholder(variable_name="question")
])
# Conversation history
history = [
HumanMessage(content="My name is Pasindu"),
AIMessage(content="Nice to meet you, Pasindu! How can I assist today?")
]
# Create chain
chain = prompt | llm | parser
# Example questions
question = "What is AI?"
response = chain.invoke({"history": history, "question": [HumanMessage(content=question)]})
print("User:", question)
print("Bot:", response)
🌍 Applications of Chatbots with AI/LLMs: ✅ Healthcare – guiding patients and answering FAQs 🏥 ✅ Banking – smart virtual assistants for transactions 💳 ✅ Education – AI tutors for students 📚 ✅ HR & Business – automate workflows and employee queries 👩💼
🔥 Benefits: