AI Mind

Learn, explore, or build the future of AI with top stories on the latest trends, tools, and…

Follow publication

Build Your Own Free AI Chatbot: A Step-by-Step Guide Using DeepSeek and FastAPI

--

Create a powerful chatbot with 1,000 free daily requests — no paid APIs needed!
GitHub Repository: [try-deepSeek-ai]

In the rapidly evolving landscape of AI development, creating a chatbot powered by a Large Language Model (LLM) might seem like a daunting task. However, with the right tools and approach, it’s more accessible than you might think. In this article, I’ll walk you through creating a simple yet powerful chatbot using DeepSeek’s language model, FastAPI, and plain HTML/JavaScript — all while staying within the free tier of HuggingFace’s Inference API.

Why This Matters
Before diving into the implementation, let’s address the elephant in the room: Why build another chatbot? The answer lies in accessibility and learning. While there are many commercial solutions available, building your own chatbot:
— Provides complete control over the user experience
— Helps understand LLM integration
— Offers a foundation for more complex applications
— Costs nothing to run (within the free API limits)

The Components
Our chatbot consists of three main parts:
1. **Frontend**: A single HTML file with vanilla JavaScript
2. **Backend**: A FastAPI server that handles API requests
3. **Model**: DeepSeek-R1-Distill-Qwen-32B via HuggingFace’s Inference API

The beauty of this setup lies in its simplicity. No heavy frameworks, no complex dependencies — just clean, functional code.

The Free Tier Advantage
HuggingFace’s Inference API provides 1,000 free requests per day. This is more than enough for:
— Personal projects
— Learning and experimentation
— Small-scale applications
— Proof of concept development

Implementation Highlights

The Backend
The backend is built with FastAPI, which provides async support and automatic API documentation. Here’s the core of our implementation:

from fastapi import FastAPI
from huggingface_hub import InferenceClient
from dotenv import load_dotenv
import os

app = FastAPI()
client = InferenceClient(
model="deepseek-ai/DeepSeek-R1-Distill-Qwen-32B",
api_key=os.getenv('HF_TOKEN')
)

@app.post("/chat")
async def chat_endpoint(chat_message: ChatMessage):
messages = [{"role": "user", "content": chat_message.message}]
completion = client.chat.completions.create(
messages=messages,
max_tokens=2048
)
return {"response": completion.choices[0].message.content.strip()}

The Frontend
Our frontend is a single HTML file with embedded CSS and JavaScript. It features:
— A WhatsApp-inspired UI
— Real-time typing indicators
— Formatted responses for mathematical expressions
— Error handling

Special Features
1. **Mathematical Expression Formatting**
The chatbot automatically formats mathematical expressions, making it perfect for educational applications.
For example:
``` Input: Solve (2+3)*5 Output:
[Beautifully formatted step-by-step solution]
```
2. **Thinking Process Display**
The model’s thought process is displayed in a distinct format, helping users understand how the AI arrives at its conclusions.
3. **Real-time Feedback** Users get immediate visual feedback through typing indicators and message status updates.

Security and Guardrails

One of the most critical aspects of deploying AI applications is ensuring they’re secure and responsible. Our implementation includes comprehensive guardrails that make it production-ready:

Input Filtering

  • Message Length Validation: Prevents token abuse and ensures optimal performance
  • Keyword Blocking: Automatically blocks dangerous or inappropriate content
  • Input Validation: Handles sensitive topics appropriately
  • Robust ChatMessage Model: Includes built-in validators for message content

Prompt Engineering

  • System Prompt: Implements clear rules and boundaries for AI responses
  • Context Handling: Maintains conversation coherence and appropriate context
  • Structured Engineering: Uses a dedicated class for maintainable prompt management

Output Filtering

  • Length Limits: Controls response size to prevent token abuse
  • Pattern Matching: Detects and filters sensitive content in responses
  • Response Filtering: Ensures appropriate and safe content delivery
  • Monitoring: Includes logging system for tracking and auditing interactions

These guardrails transform a simple chatbot into a production-ready application that promotes responsible AI usage while maintaining functionality.

Getting Started
Want to try it yourself? The complete implementation is available on GitHub: https://github.com/shelwyn/try-deepSeek-ai
To get started:
1. Get your HuggingFace API token
2. Clone the repository
3. Install dependencies
4. Run the server
5. Open the HTML file

Practical Applications
This setup can be adapted for various use cases:
— Educational tools
— Customer service prototypes
— Personal assistants
— Code explanation bots
— Math tutoring systems

Future Enhancements
While the current implementation is intentionally minimal, there are several potential enhancements:
— Conversation history persistence
— Multiple model support
— Custom response formatting
— User authentication
— Response streaming

Conclusion
Building an LLM-powered chatbot doesn’t have to be complex or expensive. With HuggingFace’s free tier and some basic web development knowledge, you can create a functional AI chatbot that serves as a foundation for more advanced applications. The complete source code is available on GitHub, and I encourage you to try it out, modify it, and build upon it. The world of AI development is more accessible than ever — all you need is curiosity and willingness to experiment. Remember, you get 1,000 free requests per day with HuggingFace’s Inference API, making this a perfect starting point for your AI journey. Happy coding! 🚀

Thank you for reading my article! If you found it helpful or interesting, I’d love to connect with you on LinkedIn. You can follow me here [https://www.linkedin.com/in/shelwyn-corte/]for more updates and insights. Let’s stay connected!

A Message from AI Mind

Thanks for being a part of our community! Before you go:

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Published in AI Mind

Learn, explore, or build the future of AI with top stories on the latest trends, tools, and technology. Share your crazy success stories or AI-fueled fails in a supportive community.

No responses yet

Write a response