Update app.py
Browse files
app.py
CHANGED
|
@@ -15,46 +15,38 @@ st.subheader("生成されたテキストがリアルタイムで表示されま
|
|
| 15 |
if 'conversation_history' not in st.session_state:
|
| 16 |
st.session_state['conversation_history'] = []
|
| 17 |
|
| 18 |
-
# ユーザー入力の受付
|
| 19 |
-
user_input = st.text_input("質問を入力してください:")
|
| 20 |
-
|
| 21 |
-
# Groq APIにリクエストを送信し、ストリーミング応答を取得する関数
|
| 22 |
-
def get_groq_response(prompt):
|
| 23 |
-
response = client.chat.completions.create(
|
| 24 |
-
model="llama3-8b-8192", # 適切なモデル名に置き換えてください
|
| 25 |
-
messages=[{"role": "user", "content": prompt}],
|
| 26 |
-
stream=True # ストリーミングモードを有効にする
|
| 27 |
-
)
|
| 28 |
-
return response
|
| 29 |
-
|
| 30 |
# 過去の会話履歴を表示
|
| 31 |
for entry in st.session_state['conversation_history']:
|
| 32 |
-
st.
|
| 33 |
|
| 34 |
-
# ユーザー
|
| 35 |
-
if
|
| 36 |
-
|
| 37 |
-
st.
|
| 38 |
|
| 39 |
# Groq APIからの応答を取得
|
| 40 |
-
response = get_groq_response(user_input)
|
| 41 |
response_text = ""
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
# 会話履歴をリセットするボタン
|
| 58 |
if st.button("会話をリセット"):
|
| 59 |
st.session_state['conversation_history'] = []
|
| 60 |
-
st.
|
|
|
|
| 15 |
if 'conversation_history' not in st.session_state:
|
| 16 |
st.session_state['conversation_history'] = []
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
# 過去の会話履歴を表示
|
| 19 |
for entry in st.session_state['conversation_history']:
|
| 20 |
+
st.chat_message(entry['role']).markdown(entry['content'])
|
| 21 |
|
| 22 |
+
# ユーザー入力の受付
|
| 23 |
+
if prompt := st.chat_input("質問を入力してください:"):
|
| 24 |
+
st.session_state['conversation_history'].append({"role": "user", "content": prompt})
|
| 25 |
+
st.chat_message("user").markdown(prompt)
|
| 26 |
|
| 27 |
# Groq APIからの応答を取得
|
|
|
|
| 28 |
response_text = ""
|
| 29 |
+
with st.chat_message("assistant"):
|
| 30 |
+
message_placeholder = st.empty() # 応答の一時表示用の空要素
|
| 31 |
+
|
| 32 |
+
response = client.chat.completions.create(
|
| 33 |
+
model="llama3-8b-8192", # 適切なモデル名に置き換えてください
|
| 34 |
+
messages=[{"role": "user", "content": prompt}],
|
| 35 |
+
stream=True # ストリーミングモードを有効にする
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
# ストリーミングで部分的な応答をリアルタイムで表示
|
| 39 |
+
for chunk in response:
|
| 40 |
+
if "choices" in chunk:
|
| 41 |
+
chunk_text = chunk['choices'][0]['delta'].get('content', '')
|
| 42 |
+
response_text += chunk_text
|
| 43 |
+
message_placeholder.markdown(response_text + "|") # 部分的な応答を表示
|
| 44 |
+
|
| 45 |
+
# 最終的な応答を確定表示
|
| 46 |
+
message_placeholder.markdown(response_text)
|
| 47 |
+
st.session_state['conversation_history'].append({"role": "assistant", "content": response_text})
|
| 48 |
|
| 49 |
# 会話履歴をリセットするボタン
|
| 50 |
if st.button("会話をリセット"):
|
| 51 |
st.session_state['conversation_history'] = []
|
| 52 |
+
st.experimental_rerun()
|