Skip to content

Commit 03244ca

Browse files
committed
📦 NEW: Pipe Run typed example
1 parent c917b31 commit 03244ca

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import json
2+
import os
3+
4+
from dotenv import load_dotenv
5+
6+
from langbase import Langbase, StreamEventType, get_typed_runner
7+
8+
9+
def main():
10+
load_dotenv()
11+
12+
# Get API key from environment variable
13+
langbase_api_key = os.getenv("LANGBASE_API_KEY")
14+
15+
# Initialize the client
16+
lb = Langbase(api_key=langbase_api_key)
17+
18+
# Name of the pipe to run
19+
pipe_name = "summary-agent" # Replace with your pipe name
20+
21+
try:
22+
# Get streaming response
23+
response = lb.pipes.run(
24+
name=pipe_name,
25+
messages=[{"role": "user", "content": "What is the weather in Tokyo?"}],
26+
stream=True,
27+
tools=[
28+
{
29+
"type": "function",
30+
"function": {
31+
"name": "get_current_weather",
32+
"description": "Get the current weather of a given location",
33+
"parameters": {
34+
"type": "object",
35+
"required": ["location"],
36+
"properties": {
37+
"unit": {
38+
"enum": ["celsius", "fahrenheit"],
39+
"type": "string",
40+
},
41+
"location": {
42+
"type": "string",
43+
"description": "The city and state, e.g. San Francisco, CA",
44+
},
45+
},
46+
},
47+
},
48+
}
49+
],
50+
)
51+
52+
# Create typed stream processor
53+
runner = get_typed_runner(response)
54+
55+
# Register event handlers
56+
runner.on(
57+
StreamEventType.CONNECT,
58+
lambda event: print(f"✓ Connected! Thread ID: {event['threadId']}\n"),
59+
)
60+
61+
runner.on(
62+
StreamEventType.CONTENT,
63+
lambda event: print(event["content"], end="", flush=True),
64+
)
65+
66+
runner.on(
67+
StreamEventType.TOOL_CALL,
68+
lambda event: print(
69+
f"\n🔧 Tool call: {event['toolCall']['function']['name']}"
70+
f"\n🔧 Tool call: {json.dumps(event['toolCall'], indent=2)}"
71+
),
72+
)
73+
74+
runner.on(
75+
StreamEventType.COMPLETION,
76+
lambda event: print(f"\n\n✓ Completed! Reason: {event['reason']}"),
77+
)
78+
79+
runner.on(
80+
StreamEventType.ERROR,
81+
lambda event: print(f"\n❌ Error: {event['message']}"),
82+
)
83+
84+
runner.on(
85+
StreamEventType.END,
86+
lambda event: print(f"⏱️ Total duration: {event['duration']:.2f}s"),
87+
)
88+
89+
# Process the stream
90+
runner.process()
91+
92+
except Exception as e:
93+
print(f"Error: {e}")
94+
95+
96+
if __name__ == "__main__":
97+
main()

0 commit comments

Comments
 (0)