-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync_usage.py
More file actions
46 lines (36 loc) · 998 Bytes
/
async_usage.py
File metadata and controls
46 lines (36 loc) · 998 Bytes
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
"""Async usage of the Cycles client."""
import asyncio
from runcycles import (
AsyncCyclesClient,
CyclesConfig,
CyclesMetrics,
cycles,
get_cycles_context,
)
config = CyclesConfig(
base_url="http://localhost:7878",
api_key="your-api-key",
tenant="acme",
)
client = AsyncCyclesClient(config)
@cycles(
estimate=lambda prompt: len(prompt) * 10,
actual=lambda result: len(result) * 5,
action_kind="llm.completion",
action_name="gpt-4",
client=client,
)
async def call_llm(prompt: str) -> str:
ctx = get_cycles_context()
if ctx:
print(f" Reservation: {ctx.reservation_id}")
ctx.metrics = CyclesMetrics(tokens_input=100, tokens_output=50)
# Simulate async LLM call
await asyncio.sleep(0.1)
return f"Response to: {prompt}"
async def main() -> None:
async with client:
result = await call_llm("Hello, world!")
print(f"Result: {result}")
if __name__ == "__main__":
asyncio.run(main())