-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathproxy_service_basic.py
More file actions
62 lines (48 loc) · 1.88 KB
/
proxy_service_basic.py
File metadata and controls
62 lines (48 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
"""
cascadeflow - ProxyService Example (Routing + Execution)
This example shows how to use the proxy components directly (router + handler).
Use this when you want to build your own gateway/service and:
- route by `provider:model` prefixes (e.g. "openai:gpt-4o-mini"), and
- centralize usage/cost tracking across providers.
For a drop-in integration that works with existing OpenAI/Anthropic SDK clients,
see the Gateway Server guide: docs/guides/gateway.md
Run:
export OPENAI_API_KEY=...
python examples/proxy_service_basic.py
"""
import asyncio
import os
from cascadeflow.proxy import ProxyHandler, ProxyRequest, ProxyRoute, ProxyRouter, ProxyService
from cascadeflow.telemetry.cost_tracker import CostTracker
async def main() -> None:
openai_key = os.getenv("OPENAI_API_KEY")
if not openai_key:
raise SystemExit("Missing OPENAI_API_KEY")
routes = [
ProxyRoute(
name="openai",
provider="openai",
base_url="https://api.openai.com",
api_key=openai_key,
models={"gpt-4o-mini", "gpt-4o"},
)
]
router = ProxyRouter(routes, default_provider="openai")
tracker = CostTracker(verbose=True)
async with ProxyHandler(cost_tracker=tracker) as handler:
service = ProxyService(router, handler)
request = ProxyRequest(
method="POST",
path="/v1/chat/completions",
headers={},
body={
"model": "openai:gpt-4o-mini",
"messages": [{"role": "user", "content": "Say hello in one sentence."}],
},
)
result = await service.handle(request)
print(result.data["choices"][0]["message"]["content"])
print(f"provider={result.provider} model={result.model} cost=${result.cost}")
print(f"tracker_total=${tracker.total_cost}")
if __name__ == "__main__":
asyncio.run(main())