commit 130c4f20fbea480a26a9df06adc241c363cf6b26 Author: kuzakhmetovartur Date: Thu May 28 13:09:30 2026 +0300 feat: solution for 'Untitled Task' diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b16538b --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +node_modules/ +.env +dist/ +build/ +*.log diff --git a/README.md b/README.md new file mode 100644 index 0000000..eecb3b0 --- /dev/null +++ b/README.md @@ -0,0 +1,34 @@ +# LangGraph Streaming Agent + +This project demonstrates how to use LangGraph's streaming capabilities to display LLM responses token by token in real time. + +## Prerequisites + +- Python 3.10+ +- An OpenAI API key. Set it in a `.env` file or export `OPENAI_API_KEY`. + +## Installation + +bash +git clone +cd +python -m venv .venv +source .venv/bin/activate # On Windows use `.venv\Scripts\activate` +pip install -r requirements.txt + + +## Usage + +bash +python src/main.py + + +You will be prompted to enter a question. The answer will stream to the console as it is generated. + +## How it works + +The script builds a simple LangGraph agent that uses the OpenAI LLM. It calls `agent.stream()` with `stream_mode=['messages', 'updates']` and iterates over the returned chunks, printing each token as it arrives. + +## License + +MIT \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..842d5d9 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +langgraph +langchain +langchain-openai +python-dotenv \ No newline at end of file diff --git a/src/main.py b/src/main.py new file mode 100644 index 0000000..74c2423 --- /dev/null +++ b/src/main.py @@ -0,0 +1,52 @@ +import os +from dotenv import load_dotenv +from langgraph import AgentBuilder +from langchain_openai import ChatOpenAI + +load_dotenv() + +OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") +if not OPENAI_API_KEY: + raise ValueError("OPENAI_API_KEY not set in environment") + +llm = ChatOpenAI(temperature=0, model="gpt-3.5-turbo") + +builder = AgentBuilder(llm=llm) +agent = builder.build() + +def format_message(message) -> str: + if message.content: + return message.content + if message.tool_calls: + tool = message.tool_calls[0] + return f"{tool['name']}({tool['args']})" + return "" + +step = 1 + +def format_chunk_message(chunk): + global step + message, meta = chunk + if meta.get("langgraph_step") != step: + step = meta.get("langgraph_step") + print("\n --- --- --- \n") + if message.content: + print(message.content, end="", flush=True) + +def main(): + user_input = input("Enter your question: ") + stream = agent.stream( + {"messages": [{"role": "human", "content": user_input}]}, + stream_mode=["messages", "updates"] + ) + for chunk_type, chunk_data in stream: + if chunk_type == "messages": + format_chunk_message(chunk_data) + elif chunk_type == "updates": + if chunk_data.get("model"): + last_message = chunk_data["model"]["messages"][-1] + print(format_message(last_message)) + print("\n\nDone.") + +if __name__ == "__main__": + main() \ No newline at end of file