6cfc23d91c3344ab9490fb18b189dcd3e08e2a06
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.
python -m venv .venv
source .venv/bin/activate # On Windows: .\.venv\Scripts\activate
📥 Installation
# 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:
langchain==0.2.*
openai==1.*
rich==13.*
Adjust the version numbers if you prefer newer releases.
🚀 Running the Agent
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
$ 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"insolution.py. - Add tools – decorate functions with
@tooland include them in the agent. - Adjust temperature – modify
temperature=0to a higher value for more creativity.
📚 Further Reading
Happy streaming! 🚀
Description
Languages
Python
100%