微軟用·chatGPT 4· 對(duì)·github copilot X·升級(jí)后,本是懷著贈(zèng)熱點(diǎn)的心態(tài)測試了一下其功能。但 Copilot 智能化程度之高,令我吃驚,兩周下來已離開不這個(gè)工具了。 下面簡單分享一下其使用過程,以及對(duì)如何使用好這個(gè)工具的個(gè)人看法.
1. 安裝與注冊(cè)
IDE開發(fā)環(huán)境我使用的是 VSCode 與 Visual Studio2022
,通過在擴(kuò)展組件中搜索 github copilot
,點(diǎn)擊安裝即可。 安裝完成后,右下角出現(xiàn)copilot 的圖標(biāo),提醒注冊(cè)。
copilot主頁也有注冊(cè)入口:https://github.com/features/copilot
注冊(cè)前需要準(zhǔn)備好信用卡,前60天免費(fèi),之后是10$/月。
注冊(cè)完成后,右下角的圖標(biāo)如下,表示已連接成功
2. 測試生成 Python 與 C++ 代碼的效果
使用 Copilot ,就是用注釋語法寫出編程要求,可以要求 Copilot 創(chuàng)建1個(gè)函數(shù),1個(gè)類,或者是語句塊,但要寫清楚要求。如果函數(shù)簡單,就一句話寫明要求,如果復(fù)雜,可以寫大致流程簡單寫出來。 如下
輸入注釋語句:
# define a function to handle image by opencv, including operations: read image, resize image, and draw a circle and put text on image, restore image into a new file.
回車后,copilot 開始在后臺(tái)匹配最佳代碼,按 Tab鍵,即彈出淡色字體的建議,再按 Tab鍵即輸入屏幕。
Copilot 對(duì)1個(gè)功能要求,會(huì)提共10條建議,按 alt + ]
為下條,alt + [
為上一條。 也可以打開Copilot窗口再選擇合適的建議雙擊。
如上面的語句生成的代碼如下:
def handle_image(filename):
img = cv2.imread(filename)
# resize image to 1/4 of original size
img = cv2.resize(img, (0,0), fx=0.25, fy=0.25)
# draw a circle on image
cv2.circle(img, (100, 100), 50, (0, 255, 0), 3)
# put text on image
cv2.putText(img, "Hello, I am PyPy", (100, 200), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
# save image to a new file
cv2.imwrite('newimage.jpg', img)
更復(fù)雜一些的例子,copilot會(huì)自動(dòng)進(jìn)行分析注釋語句。
# define a interface named "Shape" including 2 methods: calc_area, calc_perimeter, inherited from abc.ABC class, inherite by two classes: "Rectangle" and "Circle"
from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def calc_area(self):
pass
@abstractmethod
def calc_perimeter(self):
pass
# generate two subcalsses of Shape: Rectangle and Circle
class Rectangle(Shape):
def __init__(self, width, height):
self._width = width
self._height = height
#generate a property named "width"
@property
def width(self):
return self._width
@width.setter
def width(self, width):
self._width = width
#generate a property named "height"
@property
def height(self):
return self._height
@height.setter
def height(self, height):
self._height = height
def calc_area(self):
return self.width * self.height
def calc_perimeter(self):
return 2 * (self.width + self.height)
# subclass Circle
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def calc_area(self):
return 3.14 * self.radius * self.radius
def calc_perimeter(self):
return 2 * 3.14 * self.radius
注冊(cè)時(shí)我設(shè)置了“允許copilot 學(xué)習(xí)我的代碼” 選項(xiàng),所以copilot分析輸入要求時(shí),會(huì)優(yōu)先將我自己曾以寫過代碼推薦出來,也蠻方便的。
# generate a function for asyncio websockets client
async def ws_client(url):
for i in range(1, 40):
async with websockets.connect(url) as websocket:
await websocket.send("Hello, I am PyPy.")
response = await websocket.recv()
print(response)
time.sleep(1)
如果不想讓 copilot 學(xué)習(xí)自己的代碼,或者用戶項(xiàng)目,可以登錄 github 帳號(hào)在setting中,將該功能關(guān)掉。
下面再看生成C++代碼的測試
先輸入要求:
// generate a function to sort an array
// of integers in ascending order
// using the bubble sort algorithm, the array is stored as gloabl variable
// and the function is called in main
Copilot 窗口馬上給出了完整的代碼, 選擇accept suggestion
#include <iostream>
using namespace std;
const int SIZE = 10;
int numbers[SIZE] = { 10, 2, 78, 4, 45, 32, 7, 11, 0, 66 };
void sortArray(int [], int);
void showArray(int [], int);
int main()
{
cout << "The unsorted array is:";
showArray(numbers, SIZE);
sortArray(numbers, SIZE);
cout << "The sorted array is:";
showArray(numbers, SIZE);
return 0;
}
void sortArray(int array[], int size)
{
int temp;
bool swap;
do
{
swap = false;
for (int count = 0; count < (size - 1); count++)
{
if (array[count] > array[count + 1])
{
temp = array[count];
array[count] = array[count + 1];
array[count + 1] = temp;
swap = true;
}
}
} while (swap);
}
void showArray(int array[], int size)
{
for (int count = 0; count < size; count++)
cout << array[count] << " ";
cout << endl;
}
結(jié)果還是可以接收的。C++寫代碼的速度快了不少。
使用技巧:
1)生成函數(shù),寫要求時(shí),要寫明函數(shù)主要功能,參數(shù),返回值。
# generate a function to do XXXX, parameters include x: int, y:float, return the result z: float.
2)生成類,應(yīng)寫從哪個(gè)類繼承,擁有哪些屬性,方法,構(gòu)造方法要輸入的參數(shù)等,屬性是否要?jiǎng)?chuàng)建getter, setter等。 如
# create a stack class inheriting deque, override append and pop methods, both operation are done at left side, own atttibutes: length, also generate setter and getter methods
# init method include a len parameter, default value is 10
對(duì)前端Javascript, CSS, Vue.js
代碼的支持似乎比python還要好。 最近在開發(fā)1個(gè)MQTT--WebSocket
網(wǎng)絡(luò)協(xié)議網(wǎng)關(guān),在Copilot 的幫助下,順便把前端vue測試頁面寫好了。
對(duì)于 Copilot 對(duì)程序員影響的感想
兩周使用下來,總的體驗(yàn),Copilot 非常適合生成函數(shù)框架,類框架,自動(dòng)化測試代碼等,對(duì)于能用文字表述清楚的任務(wù),生成的代碼質(zhì)量可以接受,關(guān)鍵是效率很高。
再看一下不足,對(duì)于復(fù)雜任務(wù),還是無法勝任,如線程間通信。其次,對(duì)比較新的技術(shù)點(diǎn),Copilot 給出的建議往往不合適。也發(fā)現(xiàn)其對(duì)于中文的理解,不如英文好。 程序員們看來還要學(xué)好英文啊。文章來源:http://www.zghlxwxcb.cn/news/detail-460474.html
Copilot做為編程AI輔助工具,對(duì)于日常工作效率的提升,效果還是明顯的。用好這個(gè)工具,可以讓程序員騰出更多時(shí)間來思考系統(tǒng)架構(gòu)、接口、流程、算法、用戶交互體驗(yàn)、整合等方面的工作。如果不使用類似的AI工具,時(shí)間長了,真可能真的會(huì)落后于同行。所以,還是建議熟悉并積極應(yīng)用此工具。文章來源地址http://www.zghlxwxcb.cn/news/detail-460474.html
到了這里,關(guān)于測試了Copilot輔助編程后,就離不開這個(gè)AI工具了的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!