-
Notifications
You must be signed in to change notification settings - Fork 244
Expand file tree
/
Copy pathlibsy.py
More file actions
85 lines (71 loc) · 2.71 KB
/
Copy pathlibsy.py
File metadata and controls
85 lines (71 loc) · 2.71 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env python3
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
"""Drive a libsy algorithm stream from Python."""
import asyncio
from collections.abc import AsyncIterator, Mapping
from switchyard.libsy import LlmResponse, Step, algorithms
class EchoClient:
"""Return a fixed completion for any selected target."""
async def call(
self,
request: Mapping[str, object],
model: str,
) -> LlmResponse.Agg | LlmResponse.Stream:
if request.get("stream") is True:
async def events() -> AsyncIterator[Mapping[str, object]]:
yield {
"preservation": None,
"normalized": [{"MessageStart": {"id": "echo", "model": model}}],
}
yield {
"preservation": None,
"normalized": [{"TextDelta": {"index": 0, "text": "Hello"}}],
}
yield {
"preservation": None,
"normalized": [{"MessageStop": {"reason": "end_turn"}}],
}
return LlmResponse.Stream(events())
return LlmResponse.Agg(
{
"model": model,
"outputs": [
{
"role": "assistant",
"content": [{"type": "text", "text": "Hello"}],
}
],
}
)
async def main() -> None:
"""Run random routing and serve its selected target."""
request = {
"model": "auto",
"stream": True,
"messages": [{"role": "user", "content": [{"type": "text", "text": "Hello"}]}],
}
client = EchoClient()
algorithm = algorithms.random(
["fast", "quality"],
weights=[1, 3],
seed=42,
)
async for step in algorithm.run_stream(request):
match step:
case Step.CallModel(call):
call.respond(await client.call(call.request, call.models[0]))
case Step.Done(outcome):
print("Decision:", outcome.selected_model_ids[0])
response = outcome.response or await client.call(
outcome.request,
outcome.selected_model_ids[0],
)
match response:
case LlmResponse.Agg(aggregate_response):
print("Response:", aggregate_response)
case LlmResponse.Stream(response_stream):
async for event in response_stream:
print("Response event:", event)
if __name__ == "__main__":
asyncio.run(main())