Yasu777 commited on
Commit
9af2f27
·
verified ·
1 Parent(s): da35c87

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -18
app.py CHANGED
@@ -3,9 +3,11 @@ import os
3
  from groq import Groq
4
 
5
  # APIキーの設定
6
- client = Groq(
7
- api_key=st.secrets["GROQ_API_KEY"],
8
- )
 
 
9
 
10
  # Streamlitアプリケーションのタイトルと説明
11
  st.title("Groq APIによるAIチャットボット")
@@ -29,22 +31,29 @@ if prompt := st.chat_input("質問を入力してください:"):
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 and chunk['choices'][0]['delta'].get('content'):
41
- chunk_text = chunk['choices'][0]['delta']['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("会話をリセット"):
 
3
  from groq import Groq
4
 
5
  # APIキーの設定
6
+ try:
7
+ client = Groq(api_key=st.secrets["GROQ_API_KEY"])
8
+ print("APIクライアントが正常に設定されました。")
9
+ except Exception as e:
10
+ print(f"APIクライアントの設定に失敗しました: {e}")
11
 
12
  # Streamlitアプリケーションのタイトルと説明
13
  st.title("Groq APIによるAIチャットボット")
 
31
  with st.chat_message("assistant"):
32
  message_placeholder = st.empty() # 応答の一時表示用の空要素
33
 
34
+ try:
35
+ response = client.chat.completions.create(
36
+ model="llama3-8b-8192", # 適切なモデル名に置き換えてください
37
+ messages=[{"role": "user", "content": prompt}],
38
+ stream=True # ストリーミングモードを有効にする
39
+ )
40
+ print("APIリクエストが正常に送信されました。")
41
+
42
+ # ストリーミングで部分的な応答をリアルタイムで表示
43
+ for chunk in response:
44
+ print(f"受信したチャンク: {chunk}") # デバッグ: チャンクの内容を出力
45
+ if "choices" in chunk and chunk['choices'][0]['delta'].get('content'):
46
+ chunk_text = chunk['choices'][0]['delta']['content']
47
+ response_text += chunk_text
48
+ message_placeholder.markdown(response_text + "|") # 部分的な応答を表示
49
+
50
+ # 最終的な応答を確定表示
51
+ message_placeholder.markdown(response_text)
52
+ st.session_state['conversation_history'].append({"role": "assistant", "content": response_text})
53
+ print("APIからの応答が正常に処理されました。")
54
 
55
+ except Exception as e:
56
+ print(f"APIリクエストの処理中にエラーが発生しました: {e}")
 
 
 
 
 
 
 
 
57
 
58
  # 会話履歴をリセットするボタン
59
  if st.button("会話をリセット"):