import os
import openai
openai.api_key = os.environ["OPENAI_API_KEY"]
def generate_text(prompt, role, conversation_history) :
# ユーザーの質問を会話履歴に追加
conversation_history.append({"role": "user", "content": prompt})
# GPT-4モデルを使用してテキストを生成
response = openai.ChatCompletion.create(
model="gpt-4",
messages=[{"role": "system", "content": f"{role}"}] + conversation_history,
max_tokens=50,
n=1,
temperature=0.8,
)
message = response.choices[0].message['content'].strip()
# アシスタントの回答を会話履歴に追加
conversation_history.append({"role": "assistant", "content": message})
return message
if __name__ == "__main__":
# ロールプレイのモデルをユーザーに入力させる
# 学習データの読み込み
with open("file/role.txt") as f:
role = f.read()
# 会話履歴を格納するためのリストを初期化
conversation_history = []
while True:
# ユーザーに質問を入力させる
input_prompt = input("お支払い方法を教えてくださいq")
# 終了条件の確認
if input_prompt.lower() == 'q':
break
# GPT-4からの回答を生成
generated_text = generate_text(input_prompt, role, conversation_history)
# 回答を表示
## print("GPT-4からの回答:", generated_text)
Hello world!
This is the current date and time, as computed by Python: