Implement stream mode agent and update dependencies: update main.py
This commit is contained in:
@@ -1,52 +1,96 @@
|
|||||||
|
"""Console entry point that prints LangGraph agent output in stream mode."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
from agent import graph
|
from typing import Any
|
||||||
|
|
||||||
def main():
|
from agent import agent
|
||||||
print("LangGraph stream‑mode demo. Type 'exit' to quit.")
|
|
||||||
state = {"messages": []}
|
|
||||||
# Если передан аргумент, используем его как сообщение; иначе читаем из stdin
|
|
||||||
if len(sys.argv) > 1:
|
|
||||||
user_input = " ".join(sys.argv[1:])
|
|
||||||
process(user_input, state)
|
|
||||||
else:
|
|
||||||
while True:
|
|
||||||
try:
|
|
||||||
user_input = input("You: ")
|
|
||||||
except EOFError:
|
|
||||||
break
|
|
||||||
if not user_input:
|
|
||||||
continue
|
|
||||||
if user_input.lower() in {"exit", "quit"}:
|
|
||||||
break
|
|
||||||
process(user_input, state)
|
|
||||||
|
|
||||||
def process(user_input, state):
|
|
||||||
current_step = None
|
_current_step: int | None = None
|
||||||
for chunk_type, chunk_data in graph.stream(
|
|
||||||
{"input": user_input, "messages": state["messages"]},
|
|
||||||
|
def format_message(message: Any) -> str:
|
||||||
|
"""Format a completed model message or a tool-call request."""
|
||||||
|
|
||||||
|
content = getattr(message, "content", "")
|
||||||
|
if content:
|
||||||
|
return str(content)
|
||||||
|
|
||||||
|
tool_calls = getattr(message, "tool_calls", None) or []
|
||||||
|
if tool_calls:
|
||||||
|
call = tool_calls[0]
|
||||||
|
return f"{call['name']}({call['args']})"
|
||||||
|
|
||||||
|
return ""
|
||||||
|
|
||||||
|
|
||||||
|
def format_chunk_message(chunk: tuple[Any, dict[str, Any]]) -> None:
|
||||||
|
"""Print token chunks and separate LangGraph steps."""
|
||||||
|
|
||||||
|
global _current_step
|
||||||
|
|
||||||
|
message, meta = chunk
|
||||||
|
langgraph_step = meta.get("langgraph_step")
|
||||||
|
if _current_step is None:
|
||||||
|
_current_step = langgraph_step
|
||||||
|
elif langgraph_step != _current_step:
|
||||||
|
_current_step = langgraph_step
|
||||||
|
print("\n--- --- ---\n", end="")
|
||||||
|
|
||||||
|
if message.content:
|
||||||
|
print(message.content, end="", flush=True)
|
||||||
|
|
||||||
|
|
||||||
|
def run_stream(query: str) -> None:
|
||||||
|
"""Run the agent with .stream() and handle messages/updates chunks."""
|
||||||
|
|
||||||
|
global _current_step
|
||||||
|
_current_step = None
|
||||||
|
|
||||||
|
stream = agent.stream(
|
||||||
|
{"messages": [{"role": "human", "content": query}]},
|
||||||
stream_mode=["messages", "updates"],
|
stream_mode=["messages", "updates"],
|
||||||
):
|
)
|
||||||
|
|
||||||
|
for chunk in stream:
|
||||||
|
chunk_type, chunk_data = chunk
|
||||||
|
|
||||||
if chunk_type == "messages":
|
if chunk_type == "messages":
|
||||||
message, meta = chunk_data
|
format_chunk_message(chunk_data)
|
||||||
step = meta.get("langgraph_step")
|
|
||||||
if current_step is None:
|
if chunk_type == "updates":
|
||||||
current_step = step
|
model_update = chunk_data.get("model")
|
||||||
elif step != current_step:
|
if model_update:
|
||||||
print("\n--- --- ---\n", end="")
|
last_message = model_update["messages"][-1]
|
||||||
current_step = step
|
formatted = format_message(last_message)
|
||||||
if message.content:
|
if formatted:
|
||||||
print(message.content, end="")
|
print(formatted, end="", flush=True)
|
||||||
elif chunk_type == "updates":
|
|
||||||
last_message = chunk_data.get("model", {}).get("messages", [-1])[-1]
|
|
||||||
try:
|
|
||||||
if hasattr(last_message, "content") and last_message.content:
|
|
||||||
print(last_message.content, end="")
|
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
print()
|
print()
|
||||||
state["messages"] = graph.invoke(
|
|
||||||
{"input": user_input, "messages": state["messages"]}
|
|
||||||
)["messages"]
|
def main() -> None:
|
||||||
|
"""Read one CLI query or start a small interactive loop."""
|
||||||
|
|
||||||
|
if len(sys.argv) > 1:
|
||||||
|
run_stream(" ".join(sys.argv[1:]))
|
||||||
|
return
|
||||||
|
|
||||||
|
print("Stream-mode AI agent. Type 'exit' to quit.")
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
query = input("You: ").strip()
|
||||||
|
except EOFError:
|
||||||
|
print()
|
||||||
|
break
|
||||||
|
|
||||||
|
if query.lower() in {"exit", "quit"}:
|
||||||
|
break
|
||||||
|
if query:
|
||||||
|
run_stream(query)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
Reference in New Issue
Block a user