Using CoPilot for a Python-tool
Introduction
When it comes to AI, I am a bit torn between being an impressed science and R&D-person - and a sceptical citizen and employee. However, it doesn't matter which hat you wear - understanding the technology and how to use it is important.
On this site I have described some of the basics behind LLMs - Large Lanuage Models - as well as how this relates to embedded programs - see Embedded AI.
Recently I have used Microsoft Copilot, inside Visual Studio Code, to generate a helper tool - implemented in Python. I am sharing some of this experience here.
The job
I needed to scan a number of SharePoint sites and generate a list of the files found - with as many meaningful atributes as possible. These were deducted from file names, file-extensions, file-path and the parent SharePoint site. I also needed to be able to update the list. The process ended up being as follows:
- Export the existing list from SharePoint to CSV
- Configure which sites to scan in a yaml-file. Within a given site, some meta-data were common and therefore written per site in the yaml file
- Run the scanner with users credentials (for security reasons) - generating metadata and performing various mappings
- The output is another CSV-file with scan results that are not in the originally exported CSV
- Take the output CSV and manually import bundles of 100-200 rows into SharePoint. This was incredibly slow until I found that reopening the SharePoint page for every import was a boost (but still slow).
All the above may be automated into an agent, but for now it runs on demand on the desktop, and I am a bit reluctant to let any "random" user run it.
What I learned
- Note that VS Code CoPilot is the editor extension, while GitHub CoPilot is the cloud service. The two are connected, but not the same. The CoPilot described here is the two together. I prompt in VS Code CoPilot, but the actual code generation happens in GitHub CoPilot's cloud service.
- As my readers will know from my books, I have written Python code before. However, in this project I never got around to writing a single line of Python. I did update the Yaml-files, but wrote no actual code.
- I was impressed with the high level of abstraction that was displayed. On earlier occasions I have written a lot of code - only to realize later that I could have used a library or two to make life easier. CoPilot proposed to use "Graph API" on its own (for walking a SharePoint tree) and used libraries like pandas, yaml, json, pathlib, argparse, requests and several more.
- The resulting program was very modular, high-level and readable. It was not as efficient as it could be; I suggested to bundle a scan and a filter phase and saved a lot of execution time - at the cost of minor added complexity.
- My main role was as tester. However, Copilot was also capable of running it's own tests of the code in a virtual environment. It never executed any scripts without asking for permission. Frankly - these test-execution lines were not easy to understand.
- Whenever CoPilot had changed the source-code, I was given the chance to see changes in the source - marked in green and red - and could press "Keep" or something else to discard - on file level as well as for everything at once.
- CoPilot never rested. If I had written the code myself, I would probably have skipped some "nice-to-have" features, but now I went further. On the first round I modified a lot of metadata in the CSV-file before doing the final import, but with CoPilot as a helper, "we" went the extra mile and created all the necessary mappings and translations needed to generate CSV-files ready for import.
- As usual I made it a rule to store everything in git before any rounds of changes.
- CoPilot is also very helpful when you write comments. You start a line and it offers a completion which is often better than what I had planned. This means that you can start pretty "open" - like "This function performs" - and then it updates.
- Even with all the AI-help from CoPilot, I still needed a bit of help from IT as I needed OAuth/MSAL authentication, and needed IT had to register an Entra app with Client and Tenant IDs. This is an entire topic by itself - not for now.
Conclusion
At LinkedIn and other places. people often say that using AI is like having a skilled junior developer to help you.
In this case it felt like I became the project-manager, user-representative and tester, while CoPilot was the senior developer.