Building a Powerful AI Chatbot Assistant with DeepSeek R1

Introduction

In this article, we will walk through building an AI-powered coding assistant using Streamlit, LangChain, and Groq API. This assistant, called SQLChampion AI: DeepSeek, helps with code debugging, documentation, and generating solutions.

The goal is to provide a clean, efficient, and reusable implementation that anyone can integrate into their projects.


Prerequisites

Before running the code, ensure you have the following installed:

  • Python 3.8+
  • Streamlit
  • LangChain
  • Groq API Key

To install the required libraries, run:

pip install streamlit langchain_core groq

Full Code Implementation

Below is the clean and structured code for the SQLChampion AI assistant:


import streamlit as st
from groq import Groq
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.prompts.chat import (
    SystemMessagePromptTemplate,
    HumanMessagePromptTemplate,
    AIMessagePromptTemplate
)

# Custom Styling
st.markdown("""
<style>
    .main { background-color: #1a1a1a; color: #ffffff; }
    .sidebar .sidebar-content { background-color: #2d2d2d; }
    .stTextInput textarea { color: #ffffff !important; }
</style>
""", unsafe_allow_html=True)

# App Title
st.title("⚡ SQLChampion AI: DeepSeek")
st.caption("🚀 Your AI Pair Programmer with Debugging Superpowers")

# Model Selection
SELECTED_MODEL = "deepseek-r1-distill-llama-70b"

GROQ_API_KEY = "your_groq_api_key_here"  # Replace with your actual key



# Secure API Key Handling (Set this as an environment variable)
#ROQ_API_KEY = st.secrets["GROQ_API_KEY"] if "GROQ_API_KEY" in st.secrets else None
if not GROQ_API_KEY:
    st.error("🚨 API Key is missing! Please set GROQ_API_KEY in your environment.")
    st.stop()

# Initialize Client
client = Groq(api_key=GROQ_API_KEY)

# Define System Prompt
SYSTEM_PROMPT = SystemMessagePromptTemplate.from_template(
    "You are an expert AI coding assistant. Provide concise, correct solutions."
)

# Ensure Session State Exists
if "message_log" not in st.session_state:
    st.session_state.message_log = [{"role": "assistant", "content": "Hi! I'm DeepSeek. How can I help?"}]


# Build Prompt Chain
def build_prompt_chain():
    prompt_sequence = [SYSTEM_PROMPT]
    for msg in st.session_state.message_log:
        if msg["role"] == "user":
            prompt_sequence.append(HumanMessagePromptTemplate.from_template(msg["content"]))
        elif msg["role"] == "assistant":
            prompt_sequence.append(AIMessagePromptTemplate.from_template(msg["content"]))
    return ChatPromptTemplate.from_messages(prompt_sequence)


# Generate AI Response
def generate_ai_response():
    user_prompt = "\n".join(msg["content"] for msg in st.session_state.message_log if msg["role"] == "user")

    response = client.chat.completions.create(
        model=SELECTED_MODEL,
        messages=[{"role": "user", "content": user_prompt}],
        temperature=0.3,
        max_tokens=4096,
        top_p=0.95
    )

    return response.choices[0].message.content


# Chat Container
with st.container():
    for message in st.session_state.message_log:
        with st.chat_message(message["role"]):
            st.markdown(message["content"])

# Chat Input Processing
user_query = st.chat_input("Type your coding question here...")

if user_query and user_query.strip():
    st.session_state.message_log.append({"role": "user", "content": user_query})

    with st.spinner("🚀 Thinking..."):
        ai_response = generate_ai_response()

    st.session_state.message_log.append({"role": "assistant", "content": ai_response})

    st.rerun()

Output:

How It Works

  1. User Input: The user types a question related to coding.
  2. Building Prompt Sequence: The assistant builds a structured prompt using the conversation history.
  3. AI Response Generation: The request is sent to the Groq API, which returns a smart and concise response.
  4. Display Results: The response is displayed in an interactive chat format.

Key Features

✔️ Clean & Structured Code ✔️ Uses SQLChampion AI Branding ✔️ Supports Large Language Models (LLMs) ✔️ Interactive & User-Friendly ✔️ Expandable & Customizable


Conclusion

This AI-powered coding assistant is a simple, yet powerful tool for developers. Built with Streamlit, LangChain, and Groq API, it provides instant coding help, debugging assistance, and documentation suggestions.

💡 Want to build your own? Customize this code and enhance it with more features!

🔗 Stay tuned on SQLChampion.com for more updates! 🚀

Leave a Reply

Your email address will not be published. Required fields are marked *