Добавлен README.md

This commit is contained in:
2026-05-28 14:14:15 +00:00
parent 019dac3f53
commit 6cfc23d91c
+59 -55
View File
@@ -1,96 +1,100 @@
# StreamMode AI Agent # StreamMode AI Agent
A lightweight LangChain agent that prints its output **as it is generated** using the `.stream()` API. A lightweight LangChain agent that streams its responses token by token using the `rich` library for pretty console output.
The project demonstrates how to replace a single `.invoke()` call with streaming so that users see partial responses in real time.
> **Why stream?** > **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
``` - Uses **LangChain** for agent orchestration
. - Powered by **OpenAI GPT4o-mini** (or any OpenAI model)
├── agent.py # Agent definition with streaming support - Realtime streaming via `llm.stream(...)`
├── client.py # Simple CLI to interact with the agent - Pretty console output with **rich**
└── README.md - Simple, singlefile implementation (`solution.py`)
```
- **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.
--- ---
## ⚙️ Prerequisites ## ⚙️ Prerequisites
| Item | Version | | Item | Minimum Version |
|------|---------| |------|-----------------|
| Python | 3.10+ | | Python | 3.10+ |
| pip | latest | | pip | |
| LangChain | `>=0.2.0` | | OpenAI API key | Set as environment variable `OPENAI_API_KEY` |
| Rich | `>=13.0` |
> **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 ```bash
# Clone the repo (or copy the files) # Clone the repo (or copy solution.py into your project)
git clone https://github.com/your-username/stream-agent.git git clone https://github.com/<your-username>/stream-ai-agent.git
cd stream-agent cd stream-ai-agent
# Create and activate a venv (optional but recommended)
python -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
# Install dependencies # Install dependencies
pip install --upgrade pip pip install -r requirements.txt
pip install langchain rich
``` ```
> If you plan to use an OpenAI model, set the API key: `requirements.txt`:
> ```bash
> export OPENAI_API_KEY="sk-..." ```text
> ``` langchain==0.2.*
openai==1.*
rich==13.*
```
> Adjust the version numbers if you prefer newer releases.
--- ---
## 📖 Usage ## 🚀 Running the Agent
Run the client script and type your questions. The agent will stream its answer tokenbytoken.
```bash ```bash
python client.py export OPENAI_API_KEY="sk-..." # On Windows: set OPENAI_API_KEY=sk-...
python solution.py
``` ```
Example session: The script will prompt you for a question. Type your query and press **Enter**.
Youll see the answer appear token by token in real time.
```
$ 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.
--- ---
## 🛠️ Customization ## 📄 Example
- **Add more tools** decorate a function with `@tool` and pass it to `create_agent`. ```text
- **Change model** modify the `model_name` argument in `agent.py`. $ python solution.py
- **Adjust streaming format** tweak the `stream()` call or use Richs live rendering for fancy UI. 🤖 What is the capital of France?
🇫🇷 Paris
```
The agent will stream each word (or token) as it arrives, giving a smooth typinglike 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.
--- ---
## 📚 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! 🚀