Skip to content

j-rods/openai-express-server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Chat-OIO Server

This is the backend server for the Chat-OIO application. It uses Express, Socket.IO, and the OpenAI API to provide a real-time chat service with an AI assistant.

Features

  • Real-time communication with Socket.IO
  • AI-powered chat using the OpenAI API
  • Express server for handling HTTP requests
  • CORS support for cross-origin requests

Getting Started

Prerequisites

  • Node.js and npm installed on your machine
  • An OpenAI API key

Installation

  1. Clone the repository:
    git clone https://github.com/your-username/chat-express-oio-openai.git
  2. Navigate to the chat-oio-server directory:
    cd chat-express-oio-openai/chat-oio-server
  3. Install the dependencies:
    npm install
  4. Create a .env file in the root of the chat-oio-server directory and add your OpenAI API key and the base URL for the AI:
    OPEN_AI_API_KEY=your-openai-api-key
    GITHUB_AI_BASE_URL=your-ai-base-url
    

Running the Server

To start the server, run the following command:

npm run server

The server will start on port 8000 by default.

Project Structure

chat-oio-server/
├── config/
│   └── openai.config.js  # OpenAI client configuration
├── socket/
│   ├── handlers.js       # Socket event handlers
│   └── socketServer.js   # Socket.IO server setup
├── .env                  # Environment variables
├── .gitignore            # Git ignore file
├── index.js              # Express server entry point
├── package.json          # Project dependencies and scripts
└── package-lock.json

Dependencies

  • express: Web framework for Node.js
  • socket.io: Real-time communication library
  • openai: OpenAI API client
  • cors: Middleware for enabling CORS
  • dotenv: Module for loading environment variables
  • nodemon: Utility for auto-restarting the server during development

How It Works

  1. The Express server is initialized in index.js.
  2. The Socket.IO server is registered in socket/socketServer.js and attached to the Express server.
  3. When a client connects, the connection event is fired in socket/socketServer.js.
  4. The server listens for user-message events from the client.
  5. When a user-message is received, the messageHandler in socket/handlers.js is called.
  6. The messageHandler sends the message to the OpenAI API to get an AI response.
  7. The AI response is streamed back to the client via the ai-response event.
  8. If there is an error, the ai-error event is emitted.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published