ChatGPT_APIで任意のテキストを短くしたり長くするPythonコード

ChatGPTのAPIを利用して、任意のテキストを短くしたり長くするPythonコードです。

APIに投げるprompt部分をもう少し詳細に指定するようにすれば、どのくらいの短さ(長さ)にするかも制御できるかもしれません。


import openai

def convert_to_short(prompt):
    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=[
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": "次の文章を短くしてください。"},
            {"role": "user", "content": prompt}
        ],
        max_tokens=500
    )

    reply = response.choices[0].message.get('content', '')
    return reply

def convert_to_long(prompt):
    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=[
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": "次の文章を長くしてください。"},
            {"role": "user", "content": prompt}
        ],
        max_tokens=500
    )

    reply = response.choices[0].message.get('content', '')
    return reply

# OpenAI APIの設定
openai.api_key = "API Keyをここに入れる"

input_sentence = input('文章を入力してください: ')
mode = input('どちらに変換するかを選択してください(短く: 1, 長く: 2): ')

if mode == '1':
    converted_sentence = convert_to_short(input_sentence)
elif mode == '2':
    converted_sentence = convert_to_long(input_sentence)
else:
    print('無効なモードが選択されました。')

print('変換結果:', converted_sentence)

    
結果:

以下のように成功しました。サンプルとして、適当なニュース記事を入れてます。