Jump to content

ChatGPT: a reliable AI assistant for ProcessWire in the near future? Color me impressed already!


jacmaes
 Share

Recommended Posts

With all the hype about ChatGPT, I wanted to see how useful it could be for us ProcessWire lovers. In a word: wow! It's pretty darn impressive for such early public testing and for such targeted subject. I love how it proposes code with comments, then proceeds to explain it.

(Note: I've been getting more impressive and consistent results on mobile for some reason.)

Prompt: Filter blog post by category in ProcessWire

To filter blog posts by category in ProcessWire, you can use the find method of the $pages object, along with the category field and the equals operator, like this:

// Get the category slug from the URL
$category_slug = $input->urlSegment1;

// Find all blog posts in the given category
$posts = $pages->find("template=blog-post, category.slug=$category_slug");

// Loop through the posts and output their titles
foreach ($posts as $post) {
  echo "<h2>$post->title</h2>";
}

This code will find all blog posts that are in the category with the given slug, and output their titles. You can modify this code to output other information about the posts, such as their content, images, or dates.

To make it easier for users to filter posts by category, you can also create a navigation menu that lists all the available categories, like this:

// Find all categories
$categories = $pages->find("template=blog-category");

// Output a link for each category
echo "<ul>";
foreach ($categories as $category) {
  echo "<li><a href='/blog/$category->slug'>$category->title</a></li>";
}
echo "</ul>";

This code will find all blog categories and output a link for each one, using the category slug as the URL parameter. When a user clicks on a link, they will be taken to the blog page with the posts filtered by that category.

 

Prompt: Login form in ProcessWire

To create a login form in ProcessWire, you can use the following code:

// Check if form has been submitted
if ($input->post->submit) {
  // Sanitize user input
  $email = $sanitizer->email($input->post->email);
  $password = $sanitizer->text($input->post->password);

  // Check if user exists and password is correct
  $user = $users->get("email=$email");
  if ($user && $user->matches($password)) {
    // Login user
    $session->login($user);

    // Redirect to home page
    $session->redirect("/");
  } else {
    // Output error message
    $error = "Invalid email or password.";
  }
}

// Output login form
echo "<form method='post'>";
echo "<label>Email:</label>";
echo "<input type='email' name='email' value='$email'>";
echo "<label>Password:</label>";
echo "<input type='password' name='password' value='$password'>";
echo "<input type='submit' name='submit' value='Login'>";
echo "</form>";

// Output any errors
if ($error) {
  echo "<p class='error'>$error</p>";
}

This code creates a login form that accepts an email and password. When the form is submitted, it checks if a user with the given email exists, and if the password is correct. If the email and password are valid, it logs in the user and redirects to the home page. If the email and password are invalid, it outputs an error message.

 

Prompt: User registration class in ProcessWire with email, password of at least 10 characters and age over 18. Check if email is unique.

To create a user registration class in ProcessWire with email, password of at least 10 characters, and age over 18, you can use the following code:

class UserRegistration {

  public function register($email, $password, $age) {
    // Sanitize user input
    $email = $sanitizer->email($email);
    $password = $sanitizer->text($password);
    $age = (int) $age;

    // Validate user input
    if ($age < 18) {
      $errors[] = "You must be over 18 to register.";
    }
    if (strlen($password) < 10) {
      $errors[] = "Your password must be at least 10 characters long.";
    }
    if ($users->get("email=$email")) {
      $errors[] = "A user with this email already exists.";
    }

    // If no errors, create user account
    if (!$errors) {
      // Create new user
      $user = new User();
      $user->email = $email;
      $user->password = $password;
      $user->age = $age;

      // Save user to database
      $user->save();

      // Redirect to confirmation page
      $session->redirect("/confirmation");
    }
  }

}

This code creates a UserRegistration class with a register method that takes an email, password, and age as arguments. The method sanitizes and validates the input, and creates a new user if the input is valid. It also checks if a user with the given email already exists in the database, and outputs an error if one does.

To use this class, you would first instantiate it:

$registration = new UserRegistration();

Then, you can call the register method to create a new user:

$registration->register($email, $password, $age);

 

Prompt (similar to previous one): User registration form in ProcessWire with first name, last name, email, password of at least 10 characters and age over 18.

To create a user registration form in ProcessWire with first name, last name, email, password of at least 10 characters, and age over 18, you can use the following code:

// Check if form has been submitted
if ($input->post->submit) {
  // Sanitize user input
  $first_name = $sanitizer->text($input->post->first_name);
  $last_name = $sanitizer->text($input->post->last_name);
  $email = $sanitizer->email($input->post->email);
  $password = $sanitizer->text($input->post->password);
  $age = (int) $input->post->age;

  // Validate user input
  if ($age < 18) {
    $errors[] = "You must be over 18 to register.";
  }
  if (strlen($password) < 10) {
    $errors[] = "Your password must be at least 10 characters long.";
  }
  
  // If no errors, create user account
  if (!$errors) {
    // Create new user
    $user = new User();
    $user->first_name = $first_name;
    $user->last_name = $last_name;
    $user->email = $email;
    $user->password = $password;
    $user->age = $age;

    // Save user to database
    $user->save();

    // Redirect to confirmation page
    $session->redirect("/confirmation");
  }
}

// Output registration form
echo "<form method='post'>";
echo "<label>First Name:</label>";
echo "<input type='text' name='first_name' value='$first_name'>";
echo "<label>Last Name:</label>";
echo "<input type='text' name='last_name' value='$last_name'>";
echo "<label>Email:</label>";
echo "<input type='email' name='email' value='$email'>";
echo "<label>Password:</label>";
echo "<input type='password' name='password' value='$password'>";
echo "<label>Age:</label>";
echo "<input type='number' name='age' value='$age'>";
echo "<input type='submit' name='submit' value='Submit'>";
echo "</form>";

// Output any errors
if ($errors) {
  foreach ($errors as $error) {
    echo "<p class='error'>$error</p>";
  }
}

Watch out, this is addictive 😃

  • Like 12
Link to comment
Share on other sites

  • 2 weeks later...

Also tested it ... wow that's awesome. 

Phrase: how to add a new field to a processwire template via api

To add a new field to a ProcessWire template via the API, you can use the $fields->save() method. This method allows you to create a new field and add it to a template using a PHP script. Here is an example of how this could be implemented:

// Use the Wire class to access the ProcessWire API
$wire = Wire();

// Get the fields and templates modules
$fields = $wire->fields;
$templates = $wire->templates;

// Create a new field
$field = $fields->newField();

// Set the name and type of the field
$field->name = 'my_field';
$field->type = 'text';

// Save the field
$fields->save($field);

// Get the template to which you want to add the field
$template = $templates->get('my_template');

// Add the field to the template
$template->fields->add($field);

// Save the template
$template->save();


In the example above, we use the $fields->newField() method to create a new field, and then use the $fields->save() method to save the field. We also use the $templates->get() method to get the template to which we want to add the field, and then use the $template->fields->add() method to add the field to the template. Finally, we use the $template->save() method to save the template with the added field.

Note that this is just one way to add a new field to a ProcessWire template via the API. There may be other approaches that could work as well.

  • Like 1
Link to comment
Share on other sites

47 minutes ago, pwired said:

Is this for real ? Don't know if I should be surprised or worried. Are we one step closer where web designers/coders become obsolete ? I hear that ChatGPT is already in competition with iOS Swift developers ?

One step closer, yes. But realistically, that's still not very close at all. Someone always needs to enter those prompts, after all. And while I've heard some people say an AI will always create more secure code than a human being, I don't believe that at all. So any code created by an AI should always be double-checked by a human being. The code examples ChatGPT is giving us right now is only snippets. There's still a lot of manual work needed in creating, say, a new Fieldtype and Inputfield. I've asked ChatGPT to create a Fieldtype and Inputfield and it wasn't able to show me a fully working example that didn't need a lot of manual editing. It always "forgot" some things, like the constructor, or it used a wrong parameter signature. Of course this will get better, but the more complex your prompts are becoming, the higher the risk the code simply will not work out of the box.

The way I'm seeing it, AI is a great and valuable TOOL. It can save you from having to Google an answer for 30 minutes. It can give you some inspiration. But it's still just a tool, and for the foreseeable future, our jobs will be safe, because it's going to be US who will write the prompts and incorporate the code created. We shouldn't ignore it, and we need to learn to use it correctly. If all goes well, it might make our jobs a little easier, but shhh, don't tell our clients!

Seriously though, yes, it's probably going to change things in the long term. See it as an evolution of our jobs. It might make SOME jobs obsolete. But frankly, comparing the clean elegance of ProcessWire with the convoluted mess of some other web frameworks and content management systems that keep reinventing the wheel every six months, you could argue if some web developers maybe should become obsolete.

But finally, there's the issue that's been debated when it comes to art AI and Github Copilot already: copyright. AIs are always trained on existing material, and there will always be the risk that it simply lifts some code/art that has been released under an incompatible license. That's something a human will need to have an eye on, too.

  • Like 7
Link to comment
Share on other sites

Hi mattgs, reading your reply, your writing about it, I am sure that is how it is in the real world for a long time to come. Also your writing about it from an experienced coder, could only come from a real human and not from ChatGPT

  • Like 1
Link to comment
Share on other sites

20 hours ago, mattgs said:

AI is a great and valuable TOOL.

  • I agree. AI has nothing to do with computer's being intelligent or being able to do something on their own without human aid and intervention. Oxford dictionary for the word "intelligence":
    • "the ability to acquire and apply knowledge and skills"
  • A bunch of data in a database is not knowledge. What is in a human's head, that is "knowledge". Again, the Oxford dictionary says:
    • knowledge: "information, and skills acquired by a person through experience or education; the theoretical or practical understanding of a subject"
    • skill: "the ability to do something well; expertise"
  • "artificial" by the dictionary:
    • "made or produced by human beings rather than occurring naturally"
  • Artificial Intelligence by the same dictionary:
    • "the theory and development of computer systems able to perform tasks normally requiring human intelligence"

So how could one eliminate the "human" form Artificial Intelligence which – by definition – is strictly related to human beings? If we start using words and expressions which can also mean the opposite as well (or something else completely) then we instantly loose our ability to communicate. (That would be like on-the-fly redefining the commands and responses of a protocol in a computer system. The result would be a complete mess.)

Link to comment
Share on other sites

AI is great in producing text, that looks plausible, but it's not concerned with actual correctness. I'm working with elixir nowadays and there have been many examples posted, where responses claimed some API to exist in the stdlib, which just didn't. Looked totally fine on paper until you looked into the actual stdlib and found nothing. So really AI can be useful, but in the case of ChatGPT you really want someone chatting, who can actually validate the responses.

On 12/13/2022 at 8:30 AM, szabesz said:

If we start using words and expressions which can also mean the opposite as well (or something else completely) then we instantly loose our ability to communicate. (That would be like on-the-fly redefining the commands and responses of a protocol in a computer system. The result would be a complete mess.)

Languages evolve and that's not a new thing at all. Take for example computer, which used to be a human doing math, and now is a machine. There's a good reason why we don't use natural language for programming our – now machine – computers. They're messy and not at all strictly defined.

  • Like 5
Link to comment
Share on other sites

Machine learning is quite an amazing thing and I have recently looked at what happened with the Google Alpha Zero program using neural networks. In board games like Chess and Go the machine was given only the rules of the game and objective and then given huge amounts of historical games to learn from. It was also playing itself millions of times and learning from this. Apparently after 4 hours it became stronger than the strongest program (Stockfish) and plays in a radically different way compared to previous Non AI programs, even showing moves that seemed alien to human grand masters but when analysed were found to be previously undiscovered winning strategies. This completely amazed me. So I think that AI will be very powerful organising a database into very pertinent and relevant responses and will only get better and more accurate over time with the neural networks they are using now. Of course humans will intervene and make corrections but the program will "learn" from this.

  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...

Question: is processwire better than wordpress?

Answer: 

Quote

It's difficult to say definitively whether one content management system (CMS) is "better" than another, as the right choice depends on your specific needs and goals. Both ProcessWire and WordPress are popular CMS options with their own strengths and weaknesses.

ProcessWire is a flexible, open-source CMS that is well-suited for developers and web designers who want a high level of control over their site's structure and content. It has a strong focus on security and performance, and it is highly customizable, with a powerful API that allows developers to build custom functionality and integrations.

WordPress is also an open-source CMS that is widely used for building websites of all sizes and types. It is known for its user-friendly interface and large ecosystem of plugins and themes, which makes it relatively easy for non-developers to set up and manage a website. WordPress is suitable for a wide range of use cases, including blogs, e-commerce sites, and business websites.

Ultimately, the choice between ProcessWire and WordPress will depend on your specific needs and goals. If you are a developer or web designer who values flexibility and customization, ProcessWire may be the better choice. If you are a non-technical user who is looking for an easy-to-use platform with a large ecosystem of plugins and themes, WordPress may be a better fit.

 

  • Like 3
  • Thanks 1
Link to comment
Share on other sites

how friendly is the processwire community?

Quote

The ProcessWire community is generally known for being friendly and helpful. ProcessWire is an open-source content management system (CMS) that is used by developers and web designers to build and manage websites and web-based applications. The community includes a wide range of users, including developers, designers, and site administrators, who are all interested in using and improving the CMS.

There are several ways to get involved in the ProcessWire community and ask for help or advice. The ProcessWire forums are a good place to ask questions and get answers from other users. The ProcessWire documentation is also a helpful resource, with detailed guides and tutorials on using the CMS.

In addition, the ProcessWire community is active on social media platforms such as Twitter and Facebook, where users can share news, ask questions, and connect with other users. There are also several ProcessWire meetups and conferences held around the world, which provide an opportunity for users to connect in person and learn more about the CMS.

Overall, the ProcessWire community is a supportive and welcoming group of users who are passionate about the CMS and are willing to help others get the most out of it.

 

  • Like 3
  • Thanks 1
  • Haha 1
Link to comment
Share on other sites

9 hours ago, diogo said:

There are also several ProcessWire meetups and conferences held around the world, which provide an opportunity for users to connect in person and learn more about the CMS.

 

ChatGPT will teach us something ...

We really need a ProcessWire Conference 🤩

  • Like 4
  • Haha 1
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...