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
Uses Azure OpenAI GPT-4.1-Mini as the reasoning engine.
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
One simple endpoint:
POST /chat
Keeps context across messages.
Defines chatbot policy & behavior.
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
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]
The web UI for this chatbot is developed in a separate repository and mirrored here for convenience:
- GitHub repo: https://github.com/MalayJain412/Frontend-UI-for-AI-Chatbot-fastapi
- Local folder in this project:
frontend/ - Entry file:
frontend/index.html - Frontend docs:
frontend/README.md
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).
python -m venv venv
source venv/bin/activate # Mac/Linux
venv\Scripts\activate # Windowspip install -r requirements.txtCreate 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-previewNote: The
config.pyfile automatically loads these environment variables usingpython-dotenv. Deployment name must match your Azure Studio deployment.
uvicorn main:app --reloadServer runs at:
http://127.0.0.1:8000
Searches online using DuckDuckGo API.
Used for general knowledge queries.
Example:
get_user_email_id("What is Malay's email?")Returns:
malayjain1234@gmail.com
Logic:
| Name | |
|---|---|
| Malay Jain | malayjain1234@gmail.com |
| Aniket | anni990@gmail.com |
Tool docstring tells the LLM when to use it.
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
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=True
Shows:
- tool selection
- reasoning chain
- inputs & outputs
Useful for debugging.
β API keys are never exposed
β Tools run in backend only
β Email lookup prevents hallucination
β Invalid requests return safe output
Send request:
POST /chat
Content-Type: application/json
Body:
{
"message": "What is Malay's email?"
}| Component | Technology |
|---|---|
| Backend | FastAPI |
| LLM | Azure OpenAI |
| Agent | LangChain |
| Tools | Python functions |
| Memory | LangChain buffer |
LangChain handles:
β conversation history
β tool routing
β argument passing
β agent reasoning
β debug logging
So you write less glue code.
π Add MongoDB query tools
π Structured tool calling
π Auth & rate limiting
π Frontend UI
π Docker image
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.
PRs & suggestions welcome π
MIT