Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified docs/images/streamlit.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion docs/index.ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

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

Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand All @@ -18,13 +51,14 @@
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": [
StreamlitCallbackHandler(st.container()),
]
},
)
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)