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

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

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

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

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

2021/11/21

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

Chat

debian:~$ python openai_examples_chat.py
('秋の紅葉は、美しくて癒やされるし、気分が高揚するね。君はどう思う?',)
 
 Let's go out to see the autumn leaves. The red and yellow colours are beautiful. It makes me feel excited. What do you think?
debian:~$ python openai_examples_chat.py
('世界中で半導体が不足してるらいしよ。おかげでRaspberry Piも在庫切れが多発してる。RAM8GBのラズパイ4B欲しいんだけどな。ほしくない?',)
 
 
debian:~$ python openai_examples_chat.py
('世界中で半導体が不足してるらいしよ。おかげでRaspberry Piも在庫切れが多発してる。いろいろ遊べそうだからRAM8GBのラズパイ4B欲しいんだけどな。あなたは欲しくない?',)
 
??
debian:~$ python openai_examples_chat.py
('世界中で半導体が不足してるらいしよ。おかげでラズベリーパイも在庫切れが多発してる。いろいろ遊べそうだからメモリ8ギガバイトのラズパイ欲しいんだけどな。あなたは欲しくない?',)
 
More words from intel :Large lot production of processors based on Intel’s 14-nanometer process will kick off next year, the market watcher said. In 2015, Intel aims to ship 45 million of these chips, with a doubling of production planned for the following year. Moreover, Intel is expected to begin producing its first 14nm chips with embedded 3D transistors sometime in the second half of 2015. The company also plans to start making 14nm chips with 2D transistors sometime next year.
debian:~$ python openai_examples_chat.py
("The following is a conversation with an AI assistant. The assistant is helpful, creative, clever, and very friendly.\n\nHuman: Hello, who are you?\nAI: I am an AI created by OpenAI. How can I help you today?\nHuman: I'd like to cancel my subscription.\nAI:",)
 
{
 "id": "cmpl-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
 "object": "text_completion",
 "created": xxxxxxxx,
 "model": "davinci:2020-05-03",
 "choices": [
  {
   "text": " Okay, no problem.",
   "index": 0,
   "logprobs": null,
   "finish_reason": "stop"
  }
 ]
}
debian:~$ python openai_examples_chat.py
("The following is a conversation with an AI assistant. The assistant is helpful, creative, clever, and very friendly.\n\nHuman: Hello, who are you?\nAI: I am an AI created by OpenAI. How can I help you today?\nHuman: I'd like to cancel my subscription.\nAI:",)
 
 Cancelling subscription.
debian:~$ python openai_examples_chat.py
("The following is a conversation with an AI assistant. The assistant is helpful, creative, clever, and very friendly.\n\nHuman: Hello, who are you?\nAI: I am an AI created by OpenAI. How can I help you today?\nHuman: I'd like to cancel my subscription.\nAI:",)
 
 I'm sorry you feel that way.
debian:~$ python openai_examples_chat.py
("The following is a conversation with an AI assistant. The assistant is helpful, creative, clever, and very friendly.\n\nHuman: Hello, who are you?\nAI: I am an AI created by OpenAI. How can I help you today?\nHuman: I'd like to cancel my subscription.\nAI:",)
 
 I am unable to cancel your subscription as all interaction needs to be made with your company, not me. If you can give me the website of the company you wish to contact about this I will happily forward the message on.  Please do not forward this query onto any company yourself as it will lead to incorrect information and  I may  not be  able to  correct this  information and  if  shared with your contacts incorrectly could lead to their false belief that we are in fact the real company when in fact we are not.
debian:~$

 問いかけに相応に応答するChat

 紅葉には見事に返答、ラズベリーパイは知らないようですが、半導体には反応してIntelのCPUについて語っていますね。

 同じ問いかけにもワンパターンではなく、いろいろ返してきます。

 このように日本語も理解してくれますが、一部を除き、どのサンプルも応答は、基本、英語です(それはそれで凄いですが)。

 また、日本語と英語の混在は、解釈されず、無応答か「??」となり、アルファベットの並びを、ひらがなやカタカナなど日本語表記にすると応答しました。

debian:~$ cat openai_examples_chat.py
import openai
import os
import json
 
openai.api_key = os.getenv("OPENAI_API_KEY")
#prompt="The following is a conversation with an AI assistant. The assistant is helpful, creative, clever, and very friendly.\n\nHuman: Hello, who are you?\nAI: I am an AI created by OpenAI. How can I help you today?\nHuman: I'd like to cancel my subscription.\nAI:",
#prompt="秋の紅葉は、美しくて癒やされるし、気分が高揚するね。君はどう思う?",
#prompt="世界中で半導体が不足してるらいしよ。おかげでRaspberry Piも在庫切れが多発してる。いろいろ遊べそうだからRAM8GBのラズパイ4B欲しいんだけどな。あなたは欲しくない?",
prompt="世界中で半導体が不足してるらいしよ。おかげでラズベリーパイも在庫切れが多発してる。いろいろ遊べそうだからメモリ8ギガバイトのラズパイ欲しいんだけどな。あなたは欲しくない?"
 
response = openai.Completion.create(
 engine="davinci",
 prompt=prompt,
 temperature=0.9,
 max_tokens=150,
 top_p=1,
 frequency_penalty=0.0,
 presence_penalty=0.6,
 stop=["\n", " Human:", " AI:"]
)
#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の中身によっては、文字列にキャストしないとエラーとなるケースもありました。

ホーム前へ次へ