From 6cfc23d91c3344ab9490fb18b189dcd3e08e2a06 Mon Sep 17 00:00:00 2001 From: lonpatovaadelina Date: Thu, 28 May 2026 14:14:15 +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 | 116 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 60 insertions(+), 56 deletions(-) diff --git a/README.md b/README.md index 0f2f420..dc0292b 100644 --- a/README.md +++ b/README.md @@ -1,96 +1,100 @@ # Stream‑Mode AI Agent -A lightweight LangChain agent that prints its output **as it is generated** using the `.stream()` API. -The project demonstrates how to replace a single `.invoke()` call with streaming so that users see partial responses in real time. +A lightweight LangChain agent that streams its responses token by token using the `rich` library for pretty console output. > **Why stream?** -> When an LLM produces long answers or calls multiple tools, waiting for the whole result can feel like a freeze. Streaming gives instant feedback and improves UX. +> 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. --- -## 📦 Project Structure +## 📦 Features -``` -. -├── agent.py # Agent definition with streaming support -├── client.py # Simple CLI to interact with the agent -└── README.md -``` - -- **agent.py** – creates an agent that uses a single tool (`get_price`) and streams its output. -- **client.py** – runs the agent in a loop, reading user queries from stdin. +- 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`) --- ## ⚙️ Prerequisites -| Item | Version | -|------|---------| +| Item | Minimum Version | +|------|-----------------| | Python | 3.10+ | -| pip | latest | -| LangChain | `>=0.2.0` | -| Rich | `>=13.0` | +| pip | – | +| OpenAI API key | Set as environment variable `OPENAI_API_KEY` | -> **Tip:** Use a virtual environment to keep dependencies isolated. +> **Tip:** Create a virtual environment before installing dependencies. + +```bash +python -m venv .venv +source .venv/bin/activate # On Windows: .\.venv\Scripts\activate +``` --- -## 🚀 Installation +## 📥 Installation ```bash -# Clone the repo (or copy the files) -git clone https://github.com/your-username/stream-agent.git -cd stream-agent - -# Create and activate a venv (optional but recommended) -python -m venv .venv -source .venv/bin/activate # Windows: .venv\Scripts\activate +# 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 --upgrade pip -pip install langchain rich +pip install -r requirements.txt ``` -> If you plan to use an OpenAI model, set the API key: -> ```bash -> export OPENAI_API_KEY="sk-..." -> ``` +`requirements.txt`: + +```text +langchain==0.2.* +openai==1.* +rich==13.* +``` + +> Adjust the version numbers if you prefer newer releases. --- -## 📖 Usage - -Run the client script and type your questions. The agent will stream its answer token‑by‑token. +## 🚀 Running the Agent ```bash -python client.py +export OPENAI_API_KEY="sk-..." # On Windows: set OPENAI_API_KEY=sk-... +python solution.py ``` -Example session: - -``` -$ python client.py -Enter your query (Ctrl+C to exit): -> What is the price of Bitcoin today? - -Agent: Checking price... -Agent: Current BTC price is $42,300.00 -``` - -The output appears progressively as the LLM generates it. +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. --- -## 🛠️ Customization +## 📄 Example -- **Add more tools** – decorate a function with `@tool` and pass it to `create_agent`. -- **Change model** – modify the `model_name` argument in `agent.py`. -- **Adjust streaming format** – tweak the `stream()` call or use Rich’s live rendering for fancy UI. +```text +$ python solution.py +🤖 What is the capital of France? +🇫🇷 Paris +``` + +The agent will stream each word (or token) as it arrives, giving a smooth typing‑like effect. --- -## 📄 License +## 🔧 Customization -MIT © 2026 +- **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. ---- \ No newline at end of file +--- + +## 📚 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