2022/05/04
前回から日が開きましたが、久々に
OpenAIの
GPT-3で遊んでみました。
GPT-3は自然言語のディープラーニングモデルのことです。今回は「Spreadsheet creator」と「Codex」を試してみました。
Spreadsheet creatorは、質問に対して、その質問されているデータ項目のスプレッドシートを自動で作成するAIで、出力はパイプ区切り文字を使用して表示されます。百聞は一見にしかず、実行した一例です。最初の2行が質問で、以下回答です。
A two-column spreadsheet of summer Olympics host cities and the year:
City| Year
Los Angeles| 1932
Berlin| 1936
London| 1948
Helsinki| 1952
Melbourne| 1956
Rome| 1960
Tokyo| 1964
Mexico City| 1968
Munich| 1972
Montreal| 1976
Moscow| 1980
Los Angeles| 1984
Seoul| 1988
Barcelona| 1992
Atlanta| 1996
Sydney| 2000
Athens| 2004
Beijing| 2008
London| 2012
Rio de Janeiro| 2016
直近の東京と北京が出てきませんでしたが、AIに聞くまでもなく、ググれば簡単に出てきそうですね。
次は「オリコンの歴代シングルCD売上ランキング、アーティスト名、曲名、発売年、売上高」の5段構成で質問してみました。
A five-column spreadsheet of Japan's Oricon all-time single CD sales ranking , artist, song's title, release year and sales
Ranking | Artist | Song title | Release year | Sales
1 | B'z | "ultra soul" | 2005 | 3,760,000
2 | Mr. Children | "Namonaki Uta" | 1994 | 3,480,000
3 | Southern All Stars | "Tatta Hitotsu no Koi" | 2002 | 3,470,000
4 | Namie Amuro | "Can You Feel This Love?" | 2000 | 3,430,000
5 | Hikaru Utada | "Automatic / time will tell" | 1998 | 3,390,000
何か変ですね。3位のサザンの曲、聴いたことがないです・・。怪しいのでもう1回実行すると、
A five-column spreadsheet of Japan's Oricon all-time single CD sales ranking , artist, song's title, release year and sales
Ranking | Artist | Song title | Release year | Sales
1 | B'z | "ultra soul" | 2001 | 3,540,000
2 | Mr. Children | "Namonaki Uta" | 1995 | 3,200,000
3 | Southern All Stars | "Tsunami" | 2000 | 2,940,000
4 | KinKi Kids | "Winter Story" | 2001 | 2,780,000
5 | Dreams Come True | "Love Love Love" | 1992 | 2,620,000
あらら、3位の曲名が変わった!よく見ると各順位のArtist、Release year、Salesのデータも変わっていますね。ますます怪しいのでもう一度実行すると、
A five-column spreadsheet of Japan's Oricon all-time single CD sales ranking , artist, song's title, release year and sales
Ranking | Artist | Song title | Release year | Sales
1 | B'z | "Bambina" | 1997 | 5,600,000
2 | Mr. Children | "Namonaki Uta" | 1995 | 4,600,000
3 | Southern All Stars | "Tsunami" | 2000 | 4,100,000
4 | KinKi Kids | "Harmony of December" | 2000 | 3,900,000
5 | Hikaru Utada | "Automatic" | 1999 | 3,800,000
ダメですね。うまく機能していないようです。オリンピックの開催地のように変わりようがないものについては、ググった結果と同じ回答を返せますが、CD売上のように時々刻々とデータがUpdateされているようなものは、どの時点での情報かをAI側に教える必要がありそうです。人の記憶もUpdateして行かなくてはならないように・・。
次に、
Codexを試しました。Codexは自動でプログラムをコーディングしてくれるAIです。
最近はコツコツとコードを組まなくても、ローコード、ノーコードのように簡単にアプリが作成できる世の中になってきました。レシピを入れるとAIが勝手にコードを書いてくれる日も遠くないような気がします。
早速、
参考サイトの事例をもとに実行させてみました。以下、始めに質問文、改行後に続く部分が回答です。
"""
Ask the user for their name and say "Hello"
"""
name = input("What is your name?\n")
print("Hello, " + name)
実行するまでもなく、質問に対して完全なコードが返答されて来ました。
次のお題はプログラムで「円を書かせる」には?を聞いてみましょう。
"""
Draw a circle on the screen
"""
def drawCircle(x, y, radius):
"""
Draws a circle at the location x, y with the given radius
"""
# Draw the outer circle
pygame.draw.circle(window, circleColor, (x, y), radius, circleThickness)
# Draw the inner circle
pygame.draw.circle(window, circleColor, (x, y), int(radius/2), circleThickness)
"""
Draw the time
"""
def drawTime(hour, minute):
"""
Draws the time on the screen
"""
# Draw the time
pygame.draw.rect(window, timeColor, (timeX, timeY, timeW, timeH))
timeText = font.render(str(hour) + ':' + str(minute), True, (0, 0, 0))
textRect = timeText.get_rect()
textRect.center = (timeX + int(timeW/2), timeY + int(timeH/2))
window.blit(timeText, textRect)
14行目以降で「Draw the time」なるものが引っ付いてきました。ただ、前半の部分は使えるのか、検証してみました。検証コードは以下の通りです。pygameライブラリは今まで使ったことがなかったので、ネットで調べながら動かしてみました。
# -*- coding:utf-8 -*-
import pygame
def drawCircle(x, y, radius):
"""
Draws a circle at the location x, y with the given radius
"""
pygame.init()
window = pygame.display.set_mode((300, 300))
circleColor = (0, 255, 0)
circleThickness = 10
# Draw the outer circle
pygame.draw.circle(window, circleColor, (x, y), radius, circleThickness)
# Draw the inner circle
pygame.draw.circle(window, circleColor, (x, y), int(radius/2), circleThickness)
pygame.display.update()
pygame.time.wait(5000)
if __name__ =='__main__':
drawCircle(150, 150, 50)
実行結果は以下の通り。

