A decoupled full-stack AI chatbot architecture built with FastAPI, Streamlit, and LangChain. This repository implements stateful chat memory across network boundaries and leverages dynamic model orchestration.
- Backend (FastAPI + LangChain): Manages conversational state tracking using
InMemoryChatMessageHistoryinsidegpt3.pyto bypass LLM statelessness. It translates incoming user strings into structured message payloads and communicates with OpenRouter API nodes. - Frontend (Streamlit): Serves as an interactive interface inside
Chatbot.py. It communicates asynchronously with the backend API over HTTPrequestsand handles visualization persistence across page refreshes viast.session_state.
FASTAPI/
│
├── __pycache__/
├── myvenv/ # Your Python virtual environment
├── Chatbot.py # Streamlit chat user interface (Frontend)
├── gpt3.py # FastAPI & LangChain orchestration logic (Backend)
├── secret_key.py # Local OpenRouter API key credentials file
└── README.md # Project documentation
-
Clone the repository and navigate into the project directory:
git clone https://github.com cd FASTAPI -
Activate your existing virtual environment (
myvenv):# On Windows (PowerShell): .\myvenv\Scripts\Activate.ps1 # On Windows (Command Prompt): .\myvenv\Scripts\activate.bat
-
Install the required dependency libraries if you haven't already:
pip install fastapi uvicorn langchain-openrouter langchain-core streamlit requests
-
Verify your
secret_key.pycontains your token:openrouter_api_key1 = "your_actual_openrouter_api_key_here"
-
Run your FastAPI backend server using your local environment python runner:
.\myvenv\Scripts\uvicorn gpt3:app --reload
-
Open a separate terminal split window, ensure your virtual environment is active, and launch the Streamlit frontend client app:
.\myvenv\Scripts\streamlit run Chatbot.py
- Frontend UI Workspace: Open
http://localhost:8501inside your web browser. - Backend API Endpoints: Open
http://localhost:8000/docsto view the interactive FastAPI Swagger documentation.
- The user types a query into the Streamlit interactive chat box (
Chatbot.py). - The user interface logs the message locally into
st.session_stateand fires an HTTP GET network request containing the prompt string to the local addresshttp://127.0.0. - The FastAPI endpoint inside
gpt3.pycaptures the string parameter, wraps it into aHumanMessageschema object, and saves it intoInMemoryChatMessageHistory. - The historical conversation log list is bundled and pushed to OpenRouter via the universal model router
openrouter/freeto maintain conversational context. - The API parses the text answer, updates the backend memory map with an
AIMessagedata object, and returns a JSON dictionary back across the network to render the text bubble visually in Streamlit.