From 521d3719a3d025f7e1182809a2a22c6ec35eabf7 Mon Sep 17 00:00:00 2001 From: lonpatovaadelina Date: Thu, 28 May 2026 14:22:09 +0000 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20README.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 146 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 81 insertions(+), 65 deletions(-) diff --git a/README.md b/README.md index dc0292b..d4ca081 100644 --- a/README.md +++ b/README.md @@ -1,100 +1,116 @@ # Stream‑Mode AI Agent -A lightweight LangChain agent that streams its responses token by token using the `rich` library for pretty console output. - -> **Why stream?** -> When an LLM generates a long answer or calls multiple tools, waiting until the entire response is ready can feel like a freeze. Streaming lets you see the answer as it is produced, improving interactivity and user experience. +A lightweight demo of a LangChain agent that streams its output token‑by‑token instead of waiting for the entire response. +The project showcases how to replace a single `.invoke()` call with `.stream()`, so you can see the answer appear in real time. --- -## 📦 Features +## 📖 Description -- Uses **LangChain** for agent orchestration -- Powered by **OpenAI GPT‑4o-mini** (or any OpenAI model) -- Real‑time streaming via `llm.stream(...)` -- Pretty console output with **rich** -- Simple, single‑file implementation (`solution.py`) +- **Stack**: `LangChain`, `create_agent`, `@tool`, `rich` +- **Files**: + - `agent.py` – defines tools and creates an agent that streams its output. + - `client.py` – simple CLI to interact with the agent. + +The agent can call external tools (e.g., a calculator) while streaming, making it feel more interactive for long answers or multi‑step reasoning. --- ## ⚙️ Prerequisites -| Item | Minimum Version | -|------|-----------------| -| Python | 3.10+ | -| pip | – | -| OpenAI API key | Set as environment variable `OPENAI_API_KEY` | +| Item | Version | +|------|---------| +| Python | ≥ 3.10 | +| pip | Latest | +| Virtual environment (recommended) | Any | -> **Tip:** Create a virtual environment before installing dependencies. +The project uses the following packages: ```bash -python -m venv .venv -source .venv/bin/activate # On Windows: .\.venv\Scripts\activate +langchain==0.2.* # LangChain core +rich==13.* # Pretty console output ``` --- -## 📥 Installation +## 🚀 Installation + +1. **Clone the repository** + + ```bash + git clone https://github.com/your-username/stream-ai-agent.git + cd stream-ai-agent + ``` + +2. **Create a virtual environment (optional but recommended)** + + ```bash + python -m venv .venv + source .venv/bin/activate # On Windows: .venv\Scripts\activate + ``` + +3. **Install dependencies** + + ```bash + pip install -r requirements.txt + ``` + +> If you don't have a `requirements.txt`, create one with: +> +> ``` +> langchain>=0.2,<0.3 +> rich>=13,<14 +> ``` + +--- + +## 🏃‍♂️ Running the Agent ```bash -# Clone the repo (or copy solution.py into your project) -git clone https://github.com//stream-ai-agent.git -cd stream-ai-agent - -# Install dependencies -pip install -r requirements.txt +python client.py ``` -`requirements.txt`: +You will see a prompt: + +``` +Enter your question (or type 'exit' to quit): +``` + +Type any query, e.g.: + +``` +What is 12 * 8? +``` + +The agent will stream its answer token by token, and if it needs to use the calculator tool, you’ll see the intermediate tool call printed in real time. + +--- + +## 📌 Example Session ```text -langchain==0.2.* -openai==1.* -rich==13.* +$ python client.py +Enter your question (or type 'exit' to quit): What is 12 * 8? + +> Calculating... +> 96 +Answer: 96 ``` -> Adjust the version numbers if you prefer newer releases. +The `>` lines are produced by the `rich` library, showing tool calls and partial outputs as they arrive. --- -## 🚀 Running the Agent +## 🔧 Extending the Agent -```bash -export OPENAI_API_KEY="sk-..." # On Windows: set OPENAI_API_KEY=sk-... -python solution.py -``` - -The script will prompt you for a question. Type your query and press **Enter**. -You’ll see the answer appear token by token in real time. +- **Add more tools**: Decorate a function with `@tool(name="...", description="...")`. +- **Change LLM**: Pass a different model to `create_agent(..., llm=...)`. +- **Custom streaming logic**: Override the default stream handling in `agent.py`. --- -## 📄 Example +## 📄 License -```text -$ python solution.py -🤖 What is the capital of France? -🇫🇷 Paris -``` +MIT © 2026 -The agent will stream each word (or token) as it arrives, giving a smooth typing‑like effect. - ---- - -## 🔧 Customization - -- **Change model** – edit `model="gpt-4o-mi"` in `solution.py`. -- **Add tools** – decorate functions with `@tool` and include them in the agent. -- **Adjust temperature** – modify `temperature=0` to a higher value for more creativity. - ---- - -## 📚 Further Reading - -- [LangChain Docs](https://langchain.com/docs/) -- [OpenAI API Reference](https://platform.openai.com/docs/api-reference) -- [Rich Library](https://rich.readthedocs.io/en/stable/) - ---- - -Happy streaming! 🚀 \ No newline at end of file +---``` \ No newline at end of file