Cookie Consent by Free Privacy Policy Generator

Python - How to create long-form content using ChatGPT API

Creating long-form content using the ChatGPT API is fairly straightforward. Below is a script to create a piece of long-form content using ChatGPT API and Python with an example and a Colab link to copy the script.


Code:
# !pip install openai
import openai

# API Key
openai.api_key = "sk-"
prompt="Python code."

#modifiers
mod1 = "Show me Variables with example?"
mod2 = "Show me Strings with example?"
mod3 = "Show me Replace with example?"
mod4 = "Show me Join with example?"
mod5 = "Show me Split with example?"
mod6 = "Show me If Statements with example?"
mod7 = "Show me Loops with example?"
mod8 = "Show me Nested Loops with example?"
mod9 = "Show me Functions with example?"

prompt1 =  prompt + mod1
prompt2 =  prompt + mod2
prompt3 =  prompt + mod3
prompt4 =  prompt + mod4
prompt5 =  prompt + mod5
prompt6 =  prompt + mod6
prompt7 =  prompt + mod7
prompt8 =  prompt + mod8
prompt9 =  prompt + mod9

# Prompt 1
completion = openai.ChatCompletion.create(
  model="gpt-3.5-turbo",
  messages=[
    {"role": "user", "content": prompt1}
  ]
)

# prompt 1 response
print(mod1)
print(completion.choices[0].message.content)
# next prompt
# Prompt 2
completion1 = openai.ChatCompletion.create(
  model="gpt-3.5-turbo",
  messages=[
    {"role": "user", "content": prompt2}
  ]
)

# prompt 2 response
print(mod2)
print(completion1.choices[0].message.content)
# next prompt

# Prompt 3
completion2 = openai.ChatCompletion.create(
  model="gpt-3.5-turbo",
  messages=[
    {"role": "user", "content": prompt3}
  ]
)

# prompt 3 response
print(mod3)
print(completion2.choices[0].message.content)

# next prompt
# Prompt 4
completion3 = openai.ChatCompletion.create(
  model="gpt-3.5-turbo",
  messages=[
    {"role": "user", "content": prompt4}
  ]
)

# prompt 4 response
print(mod4)
print(completion3.choices[0].message.content)

# next prompt
# Prompt 5
completion4 = openai.ChatCompletion.create(
  model="gpt-3.5-turbo",
  messages=[
    {"role": "user", "content": prompt5}
  ]
)

# prompt 5 response
print(mod5)
print(completion4.choices[0].message.content)

# next prompt
# Prompt 6
completion5 = openai.ChatCompletion.create(
  model="gpt-3.5-turbo",
  messages=[
    {"role": "user", "content": prompt6}
  ]
)

# prompt 6 response
print(mod6)
print(completion5.choices[0].message.content)

# next prompt
# Prompt 7
completion6 = openai.ChatCompletion.create(
  model="gpt-3.5-turbo",
  messages=[
    {"role": "user", "content": prompt7}
  ]
)

# prompt 7 response
print(mod7)
print(completion6.choices[0].message.content)

# next prompt
# Prompt 8
completion7 = openai.ChatCompletion.create(
  model="gpt-3.5-turbo",
  messages=[
    {"role": "user", "content": prompt8}
  ]
)

# prompt 8 response
print(mod8)
print(completion7.choices[0].message.content)

# next prompt
# Prompt 9
completion8 = openai.ChatCompletion.create(
  model="gpt-3.5-turbo",
  messages=[
    {"role": "user", "content": prompt9}
  ]
)

# prompt 9 response
print(mod9)
print(completion8.choices[0].message.content)

Here is the Google Colab script - https://colab.research.google.com/drive/1sB9Uy8OAxfwamDQv8h1p_21TYyCcVRbC?usp=sharing

Below is the Output of that script

/* see below */

Show me Variables with example?
Variables in Python are used to store values that we can use or manipulate in our programs. They are like containers that hold data.

Example:
Code:
x = 5
y = "Hello, World!"
print(x)
print(y)


