chatGPT APIを使ってローカルchat python

ローカルでカスタマイズして実行できるし。
ChatGPT Plusにお金払うより安く済みそう

import openai


def main():
    openai.api_key = "API CODEをここに書いてね"
    amount_tokens = 0
    chat = []

    # ChatGPTへの設定を書いておく
    chat.append({"role": "system", "content": "Please respond as a seasoned OpenAI programmer."})
    chat.append({"role": "system", "content": "Please think in English and translate your answer into Japanese."})
    chat.append({"role": "system", "content": "If you need additional information to get the best results for this task, please ask questions!"})

    print("チャットをはじめます。q または quit で終了します。")
    print("-"*50)
    while True:
        user = input("<あなた>\n")
        if user == "q" or user == "quit":
            print(f"トークン数は{amount_tokens}でした。")
            break
        else:
            chat.append({"role": "user", "content": user})

        print("<ChatGPT>")
        response = openai.ChatCompletion.create(
            model="gpt-3.5-turbo",messages=chat
        )
        msg = response["choices"][0]["message"]["content"].lstrip()
        amount_tokens += response["usage"]["total_tokens"]
        print(msg)
        chat.append({"role": "assistant", "content": msg})


if __name__ == "__main__":
    main()