Добавлен README.md

This commit is contained in:
2026-05-28 14:22:09 +00:00
parent 0787d79295
commit 521d3719a3
+71 -55
View File
@@ -1,100 +1,116 @@
# StreamMode AI Agent # StreamMode AI Agent
A lightweight LangChain agent that streams its responses token by token using the `rich` library for pretty console output. A lightweight demo of a LangChain agent that streams its output tokenbytoken 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.
> **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.
--- ---
## 📦 Features ## 📖 Description
- Uses **LangChain** for agent orchestration - **Stack**: `LangChain`, `create_agent`, `@tool`, `rich`
- Powered by **OpenAI GPT4o-mini** (or any OpenAI model) - **Files**:
- Realtime streaming via `llm.stream(...)` - `agent.py` defines tools and creates an agent that streams its output.
- Pretty console output with **rich** - `client.py` simple CLI to interact with the agent.
- Simple, singlefile implementation (`solution.py`)
The agent can call external tools (e.g., a calculator) while streaming, making it feel more interactive for long answers or multistep reasoning.
--- ---
## ⚙️ Prerequisites ## ⚙️ Prerequisites
| Item | Minimum Version | | Item | Version |
|------|-----------------| |------|---------|
| Python | 3.10+ | | Python | 3.10 |
| pip | | | pip | Latest |
| OpenAI API key | Set as environment variable `OPENAI_API_KEY` | | Virtual environment (recommended) | Any |
> **Tip:** Create a virtual environment before installing dependencies. The project uses the following packages:
```bash
langchain==0.2.* # LangChain core
rich==13.* # Pretty console output
```
---
## 🚀 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 ```bash
python -m venv .venv python -m venv .venv
source .venv/bin/activate # On Windows: .\.venv\Scripts\activate source .venv/bin/activate # On Windows: .venv\Scripts\activate
``` ```
--- 3. **Install dependencies**
## 📥 Installation
```bash ```bash
# Clone the repo (or copy solution.py into your project)
git clone https://github.com/<your-username>/stream-ai-agent.git
cd stream-ai-agent
# Install dependencies
pip install -r requirements.txt pip install -r requirements.txt
``` ```
`requirements.txt`: > If you don't have a `requirements.txt`, create one with:
>
```text > ```
langchain==0.2.* > langchain>=0.2,<0.3
openai==1.* > rich>=13,<14
rich==13.* > ```
```
> Adjust the version numbers if you prefer newer releases.
--- ---
## 🚀 Running the Agent ## 🏃‍♂️ Running the Agent
```bash ```bash
export OPENAI_API_KEY="sk-..." # On Windows: set OPENAI_API_KEY=sk-... python client.py
python solution.py
``` ```
The script will prompt you for a question. Type your query and press **Enter**. You will see a prompt:
Youll see the answer appear token by token in real time.
```
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, youll see the intermediate tool call printed in real time.
--- ---
## 📄 Example ## 📌 Example Session
```text ```text
$ python solution.py $ python client.py
🤖 What is the capital of France? Enter your question (or type 'exit' to quit): What is 12 * 8?
🇫🇷 Paris
> Calculating...
> 96
Answer: 96
``` ```
The agent will stream each word (or token) as it arrives, giving a smooth typinglike effect. The `>` lines are produced by the `rich` library, showing tool calls and partial outputs as they arrive.
--- ---
## 🔧 Customization ## 🔧 Extending the Agent
- **Change model** edit `model="gpt-4o-mi"` in `solution.py`. - **Add more tools**: Decorate a function with `@tool(name="...", description="...")`.
- **Add tools** decorate functions with `@tool` and include them in the agent. - **Change LLM**: Pass a different model to `create_agent(..., llm=...)`.
- **Adjust temperature** modify `temperature=0` to a higher value for more creativity. - **Custom streaming logic**: Override the default stream handling in `agent.py`.
--- ---
## 📚 Further Reading ## 📄 License
- [LangChain Docs](https://langchain.com/docs/) MIT © 2026
- [OpenAI API Reference](https://platform.openai.com/docs/api-reference)
- [Rich Library](https://rich.readthedocs.io/en/stable/)
--- ---```
Happy streaming! 🚀