1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

DeepL API (Python) を使ってChatGPTと英語でやりとりする

Posted at

ChatGPT の API を、Flask に実装してみましたが、「ChatGPT は英語でやりとりした方が精度が高い」といった内容をネットでよく散見していたので、DeepL API を使って翻訳を任せるということをやってみました。

DeepLのモジュールをインストール

pip install deepl

日本語 → 英語に変換

import os
import deepl

DEEPL_API_KEY = os.environ.get('DEEPL_API_KEY')

def deepL_translate(str):
    translator = deepl.Translator(DEEPL_API_KEY)
    result = translator.translate_text(str, source_lang="JA", target_lang="EN-US")
    return result.text

後は関数の返り値を ChatGPT API のプロンプトに渡し、返答は日本語にしてもらえば良い訳ですが、たまに ChatGPT はそのまま英語で答えてくることもあるので、source_langtarget_lang を入れ替えて、返答も DeepL API で翻訳という使い方もあると思います。

DeepL API は、「1か月あたり500,000文字」まで無料で使えるので、頻度や文字量にもよりますが十分太っ腹なサービスだと思います。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?