This project demonstrates the use of LangChain with Streamlit for building a simple chat application using Groq's LLM API.
- Interactive chat interface built with Streamlit
- Conversation memory to maintain chat history
- Custom prompt templates for better responses
- Integration with Groq's fast LLM models
-
Clone the repository:
git clone https://github.com/Om99roy/Mini-Chatbot.git cd Mini-Chatbot -
Create and activate a virtual environment (optional but recommended):
conda env create -f environment.yml # if using conda # or python -m venv myenv source myenv/bin/activate # On Windows: myenv\Scripts\activate
-
Install dependencies:
pip install -r requirements.txt
-
Set up your API key:
- Create a
my_secret.pyfile (not committed to Git) with:groq_api_key = 'your_groq_api_key_here'
- Or set environment variable:
export GROQ_API_KEY='your_key_here'
- Create a
Run the simple demo:
streamlit run main.pyRun the advanced demo with memory:
streamlit run sample.pyOpen your browser to the provided URL (usually http://localhost:8501) and start chatting!
This is a simple demo of LangChain integration with Groq.
# Integrate the code with gemini api key
import os
import streamlit as st
from my_sectret import groq_api_key # Import API key from secret file
from langchain_groq import ChatGroq # Import Groq chat model
os.environ['GROQ_API_KEY'] = groq_api_key # Set environment variable
st.title('Sample Demo of Langchain') # Streamlit app title
input_text = st.text_input('Ask Anything...') # User input field
llm = ChatGroq( # Initialize Groq LLM
model="llama-3.1-8b-instant", # Model name
api_key=groq_api_key, # API key
temperature=0.8, # Creativity parameter
)
if input_text: # If user provided input
r = llm.invoke(input_text) # Invoke LLM with input
st.write(r.content) # Display responseThis is an advanced demo with prompt templates, chains, and memory.
import os
import streamlit as st
from my_sectret import groq_api_key # Import API key
from langchain_groq import ChatGroq # Groq chat model
from langchain_core.prompts import PromptTemplate # For custom prompts
from langchain_classic.chains import LLMChain # For chaining LLM operations
from langchain_classic.memory import ConversationBufferMemory # For memory
os.environ['GROQ_API_KEY'] = groq_api_key # Set API key
st.title('Sample Demo of Langchain') # App title
input_text = st.text_input('Ask Anything...') # Input field
# Prompt Template - defines how to format user input
prompt = PromptTemplate(
input_variables=["message"], # Variable to replace in template
template="You are my helpful assistant. {message}" # Template with placeholder
)
# Memory to store conversation history
chat_memory = ConversationBufferMemory(input_key="message", memory_key="chat_history")
llm = ChatGroq( # Initialize LLM
model="qwen/qwen3-32b", # Different model
temperature=0.8, # Temperature
verbose=True, # Enable verbose output
)
# Create chain combining prompt, LLM, and memory
chain = LLMChain(
prompt=prompt, # Use the prompt template
llm=llm, # Use the LLM
output_key="message", # Key for output
memory=chat_memory, # Add memory
verbose=True # Verbose mode
)
if input_text: # If input provided
r = chain.run({"message": input_text}) # Run the chain
st.write(r) # Display resultContains sensitive API keys. Never commit this file to version control!
# API keys - keep these secret!
groq_api_key = 'your_groq_api_key_here'Lists all Python dependencies needed for the project.
langchain
gemini # Note: This might be a typo, probably meant langchain-google-genai or similar
streamlit
groq # Note: Should be langchain-groq
-
User Interaction: User opens the Streamlit app and enters a question in the text input field.
-
Input Processing: The app captures the user input and passes it to the LangChain chain.
-
Prompt Engineering: For
sample.py, the input is formatted using the prompt template to create a more structured query. -
Memory Integration: Conversation history is maintained using memory buffers to provide context for follow-up questions.
-
LLM Processing: The formatted prompt is sent to Groq's API where the language model generates a response.
-
Response Display: The generated response is displayed back to the user in the Streamlit interface.
-
Memory Update: The conversation history is updated with the new interaction for future context.
- Fork the repository
- Create a feature branch
- Make your changes
- Test thoroughly
- Submit a pull request
This project is open source. Please check the license file for details.
This is a demo project. In production, never hardcode API keys in your code. Use environment variables or secure key management systems.