francisirving
Registered Member
- Dec 22, 2013
- 64
- 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
Try it out, its a lot of fun to play with
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