How to Create a Bridge Between OpenAI ChatGPT and your applications

With this post, you will accomplish the following:

  1. 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.

  2. Secure the Flask app with an SSL certificate using Let's Encrypt and Certbot, ensuring that the API is accessible over HTTPS.

  3. Configure Nginx as a reverse proxy to forward requests to the Flask app, enabling the app to be accessed using your custom domain name.

  4. 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.

server {
    listen 80;
    server_name your-domain.com;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    server_name your-domain.com;

    ssl_certificate /etc/letsencrypt/live/your-domain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/your-domain.com/privkey.pem;

    location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}


Modify the Flask app to include CORS headers.

from flask_cors import CORS

CORS(app, resources={r"/*": {"origins": "*"}})


Consumed the Flask app API from Oracle APEX using a Web Source Module.

  1. Log in to your Oracle APEX workspace.

  2. Navigate to the Application Builder and select the application where you want to consume the API.

  3. In the left-hand sidebar, click on the "Shared Components" section.

  4. Find the "Web Source Modules" option and click on it.

  5. Click the "Create" button to create a new Web Source Module.

  6. Choose "From Scratch" and click "Next."

  7. In the "Name" field, provide a descriptive name for the Web Source Module, e.g., "ChatGPT_API."

  8. In the "URL Endpoint" field, enter the URL of your Flask app's API endpoint (e.g., https://yourdomain.com/generate).

  9. Set the "HTTP Method" to "POST" since the Flask app accepts POST requests.

  10. For "Authentication," choose "No Authentication" or provide the necessary authentication details if your API requires it.

  11. Click the "Next" button.

  12. 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.

  1. Click "Next."

  2. APEX will show you the "Remote Server" settings. If you need to modify any settings (e.g., proxy server), do so; otherwise, click "Next."

  3. 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:

import requests

base_url = 'https://yourdomain.com'

def generate_text(prompt, temperature=0.7, max_tokens=100):
    data = {
        'prompt': prompt,
        'temperature': temperature,
        'max_tokens': max_tokens
    }
    response = requests.post(f'{base_url}/generate', json=data)
    if response.status_code == 200:
        return response.json()['generated_text']
    else:
        raise Exception(f"Error: {response.text}")

if __name__ == '__main__':
    prompt = "List the last 5 Guatemalan presidents"
    generated_text = generate_text(prompt)
    print(generated_text)


Resolve issues with the generated text being cut off by increasing the 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

Entradas populares de este blog

Instalación JFLEX y CUP

Expresiones Regulares