Добавлен README.md
This commit is contained in:
@@ -1,100 +1,116 @@
|
|||||||
# Stream‑Mode AI Agent
|
# Stream‑Mode 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 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.
|
||||||
> **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 GPT‑4o-mini** (or any OpenAI model)
|
- **Files**:
|
||||||
- Real‑time 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, single‑file implementation (`solution.py`)
|
|
||||||
|
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
|
## ⚙️ 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
|
```bash
|
||||||
python -m venv .venv
|
langchain==0.2.* # LangChain core
|
||||||
source .venv/bin/activate # On Windows: .\.venv\Scripts\activate
|
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
|
```bash
|
||||||
# Clone the repo (or copy solution.py into your project)
|
python client.py
|
||||||
git clone https://github.com/<your-username>/stream-ai-agent.git
|
|
||||||
cd stream-ai-agent
|
|
||||||
|
|
||||||
# Install dependencies
|
|
||||||
pip install -r requirements.txt
|
|
||||||
```
|
```
|
||||||
|
|
||||||
`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, you’ll see the intermediate tool call printed in real time.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 📌 Example Session
|
||||||
|
|
||||||
```text
|
```text
|
||||||
langchain==0.2.*
|
$ python client.py
|
||||||
openai==1.*
|
Enter your question (or type 'exit' to quit): What is 12 * 8?
|
||||||
rich==13.*
|
|
||||||
|
> 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
|
- **Add more tools**: Decorate a function with `@tool(name="...", description="...")`.
|
||||||
export OPENAI_API_KEY="sk-..." # On Windows: set OPENAI_API_KEY=sk-...
|
- **Change LLM**: Pass a different model to `create_agent(..., llm=...)`.
|
||||||
python solution.py
|
- **Custom streaming logic**: Override the default stream handling in `agent.py`.
|
||||||
```
|
|
||||||
|
|
||||||
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.
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 📄 Example
|
## 📄 License
|
||||||
|
|
||||||
```text
|
MIT © 2026
|
||||||
$ 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.
|
---```
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## 🔧 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! 🚀
|
|
||||||
Reference in New Issue
Block a user