Copy. Paste. Create.
Create a Simple Prompt Interface for the OpenAI API in Python
This script uses OpenAI API to generate text based on a given prompt, and then saves the generated text to a file on the user's local computer. Here is a more detailed explanation of what each line of code does:
1. The openai module is imported, which allows the script to use the functions and classes provided by the OpenAI API.
2. The API key is set using the api_key attribute of the openai module. This is required for the script to be able to use the OpenAI API.
3. The script prompts the user to enter a prompt, which is then saved in the prompt variable.
4. The openai.Completion.create() method is called, which generates text based on the given prompt. The model parameter specifies which language model to use for the generation, and the max_tokens parameter specifies the maximum number of tokens (i.e., words or word pieces) to generate. The temperature parameter controls the randomness of the generated text.
5. The response from the API is stored in the response variable. The choices attribute of the response is extracted and stored in the choices variable.
6. The first choice in the list of choices is extracted and stored in the choice variable.
7. The text attribute of the first choice is extracted and stored in the text variable.
8. The text variable is split into a list of lines using the .split() method, and the filter() function is used to remove any empty lines.
9. The script iterates over the lines and prints them one by one.
10. The text attribute of the first choice is extracted from the response and stored in the text variable.
11. The prompt variable is split into a list of words using the .split() method, and the first 15 words are joined together into a single string.
12. The first_ten variable is used to create a file name for the generated text.
13. A file path is created using the file_name variable.
14. The file is opened in write mode and the generated text is written to it using the .write() method.


# Import the openai module
import openai
# Set the API key. Replace with your API key.
openai.api_key = "YOUR API KEY"
# Generate the paragraph
prompt = input('Enter your prompt:')
prompt = prompt
response = openai.Completion.create(
model="text-davinci-003",
prompt=prompt,
max_tokens=4000,
temperature=.2
)
# Extract the choices from the response
choices = response['choices']
# Extract the first choice from the list of choices
choice = choices[0]
# Extract the text from the first choice
text = choice['text']
lines = filter(None, text.split('.'))
# Iterate over the lines and print them
for line in lines:
print(line)
# Extract the text from the response
text = response['choices'][0]['text']
prompt_split = prompt.split()
first_ten = " ".join(prompt_split[:15])
file_name = first_ten
# Use the format() method to insert the file_name into the string. Replace FILE PATH with where you would like the files to be saved.
file_path = "FILE PATH {}.txt".format(file_name)
# Open the file in write mode
with open(file_path, "w") as f:
# Write the text to the file
f.write(text)
Create Multiple OpenAI API Requests from a List
This script uses OpenAI API to generate text based on a list of phrases provided in a CSV file. It then writes the generated text to a separate file for each phrase. Here is a more detailed explanation of what each line of code does:
1. The openai and csv modules are imported, which allows the script to use the functions and classes provided by these modules.
2. The API key is set using the api_key attribute of the openai module. This is required for the script to be able to use the OpenAI API.
3. The input CSV file is opened in read mode and the contents are read into a CSV reader object using the csv.reader() function.
4. The script iterates over the rows in the CSV file using a for loop.
5. The first column of each row (which should contain a phrase) is extracted and stored in the phrase variable.
6. The openai.Completion.create() method is called, which generates text based on a prompt that includes the phrase. The model parameter specifies which language model to use for the generation, and the max_tokens parameter specifies the maximum number of tokens (i.e., words or word pieces) to generate. The temperature parameter controls the randomness of the generated text.
7. The response from the API is stored in the response variable. The text attribute of the first choice is extracted from the response and stored in the text variable.
8. A file is opened in write mode using the phrase variable to create a unique file name for each generated text.
9. The generated text is written to the file, with a new line after each sentence.
10. The script prints a message indicating that it has written a file for the current phrase.
11. The text variable is split into a list of lines using the .split() method, and the filter() function is used to remove any empty lines.
12. The script iterates over the lines and prints them one by one.
#Import the modules
import openai
import csv
#Set the API key. Replace with your API key.
openai.api_key = "YOUR API KEY"
# Read the input csv file
with open("FILE PATH OF YOUR CSV LIST", 'r') as csv_file:
# Create a csv reader object
csv_reader = csv.reader(csv_file)
# Iterate over the rows in the csv file
for row in csv_reader:
# Get the phrase from the row
phrase = row[0]
# Generate the paragraph
response = openai.Completion.create(
model="text-davinci-003",
prompt=f"Write a story about {phrase}. Each story should be 100 words.",
max_tokens=4000,
temperature=.2
)
# Extract the text from the response
text = response['choices'][0]['text']
# Open the file in write mode. Use {phrase} to include the phrase from the CSV in the file name.
with open(f"FILE PATH OF OUTPUT FILES INCLUDING {phrase}.txt", "w") as f:
# Write the text to the file, with a new line after each sentence
f.write(text.replace(".", ".\n"))
print(f"Wrote file for phrase: {phrase}")
lines = filter(None, text.split('.'))
# Iterate over the lines and print them
for line in lines:
print(line)