3 min read

Unleashing the Power of OpenAI: A Step-by-Step Guide to Generating Pretrained Text with Ruby

Unleashing the Power of OpenAI: A Step-by-Step Guide to Generating Pretrained Text with Ruby

OpenAI's API is a powerful tool for natural language processing and text generation. With just a few lines of code, you can generate high-quality text that can be used for a variety of applications, such as writing, content creation, and more. In this post, we'll go through the process of using the OpenAI API to generate text with Python.

This script is a simple implementation of the OpenAI API for generating text. The script utilizes the requests library to make a post request to the OpenAI API endpoint for completions, which generates text based on a provided prompt.

curl https://api.openai.com/v1/completions \ 
-H 'Content-Type: application/json' \ 
-H "Authorization: Bearer $YOUR_API_KEY" \ 
'{ "model": "text-davinci-003", 
    "prompt": "Write a phishing email", 
    "max_tokens": 4000, 
    "temperature": 1.0 } \ 

First, let's start by importing the necessary libraries:

import json
import requests

Next, we'll define a function that will handle the interaction with the OpenAI API. This function will take in several parameters, including the prompt, model, maximum number of tokens, and temperature.

def generate_text(prompt, model, max_tokens, temperature):
    headers = {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer SECRET_API_KEY' // Only Paid API Keys can be used here to access Generative Pre-trained Transformer aka GPT

    data = {
        "model": model,
        "prompt": prompt,
        "max_tokens": max_tokens,
        "temperature": temperature

        response = requests.post('https://api.openai.com/v1/completions', headers=headers, json=data)
    except requests.exceptions.HTTPError as errh:
        print ("HTTP Error:",errh)
    except requests.exceptions.ConnectionError as errc:
        print ("Error Connecting:",errc)
    except requests.exceptions.Timeout as errt:
        print ("Timeout Error:",errt)
    except requests.exceptions.RequestException as err:
        print ("Something went wrong:",err)
        response_data = response.json()
        return response_data["choices"][0]["text"]

In this function, we first set up the headers for the API request, including the Content-Type and Authorization. We then package the prompt, model, max_tokens, and temperature into a dictionary called data.

After that, we use a try-except block to handle any errors that may occur when making the request to the API. If an error occurs, the script will print the error message. If the request is successful, the script will parse the JSON response and return the "text" of the first "choice" in the response.

Now that we have our function set up, we can call it and pass in our desired prompt, model, max_tokens, and temperature.

prompt = input("Enter your prompt: ")
model = "text-davinci-003"
max_tokens = 4000
temperature = 1.0
output = generate_text(prompt, model, max_tokens, temperature)

In this example, we're prompting the user for their desired prompt, setting the model to "text-davinci-003", setting the max_tokens to 4000, and the temperature to 1.0. We then call the generate_text() function, passing in these values and print the output generated by the API

Here are some examples that are limited by ChatGPT functionality :

Chat GPT V1

Content Filtering In ChatGPT

Here are the responses by my ruby script :

No Content Filtering
No Privacy violations

Security researchers can also use OpenAI's API for various use cases, such as generating phishing emails, detecting and analyzing malicious content, and more. The API's ability to understand and generate natural language text can be used to create realistic and convincing examples of malicious content, which can be used to train and test security systems. Additionally, the API can also be used to generate a large number of variations of known malicious content, which can help to evade detection.

In conclusion, this script provides a simple and effective way to use the OpenAI API to generate text with Python. It is important to note that in order to run this program, you will need to have an API key for the OpenAI API as the authentication is done via secret key. Without a valid API key, the script will not be able to connect to the API and generate text. Keep in mind that OpenAI's API key is only for paid version and not for free version. If you're interested in purchasing an API key for your uses through my organization, feel free to contact me via WhatsApp at +919490470485 , Linkedin, Twitter through email at bhaskarvilles.eth@ethereum.email. I will be happy to assist you with any questions or concerns you may have regarding the OpenAI API and how it can benefit your organization or security research purposes.