A simple GPT3 Python script

francisirving

Registered Member
Joined
Dec 22, 2013
Messages
64
Reaction score
46
For my own testing I'd thought I'd share a simple Python script that makes 3 API calls to create some content. If you dont know where to start with GPT3 this will hopefully give you a starting point.
An FYI, I am a network engineer, not a programmer so don't beat me up, I'm learning stuff.

This script does a few things.
- asks GPT3 a question based on each item I put in a list I cleverly named 'list1'. These were just PAA questions about crypto
- asks GPT3 to figure out the main subject of each answer
- asks GPT3 to write a paragraph about each subject from the above question.
- output it to the screen at the end (also saves the prose in files if you want to keep them)

This is done via a while loop, and the output is pretty plain since I'm using Curie instead of DaVinci (noted in the script) since Curie is 1/10th the cost , but it's fun to play around with.

Here's what you do:

Create 3 blank text files, save and close (clear these out after each use or use the 'save_file' function, not the 'save_file_append' function:
'questions.txt'
'subject.txt'
'paragraph.txt'

Create the following text files with the following text in it, save then close (dont have to touch these again other then to make it better then what I have):

'subject_feed.txt':
WHAT IS THE MAIN SUBJECT OF THE FOLLOWING PARAGRAPH.

<<SUBJECT>>

'paragraph_feed.txt':
WRITE A PARAGRAPH ABOUT THE SUBJECT OF THE FOLLOWING SENTENCE.

<<PARAGRAPH>>

Here's the code. Of course you will need an openAI API account to do this, as well as the 'openai' python module installed

Python:
import os
import openai

# just some file saving functions if you use them

def open_file(filepath):
    with open(filepath, 'r', encoding='utf-8') as infile:
        return infile.read()

def save_file(filepath, content):
    with open(filepath, 'w', encoding='utf-8') as outfile:
        outfile.write(content)
        
def save_file_append(filepath, content):
    with open(filepath, 'a', encoding='utf-8') as outfile:
        outfile.write(content)


openai.api_key = 'your api key here'

def gpt3 (prompt):
    response = openai.Completion.create(
        # model="text-davinci-003",  latest and greatest, works better but costs more
        model = "text-curie-001",
        prompt=prompt,
        temperature=0.6,
        max_tokens=1000,
        top_p=1,
        frequency_penalty=0,
        presence_penalty=0,
        )
    text = response['choices'][0]['text'].strip()
    return text

list1 = ['Will crypto Rise Again 2023?', 'Which crypto will grow most in 2023?', 'Will Crypto go back up in 2024?', 'Will Bitcoin go back up in 2025?', 'What will crypto be worth in 2030?', 'What will crypto coin be worth in 2023?']

# ask question.

for i in list1:

    # ask question.
    prompt = (i)
    proseQuestion = gpt3(prompt)
    
    save_file_append('questions.txt', prompt + '\n')
    save_file_append('questions.txt', proseQuestion)
    save_file_append('questions.txt', '\n\n')
    
    # find subject section
    subject_feed = open_file('subject_feed.txt').replace('<<SUBJECT>>', proseQuestion)
    subject_answer = gpt3(subject_feed)
 
    save_file_append('subject.txt', '\n')
    save_file_append('subject.txt', subject_answer)
    save_file_append('subject.txt', '\n\n')
    
    # create paragraph about subject
    paragraph_feed = open_file('paragraph_feed.txt').replace('<<PARAGRAPH>>', subject_answer)
    paragraph_output = gpt3(paragraph_feed)
    
    save_file_append('paragraph.txt', '\n')
    save_file_append('paragraph.txt', paragraph_output)
    save_file_append('paragraph.txt', '\n\n')
    
    # output it all together
    print(prompt)
    print(proseQuestion)
    print(paragraph_output)

Try it out, its a lot of fun to play with
 
What version of python did you used for this code?
 
i wish it could create 1000 word articles

i requested it but only got a 500 word one
 
I´m lost
Stick that code in free Pycharm community edition, import the 2 libraries at the top if they have red underlines in them, and then put your registered API key from open API in the part that asks in the script. Press the run button in pycharm.

I haven't used the script, but I know python, and this looks like a piece of cake compared to what I know as a python noob!

You don't need the file saving. You could readjust the code to take what you give it in the console line input each time. Quicker.

Don't be put off by confusion, that means you're on the verge of a breakthrough! :D
 
Stick that code in free Pycharm community edition, import the 2 libraries at the top if they have red underlines in them, and then put your registered API key from open API in the part that asks in the script. Press the run button in pycharm.

I haven't used the script, but I know python, and this looks like a piece of cake compared to what I know as a python noob!

You don't need the file saving. You could readjust the code to take what you give it in the console line input each time. Quicker.

Don't be put off by confusion, that means you're on the verge of a breakthrough! :D
I got started with this guys site:
allabtai com (i dont remember the rule linking here)
he got a lot of his info (and so did I) from davidShaproAutomater on you tubes
I agree about the file saving thing, I'm a noob so I just adjust and experiment as I go :)
 
i wish it could create 1000 word articles

i requested it but only got a 500 word one
It can, but the trick seems to be to keep adding API calls to elaborate on what you have. From what @Sartre mentioned in one of his posts he has something like 10 thousand lines of code. I dont know if all that is just content creation/spellcheck/formatting/etc or if it includes his keyword and research niche tools but it sounds like to make great content will take a lot of work and experimentation.
In my code I posted it produced about 825 words using the DaVinci model, so it's a good start.
 
It can, but the trick seems to be to keep adding API calls to elaborate on what you have. From what @Sartre mentioned in one of his posts he has something like 10 thousand lines of code. I dont know if all that is just content creation/spellcheck/formatting/etc or if it includes his keyword and research niche tools but it sounds like to make great content will take a lot of work and experimentation.
In my code I posted it produced about 825 words using the DaVinci model, so it's a good start.
1 article = many api calls. try thinking outside of the box.
 
Good stuff, thank you.

I've been thinking about trying out the OpenAI's API for a while. With your code was able to get it up and running in a couple of minutes.
 
Back
Top