The above code assigns the value "5" to the variable "x" and the value "Hello, World!" to the variable "y". We can use these variables later in our code to perform operations, display information, or manipulate data.

Output:

5
Hello, World!

Show me Strings with example?
Here are some examples of working with strings in Python:

1. Basic string declaration:
Code:
```python
msg = "Hello, World!"
```


2. String concatenation:
Code:
```python
name = "John"
message = "Hello, " + name + "!"
print(message)
```
Output:
```
Hello, John!
```


3. String formatting using placeholders:

Code:
```python
name = "John"
age = 30
message = "My name is {} and I'm {} years old.".format(name, age)
print(message)
```
Output:
```
My name is John and I'm 30 years old.
```


4. String formatting using f-strings (only works in Python3.6 and above):
Code:
```python
name = "John"
age = 30
message = f"My name is {name} and I'm {age} years old."
print(message)
```
Output:
```
My name is John and I'm 30 years old.
```


5. String slicing:
Code:
```python
message = "Hello, World!"
print(message[0])       # H
print(message[7:12])    # World
print(message[-1])      # !
```

6. String methods:
Code:
```python
message = "Hello, World!"
print(message.upper())      # HELLO, WORLD!
print(message.lower())      # hello, world!
print(message.replace("Hello", "Hi"))     # Hi, World!
```

These are just a few examples of what you can do with strings in Python. Hope it helps!

Show me Replace with example?
Here is an example of how to use the replace() function in Python:
Code:
string = "Python is fun"

# Replace "Python" with "Java"
new_string = string.replace("Python", "Java")

print(new_string)

# Output: "Java is fun"


In this example, we have a string "Python is fun" and we want to replace the word "Python" with "Java". We use the replace() method on the string object and pass in two arguments: the string to be replaced ("Python") and the new string to replace it with ("Java"). The method returns a new string with the replacement made, which we store in a new variable called new_string. Finally, we print out the new string to the console, which will output "Java is fun".
Show me Join with example?


Join is a string method in Python that is used to join any number of strings together. The syntax for the join method is:

string.join(iterable)

where string is the separator that is used to join the strings in the iterable.

For example:
Code:
# Joining a list of strings with a space separator
str_list = ["Hello", "world", "in", "Python"]
separator = " "
result = separator.join(str_list)
print(result)

Output:
Hello world in Python

# Joining a tuple of strings with a hyphen separator
str_tuple = ("first", "second", "third")
separator = "-"
result = separator.join(str_tuple)
print(result)

Output:
first-second-third


Show me Split with example?
Here's an example of how to use the split() function in Python:
Code:
```python
# Example 1: Splitting a string into a list of words
my_string = "Hello, this is a sentence."
words = my_string.split()
print(words)
# Output: ['Hello,', 'this', 'is', 'a', 'sentence.']

# Example 2: Splitting a string with a delimiter
my_string = "apple,banana,grape,orange"
fruits = my_string.split(",")
print(fruits)
# Output: ['apple', 'banana', 'grape', 'orange']

# Example 3: Limiting the number of splits
my_string = "apple,banana,grape,orange"
fruits = my_string.split(",", 2)
print(fruits)
# Output: ['apple', 'banana', 'grape,orange']
```

In Example 1, we split a sentence into a list of individual words using the split() function. By default, split() splits the string at every whitespace, so we don't need to provide any arguments.

In Example 2, we split a string of comma-separated values into a list of individual items using the split() function. We specify the delimiter as a comma within the split() function.

In Example 3, we limit the number of splits that can be made by the split() function by passing a second argument (in this case, the value 2). This splits the string into a maximum of 2 items.

Show me If Statements with example?
Here's an example of an if statement in Python:

Code:
```
age = 18
if age >= 18:
    print("You are an adult.")
```
[code]

In this example, we've used an if statement to check if the variable `age` is greater than or equal to 18. If it is, we print the message "You are an adult".

Here's another example that uses an if-else statement:

[code]

```
day = "Saturday"

if day == "Sunday":
    print("It's the weekend!")
else:
    print("It's a weekday.")
```

