-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01_my_first_AI.py
More file actions
55 lines (47 loc) · 2.18 KB
/
01_my_first_AI.py
File metadata and controls
55 lines (47 loc) · 2.18 KB
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
import streamlit as st
from langchain.chat_models import ChatOpenAI
from langchain.schema import (SystemMessage, HumanMessage, AIMessage)
def main():
llm = ChatOpenAI(temperature=0.9)
# タブのタイトルなどを決める
st.set_page_config(
page_title="My Great ChatGPT",
page_icon=""
)
st.header("My Great ChatGPT 🤗")
# チャット履歴の初期化
if "messages" not in st.session_state:
st.session_state.messages = [
SystemMessage(content="I am a helpful assistant.")
]
# ユーザーの入力を監視
# if user_input := st.chat_input("聞きたいことを入力してね!"):
# st.session_state.messages.append(HumanMessage(content=user_input))
# with st.spinner("ChatGPT is typing ..."):
# response = llm(st.session_state.messages)
# st.session_state.messages.append(AIMessage(content=response.content))
container = st.container()
with container:
with st.form(key='my_form', clear_on_submit=True):
user_input = st.text_area(label='Message: ', key='input', height=100)
submit_button = st.form_submit_button(label='Send')
if submit_button and user_input:
# 何か入力されて Submit ボタンが押されたら実行される
st.session_state.messages.append(HumanMessage(content=user_input))
# Streamlitのスピナー機能を利用し、ユーザーに対して処理中であることを伝える
with st.spinner("ChatGPT is typing ..."):
response = llm(st.session_state.messages)
st.session_state.messages.append(AIMessage(content=response.content))
# チャット履歴の表示
messages = st.session_state.get('messages', [])
for message in messages:
if isinstance(message, AIMessage):
with st.chat_message('assistant'):
st.markdown(message.content)
elif isinstance(message, HumanMessage):
with st.chat_message('user'):
st.markdown(message.content)
else: # isinstance(message, SystemMessage):
st.write(f"System message: {message.content}")
if __name__ == '__main__':
main()