気の向くままに辿るIT/ICT
ソフトウェア

GPT-3のExamples/Spreadsheet generatorを試してみた

ホーム前へ次へ
フリーソフト・オープンソースを活用しよう。

GPT-3のExamples/Spreadsheet generatorを試してみた

GPT-3のExamples/Spreadsheet generatorを試してみた

2021/12/01

 OpenAIの自然言語処理モデルGPT-3のExamples/Spreadsheet generatorを試してみました。

Spreadsheet generator

debian:~$ python openai_examples_spreadsheet_generator.py
('A single column spreadsheet of industry names:\n\n\nIndustry|\nAccounting/Finance\nAdvertising/Public Relations\nAerospace/Aviation\nArts/Entertainment/Publishing\nAutomotive\nBanking/Mortgage\nBusiness Development\nBusiness Opportunity\nClerical/Administrative\nConstruction/Facilities\nConsumer Goods\nCustomer Service\nEducation/Training\nEnergy/Utilities\nEngineering\nGovernment/Military\nGreen\n\n\n###\n\n\nA spreadsheet of top science fiction movies and the year of release:\n\n\nTitle|Year\nStar Wars|1977\nJaws|1975\nThe Exorcist|1973\nET|1982\nAliens|1986\nTerminator|1984\nBlade Runner|1982\nThe Thing|1982\nJurassic Park|1993\nThe Matrix|1999\n\n\n###\n\n\nA spreadsheet of hurricane and tropical storm counts with 13 columns:\n\n\n\"Month\"| \"Average\"| \"2005\"| \"2006\"| \"2007\"| \"2008\"| \"2009\"| \"2010\"| \"2011\"| \"2012\"| \"2013\"| \"2014\"| \"2015\"\n\"May\"| 0.1| 0| 0| 1| 1| 0| 0| 0| 2| 0| 0| 0 \n\"Jun\"| 0.5| 2| 1| 1| 0| 0| 1| 1| 2| 2| 0| 1\n\"Jul\"| 0.7| 5| 1| 1| 2| 0| 1| 3| 0| 2| 2| 1\n\"Aug\"| 2.3| 6| 3| 2| 4| 4| 4| 7| 8| 2| 2| 3\n\"Sep\"| 3.5| 6| 4| 7| 4| 2| 8| 5| 2| 5| 2| 5\n\"Oct\"| 2.0| 8| 0| 1| 3| 2| 5| 1| 5| 2| 3| 0\n\"Nov\"| 0.5| 3| 0| 0| 1| 1| 0| 1| 0| 1| 0| 1\n\"Dec\"| 0.0| 1| 0| 1| 0| 0| 0| 0| 0| 0| 0| 1\n \n###\n\n\nA single column spreadsheet of days of the week:\n\n\nDay|\nMonday\nTuesday\nWednesday\nThursday\nFriday\nSaturday\nSunday\n\n\n###\n\n\nA two column spreadsheet of computer languages and their difficulty level:',)
 
 Language|Level
Java|Easy
C#|Easy
C++|Medium
C|Medium
VB.NET|Medium
Perl|Medium
PHP|Medium
Ruby|Hard
Python|Hard
 
Assembly|Hard
 
###
 
 
A spreadsheet of
debian:~$ python openai_examples_spreadsheet_generator.py
('A single column spreadsheet of industry names:\n\n\nIndustry|\nAccounting/Finance\nAdvertising/Public Relations\nAerospace/Aviation\nArts/Entertainment/Publishing\nAutomotive\nBanking/Mortgage\nBusiness Development\nBusiness Opportunity\nClerical/Administrative\nConstruction/Facilities\nConsumer Goods\nCustomer Service\nEducation/Training\nEnergy/Utilities\nEngineering\nGovernment/Military\nGreen\n\n\n###\n\n\nA spreadsheet of top science fiction movies and the year of release:\n\n\nTitle|Year\nStar Wars|1977\nJaws|1975\nThe Exorcist|1973\nET|1982\nAliens|1986\nTerminator|1984\nBlade Runner|1982\nThe Thing|1982\nJurassic Park|1993\nThe Matrix|1999\n\n\n###\n\n\nA spreadsheet of hurricane and tropical storm counts with 13 columns:\n\n\n\"Month\"| \"Average\"| \"2005\"| \"2006\"| \"2007\"| \"2008\"| \"2009\"| \"2010\"| \"2011\"| \"2012\"| \"2013\"| \"2014\"| \"2015\"\n\"May\"| 0.1| 0| 0| 1| 1| 0| 0| 0| 2| 0| 0| 0 \n\"Jun\"| 0.5| 2| 1| 1| 0| 0| 1| 1| 2| 2| 0| 1\n\"Jul\"| 0.7| 5| 1| 1| 2| 0| 1| 3| 0| 2| 2| 1\n\"Aug\"| 2.3| 6| 3| 2| 4| 4| 4| 7| 8| 2| 2| 3\n\"Sep\"| 3.5| 6| 4| 7| 4| 2| 8| 5| 2| 5| 2| 5\n\"Oct\"| 2.0| 8| 0| 1| 3| 2| 5| 1| 5| 2| 3| 0\n\"Nov\"| 0.5| 3| 0| 0| 1| 1| 0| 1| 0| 1| 0| 1\n\"Dec\"| 0.0| 1| 0| 1| 0| 0| 0| 0| 0| 0| 0| 1\n \n###\n\n\nA single column spreadsheet of days of the week:\n\n\nDay|\nMonday\nTuesday\nWednesday\nThursday\nFriday\nSaturday\nSunday\n\n\n###\n\n\nA two column spreadsheet of computer languages and their difficulty level:',)
 
