Hazrat-Ali9/StudyMate-AI-LangChain-Chatbot
ุญูู
๐ณ StudyMate ๐ฌ AI ๐ฆ LangChain ๐นChatbot ๐ is ๐บ an ๐ AI ๐ต powered ๐ educational ๐ assistant ๐ built ๐ซ with LangChain ๐ and ๐ Large ๐ฐ Language ๐ฝ Models ๐ to โ deliver ๐ personalized ๐ซ context ๐ธ aware ๐ and ๐ interactive ๐ข learning ๐ผ experiences ๐
ุงูุชุนููู ูุงูุชุนูููู ุง ูู ููู ูุนูู
- Streamlit chat interface with conversation history
- Groq chat model configured through a .env file
- PromptTemplate objects with dynamic question and history variables
- Programming, mathematics, and general routes with RunnableBranch
ุงููู ู ุงูู ูุงุณ ูููุฌูู
- ุฎูุงู 24 ุณุงุนุฉ
- +ูง
- ุฎูุงู 7 ุฃูุงู
- ุฌุงุฑู ุฌู ุน ุงูุณุฌู
- ุฎูุงู 15 ููู ูุง
- ุฌุงุฑู ุฌู ุน ุงูุณุฌู
- ุฎูุงู ุดูุฑ
- ุฌุงุฑู ุฌู ุน ุงูุณุฌู
ุชุบููุฑ ุตุงูู ุงููุฌูู ุฎูุงู ูุชุฑุงุช ู ุชุฏุงุฎูุฉุ ูููุณ ู ุฌู ูุนูุง ููู ููุง. ุชุจูู ุงูููุงุณุงุช ุงูู ูููุฏุฉ ูุงุฑุบุฉ. ุงููุฌูู ุชููุณ ุงูุงูุชู ุงู ูุง ุฌูุฏุฉ ุงูุจุฑู ุฌูุงุช.
README.md
ุงูุฃุตูStudyMate AI โ LangChain Chatbot
StudyMate AI is a beginner-friendly chatbot built with Streamlit and the latest LangChain Expression Language APIs. It routes questions to a specialized assistant, creates several useful outputs concurrently, and returns a validated Pydantic object.
Features
- Streamlit chat interface with conversation history
- Groq chat model configured through a
.envfile PromptTemplateobjects with dynamic question and history variables- Programming, mathematics, and general routes with
RunnableBranch - Concurrent answer, summary, and keyword pipelines with
RunnableParallel - Strict, validated structured output using Pydantic
BaseModel - Optional Clear chat button
- Friendly missing-key and API error messages
How the pipeline works
User question
|
v
RunnableBranch
|-- Programming prompt
|-- Mathematics prompt
`-- General prompt (default)
|
v
RunnableParallel
|-- Routed detailed answer
|-- Short answer / summary
`-- Keywords
|
v
Structured-output model
|
v
ChatbotResponse (Pydantic validation)
|
v
Streamlit chat UI
RunnableBranch
chatbot.py creates three prompt pipelines. Transparent keyword
rules inspect the question, and RunnableBranch selects the programming or
mathematics pipeline. If neither condition matches, LangChain runs the general
pipeline as the default.
answer_branch = RunnableBranch(
(_question_is("Programming"), programming_chain),
(_question_is("Mathematics"), mathematics_chain),
general_chain,
)
RunnableParallel
The selected detailed answer, a concise answer, and keywords all use the same
user request. RunnableParallel starts these independent pipelines concurrently
and returns their results in one dictionary.
parallel_outputs = RunnableParallel(
answer=answer_branch,
summary=summary_chain,
keywords=keywords_chain,
question=RunnableLambda(lambda data: data["question"]),
category=RunnableLambda(lambda data: route_question(data["question"])),
)
Pydantic structured output
schemas.py defines ChatbotResponse with five required fields:
answer, summary, confidence, category, and keywords. The Groq model is
bound to this schema with with_structured_output(..., method="json_schema", strict=True). The application receives a Pydantic object and calls
ChatbotResponse.model_validate as an additional validation boundary when a
provider returns a dictionary.
The UI displays every field and includes an expandable JSON view so the exact validated structure is easy to inspect.
Project structure
chatbot-phitron/
|-- app.py # Streamlit interface and chat history
|-- chatbot.py # LangChain runnable pipeline
|-- prompts.py # All PromptTemplate definitions
|-- schemas.py # Pydantic response schema
|-- requirements.txt # Pinned Python dependencies
|-- .env.example # Safe environment variable template
|-- .gitignore # Prevents secrets and local files being committed
`-- README.md
Installation
Python 3.10 or newer is recommended.
Clone the repository and enter its directory:
git clone <your-github-repository-url> cd chatbot-phitronCreate and activate a virtual environment:
python3 -m venv .venv source .venv/bin/activateOn Windows PowerShell, activate it with:
.venv\Scripts\Activate.ps1Install the dependencies:
python -m pip install -r requirements.txtCopy the environment template:
cp .env.example .envCreate a key at Groq Console, then replace
your_groq_api_key_hereinside.env. Never commit the.envfile.
Run the application
streamlit run app.py
Open the local address shown by Streamlit, normally
http://localhost:8501.
Try these example questions:
- Programming:
Explain Python lists with a small example. - Mathematics:
Solve 3x + 6 = 18 step by step. - General:
Why is sleep important for learning?
Security
API keys are read from environment variables by python-dotenv. The real .env
file and the local .venv directory are ignored by Git. Only .env.example
should be committed.
ุงุณุชูุดู ู ุดุงุฑูุน ู ุดุงุจูุฉ ูู ุฑุชุจุทุฉ
ุฌุงุฑู ุงูุชุญู ููโฆ