diff --git a/docs/images/streamlit.png b/docs/images/streamlit.png index b3162f0..a7959e0 100644 Binary files a/docs/images/streamlit.png and b/docs/images/streamlit.png differ diff --git a/docs/index.ja.md b/docs/index.ja.md index 871ddaa..6663b6f 100644 --- a/docs/index.ja.md +++ b/docs/index.ja.md @@ -222,7 +222,7 @@ uv run streamlit run \ Streamlit アプリのデモ: -[![streamlit.png](./images/streamlit.png)](https://youtu.be/z7QhEsqIGAk) +[![streamlit.png](./images/streamlit.png)](https://youtu.be/undxBwyJ3Sc) ## 実演されている主要概念 diff --git a/docs/index.md b/docs/index.md index dd98fe1..795a04c 100644 --- a/docs/index.md +++ b/docs/index.md @@ -222,7 +222,7 @@ uv run streamlit run \ Demonstration of the Streamlit app: -[![streamlit.png](./images/streamlit.png)](https://youtu.be/z7QhEsqIGAk) +[![streamlit.png](./images/streamlit.png)](https://youtu.be/undxBwyJ3Sc) ## Key Concepts Demonstrated diff --git a/template_langgraph/services/streamlits/pages/chat_with_tools_agent.py b/template_langgraph/services/streamlits/pages/chat_with_tools_agent.py index f01c749..e9562e7 100644 --- a/template_langgraph/services/streamlits/pages/chat_with_tools_agent.py +++ b/template_langgraph/services/streamlits/pages/chat_with_tools_agent.py @@ -3,11 +3,44 @@ StreamlitCallbackHandler, ) -from template_langgraph.agents.chat_with_tools_agent.agent import AgentState, graph +from template_langgraph.agents.chat_with_tools_agent.agent import ( + AgentState, + ChatWithToolsAgent, +) +from template_langgraph.tools.common import get_default_tools if "chat_history" not in st.session_state: st.session_state["chat_history"] = [] +# Sidebar: ツール選択とエージェントの構築 +with st.sidebar: + st.subheader("使用するツール") + + # 利用可能なツール一覧を取得 + available_tools = get_default_tools() + tool_name_to_obj = {t.name: t for t in available_tools} + tool_names = list(tool_name_to_obj.keys()) + + # 初期選択は全選択 + if "selected_tool_names" not in st.session_state: + st.session_state["selected_tool_names"] = tool_names + + selected_tool_names = st.multiselect( + "有効化するツールを選択", + options=tool_names, + default=st.session_state["selected_tool_names"], + ) + st.session_state["selected_tool_names"] = selected_tool_names + + # 選択されたツールでグラフを再構築(選択が変わった時のみ) + selected_tools = [tool_name_to_obj[name] for name in selected_tool_names] + signature = tuple(selected_tool_names) + if "graph" not in st.session_state or st.session_state.get("graph_tools_signature") != signature: + st.session_state["graph"] = ChatWithToolsAgent(tools=selected_tools).create_graph() + st.session_state["graph_tools_signature"] = signature + # 選択中のツール表示(簡易) + st.caption("選択中: " + (", ".join(selected_tool_names) if selected_tool_names else "なし")) + for msg in st.session_state["chat_history"]: if isinstance(msg, dict): st.chat_message(msg["role"]).write(msg["content"]) @@ -18,7 +51,7 @@ st.session_state["chat_history"].append({"role": "user", "content": prompt}) st.chat_message("user").write(prompt) with st.chat_message("assistant"): - response: AgentState = graph.invoke( + response: AgentState = st.session_state["graph"].invoke( {"messages": st.session_state["chat_history"]}, { "callbacks": [ @@ -26,5 +59,6 @@ ] }, ) - st.session_state["chat_history"].append(response["messages"][-1]) - st.write(response["messages"][-1].content) + last_message = response["messages"][-1] + st.session_state["chat_history"].append(last_message) + st.write(last_message.content)