This project implements a local AI chat web server using FastAPI for the backend and a simple HTML/CSS/JavaScript frontend. It allows users to interact with an AI agent (powered by gh copilot CLI) in a conversational manner, maintaining session history and enforcing turn limits.
- Chat Interface: A clean web interface for sending messages to the AI.
- AI Integration: Backend integration with the
gh copilotCLI for AI responses. - Session Management: Maintains conversation history for each user session.
- Turn Limit: Enforces a configurable turn limit per session (default 10 turns).
- Conversation History: Users can view their past messages and AI responses.
- New Chat: Option to clear the current conversation and start fresh.
- Error Handling: Robust error display for API issues and validation errors.
.
├── backend/
│ ├── api/ # FastAPI endpoints (chat, health, session management)
│ ├── models/ # Pydantic models for data validation and serialization
│ ├── services/ # Business logic (AI agent, session management)
│ └── main.py # Main FastAPI application entry point
├── frontend/
│ ├── css/ # Stylesheets
│ ├── js/ # JavaScript for frontend logic and API calls
│ └── index.html # Main HTML page
├── tests/
│ ├── unit/ # Unit tests for services and models
│ └── integration/ # Integration tests for API endpoints
├── pyproject.toml # Python project configuration and dependencies
├── uv.lock # uv lock file for reproducible dependencies
└── README.md # Project README (this file)
- Python 3.10+: Ensure you have Python installed.
- uv: A fast Python package installer and resolver.
curl -LsSf https://astral.sh/uv/install.sh | sh - gh copilot CLI: The GitHub Copilot CLI must be installed and configured.
Ensure you are logged in to GitHub and have Copilot access.
gh extension install github/gh-copilot
-
Clone the repository:
git clone <repository_url> cd ai-chat
-
Install Python dependencies:
uv pip install -e ".[dev]"
-
Start the Backend Server:
uv run python backend/main.py
The FastAPI server will start, typically on
http://127.0.0.1:8000. -
Open the Frontend: Navigate to
frontend/index.htmlin your web browser. You can simply open the file directly, or serve it using a simple HTTP server (e.g.,python -m http.serverin the project root and then navigate tohttp://localhost:8000/frontend/index.html).Note: For local development, opening
index.htmldirectly might lead to CORS issues if you're making API calls to a different origin (e.g.,file://vshttp://). Serving the frontend via a local HTTP server is recommended.
To run all unit and integration tests:
uv run pytest tests/The backend exposes the following API endpoints:
POST /api/chat: Submit a question to the AI agent and get a response.GET /api/chat/history: Retrieve the current conversation history for the session.POST /api/session/clear: Clear the current session's conversation history.GET /api/session/status: Get the current session's status (e.g., turn count, remaining turns).GET /health: Health check endpoint.
- Type your question into the input field and press Enter or click "發送".
- The AI's response will appear in the chat history.
- A loading indicator will show when the AI is processing your request.
- The remaining turn count for the session is displayed.
- Click "開始新對話" to clear the current conversation and start a new one.
- Error messages from the API will be displayed in the chat history.