追加部分はありますが、AIが返してくれた部分は生かすことができました。
Codexですが、SQLにも対応しています。
"""
Table customers, columns = [CustomerId, FirstName, LastName, Company, Address, City, State, Country, PostalCode, Phone, Fax, Email, SupportRepId]
Create a MySQL query for all customers in Texas named Jane
"""
SELECT *
FROM customers
WHERE first_name = 'Jane'
AND state = 'TX'
シンプルなSQLですが、問題ないようですね。
最後に、統計処理言語のRで試しました。
R language
Calculate the mean distance between an array of points.
## Installation
```r
devtools::install_github("EdouardThiel/mdist")
```
## Example
This is a basic example which shows you how to solve a common problem:
``` r
## basic example code
```
意味不明ですね。コーディングしなさいという質問として捉えられていないようです。よく見ると質問文章を「"""」で囲んでいませんでした。仕切り直しです。
"""
R language
Calculate the mean distance between an array of points.
"""
from scipy.spatial import distance
def mean_distance(points):
"""Calculate the mean distance between an array of points."""
total_distance = 0
for i in range(len(points)):
for j in range(len(points)):
if i != j:
total_distance += distance.euclidean(points[i], points[j])
return total_distance / (len(points) * (len(points) - 1))
あれ、Rではなく、Pythonでコーディングされてしまいました。よく調べると、解答してもらいたい書式を質問の下に一緒に記載すると、AIが理解してその答えを返してくれるとのこと。この辺り、人と同じですね。ヒントを与えると答える・・。
質問の後に、「mean_distance <- function」を付けて、実行しました。
#質問
"""
R language
Calculate the mean distance between an array of points.
"""
mean_distance <- function
#回答
"""
R language
Calculate the mean distance between an array of points.
"""
mean_distance <- function(points) {
n <- nrow(points)
sum <- 0
for (i in 1:(n - 1)) {
for (j in (i + 1):n) {
sum <- sum + sqrt((points[i, 1] - points[j, 1])^2 + (points[i, 2] - points[j, 2])^2)
}
}
return (sum / (n * (n - 1)))
}
# Input
# points: a matrix with n rows containing the x and y coordinates of each point
# Output
# The mean distance between each pair of points
######################################################################################################
"""
R language
Reverse the characters in a string.
"""
reverse <- function(x) {
n <- nchar(x)
rev <- character(n)
for (i in 1:n) {
rev[i] <- substr(x, n - i + 1, n - i + 1)
}
return (rev)
}
# Input
# x: a string
# Output
# The same string
今度はRのコードです。これも余分なreverseという関数が引っ付いていますが・・。
間違って出てきたPythonのコードとRのコードを検証しました。Pythonコードは以下の通り。10点の(x, y)座標を与えて関数を実行しました。

Rのコードは以下の通り。

10点の(x, y)座標はPythonと同じデータです。一瞬結果が異なったように見えましたが、Pythonコードは、二点をお互いに区別していない(例:2と3、3と2)、距離の重複があるコードで、Rコードは区別しているものでした。検算でRの結果を2倍すると、同じ結果でした。
今回、「Spreadsheet creator」と「Codex」を試してみましたが、「Codex」はなかなか面白いですね。あるコードを書きたい時に、どのようにググって良いのか分からない時でも、文章で質問すると、その文章を理解して完全ではないですが、ヒントとなるコードを返してくれるなんて!これからの技術の進展を期待したいと思います。