ソフトウェア
GPT-3のExamples/Interview questionsを試してみた
フリーソフト・オープンソースを活用しよう。
GPT-3のExamples/Interview questionsを試してみた
GPT-3のExamples/Interview questionsを試してみた
2021/11/27
OpenAIの自然言語処理モデルGPT-3のExamples/Interview questionsを試してみました。
Interview questions
debian:~$ python openai_examples_interview_questions.py
('ピエロにインタビューするための質問リストを作って:\n\n1.',)
どのようにピエロは生まれたのか
debian:~$ python openai_examples_interview_questions.py
('ピエロにインタビューするための質問リストを作って:\n\n1.',)
{
"id": "cmpl-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"object": "text_completion",
"created": xxxxxxxx,
"model": "davinci:2020-05-03",
"choices": [
{
"text": " ピエロは?",
"index": 0,
"logprobs": null,
"finish_reason": "stop"
}
]
}
debian:~$
「xxに関するインタビュー用の質問リストを作って、1.」などと投げると質問の例文を作ってくれるInterview questions。
2回めは、ともかく、1回めの提案は、ありですね。
debian:~$ cat openai_examples_interview_questions.py
import openai
import os
import json
openai.api_key = os.getenv("OPENAI_API_KEY")
#prompt="Create a list of questions for my interview with a science fiction author:\n\n1.",
prompt="ピエロにインタビューするための質問リストを作って:\n\n1."
response = openai.Completion.create(
engine="davinci",
prompt=prompt,
temperature=0.8,
max_tokens=64,
top_p=1.0,
frequency_penalty=0.0,
presence_penalty=0.0,
stop=["\n\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の中身によっては、文字列にキャストしないとエラーとなるケースもありました。
関連リンク