βοΈ The basis of the bot we've written can serve as the basis for many other projects. Especially those where multi-project capability is important.
For example, for generating WordPress websites.
For example, for generating WordPress websites.
π₯22
So, let's begin developing the ScriptWriter class.
How can we implement this task?
1. By using the API of one of the AI assistants, such as ChatGPT or Claude. We'll choose ChatGPT.
2. We'll also add the option to use the g4f library, which allows access to various AI models for free.
Let's start by installing the necessary packages:
How can we implement this task?
1. By using the API of one of the AI assistants, such as ChatGPT or Claude. We'll choose ChatGPT.
2. We'll also add the option to use the g4f library, which allows access to various AI models for free.
Let's start by installing the necessary packages:
pip install openai
pip install g4fπ₯14π1
In the ScriptWriter class, we have declared the execute method. This will be the central hub method where we build a pipeline to run other helper methods that we will write now.
To begin, let's decompose the tasks of the execute method into smaller steps.
We need to:
1. Accept the initial data in the method (a prompt for the request to the AI assistant).
2. Send a request to the AI assistant's API and save the response to a variable.
3. Write the result to a CSV file named script.csv and save it in the project folder.
To begin, let's decompose the tasks of the execute method into smaller steps.
We need to:
1. Accept the initial data in the method (a prompt for the request to the AI assistant).
2. Send a request to the AI assistant's API and save the response to a variable.
3. Write the result to a CSV file named script.csv and save it in the project folder.
π11π₯6
But before we develop the method, let's refine the class constructor.
What arguments should it accept besides
First, we need to pass the OpenAI API key. To do this, we'll accept an
We'll also set a
* I will provide the code a bit later
What arguments should it accept besides
project_folder?First, we need to pass the OpenAI API key. To do this, we'll accept an
api_key argument.We'll also set a
model parameter to specify the OpenAI model, with a default value of "gpt-4".* I will provide the code a bit later
π₯15
Once again, pay attention to the parameters in the class constructor.
β’ The parameters
β’ The parameter
β’ The parameters
project_folder and api_key are defined without default values. These are called required parameters, meaning they must be provided when creating an object.β’ The parameter
model has a default value ("gpt-4"). Such parameters are called optional. This means that if no value is provided for model when creating an object, the default value ("gpt-4") will be used.π₯11π2
Now, based on the arguments passed to the constructor, we will create attributes of the ScriptWriter class object.
1. First, weβll create an OpenAI client object (
According to the documentation of the openai-python library or the OpenAI API documentation, we can do this by passing the API key to the constructor of the OpenAI class.
2. Next, we will create an attribute for setting the model (
* Note that object attributes should be initialized after calling the parent class constructor (super function). We discussed this earlier in one of the previous posts.
1. First, weβll create an OpenAI client object (
self.client).According to the documentation of the openai-python library or the OpenAI API documentation, we can do this by passing the API key to the constructor of the OpenAI class.
2. Next, we will create an attribute for setting the model (
self.model) and assign it the value of the model argument.* Note that object attributes should be initialized after calling the parent class constructor (super function). We discussed this earlier in one of the previous posts.
π₯15
In the execute method, we will accept the
Next, we need to write a method to send a request to the OpenAI API and return the response. This method should also take the prompt as an argument.
We will name this method
prompt value as an argument.Next, we need to write a method to send a request to the OpenAI API and return the response. This method should also take the prompt as an argument.
We will name this method
generate_text and write it in the next post. For now, in the execute method, letβs simply call this method, pass the prompt as an argument, and store the result in the response variable.π8π₯6
Now let's write the method for generating text via the OpenAI API.
According to the documentation of the openai-python library or the OpenAI API documentation, the implementation of this method could be as follows.
1. In the
2. We assign the value of the
3. We place the value of the
4. The model returns a response object that contains the response options. We take the first item from the
According to the documentation of the openai-python library or the OpenAI API documentation, the implementation of this method could be as follows.
1. In the
generate_text method, we call the chat.completions.create method via self.client, which we created in the class constructor, to send a request to the model for text generation.2. We assign the value of the
self.model attribute to the model parameter of the chat.completions.create method.3. We place the value of the
prompt argument into a dictionary that describes the message for the model.4. The model returns a response object that contains the response options. We take the first item from the
choices list and extract the response text.π₯12π1
Now let's add the option to use
According to the documentation of the g4f library, the method for generating text can be the same as our
However, for g4f, we need to create a separate client, an instance of the Client class:
At any given time, the bot will work with only one client, so creating two clients simultaneously in the class constructor would be inefficient.
To avoid creating two clients at once in the class constructor, we will add a new parameter
Thus, the class constructor needs to be modified as follows.
gpt4free instead of the official API.According to the documentation of the g4f library, the method for generating text can be the same as our
generate_text method.However, for g4f, we need to create a separate client, an instance of the Client class:
self.client = Client()At any given time, the bot will work with only one client, so creating two clients simultaneously in the class constructor would be inefficient.
To avoid creating two clients at once in the class constructor, we will add a new parameter
use_g4f to the constructor and initialize only the necessary client based on the value of this parameter.Thus, the class constructor needs to be modified as follows.
π₯12
Now the generate_text method will use the
If the methods for OpenAI API and g4f had different logic, we would need to separate them to maintain the specific logic for each client.
In that case, we would need to implement different methods for working with each API. I'll show an example of this in the next post.
self.client chosen in the constructor. Note that the logic of the method for both OpenAI API and g4f is the same in our case.If the methods for OpenAI API and g4f had different logic, we would need to separate them to maintain the specific logic for each client.
In that case, we would need to implement different methods for working with each API. I'll show an example of this in the next post.
π₯13
The work on the ScriptWriter class is complete.
We just need to make some changes to the
Our course is coming to an end.
We just need to make some changes to the
main.py file to run and test this mode of the bot.Our course is coming to an end.
π₯7π1
Additional recommendations for the class code:
1. File paths will need to be used repeatedly and across different classes, so they can be made into object attributes and moved to the base class.
2. Where necessary, add exception handling using try-except.
3. Add logging (messages about the start and end of tasks or errors).
1. File paths will need to be used repeatedly and across different classes, so they can be made into object attributes and moved to the base class.
2. Where necessary, add exception handling using try-except.
3. Add logging (messages about the start and end of tasks or errors).
π9π₯3
After writing the ScriptWriter class, we need to prepare the input data (prompts).
One way to structure this data is to create a prompt builder based on Google Sheets.
1. Create a sheet for the prompt builder.
2. Create sheets with different types of prompts, where ready-made prompts from the prompt builder will be parsed.
3. Write a method to read data from Google Sheets.
4. Read the data depending on the sheet. Pass the required sheet as an argument to the method, for example, as shown here.
5. Return a dictionary or list of prompts from the method.
You will need to implement this on your own; we covered it in more detail and wrote the code in Lesson 4 of the Pinterest Money course.
For now, we'll choose another simple method as an example.
One way to structure this data is to create a prompt builder based on Google Sheets.
1. Create a sheet for the prompt builder.
2. Create sheets with different types of prompts, where ready-made prompts from the prompt builder will be parsed.
3. Write a method to read data from Google Sheets.
4. Read the data depending on the sheet. Pass the required sheet as an argument to the method, for example, as shown here.
5. Return a dictionary or list of prompts from the method.
You will need to implement this on your own; we covered it in more detail and wrote the code in Lesson 4 of the Pinterest Money course.
For now, we'll choose another simple method as an example.
π₯9π1
For now, weβll simply declare a list of prompts as an example.
Then, in the
1. Note that the
2. Through the named argument
Then, in the
run_script_writer function, we will create an instance of the ScriptWriter class, after which we will iterate over the prompts from the list using a for loop, and for each one, call the execute method.1. Note that the
Config.OPENAI_API_KEY argument contains the OpenAI API key, which was retrieved through environment variables from the .env file.2. Through the named argument
model, we specify the model, and through the named argument use_g4f, we indicate the use of the g4f library.π₯9π1
Main GPT models:
You can learn about the rest in the OpenAI API documentation.
gpt-4o, gpt-4o-mini, gpt-4-turbo, gpt-4, gpt-3.5-turboYou can learn about the rest in the OpenAI API documentation.
π₯9π1