116 lines
2.4 KiB
Markdown
116 lines
2.4 KiB
Markdown
# Stream‑Mode AI Agent
|
||
|
||
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.
|
||
|
||
---
|
||
|
||
## 📖 Description
|
||
|
||
- **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 | Version |
|
||
|------|---------|
|
||
| Python | ≥ 3.10 |
|
||
| pip | Latest |
|
||
| Virtual environment (recommended) | Any |
|
||
|
||
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
|
||
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
|
||
python client.py
|
||
```
|
||
|
||
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
|
||
$ python client.py
|
||
Enter your question (or type 'exit' to quit): What is 12 * 8?
|
||
|
||
> Calculating...
|
||
> 96
|
||
Answer: 96
|
||
```
|
||
|
||
The `>` lines are produced by the `rich` library, showing tool calls and partial outputs as they arrive.
|
||
|
||
---
|
||
|
||
## 🔧 Extending the Agent
|
||
|
||
- **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`.
|
||
|
||
---
|
||
|
||
## 📄 License
|
||
|
||
MIT © 2026
|
||
|
||
---``` |