Skip to content

chingtechrd/ai-chat

Repository files navigation

AI Chat Web Server

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.

Features

  • Chat Interface: A clean web interface for sending messages to the AI.
  • AI Integration: Backend integration with the gh copilot CLI 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.

Project Structure

.
├── 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)

Setup and Running

Prerequisites

  • 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.
    gh extension install github/gh-copilot
    Ensure you are logged in to GitHub and have Copilot access.

Installation

  1. Clone the repository:

    git clone <repository_url>
    cd ai-chat
  2. Install Python dependencies:

    uv pip install -e ".[dev]"

Running the Application

  1. Start the Backend Server:

    uv run python backend/main.py

    The FastAPI server will start, typically on http://127.0.0.1:8000.

  2. Open the Frontend: Navigate to frontend/index.html in your web browser. You can simply open the file directly, or serve it using a simple HTTP server (e.g., python -m http.server in the project root and then navigate to http://localhost:8000/frontend/index.html).

    Note: For local development, opening index.html directly might lead to CORS issues if you're making API calls to a different origin (e.g., file:// vs http://). Serving the frontend via a local HTTP server is recommended.

Running Tests

To run all unit and integration tests:

uv run pytest tests/

API Endpoints

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.

Frontend Usage

  • 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.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors