feat: solution for 'Untitled Task'
This commit is contained in:
@@ -0,0 +1,5 @@
|
|||||||
|
node_modules/
|
||||||
|
.env
|
||||||
|
dist/
|
||||||
|
build/
|
||||||
|
*.log
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
# LangGraph Streaming Agent
|
||||||
|
|
||||||
|
This project demonstrates how to use LangGraph's streaming capabilities to display LLM responses token by token in real time.
|
||||||
|
|
||||||
|
## Prerequisites
|
||||||
|
|
||||||
|
- Python 3.10+
|
||||||
|
- An OpenAI API key. Set it in a `.env` file or export `OPENAI_API_KEY`.
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
bash
|
||||||
|
git clone <repo-url>
|
||||||
|
cd <repo-dir>
|
||||||
|
python -m venv .venv
|
||||||
|
source .venv/bin/activate # On Windows use `.venv\Scripts\activate`
|
||||||
|
pip install -r requirements.txt
|
||||||
|
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
bash
|
||||||
|
python src/main.py
|
||||||
|
|
||||||
|
|
||||||
|
You will be prompted to enter a question. The answer will stream to the console as it is generated.
|
||||||
|
|
||||||
|
## How it works
|
||||||
|
|
||||||
|
The script builds a simple LangGraph agent that uses the OpenAI LLM. It calls `agent.stream()` with `stream_mode=['messages', 'updates']` and iterates over the returned chunks, printing each token as it arrives.
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
MIT
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
langgraph
|
||||||
|
langchain
|
||||||
|
langchain-openai
|
||||||
|
python-dotenv
|
||||||
+52
@@ -0,0 +1,52 @@
|
|||||||
|
import os
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
from langgraph import AgentBuilder
|
||||||
|
from langchain_openai import ChatOpenAI
|
||||||
|
|
||||||
|
load_dotenv()
|
||||||
|
|
||||||
|
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
||||||
|
if not OPENAI_API_KEY:
|
||||||
|
raise ValueError("OPENAI_API_KEY not set in environment")
|
||||||
|
|
||||||
|
llm = ChatOpenAI(temperature=0, model="gpt-3.5-turbo")
|
||||||
|
|
||||||
|
builder = AgentBuilder(llm=llm)
|
||||||
|
agent = builder.build()
|
||||||
|
|
||||||
|
def format_message(message) -> str:
|
||||||
|
if message.content:
|
||||||
|
return message.content
|
||||||
|
if message.tool_calls:
|
||||||
|
tool = message.tool_calls[0]
|
||||||
|
return f"{tool['name']}({tool['args']})"
|
||||||
|
return ""
|
||||||
|
|
||||||
|
step = 1
|
||||||
|
|
||||||
|
def format_chunk_message(chunk):
|
||||||
|
global step
|
||||||
|
message, meta = chunk
|
||||||
|
if meta.get("langgraph_step") != step:
|
||||||
|
step = meta.get("langgraph_step")
|
||||||
|
print("\n --- --- --- \n")
|
||||||
|
if message.content:
|
||||||
|
print(message.content, end="", flush=True)
|
||||||
|
|
||||||
|
def main():
|
||||||
|
user_input = input("Enter your question: ")
|
||||||
|
stream = agent.stream(
|
||||||
|
{"messages": [{"role": "human", "content": user_input}]},
|
||||||
|
stream_mode=["messages", "updates"]
|
||||||
|
)
|
||||||
|
for chunk_type, chunk_data in stream:
|
||||||
|
if chunk_type == "messages":
|
||||||
|
format_chunk_message(chunk_data)
|
||||||
|
elif chunk_type == "updates":
|
||||||
|
if chunk_data.get("model"):
|
||||||
|
last_message = chunk_data["model"]["messages"][-1]
|
||||||
|
print(format_message(last_message))
|
||||||
|
print("\n\nDone.")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Reference in New Issue
Block a user