Github Co-Pilot -- Part 1
Image by VectorPortal.com ( https://vectorportal.com/ )

Github Co-Pilot -- Part 1

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.

  • Hello World Project - As with any other project, hello world is the right place to start. I started the Visual Studio code, created a folder hello and main.go. This was a very simple task for Co Pilot. I could compile and execute the code.

  • Scenario based code generation 1 - Simple CSV file as a reference - I have a csv file and I wanted to check if I can be presented with a the template for reading that file and printing out the contents. I wanted to be very specific as well, as a seasoned programmer I asked my Co-Pilot to use a struct. The important part is I did not show the file or have the file in my project.

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)
	}
}        

  • Scenario based code generation 2 - CSV file as a reference and mongodb as the destination. I gave the following to my Co-Pilot and this time I did not mention anything about struct.
  • Prompt : "userDataset.csv is a file having userName, userPassword and userRole. Generate a golang program to read the contents of this file and push the contents onto a local mongodb host, having a database called bot"
  • The following code was generated, well we have to install the mongodb libraries through go get

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.

Anindita Desarkar, PhD

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.

要查看或添加评论,请登录

Vishwanathan Raman的更多文章

  • Generating Assessments using Gen AI -- Part 2

    Generating Assessments using Gen AI -- Part 2

    This is in continuation to the article https://www.linkedin.

    3 条评论
  • Generating Assessments using Gen AI

    Generating Assessments using Gen AI

    Assessment Generation has always been a task of a SME. It is a complex and time consuming task of both Development &…

    5 条评论
  • Flutter/Dart, Gemini API

    Flutter/Dart, Gemini API

    #ignAIte, #AI, #GenAI The past few weeks have been crazy, relearning a lost knowledge and then creating something fun…

    7 条评论
  • Github Co-Pilot -- Part 3

    Github Co-Pilot -- Part 3

    This is a continuation to my earlier article I am just starting from the place I left off. Its time for some complex…

    4 条评论
  • Github Co-Pilot -- Part 2

    Github Co-Pilot -- Part 2

    This is a continuation to my earlier article Scenario based code generation 3 - CSV file as a reference and mongodb as…

    2 条评论
  • Gen AI -- Quantised Models -- llama.cpp -- Long Weekend Project Part 3

    Gen AI -- Quantised Models -- llama.cpp -- Long Weekend Project Part 3

    Here are the links to the my earlier articles first article, second article on building a Gen AI solution on the…

    3 条评论
  • Gen AI -- Long Weekend Project Part 2

    Gen AI -- Long Weekend Project Part 2

    Here is the followup to my first article I remember the phrase "When the GOING gets tough the TOUGH gets going" and…

    2 条评论
  • Gen AI Weekend Project

    Gen AI Weekend Project

    The World Cup is on. The match against PAK on 14-Oct was anti-climax, maybe our Indian team was too good for the other…

    8 条评论
  • Lessons learnt using Golang and MongoDB

    Lessons learnt using Golang and MongoDB

    This past quarter I have been experimenting with Golang and this past week in particular, my downtime from work, I have…

  • Golang Quirkiness

    Golang Quirkiness

    Over last few days I have been fiddling with Golang and was quick to realize that it was not for faint heart…

社区洞察

其他会员也浏览了