Bard is coming along

Maurizio Berti maurizio.berti at gmail.com
Fri May 5 05:05:19 BST 2023


Hi David,
I'm not really sure about what you were trying to accomplish; if you were
just doing it for fun or to understand about its capabilities, that's it:
it's fun and interesting.

But if you were hoping to get Bard or GPT to be able to write valid PyQt
(and even Python) code, the issues you're listing are well known, and they
are one of the reasons for which ChatGPT has been banned from StackOverflow
(besides the fact that many people tried to use AI to provide answers and
get reputation without considering the consequences of those poorly pasted
answers).

These current AI models are not able to write thoughtful code: they do not
really know anything about python or PyQt; and by "know" I mean it in the
*awareness* sense.
They are able, based on their "training" to write limited sequences of
concepts within a specified language interface, but they lack many complex
aspects required by programming, especially since they have been trained
using large model data that rewards more longer content than *valid*
content. It's known <https://ai.stackexchange.com/a/38663> that ChatGPT was
trained using StackOverflow posts, but that means that it uses *all* posts,
no matter the quality of their code.
Note that this is also valid for "natural" human languages to some extent:
the main difference is that most training data was natural language, so
there's a larger data pool (which theoretically makes it more accurate, at
least for language rules, grammar, syntax, etc), but when going into the
semantics, there are problems, inconsistencies, inaccuracies and
unreliability.

Poorly put (but note that this is an *extreme* approximation), it
"statistically" writes word by word, based on the previous context (what
was asked and what was previously written by it) and followed on by what
its training suggests it. There's little if none hierarchical and
structured thinking, which is what real programming normally requires.
The more you go into niche and complex subjects, including context,
correlation and code design, the more invalid the answer is.

The main purpose of these AIs is to provide well written answers, not
*valid* ones.
It has a lot of knowledge, but it's not really capable of intelligently
putting it together and *validate* it.
It's almost like asking an intern to go to the library and do research on a
subject they know nothing about: they will probably get you lots of useful
material, but they have not become experts in that subject, they probably
correlated data inconclusively, and you'll need to check their research on
your own anyway.

Interestingly enough, it's not too different from what I've seen in many
Youtube tutorials, where their authors create content with little awareness
and almost no experience of the tools they're using. They find some
toolkit, play around with it for a few hours (days at best), do some
superficial research on the web to get some tutorial-aimed code working,
and then release a video with poorly written code (often with typos that
are rarely checked or mentioned), which is full of bad practices and
terrible suggestions, like using hardcoded geometries without layout
managers, or accessing the UI from external threads.

In fact, as you already found out, the given code is incomplete to begin
with.
But that's not the only issue.
While your prompt was respected, conceptually speaking the dialog should be
a separate class on its own, eventually called by signals or events.
Monkey patching event handlers is also often discouraged, and keyPressEvent
should have been explicitly overridden instead. An even better solution
would be to use QShortcut instead (but the AI doesn't consider that at
first, as it has no "creative" nor "mindful" consideration of what it's
doing).
Also, as you mentioned too, there are issues with the new PyQt6 flags, but
that's understandable: even us humans still struggle with them :-)

As impressive as this technology is, it's still not ready for these
purposes, at least right now.
Under careful human scrutiny, it can be a quite useful tool (a friend of
mine already uses it for some basic js automations and repeated basic code
snippets), but there's not much more than that, yet.
We need to wait for *a lot* of further AI generations before that point,
and, most importantly, further "layers" of concepts, context and awareness.

Note that by "a lot" I don't necessarily mean decades. It will probably
happen within the next few years. Especially considering that the evolution
of AI also means that it could improve itself on its own, and that also
means a recursive and exponential improvement: it's already happening :-)

By the time Qt7 EOL will arrive, we might have reached the point at which
we won't really need to care about Qt8...

Cheers,
MaurizioB

Il giorno mer 3 mag 2023 alle ore 21:29 David Cortesi <davecortesi at gmail.com>
ha scritto:

