From d4c437060594874b12295d94e528def4e934a18b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A0=D0=B8=D0=BD=D0=B0=D1=80=20=D0=9C=D0=B8=D1=80=D0=B7?= =?UTF-8?q?=D0=B0=D0=B3=D0=B8=D1=82=D0=BE=D0=B2?= Date: Tue, 2 Jun 2026 17:01:26 +0000 Subject: [PATCH] Submit solution for task 699cc158d6d3a5544a3ed35b: update main.py --- main.py | 78 ++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 47 insertions(+), 31 deletions(-) diff --git a/main.py b/main.py index da57a0d..a0b8429 100644 --- a/main.py +++ b/main.py @@ -1,36 +1,52 @@ import sys from agent import graph -if __name__ == "__main__": +def main(): print("LangGraph stream‑mode demo. Type 'exit' to quit.") state = {"messages": []} - for line in sys.stdin: - user_input = line.rstrip('\n') - if not user_input: - continue - if user_input.lower() in {"exit", "quit"}: - break - # Stream output with separators between steps - current_step = None - for chunk_type, chunk_data in graph.stream({"input": user_input, "messages": state["messages"]}, stream_mode=["messages", "updates"]): - if chunk_type == "messages": - message, meta = chunk_data - step = meta.get("langgraph_step") - if current_step is None: - current_step = step - elif step != current_step: - print("\n--- --- ---\n", end="") - current_step = step - if message.content: - print(message.content, end="") - elif chunk_type == "updates": - last_message = chunk_data.get("model", {}).get("messages", [-1])[-1] - # If the last message is an AIMessage with content, print it - try: - if hasattr(last_message, "content") and last_message.content: - print(last_message.content, end="") - except Exception: - pass - print() - state["messages"] = graph.invoke({"input": user_input, "messages": state["messages"]})["messages"] - print("Goodbye!") \ No newline at end of file + # Если передан аргумент, используем его как сообщение; иначе читаем из 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 + for chunk_type, chunk_data in graph.stream( + {"input": user_input, "messages": state["messages"]}, + stream_mode=["messages", "updates"], + ): + if chunk_type == "messages": + message, meta = chunk_data + step = meta.get("langgraph_step") + if current_step is None: + current_step = step + elif step != current_step: + print("\n--- --- ---\n", end="") + current_step = step + if message.content: + print(message.content, end="") + 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() + state["messages"] = graph.invoke( + {"input": user_input, "messages": state["messages"]} + )["messages"] + +if __name__ == "__main__": + main() \ No newline at end of file