Skip to content

A multi-model AI chat platform built to be a complete solution for your own personal assistant or an ai assistant service offering.

License

Notifications You must be signed in to change notification settings

nicholasgriffintn/ai-platform

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AI Platform

A complete AI platform that makes multiple models available from a single application. It features an API platform that has been built out to provide a range of AI interactions and applications alongside a React frontend and a mobile application (in development).

Check out my write up on this project here. I've also launched a version of this to try out at polychat.app.

Note

Please note that this project is still in active development so there are a few features that are not yet fully working or fully imagined. You can check out our roadmap here.

A screenshot of a chat in the frontend application

Example Chats

Here are some example chats that you can try out:

Project Structure

This project is organized as a monorepo with multiple applications:

  • app - Web and mobile frontend application built with React, TailwindCSS, and React Router
  • api - Backend API built with Cloudflare Workers that interfaces with AI models and manages user data
  • metrics - Analytics and monitoring application to track usage and performance

Features

Usage Limits

Polychat is configured with usage limits to prevent abuse. These limits are as follows:

  • 10 standard messages per day for unauthenticated users
  • 50 standard messages per day for authenticated users
  • 200 pro tokens per day for authenticated users

Pro tokens are calculated based on a multiplier of the cost of the model. For example, if a model costs $0.01 per 1000 input tokens and $0.05 per 1000 output tokens, then the pro token limit is 200 * (0.01 + 0.05) / 2 = 6.

This equates to around:

  • Expensive models (9x): ~22 messages
  • Mid-tier models (3x): ~66 messages
  • Cheaper models (1-2x): 100-200 messages

If you are providing your own service and would like to change these limits, you can do so by changing the USAGE_CONFIG object in the apps/api/src/constants/app.ts file.

Setup and Installation

Getting Started

  1. Clone the repository

  2. Install dependencies

    pnpm install
  3. Configure environment variables:

    • Copy .dev.vars.example to .dev.vars in all the apps directories that have them.
    • Copy wrangler.jsonc.example to wrangler.jsonc in all the apps directories that have them.
    • Adjust with your API keys and configuration values.
  4. Start the development servers:

    # Start all apps in development mode
    pnpm run dev
    
    # Or start individual apps
    pnpm run dev:app
    pnpm run dev:api
    pnpm run dev:metrics

Deployment

The applications are designed to be deployed to Cloudflare:

# Deploy all applications
npm run deploy

# Deploy individual applications
npm run deploy:app
npm run deploy:api
npm run deploy:metrics

API Documentation

Authentication Flow

  1. User initiates login by visiting: https://api.polychat.app/auth/github
  2. User is redirected to GitHub to authorize the application
  3. After authorization, GitHub redirects back to https://api.polychat.app/auth/github/callback
  4. The API creates or updates the user record and generates a session
  5. User is redirected back to the specified redirect_uri with a session cookie set
  6. The application can choose to use the session cookie or generate a JWT token

Using the Authentication

The authentication system uses HTTP cookies for session management. When a user successfully authenticates, a session cookie is set with the session ID.

For API requests, the session ID can be included in one of these ways:

  1. Automatically via the session cookie (for browser-based requests)
  2. As a Bearer token in the Authorization header:
    Authorization: Bearer your_session_id
    

User Information

To get information about the authenticated user, make a GET request to /auth/me. This endpoint will use the session cookie or Authorization header to identify the user.

Generating a JWT Token

To generate a JWT token for the authenticated user, make a POST request to /auth/token. This endpoint will use the session cookie or Authorization header to identify the user.

The JWT token will be returned in the response body and can then be used to make API requests to the other endpoints.

Logging Out

To log out, make a POST request to /auth/logout. This will invalidate the session and clear the session cookie.

AI Model Integration

The application supports multiple AI providers through a unified API interface. To use a specific provider:

  1. Configure the API keys in your environment variables
  2. Select the provider and model in the frontend settings or specify them in API requests

Example API request:

POST /chat/completions
{
  "model": "gpt-4",
  "messages": [
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "Hello world!"}
  ]
}

Database Management

The application uses a Cloudflare D1 database with Drizzle ORM for schema management and migrations.

Running Migrations

cd apps/database
# Migrate to the local database
pnpm run db:migrate:local

# Migrate to the preview database
pnpm run db:migrate:preview

# Migrate to the production database
pnpm run db:migrate:prod

To generate a new migration, run:

pnpm run db:generate

Monitoring and Analytics

The metrics application provides dashboards for monitoring:

  • API usage and performance
  • Model performance and costs
  • User activity and engagement

Access the metrics dashboard at metrics.polychat.app.

Mobile Application

Note

The mobile application is not yet fully developed and is not ready for use.

The project includes a mobile application built with Capacitor:

# Build the web app
cd apps/app
npm run build

# Add mobile platforms
npx cap add ios
npx cap add android

# Sync web code to mobile projects
npx cap sync

# Open in native IDEs
npx cap open ios
npx cap open android

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the terms of the license included in the repository.

About

A multi-model AI chat platform built to be a complete solution for your own personal assistant or an ai assistant service offering.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Contributors 2

  •  
  •  

Languages