import openai
import deepl
import pickle
import datetime
import os
openai.organization = "org-EAsxLPeDr1QvJyQVOYggQmMc"
openai.api_key = os.environ["OPENAI_API_KEY"]
def select_translate():
dic = {'y': True, 'yes': True, 'n': False, 'no': False}
while True:
can_translated = dic.get(input('ENに翻訳して実行するか [Y]es/[N]o? >> ').lower(), -1)
if type(can_translated) is bool:
break
print('Error! Input again.')
return can_translated
def save_chat(messages,response):
file_name = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
chat_file = open(f'./outputs/{file_name}.pickle','wb')
messages.append(response)
pickle.dump(messages,chat_file)
chat_file.close()
def main(input_prompt,can_translate=True):
if can_translate:
input_to_chatgpt = deepl.translate(source_language='JA',target_language='EN',text=input_prompt)
else:
input_to_chatgpt = input_prompt
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": input_to_chatgpt}
]
res = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages,
max_tokens=100
)
print(input_to_chatgpt)
print(res['choices'][0]['message']['content'])
print(f"{deepl.translate(source_language='EN',target_language='JA',text=res['choices'][0]['message']['content'])}")
save_chat(messages=messages,response={'response':res})
print(res)
if __name__ == "__main__":
user_input = input('ChatGPTに聞きたいことを入力:')
can_translated = select_translate()
main(input_prompt=user_input,can_translate=bool(can_translated))