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

GPT-3のExamples/Ad from product descriptionを試してみた

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

GPT-3のExamples/Ad from product descriptionを試してみた

GPT-3のExamples/Ad from product descriptionを試してみた

2021/11/21

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

Ad from product description

debian:~$ python openai_examples_ad-product-description.py
('鳥よけ装置用のクリエイティブ広告を書いて:\n""""""\n鳥バイバイは、花壇やベランダに飛来して果実を食べてしまう鳥たちになるべく来ないでねと伝えるための装置。それは鳥に危害を及ぼすものではありません。\n""""""\nこれは、鳥は好きだけど果実を守りたい人たち向けの商品です。:\n""""""',)
 
This is a product for people who like birds but want to protect the fruit.
 
This is a device that tells birds not to come near the garden or the balcony because they eat the fruit. It is not harmful to the birds.
 
This is a product for people who like
debian:~$ python openai_examples_ad-product-description.py
('鳥よけ装置用のクリエイティブ広告を書いて:\n""""""\n鳥バイバイは、花壇やベランダに飛来して果実を食べてしまう鳥たちになるべく来ないでねと伝えるための装置。それは鳥に危害を及ぼすものではありません。\n""""""\nこれは、鳥は好きだけど果実を守りたい人たち向けの商品です。:\n""""""',)
 
{
 "id": "cmpl-46w9lr4dwBqWnvYfDDsMrK3IewHU1",
 "object": "text_completion",
 "created": 1637558869,
 "model": "if-davinci-v2",
 "choices": [
  {
   "text": "\n\nTo stop birds from eating your fruit, you can use a device called bird-b-gone. This device is not harmful to the birds.\n\nThis is a product for people who like birds but don't want them to eat their fruit.",
   "index": 0,
   "logprobs": null,
   "finish_reason": "stop"
  }
 ]
}
debian:~$

 商品の特徴、目的や条件を投げると広告に使える文章を返すAd from product description

 適当に書いた『鳥バイバイ』が、[bird-b-gone]になったりすることもあるみたいですが、それはそれで、すごっ。

 途中まで繰り返しちゃうクセがあるっぽい点は、ご愛嬌ということで。

debian:~$ cat openai_examples_ad-product-description.py
import os
import json
import openai
 
openai.api_key = os.getenv("OPENAI_API_KEY")
 
#prompt="Write a creative ad for the following product to run on Facebook:\n\"\"\"\"\"\"\nAiree is a line of skin-care products for young women with delicate skin. The ingredients are all-natural.\n\"\"\"\"\"\"\nThis is the ad I wrote for Facebook aimed at teenage girls:\n\"\"\"\"\"\"",
prompt="鳥よけ装置用のクリエイティブ広告を書いて:\n\"\"\"\"\"\"\n鳥バイバイは、花壇やベランダに飛来して果実を食べてしまう鳥たちになるべく来ないでねと伝えるための装置。それは鳥に危害を及ぼすものではありません。\n\"\"\"\"\"\"\nこれは、鳥は好きだけど果実を守りたい人たち向けの商品です。:\n\"\"\"\"\"\"",
 
response = openai.Completion.create(
 engine="davinci-instruct-beta",
 prompt=prompt,
 temperature=0.5,
 max_tokens=60,
 top_p=1.0,
 frequency_penalty=0.0,
 presence_penalty=0.0,
 stop=["\"\"\"\"\"\""]
)
print(prompt)
print("")
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の中身によっては、このように文字列にキャストしないとエラーとなるケースもあります。

ホーム前へ次へ