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

GPT-3のExamples/English to Frenchを試してみた

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

GPT-3のExamples/English to Frenchを試してみた

GPT-3のExamples/English to Frenchを試してみた

2021/12/04

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

English to French

debian:~$ python openai_examples_translate.py
('English: I do not speak French.\nFrench: Je ne parle pas français.\n\nEnglish: See you later!\nFrench: À tout à l'heure!\n\nEnglish: Where is a good restaurant?\nFrench: Où est un bon restaurant?\n\nEnglish: What rooms do you have available?\nFrench: Quelles chambres avez-vous de disponible?\n\nEnglish: What time is breakfast?\nFrench:',)
 
 A quelle heure est le petit-déjeuner?
debian:~$ python openai_examples_translate.py
('English: I do not speak French.\nFrench: Je ne parle pas français.\n\nEnglish: See you later!\nFrench: À tout à l'heure!\n\nEnglish: Where is a good restaurant?\nFrench: Où est un bon restaurant?\n\nEnglish: What rooms do you have available?\nFrench: Quelles chambres avez-vous de disponible?\n\nEnglish: What time is breakfast?\nFrench:',)
 
{
 "id": "cmpl-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
 "object": "text_completion",
 "created": xxxxxxxx,
 "model": "davinci:2020-05-03",
 "choices": [
  {
   "text": " À quelle heure est le petit déjeuner?",
   "index": 0,
   "logprobs": null,
   "finish_reason": "stop"
  }
 ]
}
debian:~$

 英語をフランス語に翻訳するEnglish to French

 オリジナルサンプルのままで実行してみました。

debian:~$ cat openai_examples_translate.py
import openai
import os
import json
 
openai.api_key = os.getenv("OPENAI_API_KEY")
prompt="English: I do not speak French.\nFrench: Je ne parle pas français.\n\nEnglish: See you later!\nFrench: À tout à l'heure!\n\nEnglish: Where is a good restaurant?\nFrench: Où est un bon restaurant?\n\nEnglish: What rooms do you have available?\nFrench: Quelles chambres avez-vous de disponible?\n\nEnglish: What time is breakfast?\nFrench:",
 
response = openai.Completion.create(
 engine="davinci",
 prompt=prompt,
 temperature=0.5,
 max_tokens=100,
 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の中身によっては、文字列にキャストしないとエラーとなるケースもありました。

ホーム前へ次へ