100 lines
2.3 KiB
Markdown
100 lines
2.3 KiB
Markdown
# Stream‑Mode 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.
|
||
|
||
---
|
||
|
||
## 📦 Features
|
||
|
||
- Uses **LangChain** for agent orchestration
|
||
- Powered by **OpenAI GPT‑4o-mini** (or any OpenAI model)
|
||
- Real‑time streaming via `llm.stream(...)`
|
||
- Pretty console output with **rich**
|
||
- Simple, single‑file implementation (`solution.py`)
|
||
|
||
---
|
||
|
||
## ⚙️ Prerequisites
|
||
|
||
| Item | Minimum Version |
|
||
|------|-----------------|
|
||
| Python | 3.10+ |
|
||
| pip | – |
|
||
| OpenAI API key | Set as environment variable `OPENAI_API_KEY` |
|
||
|
||
> **Tip:** Create a virtual environment before installing dependencies.
|
||
|
||
```bash
|
||
python -m venv .venv
|
||
source .venv/bin/activate # On Windows: .\.venv\Scripts\activate
|
||
```
|
||
|
||
---
|
||
|
||
## 📥 Installation
|
||
|
||
```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
|
||
```
|
||
|
||
`requirements.txt`:
|
||
|
||
```text
|
||
langchain==0.2.*
|
||
openai==1.*
|
||
rich==13.*
|
||
```
|
||
|
||
> Adjust the version numbers if you prefer newer releases.
|
||
|
||
---
|
||
|
||
## 🚀 Running 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**.
|
||
You’ll see the answer appear token by token in real time.
|
||
|
||
---
|
||
|
||
## 📄 Example
|
||
|
||
```text
|
||
$ 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! 🚀 |