Here's a short README.md for your script:
This project is an Express.js server that integrates with OpenAI's ChatGPT API, allowing you to send a message to the API and receive a response via an HTTP endpoint.
-
Clone the repository.
-
Install dependencies:
$ npm install
-
Create a
.envfile and add your OpenAI API key:OPENAI_API_KEY=your-openai-api-key-here
-
Start the server:
$ node scripts/chat.js
The server will start on port 3327.
To send a message to ChatGPT, make a POST request to the /chat endpoint with the following format:
curl -X POST http://localhost:3327/chat \
-H "Content-Type: application/json" \
-d '{"message": "Tell me about Ethereum."}'The server will return a JSON object with the ChatGPT response, for example:
{
"response": "Ethereum is a decentralized, open-source blockchain platform..."
}- If the
messagefield is missing in the request body, the server will return a400 Bad Requesterror. - If the API request to OpenAI fails, a
500 Internal Server Erroris returned.