% # AI Chatbot (FastAPI backend + React + Vite frontend)
This repository contains a small AI Chatbot application with a FastAPI backend and a React + Vite frontend. The backend uses a LangGraph/LangChain flow and an LLM; the frontend provides a friendly chat UI.
- FastAPI backend with a
/chatendpoint that drives a LangGraph agent (chat + tool routing). - React + Vite frontend with an improved chat UI:
- Animated bot text generation (typewriter-style) for better UX.
- Tool badges: when the assistant calls an external tool (Wikipedia, DuckDuckGo, etc.), the UI displays a small badge showing which tool was used.
backend/api.py- FastAPI app and/chatendpoint (production entry point).chatbot.py- standalone runnable example of the LangGraph chatbot (used for local CLI testing).requirements.txt- Python dependencies for the backend..env.example- example environment file; copy to.envand add your API key.
frontend/src/components/Chat.jsx- main chat UI (typewriter effect + tool badges).src/styles.css- UI styles for the chat.package.json,vite.config.js- frontend scripts and dev proxy config.
- User types a message in the React UI and presses Enter / Send.
- The frontend POSTs to
/chatwith JSON:{ user_message, thread_id }. - FastAPI's
/chatendpoint wraps the message in aHumanMessageand invokes the LangGraph graph. The graph may return an AI response directly or include tool calls. - If the agent calls a tool, the backend executes the tool and returns the tool output. The backend includes tool metadata in the response so the frontend can display a tool badge.
- The frontend displays the assistant response. Bot responses are animated; if a tool was called, a badge like
wikipedia_toolappears above the response.
- The backend uses
python-dotenvand will loadbackend/.env(copybackend/.env.exampleto.env). - Required environment variable (one of):
OPENROUTER_API_KEY(for OpenRouter)OPENAI_API_KEY(fallback)
Prerequisites:
- Python 3.10+ (recommended)
- Node.js (LTS)
- Install Python dependencies:
cd c:\Users\hp\Desktop\ml\project1\backend
pip install -r requirements.txt- Create
.envand add your API key:
copy .env.example .env
rem # then open backend\.env in an editor and set OPENROUTER_API_KEY or OPENAI_API_KEY- Run the backend (development):
cd c:\Users\hp\Desktop\ml\project1\backend
uvicorn api:app --reload --host 0.0.0.0 --port 8000- Install dependencies and start the dev server:
cd c:\Users\hp\Desktop\ml\project1\frontend
npm install
npm run dev- Open the site (usually
http://localhost:5173) and use the chat UI.
- Vite dev server includes a proxy for
/chattohttp://localhost:8000(seevite.config.js) so browser requests avoid CORS issues. - The backend also configures
CORSMiddlewarefor the dev origin.
- Animated generation: bot responses are shown with a typewriter-style reveal. This improves readability and gives a sense of streaming.
- Tool badges: when the assistant calls a tool, the backend prefixes or annotates the response so the frontend can show a badge, e.g.
(tool: wikipedia_tool) ...or structured responses. The UI then displays the badge above the response.
If you prefer structured tool metadata instead of a prefix, I can update the API to return JSON like { response: string, tool: 'wikipedia_tool' } and wire the frontend to read that directly.
- When building the frontend for production, set
VITE_API_URLto your backend URL at build time. Example:
set VITE_API_URL=https://your.api.host
npm run build- Change the LLM model or settings by editing
backend/api.py(orbackend/chatbot.pyfor standalone tests). Look forChatOpenAI(...). - Change frontend API base: set
VITE_API_URLor editChat.jsx'sapiBasedefault.
- If the frontend shows
Error: Failed to fetch:- Confirm the backend is running on port 8000 and accessible.
- Check browser devtools network tab for the
/chatrequest and backend logs for exceptions. - Ensure
backend/.envcontains a valid API key.
- If you see pydantic validation errors from tools, ensure the backend normalizes tool inputs before calling library methods — that has been addressed in recent updates.
- Backend requirements are listed in
backend/requirements.txt. Install them with:
pip install -r backend\requirements.txt- Some packages (langgraph, langchain-core, langchain-community) may require compatible versions; if you run into installation issues, share the pip output and I'll suggest pinned versions.
- Do not commit your
backend/.envor any file containing API keys. The repo provides.env.examplefor reference.
- Convert the backend->frontend contract to structured JSON for tool metadata and update the frontend to rely on that (recommended).
- Add a Dockerfile and
docker-compose.ymlfor easy local deployment. - Add tests and CI for the API and the frontend component.
If you want any of these, tell me which and I'll implement it.
README updated to reflect UI improvements and dependency changes (2025-11-17).