How to Create a Bridge Between OpenAI ChatGPT and your applications
With this post, you will accomplish the following:
Set up a Flask app on an Ubuntu server hosted on Azure to create a bridge between ChatGPT-4 and your applications. The Flask app listens on port 8080 and exposes an API endpoint to generate text based on the provided input.
Secure the Flask app with an SSL certificate using Let's Encrypt and Certbot, ensuring that the API is accessible over HTTPS.
Configure Nginx as a reverse proxy to forward requests to the Flask app, enabling the app to be accessed using your custom domain name.
Create a Web Source Module in Oracle APEX to consume the Flask app's API. This involves configuring the API endpoint, HTTP method, authentication, and required parameters in APEX. The Web Source Module can be used in various components within your APEX application, such as Interactive Reports, Classic Reports, or Interactive Grids.
Throughout the post, I will provide detailed instructions, code snippets, and troubleshooting steps to guide you in setting up and configuring each component. As a result, you will have a fully functional API that connects your applications to ChatGPT-4, allowing you to generate text based on user input or other data sources.
If you want instructions on how to create and configure the ubuntu server you can go here.
Flask App Setup and Troubleshooting
We set up a Flask app to create a bridge between ChatGPT-4 and our applications using Azure, an Ubuntu server, and Python.
Key Steps
1. Created a Flask app to serve as the API endpoint for ChatGPT-4.
2. Encountered issues with CORS, Nginx configuration, and SSL certificate.
3. Resolved issues by:
- Modifying Flask app to handle CORS properly.
- Fixing the Nginx configuration and removing conflicting server blocks.
- Setting up SSL certificates with Let's Encrypt and Certbot.
4. Adjusted `max_tokens` parameter to control the length of the generated text.
Key Files and Code Snippets
Create a Flask app that takes user input and generates text using the ChatGPT-4 API.
import os
from flask import Flask, request, jsonify
import requests
from flask_cors import CORS
app = Flask(__name__)
cors = CORS(app, resources={r"/generate": {"origins": "https://a-site.com"}}) # Enable CORS for all routes and origins
# Replace with your API key
api_key = "your-api-key"
@app.route('/generate', methods=['POST'])
def generate_text():
data = request.get_json()
prompt = data.get('prompt', '')
temperature = data.get('temperature', 0.7)
max_tokens = data.get('max_tokens', 100)
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {api_key}'
}
payload = {
'model': 'gpt-4',
'messages': [{"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": prompt}],
'temperature': temperature,
'max_tokens': max_tokens
}
try:
response = requests.post('https://api.openai.com/v1/chat/completions', headers=headers, json=payload)
response.raise_for_status()
json_response = response.json()
return jsonify({'generated_text': json_response['choices'][0]['message']['content']})
except Exception as e:
return jsonify({'error': str(e)}), 400
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080)
Set up HTTPS for the Flask app using Certbot and configured Nginx.
Modify the Flask app to include CORS headers.
Consumed the Flask app API from Oracle APEX using a Web Source Module.
Log in to your Oracle APEX workspace.
Navigate to the Application Builder and select the application where you want to consume the API.
In the left-hand sidebar, click on the "Shared Components" section.
Find the "Web Source Modules" option and click on it.
Click the "Create" button to create a new Web Source Module.
Choose "From Scratch" and click "Next."
In the "Name" field, provide a descriptive name for the Web Source Module, e.g., "ChatGPT_API."
In the "URL Endpoint" field, enter the URL of your Flask app's API endpoint (e.g., https://yourdomain.com/generate).
Set the "HTTP Method" to "POST" since the Flask app accepts POST requests.
For "Authentication," choose "No Authentication" or provide the necessary authentication details if your API requires it.
Click the "Next" button.
In the "Parameters" section, click the "Add Parameter" button to add the required parameters for the API request:
prompt: The text prompt you want to send to the API.temperature: (Optional) The temperature value for the API.max_tokens: (Optional) The maximum number of tokens you want in the generated text.
Log in to your Oracle APEX workspace.
Navigate to the Application Builder and select the application where you want to consume the API.
In the left-hand sidebar, click on the "Shared Components" section.
Find the "Web Source Modules" option and click on it.
Click the "Create" button to create a new Web Source Module.
Choose "From Scratch" and click "Next."
In the "Name" field, provide a descriptive name for the Web Source Module, e.g., "ChatGPT_API."
In the "URL Endpoint" field, enter the URL of your Flask app's API endpoint (e.g., https://yourdomain.com/generate).
Set the "HTTP Method" to "POST" since the Flask app accepts POST requests.
For "Authentication," choose "No Authentication" or provide the necessary authentication details if your API requires it.
Click the "Next" button.
In the "Parameters" section, click the "Add Parameter" button to add the required parameters for the API request:
prompt: The text prompt you want to send to the API.temperature: (Optional) The temperature value for the API.max_tokens: (Optional) The maximum number of tokens you want in the generated text.For each parameter, provide the name, set "Parameter Scope" to "Request," and specify the parameter type (e.g., "Static" or "Dynamic"). If using "Static," provide a default value for testing purposes.
Click "Next."
APEX will show you the "Remote Server" settings. If you need to modify any settings (e.g., proxy server), do so; otherwise, click "Next."
APEX will now attempt to fetch sample data from your API. If successful, you should see a JSON response. Click "Create Web Source" to finalize the Web Source Module creation.
Now you have a Web Source Module that connects to your Flask app's API. You can use this module in various components in your APEX application, such as Interactive Reports, Classic Reports, or Interactive Grids.
To use the Web Source Module in an APEX component, create a new component, and select the "Data Source" as "Web Source." Then, choose the Web Source Module you created earlier (e.g., "ChatGPT_API"). APEX will automatically handle sending the POST request to your API and display the generated text in the component.
Test the API consumption in Python:
max_tokens parameter.This summary should give you an overview of the process and the code snippets used to set up the Flask app, enable HTTPS, configure Nginx, and consume the generated text in Oracle APEX.
Comentarios
Publicar un comentario