Skip to content

Repository files navigation

🧠 AI Chatbot with Tool Calling (FastAPI + Azure OpenAI + LangChain)

This project is a production-ready AI chatbot backend built with:

βœ… Azure OpenAI (GPT-4.1-Mini)
βœ… LangChain Agents & Tools
βœ… FastAPI backend
βœ… Custom tools including email lookup & web search
βœ… Extensible design to support MongoDB queries

The chatbot is capable of:

  • Understanding natural language queries
  • Deciding whether a tool is needed
  • Calling backend functions automatically
  • Returning natural human-like responses

πŸš€ Features

πŸ€– LLM-Powered Chatbot

Uses Azure OpenAI GPT-4.1-Mini as the reasoning engine.

πŸ›  Tool Calling

The chatbot intelligently calls backend functions such as:

  • πŸ” web_search β€” Searches DuckDuckGo
  • πŸ“§ get_user_email_id β€” Returns email IDs based on query
  • (Future) πŸ—„ MongoDB query tools

🌐 REST API

One simple endpoint:

POST /chat

πŸ’¬ Conversational Memory

Keeps context across messages.

πŸ“œ System Prompt Control

Defines chatbot policy & behavior.


πŸ— Architecture Overview

User β†’ FastAPI β†’ Azure OpenAI β†’ Tool Selected β†’ Backend Executes
        ↑                                       ↓
        ←────────────── Final Answer ───────────

LLM = Brain
Tools = Muscles
Backend = Orchestrator

graph TD
    A[User] --> B[FastAPI]
    B --> C[Azure OpenAI]
    C --> D{Decision Point}
    D -->|Tool Needed| E[Tool Selection]
    E --> F[Tool Execution]
    F --> G[Result Processing]
    G --> H[LLM Response]
    H --> I[Final Answer]
    D -->|No Tool| J[Direct Response]
    J --> I
    I --> A
Loading

πŸ“‚ Project Structure

AI-chatbot-with-toolcall/
β”œβ”€β”€ main.py              # FastAPI app + Agent logic
β”œβ”€β”€ tools.py             # Tool functions
β”œβ”€β”€ config.py            # Azure credentials
β”œβ”€β”€ malay.txt            # Profile data file
β”œβ”€β”€ requirements.txt     # Python dependencies
β”œβ”€β”€ send_email.py        # Email sending helper
β”œβ”€β”€ templates.py         # Template selection / utilities
β”œβ”€β”€ email_templates/     # HTML email templates
β”‚   β”œβ”€β”€ admin_templete.html
β”‚   └── user_templete.html
β”œβ”€β”€ frontend/            # Simple web UI for the chatbot
β”‚   β”œβ”€β”€ index.html
β”‚   └── README.md
└── README.md            # Backend & project docs
graph TD
    A[AI-chatbot-with-toolcall] --> B[main.py<br/>FastAPI + Agent]
    A --> C[tools.py<br/>Tool Functions]
    A --> D[config.py<br/>Azure Config]
    A --> E[malay.txt<br/>Data File]
    A --> F[requirements.txt<br/>Deps]
    A --> G[send_email.py<br/>Email Helper]
    A --> H[templates.py<br/>Template Utils]
    A --> I[email_templates/<br/>HTML Email Templates]
    A --> J[frontend/<br/>Web UI]
    A --> K[README.md<br/>Docs]
Loading

🌐 Frontend UI

The web UI for this chatbot is developed in a separate repository and mirrored here for convenience:

To try it out, start the FastAPI backend, then either open frontend/index.html directly in your browser or serve the frontend/ directory with a static server (for example, VS Code Live Server or python -m http.server). Make sure any API base URL used in the frontend points to your running backend (for example, http://127.0.0.1:8000/chat).


βš™οΈ Installation & Setup

1️⃣ Clone repo & create venv

python -m venv venv
source venv/bin/activate   # Mac/Linux
venv\Scripts\activate      # Windows

2️⃣ Install dependencies

pip install -r requirements.txt

3️⃣ Configure Azure OpenAI

Create a .env file in the root directory of the project:

AZURE_OPENAI_ENDPOINT=https://YOUR-RESOURCE.openai.azure.com/
AZURE_OPENAI_API_KEY=YOUR_KEY
AZURE_DEPLOYMENT=gpt-4.1-mini
AZURE_API_VERSION=2025-01-01-preview

Note: The config.py file automatically loads these environment variables using python-dotenv. Deployment name must match your Azure Studio deployment.


4️⃣ Run server

uvicorn main:app --reload

Server runs at:

http://127.0.0.1:8000

🧰 Tools Explained

πŸ” 1. Web Search Tool

Searches online using DuckDuckGo API.

Used for general knowledge queries.


πŸ“§ 2. Email Lookup Tool

Example:

get_user_email_id("What is Malay's email?")

Returns:

malayjain1234@gmail.com

Logic:

Name Email
Malay Jain malayjain1234@gmail.com
Aniket anni990@gmail.com

Tool docstring tells the LLM when to use it.


🧠 How Tool Calling Works (Simple Explanation)

1️⃣ User asks a question
2️⃣ LLM decides whether a tool is needed
3️⃣ If yes β†’ passes arguments to tool
4️⃣ Backend executes Python function
5️⃣ Result is returned to LLM
6️⃣ LLM writes natural reply

Example:

User: What is Malay's email?
LLM β†’ ToolCall(get_user_email_id)
Backend returns email
LLM responds naturally

Magic ✨

sequenceDiagram
    participant U as User
    participant L as LLM
    participant T as Tool
    participant B as Backend

    U->>L: Asks question
    L->>L: Evaluates need for tool
    L->>T: Calls tool with arguments
    T->>B: Executes function
    B->>T: Returns result
    T->>L: Provides result
    L->>U: Generates natural response
Loading

🧩 System Prompt Role

Controls:

βœ” assistant behavior
βœ” tone
βœ” policy
βœ” general rules

Example rules:

  • Prefer local tools for internal data
  • Use web search only when required
  • Do not mention tool names

πŸ”Š Verbose Logging

verbose=True

Shows:

  • tool selection
  • reasoning chain
  • inputs & outputs

Useful for debugging.


πŸ›‘ Security Notes

βœ” API keys are never exposed
βœ” Tools run in backend only
βœ” Email lookup prevents hallucination
βœ” Invalid requests return safe output


πŸ§ͺ Testing the Chat API

Send request:

POST /chat
Content-Type: application/json

Body:

{
  "message": "What is Malay's email?"
}

πŸ› Tech Stack

Component Technology
Backend FastAPI
LLM Azure OpenAI
Agent LangChain
Tools Python functions
Memory LangChain buffer

πŸ“Œ Why LangChain?

LangChain handles:

βœ” conversation history
βœ” tool routing
βœ” argument passing
βœ” agent reasoning
βœ” debug logging

So you write less glue code.


🧠 Future Enhancements

πŸ“Œ Add MongoDB query tools
πŸ“Œ Structured tool calling
πŸ“Œ Auth & rate limiting
πŸ“Œ Frontend UI
πŸ“Œ Docker image


🏁 Summary

This project demonstrates a realistic production-grade AI chatbot backend that combines:

🧠 Azure OpenAI reasoning
πŸ›  Python business logic
πŸš€ FastAPI deployment
🧩 LangChain agents

It's clean, extensible, and powerful.


🀝 Contributing

PRs & suggestions welcome πŸ‘


πŸ“„ License

MIT

Releases

Packages

Contributors

Languages