In this example, we've used an if-else statement to check if the variable `day` is equal to "Sunday". If it is, we print "It's the weekend!". Otherwise, we print "It's a weekday".

Show me Loops with example?
Loops are used to repeat a sequence of instructions multiple times. In Python, there are two types of loops: `for` loop and `while` loop.

1. For Loop:
For loop is used to repeat a block of code for a certain number of times. The syntax of the for loop is as follows:
Code:
```python
for variable in sequence:
    # code to be executed
```


Here `variable` is the iteration variable that takes the values from `sequence`. `sequence` is any iterable object such as a list, tuple or string.

Example:
Code:
```python
# printing the numbers from 0 to 4
for i in range(5):
    print(i)
 
# printing the elements of a list
fruits = ['apple', 'banana', 'mango']
for fruit in fruits:
    print(fruit)
```

Output:

```
0
1
2
3
4
apple
banana
mango
```

2. While Loop:
While loop is used to repeat a block of code as long as a certain condition is true. The syntax of the while loop is as follows:
Code:
```python
while condition:
    # code to be executed
```

Here, `condition` is any expression that evaluates to True or False. The loop iterates as long as the `condition` is True.

Example:
Code:
```python
# printing the numbers from 0 to 4 using while loop
i = 0
while i < 5:
    print(i)
    i += 1
 
# printing the elements of a list using while loop
fruits = ['apple', 'banana', 'mango']
i = 0
while i < len(fruits):
    print(fruits[i])
    i += 1
```

Output:

```
0
1
2
3
4
apple
banana
mango
```

These are the two basic loop structures in Python. There are also nested loops, break and continue statements that can be used within a loop.

Show me Nested Loops with example?
A nested loop is a loop within a loop in Python. It is used to iterate over a sequence of items for each item in another sequence. We can use nested loops to perform certain operations for each item in a sequence.

Let's take an example of nested loop in Python:
Code:
```
for x in range(1, 4):
    for y in range(1, 4):
        print(x, y)
```

In this example, we have a loop with the `range(1, 4)` which will iterate three times, starting from 1 and ending at 3. Within this loop, we have another loop which will also iterate three times for each iteration of the outer loop.

The output of this code will be:
Code:
```
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
```

Here, for each value of `x` in the outer loop, the inner loop will iterate three times and print the values of `x` and `y` together. This is an example of a simple nested loop in Python.

We can also use nested loops to perform certain operations or manipulate data for each item in a sequence. It is a powerful tool in programming that allows us to perform complex operations with minimal code.

Show me Functions with example?
Here are some examples of functions in Python:

1. Basic function
Code:
```
def greet(name):
    print("Hello, " + name + "!")

greet("Alice")
```

This `greet` function takes in a parameter `name` and prints out a greeting message with the passed-in name. When you call `greet("Alice")`, it will output: `Hello, Alice!`

2. Function with a return value

Code:
```
def multiply(x, y):
    product = x * y
    return product

num1 = 5
num2 = 10
result = multiply(num1, num2)
print(result)
```

In this example, the `multiply` function multiplies two numbers `x` and `y` and returns their product. When you call `multiply(num1, num2)` with values `5` and `10`, it will return `50`.

3. Function with optional parameters

Code:
```
def add_numbers(num1, num2, num3=0):
    sum = num1 + num2 + num3
    return sum

print(add_numbers(5, 10)) # output: 15
print(add_numbers(5, 10, 20)) # output: 35
```

This `add_numbers` function calculates the sum of two or three numbers, depending on if the optional `num3` parameter is included. When you call `add_numbers(5, 10)`, it will output `15`. And when you call `add_numbers(5, 10, 20)`, it will output `35`.

4. Recursive function
Code:
```
def factorial(n):
    if n == 1:
        return 1
    else:
        return n * factorial(n-1)

print(factorial(5)) # output: 120
```

This `factorial` function calculates the factorial of a given number `n` using recursion. When you call `factorial(5)`, it will output `120` as `5!` is equal to `5 * 4 * 3 * 2 * 1`.

Hope these examples help!
 
Last edited:
Back
Top