Skip to content

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

LangChain Demo with Streamlit and Groq

This project demonstrates the use of LangChain with Streamlit for building a simple chat application using Groq's LLM API.

Features

  • 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

Installation

  1. Clone the repository:

    git clone https://github.com/Om99roy/Mini-Chatbot.git
    cd Mini-Chatbot
  2. 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
  3. Install dependencies:

    pip install -r requirements.txt
  4. Set up your API key:

    • Create a my_secret.py file (not committed to Git) with:
      groq_api_key = 'your_groq_api_key_here'
    • Or set environment variable: export GROQ_API_KEY='your_key_here'

Usage

Run the simple demo:

streamlit run main.py

Run the advanced demo with memory:

streamlit run sample.py

Open your browser to the provided URL (usually http://localhost:8501) and start chatting!

Code Explanation

main.py

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 response

sample.py

This 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 result

my_secret.py

Contains sensitive API keys. Never commit this file to version control!

# API keys - keep these secret!
groq_api_key = 'your_groq_api_key_here'

requirements.txt

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

Project Workflow

  1. User Interaction: User opens the Streamlit app and enters a question in the text input field.

  2. Input Processing: The app captures the user input and passes it to the LangChain chain.

  3. Prompt Engineering: For sample.py, the input is formatted using the prompt template to create a more structured query.

  4. Memory Integration: Conversation history is maintained using memory buffers to provide context for follow-up questions.

  5. LLM Processing: The formatted prompt is sent to Groq's API where the language model generates a response.

  6. Response Display: The generated response is displayed back to the user in the Streamlit interface.

  7. Memory Update: The conversation history is updated with the new interaction for future context.

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Test thoroughly
  5. Submit a pull request

License

This project is open source. Please check the license file for details.

Disclaimer

This is a demo project. In production, never hardcode API keys in your code. Use environment variables or secure key management systems.

About

Built a simple end - to - end LLM chatbot using Langchain

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages