|
1 | 1 | """ |
2 | | -Run Agent Streaming |
| 2 | +Run Agent Streaming with get_runner |
3 | 3 |
|
4 | | -This example demonstrates how to run an agent with streaming response. |
| 4 | +This example demonstrates how to run an agent with streaming response using get_runner. |
5 | 5 | """ |
6 | 6 |
|
7 | 7 | import os |
8 | | -import sys |
9 | 8 |
|
10 | 9 | from dotenv import load_dotenv |
11 | 10 |
|
12 | | -from langbase import Langbase |
13 | | -from langbase.helper import stream_text |
| 11 | +from langbase import Langbase, get_runner |
14 | 12 |
|
15 | 13 | load_dotenv() |
16 | 14 |
|
17 | 15 |
|
18 | 16 | def main(): |
19 | 17 | # Check for required environment variables |
20 | 18 | langbase_api_key = os.environ.get("LANGBASE_API_KEY") |
21 | | - llm_api_key = os.environ.get("LLM_API_KEY") |
| 19 | + api_key = os.environ.get("LLM_API_KEY") |
22 | 20 |
|
23 | 21 | if not langbase_api_key: |
24 | 22 | print("❌ Missing LANGBASE_API_KEY in environment variables.") |
25 | 23 | print("Please set: export LANGBASE_API_KEY='your_langbase_api_key'") |
26 | 24 | exit(1) |
27 | 25 |
|
28 | | - if not llm_api_key: |
29 | | - print("❌ Missing LLM_API_KEY in environment variables.") |
30 | | - print("Please set: export LLM_API_KEY='your_llm_api_key'") |
31 | | - exit(1) |
32 | | - |
33 | 26 | # Initialize Langbase client |
34 | 27 | langbase = Langbase(api_key=langbase_api_key) |
35 | 28 |
|
36 | | - # Run the agent with streaming |
37 | | - response = langbase.agent_run( |
38 | | - stream=True, |
39 | | - model="openai:gpt-4.1-mini", |
40 | | - api_key=llm_api_key, |
41 | | - instructions="You are a helpful assistant that help users summarize text.", |
42 | | - input=[{"role": "user", "content": "Who is an AI Engineer?"}], |
43 | | - ) |
44 | | - |
45 | | - print("Stream started.\n") |
46 | | - |
47 | | - # Process the streaming response |
48 | | - for line in response.iter_lines(): |
49 | | - if line: |
50 | | - line_str = line.decode("utf-8") |
51 | | - if line_str.startswith("data: "): |
52 | | - data = line_str[6:] # Remove 'data: ' prefix |
53 | | - if data.strip() == "[DONE]": |
54 | | - print("\nStream ended.") |
55 | | - break |
56 | | - try: |
57 | | - import json |
58 | | - |
59 | | - json_data = json.loads(data) |
60 | | - if "choices" in json_data and len(json_data["choices"]) > 0: |
61 | | - delta = json_data["choices"][0].get("delta", {}) |
62 | | - if "content" in delta: |
63 | | - sys.stdout.write(delta["content"]) |
64 | | - sys.stdout.flush() |
65 | | - except json.JSONDecodeError: |
66 | | - # Skip invalid JSON lines |
67 | | - continue |
| 29 | + try: |
| 30 | + # Get readable stream - equivalent to const {stream} = await langbase.agent.run(...) |
| 31 | + response = langbase.agent_run( |
| 32 | + stream=True, |
| 33 | + model="openai:gpt-4.1-mini", |
| 34 | + instructions="You are a helpful assistant that help users summarize text.", |
| 35 | + input=[{"role": "user", "content": "Who is an AI Engineer?"}], |
| 36 | + api_key=api_key |
| 37 | + ) |
| 38 | + |
| 39 | + # Convert the stream to a stream runner - equivalent to getRunner(stream) |
| 40 | + runner = get_runner(response) |
| 41 | + |
| 42 | + # Event-like handling in Python |
| 43 | + # Method 1: Using iterator pattern (Python equivalent of event listeners) |
| 44 | + |
| 45 | + # Equivalent to runner.on('connect', ...) |
| 46 | + print("Stream started.\n") |
| 47 | + |
| 48 | + try: |
| 49 | + # Equivalent to runner.on('content', content => {...}) |
| 50 | + for content in runner.text_generator(): |
| 51 | + print(content, end="", flush=True) |
| 52 | + |
| 53 | + # Equivalent to runner.on('end', ...) |
| 54 | + print("\nStream ended.") |
| 55 | + |
| 56 | + except Exception as error: |
| 57 | + # Equivalent to runner.on('error', error => {...}) |
| 58 | + print(f"Error: {error}") |
| 59 | + |
| 60 | + except Exception as e: |
| 61 | + print(f"Error: {e}") |
68 | 62 |
|
69 | 63 |
|
70 | 64 | if __name__ == "__main__": |
|
0 commit comments