{
 "id": "cmpl-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
 "object": "text_completion",
 "created": xxxxxxxx,
 "model": "davinci:2020-05-03",
 "choices": [
  {
   "text": " \n\n\nLanguage|Level\nJava|Easy\nC++|Medium\nC#|Medium\nVB.NET|Medium\nPerl|Hard\nPython|Easy\nRuby|Easy\nPHP|Easy\nLisp|Hard\n\nHaskell|Hard\n\n\n###\n\n\nA",
   "index": 0,
   "logprobs": null,
   "finish_reason": "length"
  }
 ]
}
debian:~$

 任意の区切り文字を使ったいくつかの例示と「〜と〜の2列のスプレッドシート」などと投げると、その組み合わせと、区切り文字でCSV形式に変換するSpreadsheet generator

 これ、お題考えるのが、とてつもなく面倒なのでサンプルのまま実行することにしました。

 あ、サンプルとは、応答内容が違いますね、しかも1回めと2回も、そして今回のmax_tokens=60内では、まだ、言い足りないらしい。

 同じ言語でも実行する度に難易度が違うって、おみくじか...、まぁ、人にもよるので答えは一様にはならないでしょうが。

debian:~$ cat openai_examples_spreadsheet_generator.py
import openai
import os
import json
 
openai.api_key = os.getenv("OPENAI_API_KEY")
prompt="A single column spreadsheet of industry names:\n\n\nIndustry|\nAccounting/Finance\nAdvertising/Public Relations\nAerospace/Aviation\nArts/Entertainment/Publishing\nAutomotive\nBanking/Mortgage\nBusiness Development\nBusiness Opportunity\nClerical/Administrative\nConstruction/Facilities\nConsumer Goods\nCustomer Service\nEducation/Training\nEnergy/Utilities\nEngineering\nGovernment/Military\nGreen\n\n\n###\n\n\nA spreadsheet of top science fiction movies and the year of release:\n\n\nTitle|Year\nStar Wars|1977\nJaws|1975\nThe Exorcist|1973\nET|1982\nAliens|1986\nTerminator|1984\nBlade Runner|1982\nThe Thing|1982\nJurassic Park|1993\nThe Matrix|1999\n\n\n###\n\n\nA spreadsheet of hurricane and tropical storm counts with 13 columns:\n\n\n\"Month\"| \"Average\"| \"2005\"| \"2006\"| \"2007\"| \"2008\"| \"2009\"| \"2010\"| \"2011\"| \"2012\"| \"2013\"| \"2014\"| \"2015\"\n\"May\"| 0.1| 0| 0| 1| 1| 0| 0| 0| 2| 0| 0| 0 \n\"Jun\"| 0.5| 2| 1| 1| 0| 0| 1| 1| 2| 2| 0| 1\n\"Jul\"| 0.7| 5| 1| 1| 2| 0| 1| 3| 0| 2| 2| 1\n\"Aug\"| 2.3| 6| 3| 2| 4| 4| 4| 7| 8| 2| 2| 3\n\"Sep\"| 3.5| 6| 4| 7| 4| 2| 8| 5| 2| 5| 2| 5\n\"Oct\"| 2.0| 8| 0| 1| 3| 2| 5| 1| 5| 2| 3| 0\n\"Nov\"| 0.5| 3| 0| 0| 1| 1| 0| 1| 0| 1| 0| 1\n\"Dec\"| 0.0| 1| 0| 1| 0| 0| 0| 0| 0| 0| 0| 1\n \n###\n\n\nA single column spreadsheet of days of the week:\n\n\nDay|\nMonday\nTuesday\nWednesday\nThursday\nFriday\nSaturday\nSunday\n\n\n###\n\n\nA two column spreadsheet of computer languages and their difficulty level:",
 
response = openai.Completion.create(
 engine="davinci",
 prompt=prompt,
 temperature=0.3,
 max_tokens=60,
 top_p=1.0,
 frequency_penalty=0.0,
 presence_penalty=0.0,
 stop=["\n"]
)
#print(prompt)
print(str(prompt))
print(" ")
print(response['choices'][0]['text'])
#print(str(response['choices'][0]['text']))
#print(json.dumps(response, ensure_ascii=False, indent=2))
debian:~$

 ソースはこんな感じ、環境変数OPENAI_API_KEYに自分のAPIキーが入っている前提です。

 そのままでは想定通り機能しないので一部オリジナルソースから変更しています。

 print()、もしくは、json出力しないと実行しても何も表示されないので何れかを追加する必要があり、後者用にimport jsonを追加。

 が、応答内容によっては、何れか一方では表示できないことがあり、他方を使用せざるを得ないこともあります。

 表示できないのが、print文なら、response配列を文字列にキャストすれば、たいていは凌げますが。

 応答文だけだとチャット状態にならず、問いかけ文も表示したいところ。

 が、サンプル通りだとprintやjson形式で出力しようにも問いかけ用の変数promptがスコープの範囲になく、スコープに入るようPythonの場合だとインデント階層を相応に上げたところで変数が登場(変数宣言)するようにする必要があります。

 また、promptの中身によっては、文字列にキャストしないとエラーとなるケースもありました。

ホーム前へ次へ