Добавлен README.md

This commit is contained in:
2026-05-28 14:14:15 +00:00
parent 019dac3f53
commit 6cfc23d91c
+60 -56
View File
@@ -1,96 +1,100 @@
# StreamMode AI Agent
A lightweight LangChain agent that prints its output **as it is generated** using the `.stream()` API.
The project demonstrates how to replace a single `.invoke()` call with streaming so that users see partial responses in real time.
A lightweight LangChain agent that streams its responses token by token using the `rich` library for pretty console output.
> **Why stream?**
> When an LLM produces long answers or calls multiple tools, waiting for the whole result can feel like a freeze. Streaming gives instant feedback and improves UX.
> 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.
---
## 📦 Project Structure
## 📦 Features
```
.
├── agent.py # Agent definition with streaming support
├── client.py # Simple CLI to interact with the agent
└── README.md
```
- **agent.py** creates an agent that uses a single tool (`get_price`) and streams its output.
- **client.py** runs the agent in a loop, reading user queries from stdin.
- 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`)
---
## ⚙️ Prerequisites
| Item | Version |
|------|---------|
| Item | Minimum Version |
|------|-----------------|
| Python | 3.10+ |
| pip | latest |
| LangChain | `>=0.2.0` |
| Rich | `>=13.0` |
| pip | |
| OpenAI API key | Set as environment variable `OPENAI_API_KEY` |
> **Tip:** Use a virtual environment to keep dependencies isolated.
> **Tip:** Create a virtual environment before installing dependencies.
```bash
python -m venv .venv
source .venv/bin/activate # On Windows: .\.venv\Scripts\activate
```
---
## 🚀 Installation
## 📥 Installation
```bash
# Clone the repo (or copy the files)
git clone https://github.com/your-username/stream-agent.git
cd stream-agent
# Create and activate a venv (optional but recommended)
python -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
# 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 --upgrade pip
pip install langchain rich
pip install -r requirements.txt
```
> If you plan to use an OpenAI model, set the API key:
> ```bash
> export OPENAI_API_KEY="sk-..."
> ```
`requirements.txt`:
```text
langchain==0.2.*
openai==1.*
rich==13.*
```
> Adjust the version numbers if you prefer newer releases.
---
## 📖 Usage
Run the client script and type your questions. The agent will stream its answer tokenbytoken.
## 🚀 Running the Agent
```bash
python client.py
export OPENAI_API_KEY="sk-..." # On Windows: set OPENAI_API_KEY=sk-...
python solution.py
```
Example session:
```
$ python client.py
Enter your query (Ctrl+C to exit):
> What is the price of Bitcoin today?
Agent: Checking price...
Agent: Current BTC price is $42,300.00
```
The output appears progressively as the LLM generates it.
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.
---
## 🛠️ Customization
## 📄 Example
- **Add more tools** decorate a function with `@tool` and pass it to `create_agent`.
- **Change model** modify the `model_name` argument in `agent.py`.
- **Adjust streaming format** tweak the `stream()` call or use Richs live rendering for fancy UI.
```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 typinglike effect.
---
## 📄 License
## 🔧 Customization
MIT © 2026
- **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! 🚀