Skip to content

Commit 0595310

Browse files
authored
Merge pull request #62 from ks6088ts-labs/feature/issue-61_select-tools
add UI to select tools
2 parents 53ac219 + 1870011 commit 0595310

File tree

4 files changed

+40
-6
lines changed

4 files changed

+40
-6
lines changed

docs/images/streamlit.png

-157 KB
Loading

docs/index.ja.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ uv run streamlit run \
222222

223223
Streamlit アプリのデモ:
224224

225-
[![streamlit.png](./images/streamlit.png)](https://youtu.be/z7QhEsqIGAk)
225+
[![streamlit.png](./images/streamlit.png)](https://youtu.be/undxBwyJ3Sc)
226226

227227
## 実演されている主要概念
228228

docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ uv run streamlit run \
222222

223223
Demonstration of the Streamlit app:
224224

225-
[![streamlit.png](./images/streamlit.png)](https://youtu.be/z7QhEsqIGAk)
225+
[![streamlit.png](./images/streamlit.png)](https://youtu.be/undxBwyJ3Sc)
226226

227227
## Key Concepts Demonstrated
228228

template_langgraph/services/streamlits/pages/chat_with_tools_agent.py

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,44 @@
33
StreamlitCallbackHandler,
44
)
55

6-
from template_langgraph.agents.chat_with_tools_agent.agent import AgentState, graph
6+
from template_langgraph.agents.chat_with_tools_agent.agent import (
7+
AgentState,
8+
ChatWithToolsAgent,
9+
)
10+
from template_langgraph.tools.common import get_default_tools
711

812
if "chat_history" not in st.session_state:
913
st.session_state["chat_history"] = []
1014

15+
# Sidebar: ツール選択とエージェントの構築
16+
with st.sidebar:
17+
st.subheader("使用するツール")
18+
19+
# 利用可能なツール一覧を取得
20+
available_tools = get_default_tools()
21+
tool_name_to_obj = {t.name: t for t in available_tools}
22+
tool_names = list(tool_name_to_obj.keys())
23+
24+
# 初期選択は全選択
25+
if "selected_tool_names" not in st.session_state:
26+
st.session_state["selected_tool_names"] = tool_names
27+
28+
selected_tool_names = st.multiselect(
29+
"有効化するツールを選択",
30+
options=tool_names,
31+
default=st.session_state["selected_tool_names"],
32+
)
33+
st.session_state["selected_tool_names"] = selected_tool_names
34+
35+
# 選択されたツールでグラフを再構築(選択が変わった時のみ)
36+
selected_tools = [tool_name_to_obj[name] for name in selected_tool_names]
37+
signature = tuple(selected_tool_names)
38+
if "graph" not in st.session_state or st.session_state.get("graph_tools_signature") != signature:
39+
st.session_state["graph"] = ChatWithToolsAgent(tools=selected_tools).create_graph()
40+
st.session_state["graph_tools_signature"] = signature
41+
# 選択中のツール表示(簡易)
42+
st.caption("選択中: " + (", ".join(selected_tool_names) if selected_tool_names else "なし"))
43+
1144
for msg in st.session_state["chat_history"]:
1245
if isinstance(msg, dict):
1346
st.chat_message(msg["role"]).write(msg["content"])
@@ -18,13 +51,14 @@
1851
st.session_state["chat_history"].append({"role": "user", "content": prompt})
1952
st.chat_message("user").write(prompt)
2053
with st.chat_message("assistant"):
21-
response: AgentState = graph.invoke(
54+
response: AgentState = st.session_state["graph"].invoke(
2255
{"messages": st.session_state["chat_history"]},
2356
{
2457
"callbacks": [
2558
StreamlitCallbackHandler(st.container()),
2659
]
2760
},
2861
)
29-
st.session_state["chat_history"].append(response["messages"][-1])
30-
st.write(response["messages"][-1].content)
62+
last_message = response["messages"][-1]
63+
st.session_state["chat_history"].append(last_message)
64+
st.write(last_message.content)

0 commit comments

Comments
 (0)