-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathagent.run.stream.py
More file actions
65 lines (46 loc) · 1.88 KB
/
agent.run.stream.py
File metadata and controls
65 lines (46 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""
Run Agent Streaming with get_runner
This example demonstrates how to run an agent with streaming response using get_runner.
"""
import os
from dotenv import load_dotenv
from langbase import Langbase, get_runner
load_dotenv()
def main():
# Check for required environment variables
langbase_api_key = os.environ.get("LANGBASE_API_KEY")
api_key = os.environ.get("LLM_API_KEY")
if not langbase_api_key:
print("❌ Missing LANGBASE_API_KEY in environment variables.")
print("Please set: LANGBASE_API_KEY='your_langbase_api_key' in .env file")
exit(1)
# Initialize Langbase client
langbase = Langbase(api_key=langbase_api_key)
try:
# Get readable stream - equivalent to const {stream} = await langbase.agent.run(...)
response = langbase.agent.run(
stream=True,
model="openai:gpt-4.1-mini",
instructions="You are a helpful assistant that help users summarize text.",
input=[{"role": "user", "content": "Who is an AI Engineer?"}],
api_key=api_key,
)
# Convert the stream to a stream runner - equivalent to getRunner(stream)
runner = get_runner(response)
# Event-like handling in Python
# Method 1: Using iterator pattern (Python equivalent of event listeners)
# Equivalent to runner.on('connect', ...)
print("Stream started.\n")
try:
# Equivalent to runner.on('content', content => {...})
for content in runner.text_generator():
print(content, end="", flush=True)
# Equivalent to runner.on('end', ...)
print("\nStream ended.")
except Exception as error:
# Equivalent to runner.on('error', error => {...})
print(f"Error: {error}")
except Exception as e:
print(f"Error: {e}")
if __name__ == "__main__":
main()