> On further conversation with Bard about this code I got it to add the
> following, to make the search dialog visible:
>
>         # Connect the shortcut
>>         self.keyPressEvent = self.on_key_press
>>
>>     def on_key_press(self, event):
>>         if event.modifiers() & QtCore.Qt.ControlModifier and event.key() == QtCore.Qt.Key_F:
>>             self.search_replace_dialog.show()
>>             return True
>>         else:
>>             return super().keyPressEvent(event)
>>
>>
>>
> On Wed, May 3, 2023 at 12:15 PM David Cortesi <davecortesi at gmail.com>
> wrote:
>
>> I got my invite to try Google Bard, and after asking it some general
>> questions in other areas I tried its skill at writing PyQt code. Here is
>> one question and its response. The first two lines of the prompt were
>> suggested earlier when I was asking it about code prompts in general.
>>
>> python=3.10
>>> qt=6.5
>>> Write a Python subclass of QPlainTextEdit that includes methods for
>>> implementing search and replace
>>>
>> Sure, here is a Python subclass of QPlainTextEdit that includes methods
>>> for implementing search and replace:
>>> Python
>>>
>>> import sysfrom PyQt6.QtWidgets import QPlainTextEdit
>>> class SearchReplacePlainTextEdit(QPlainTextEdit):
>>>     def __init__(self):
>>>         super().__init__()
>>>
>>>         # Create the search and replace dialog
>>>         self.search_replace_dialog = QDialog()
>>>         self.search_replace_line_edit = QLineEdit()
>>>         self.replace_line_edit = QLineEdit()
>>>         self.case_sensitive_checkbox = QCheckBox("Case sensitive")
>>>         self.whole_words_checkbox = QCheckBox("Whole words")
>>>         self.search_button = QPushButton("Search")
>>>         self.replace_button = QPushButton("Replace")
>>>         self.replace_all_button = QPushButton("Replace all")
>>>
>>>         # Layout the dialog
>>>         layout = QVBoxLayout()
>>>         layout.addWidget(self.search_replace_line_edit)
>>>         layout.addWidget(self.replace_line_edit)
>>>         layout.addWidget(self.case_sensitive_checkbox)
>>>         layout.addWidget(self.whole_words_checkbox)
>>>         layout.addWidget(self.search_button)
>>>         layout.addWidget(self.replace_button)
>>>         layout.addWidget(self.replace_all_button)
>>>         self.search_replace_dialog.setLayout(layout)
>>>
>>>         # Connect the buttons
>>>         self.search_button.clicked.connect(self.on_search)
>>>         self.replace_button.clicked.connect(self.on_replace)
>>>         self.replace_all_button.clicked.connect(self.on_replace_all)
>>>
>>>     def on_search(self):
>>>         # Get the search text
>>>         search_text = self.search_replace_line_edit.text()
>>>
>>>         # Get the replace text
>>>         replace_text = self.replace_line_edit.text()
>>>
>>>         # Set the search flags
>>>         search_flags = QTextDocument.FindFlag.NoHighlight
>>>         if self.case_sensitive_checkbox.isChecked():
>>>             search_flags |= QTextDocument.FindFlag.CaseSensitive
>>>         if self.whole_words_checkbox.isChecked():
>>>             search_flags |= QTextDocument.FindFlag.WholeWords
>>>
>>>         # Find the text
>>>         self.find(search_text, search_flags)
>>>
>>>     def on_replace(self):
>>>         # Get the search text
>>>         search_text = self.search_replace_line_edit.text()
>>>
>>>         # Get the replace text
>>>         replace_text = self.replace_line_edit.text()
>>>
>>>         # Replace the text
>>>         self.replace(search_text, replace_text)
>>>
>>>     def on_replace_all(self):
>>>         # Get the search text
>>>         search_text = self.search_replace_line_edit.text()
>>>
>>>         # Get the replace text
>>>         replace_text = self.replace_line_edit.text()
>>>
>>>         # Replace all instances of the search text with the replace text
>>>         self.replaceAll(search_text, replace_text)
>>>
>>>
>>> Use code with caution. Learn more <https://bard.google.com/faq#coding>
>>>
>>> This code creates a subclass of QPlainTextEdit that includes methods for
>>> implementing search and replace. The search and replace dialog has the
>>> following features:
>>>
>>>    - A search text box
>>>    - A replace text box
>>>    - A case sensitive checkbox
>>>    - A whole words checkbox
>>>    - A search button
>>>    - A replace button
>>>    - A replace all button
>>>
>>> When the user clicks the search button, the text in the search text box
>>> is searched for in the text editor. If the text is found, the cursor is
>>> moved to the found text. When the user clicks the replace button, the text
>>> in the replace text box is substituted for the found text. When the user
>>> clicks the replace all button, all instances of the search text are
>>> replaced with the replace text.
>>>
>>> I hope this is what you are looking for. Let me know if you have any
>>> other questions.
>>>
>> It looks good but when I run it only the edit widget is visible. Not sure
>> how it messed up the layout so the buttons etc. do not show.
>>
>> Still, Bard gives a decent starting point.
>>
>>
>

-- 
È difficile avere una convinzione precisa quando si parla delle ragioni del
cuore. - "Sostiene Pereira", Antonio Tabucchi
http://www.jidesk.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20230505/1a0ce868/attachment-0001.htm>


More information about the PyQt mailing list