Добавлен README.md

This commit is contained in:
2026-05-28 14:22:09 +00:00
parent 0787d79295
commit 521d3719a3
+81 -65
View File
@@ -1,100 +1,116 @@
# StreamMode 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 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.
---
## 📦 Features
## 📖 Description
- Uses **LangChain** for agent orchestration
- Powered by **OpenAI GPT4o-mini** (or any OpenAI model)
- Realtime streaming via `llm.stream(...)`
- Pretty console output with **rich**
- Simple, singlefile 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 multistep 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/<your-username>/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, youll 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**.
Youll 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 typinglike 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! 🚀
---```