we give ChatGPT full access to the computer

<python>
#Какой-то код...
</python>

To obtain program code from text, you can use, for example, regular expressions.

So, we have a line of code… What should we do with it? Should I use the exec() function? But sometimes we will need to get exactly the result of executing the code: the user may ask, for example, to tell him information about the battery charge, the current date, or the exchange rate.

I'm afraid my decision will confuse many experienced developers, however, as I already said, I do not claim to be a Middle Python Senior Super Duper Developer. My goal is to show my idea, and if it catches someone’s attention, they may be able to find a more correct and beautiful way to implement it.

So… drum roll…

We ask the neural network to format its code to look like this:

def answer():    
  #какой-то код     
  return "Ответ"

We write the code to a file. In my case it is execute.py. We import it in advance at the very beginning. And then, when the file has changed, we re-import the module using the importlib library. Then we call the answer() function and get the result of its execution. It all looks something like this:

with open("execute.py", "w", encoding='utf-8') as file:         
  file.write(code)
importlib.reload(execute)
result = execute.answer()

And despite all the “crutch” of this solution, it works and works as I intended.

Bottom line

Conclusions

  • It turned out to be an interesting experiment. The neural network writes the code itself and will execute it. ChatGPT literally gained access to our PC.

  • On the one hand, the assistant turned out to be quite flexible. He navigates the system well. On the other hand, sometimes the result may not turn out exactly as we expected.

  • Perhaps code examples can be added to the initial prompt. This can improve the accuracy of your answers.

Is it safe?

Definitely NO. A neural network gives inaccurate answers, and therefore there is always a chance that it will accidentally delete, create or change something wrong. This project is nothing more than an experiment, and therefore you should not immediately run and try to install this creation on your PC. However, this is no longer my business – I warned you.

Project on GitHub so you can laugh at the quality of my code with me:

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *