-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
111 lines (79 loc) · 3.32 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import streamlit as st
import os
from dotenv import load_dotenv
from src.api.chatgpt import ChatGPT
# .envファイルから環境変数をロードする
load_dotenv()
# APIキーの設定
API_KEY = os.environ.get("OPENAI_API_KEY")
MODEL_ENGINE = "gpt-3.5-turbo"
# ページのタイトルを設定する
st.set_page_config(page_title="Chatbot App")
#
# SessionStateを使用して状態を管理する
#
messages = []
if 'message_count' not in st.session_state:
st.session_state.message_count = 1
if 'messages' not in st.session_state:
st.session_state.messages = []
st.title("事前入力プロンプト")
btn_col1, btn_col2 = st.columns(2)
with btn_col1:
if st.button('プロンプトの追加'):
st.session_state.message_count += 1
with btn_col2:
if st.session_state.message_count > 1 and st.button('プロンプトの削除'):
st.session_state.message_count -= 1
st.session_state.messages.pop()
st.caption("---")
for i in range(st.session_state.message_count):
col1, col2 = st.columns(2)
with col1:
role = st.selectbox("", ["system", "user", "assistant"], key=f"role_{i}")
st.caption("Role")
with col2:
if role == "system":
message = st.text_input("", "あなたは親身に回答してくれるチャットボットです。", key=f"message_{i}")
else:
message = st.text_input("", "", key=f"message_{i}")
st.caption("Message")
messages.append({'role': role, 'content': message})
# プログラム側で初期値を規定する場合は、以下のコメントアウトを外す
# messages = [{'role': 'system', 'content': 'あなたは親身に回答してくれるチャットボットです。'}]
st.session_state.messages = messages
st.write(messages)
# Chat GPT インスタンスの作成
chatbot = ChatGPT(api_key=API_KEY, model_engine=MODEL_ENGINE, messages=messages)
# SessionStateを使用して状態を管理する
session_state = st.session_state
session_state.response = "ここにChatGPTからの出力結果が表示されます。"
if "chat_log_list" not in session_state:
session_state.chat_log_list = []
st.title('ユーザー入力文')
# ユーザーのメッセージを入力するテキストボックス
user_input = st.text_input("Enter your message")
# ボタンがクリックされたときに実行される関数
def get_response(user_input):
return chatbot.send_message(user_input)
def on_button_click():
# ユーザーのメッセージを送信し、チャットのログを更新する
response = get_response(user_input)
# レスポンスを上書きする
session_state.response = response
# チャットログをリストの先頭に追加する
session_state.chat_log_list.insert(0, ("You: " + user_input, "Chatbot: " + response))
return response
# ボタンの作成とクリックイベントの設定
if st.button("Send", key="send"):
on_button_click()
# 出力結果を表示する
st.title('出力結果')
session_state.response
st.caption("---")
if st.checkbox("チャットログを表示する", value=False):
with st.container():
for you_ms, chatbot_ms in session_state.chat_log_list:
st.write(f"{you_ms}", key=f"{you_ms}")
st.write(f"{chatbot_ms}", key=f"{chatbot_ms}")
st.caption("---")