Github Co-Pilot -- Part 1
Vishwanathan Raman
6 Patents filed on AI/Gen AI||M.Tech (BITS Pilani)||Technology Leader||Technical Author||Story Teller||AI Strategist||Trainer||Programmer
Co-Pilot is an interesting topic within the Gen AI landscape and one of the most widely adopted areas. A report by McKinsey states that such tools can speed up a developer’s code generation by as much as 50 percent, an awesome jump in productivity. I can relate to this, since as programmers we spend quite a bit of time coding and debugging programs which also involves learning and unlearning new concepts. An extra pair of eyes that can help speed up, optimise, recommend or debug is the need of the hour. This article is a summary of my experiments on Github Co-Pilot, I have not delved into areas of intellectual property which is an important topic as well.
Here are some of the quick pointers (definitely not exhaustive) on where the Co-Pilot can come really handy
1) Jump starters - More than often than not we fallback on content in the internet space to get started. I have been there looking for code snippets to get a jumpstart for e.g. updating a data point within the pandas dataframe in python.
2) Debug - I am sure as programmers we have spent gruelling amount of time debugging code snippets. Sometimes it's so obvious yet you cannot solve it. Here is one such experience that I documented sometime back in 2020 on golang and mongodb.
3) Recommendations - This is definitely an area to watch out for and is a real value add of having a Co-Pilot. Given a code base is there a better way to rewrite it leading to optimal execution and then programmers are given the responsibility of choosing the right recommendation.
4) Test Case Generation - Auto generation of test cases helps save a lot of programmers time.
5) Documentation
First things first, Github Co-Pilot is not free, it has to be subscribed and comes with a 30 day free trial. I subscribed to an individual plan at 10$. There is an extension with the Visual Studio Code which is neat. I choose golang as the topic of the experiment. I have tons of golang code that I have written in the recent past, some of them simple and a lot of them complex involving mongodb, they are from my pet projects.
Let's get started. I use mac m2 so the keyboard keys that I use may be a little different from the windows.
领英推荐
The following code got generated and I executed the code after placing the CSV file in my project folder. The code worked like a charm.
package main
import (
"encoding/csv"
"fmt"
"os"
)
type User struct {
UserName string
UserPassword string
UserRole string
}
func main() {
// Open the CSV file
csvFile, err := os.Open("userDataset.csv")
if err != nil {
fmt.Println(err)
}
defer csvFile.Close()
// Parse the CSV file
csvReader := csv.NewReader(csvFile)
csvData, err := csvReader.ReadAll()
if err != nil {
fmt.Println(err)
}
// Create a slice of User structs
var users []User
// Iterate through each line of the CSV file
for _, line := range csvData {
// Create a new User struct
user := User{
UserName: line[0],
UserPassword: line[1],
UserRole: line[2],
}
// Append the User struct to the slice
users = append(users, user)
}
// Print the contents of the slice
for _, user := range users {
fmt.Printf("UserName: %s, UserPassword: %s, UserRole: %s\n", user.UserName, user.UserPassword, user.UserRole)
}
}
package main
import (
"context"
"encoding/csv"
"fmt"
"log"
"os"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type User struct {
UserName string
UserPassword string
UserRole string
}
func main() {
// Open the CSV file
csvFile, err := os.Open("userDataset.csv")
if err != nil {
log.Fatal(err)
}
defer csvFile.Close()
// Parse the CSV file
csvReader := csv.NewReader(csvFile)
csvData, err := csvReader.ReadAll()
if err != nil {
log.Fatal(err)
}
// Create a slice of User structs
var users []User
// Iterate through each line of the CSV file
for _, line := range csvData {
// Create a new User struct
user := User{
UserName: line[0],
UserPassword: line[1],
UserRole: line[2],
}
// Append the User struct to the slice
users = append(users, user)
}
// Set client options
clientOptions := options.Client().ApplyURI("mongodb://localhost:27017")
// Connect to MongoDB
client, err := mongo.Connect(context.Background(), clientOptions)
if err != nil {
log.Fatal(err)
}
// Check the connection
err = client.Ping(context.Background(), nil)
if err != nil {
log.Fatal(err)
}
fmt.Println("Connected to MongoDB!")
// Get a handle for the users collection
usersCollection := client.Database("bot").Collection("users")
// Insert the users into the collection
for _, user := range users {
_, err := usersCollection.InsertOne(context.Background(), user)
if err != nil {
log.Fatal(err)
}
}
fmt.Println("Users inserted into MongoDB!")
// Disconnect from MongoDB
err = client.Disconnect(context.Background())
if err != nil {
log.Fatal(err)
}
fmt.Println("Disconnected from MongoDB!")
}
After executing the mongodb libraries using go get, I ran the code go run main.go. Well it worked like a charm. I must confess I am impressed. I had spent quite a bit of time a few years back writing this code to input a bunch of files in mongodb collection. This literally automates all of the work in a jiffy leaving me to focus only the critical parts which is the logic behind the app. Here is a snapshot of my mongodb, it has a database called bot and the collection is Users. Sweet.
This definitely deserves a lot more attention and I am a lot more excited now with the findings. As I was going through my old code base written back in 2020, I was sulking as I had to relearn what I had written and I thought it will be an arduous task but now I am feeling a lot better. A few more posts will be coming in next few days where I will explore the other features of Github Co-Pilot. I believe I have simply scratched the surface.
Happy learning. Keep experimenting. Keep pushing yourself to learn more and you will find wonders. Please do share your experiences with Github Co-Pilot or any other system that you have interacted with. I also had a great experience with PaLM 2 as well.
PhD in CSE (JU) || Product Owner || Gen AI Practitioner || Director @LTIMindtree|| Dedicated Researcher in Data Science, Gen AI || Mentor || Patents on AI/DS/Gen AI
1 年Great one Vishwa! Will definitely save lots of programmers time. Hope the reverse way is also possible like provide a codebase and generate the test cases.