Skip to content

OCG-labs/gpt-wordpress-api

Repository files navigation

🎮 OCG GPT API Server

This is a node server built to serve as a API resource to interact with ChatGPT inside of the Vtiger CRM.

📋 Prerequisites

You need to have Node.js and npm installed on your machine. If you don't have them installed, you can download them from here.

🔧 Installing

  1. Fork and clone the repo.
  2. Navigate to the project directory.
  3. Run npm install to install the dependencies.
  4. Proceed to Docker Section to deploy with Docker.

🔧 API

  • /api/chat/article - POST request that takes in a message and returns a JSON object.
Screenshot 2024-05-09 at 10 02 10 AM
  • /api/chat/article/post - POST request that posts generated article to specified wordpress site.

  • /api/chat/post/contact - POST request generates a Oxygen JSON output for a contact page.

  • /api/chat/post/about - POST request generates a Oxygen JSON output for a about page.

  • /api/test - GET request to calls a free api for testing purposes.

API Usage

Interaction with the OpenAI API is made easy with this simple Node server.

/chat/article

  • Edit the GPT model to your liking in server/server.js IMPORTANT NOTE: CHANGING THE MODEL CAN HAVE UNFORSEEN BUGS
Screenshot 2024-05-03 at 12 38 03 PM
  • Using the /api/chat/article endpoint, send a post request with a body including a message for chat completion
Screenshot 2024-05-03 at 12 40 49 PM

HTTP Response will be content from a JSON object.

🚢 Docker

Included in the root directory are a Dockerfile and a docker-compose.yml. This allows the server to be spun up as a docker container locally, or on a hosted service that supports docker deployment.

Local Docker Deployment

  • Ensure docker daemon is installed on the local machine (Docker Desktop).
  • Ensure you have a api_txt file in the root directory containing Openai api key.
  • Ensure you have a .env file with Port variable.
  • Change
// Check if the Docker secret file exists
if (fs.existsSync('/etc/secrets/openai_api_key')) {
  // Read the API key from the Docker secret
  OPENAI_API_KEY = fs.readFileSync('/etc/secrets/openai_api_key', 'utf8').trim();
} else {
  // Log error
  console.log("No api key")
}

To

// Check if the Docker secret file exists
if (fs.existsSync('/run/secrets/openai_api_key')) {
  // Read the API key from the Docker secret
  OPENAI_API_KEY = fs.readFileSync('/run/secrets/openai_api_key', 'utf8').trim();
} else {
  // Log error
  console.log("No api key")
}
  • Run docker-compose up to spin up the image and container via docker-compose.yml file.
  • The server should now be running locally on specified port and can be tested with end points.

Hosted Docker Deployment

Render will be used as a example of hosted Docker deployment.

  • Click on the New + button to create a new deployed asset.
  • Choose Web service option.
Screenshot 2024-05-05 at 07 23 34
  • Link github repo.
Screenshot 2024-05-05 at 07 23 40
  • Ensure runtime displays "Docker".
Screenshot 2024-05-05 at 07 25 37
  • Add .env PORT variable and Docker secret file labeled as "open_ai_key".
Screenshot 2024-05-05 at 07 25 58 Screenshot 2024-05-09 at 11 28 28 AM

After deployment, test end points to verify deployment success.

🔒 Docker Secrets

API keys are valuable and are not secure when placed inside of a .env file of the docker container. For this reason, you want to create a docker secret file for added security. IMPORTANT NOTE: This method is not entirely secure. For maximum security, use docker swarm.

🐅 Vtiger Usage

Once you are on the workflow designer, set up a web-service call and associated action once data is received.

Screenshot 2024-05-06 at 9 48 36 AM

Use the parameter section and name it "message". Make the value your prompt to GPT. IMPORTANT NOTE: GPT is in JSON mode so your prompt needs to say, "Return a JSON object with the following structure:"

Screenshot 2024-05-06 at 9 50 18 AM

🛠️ Built With

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published