-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreaming.py
More file actions
32 lines (26 loc) · 831 Bytes
/
streaming.py
File metadata and controls
32 lines (26 loc) · 831 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
"""
Streaming example for LessTokens SDK
"""
import asyncio
import os
from lesstokens_sdk import LessTokensSDK
async def main():
sdk = LessTokensSDK(
api_key=os.getenv("LESSTOKENS_API_KEY", "your-less-tokens-api-key"), provider="openai"
)
async for chunk in sdk.process_prompt_stream(
{
"prompt": "Tell a story about a robot learning to paint",
"llm_config": {
"api_key": os.getenv("OPENAI_API_KEY", "your-openai-api-key"),
"model": "gpt-4",
},
}
):
if chunk.done:
print(f"\n\nTokens saved: {chunk.usage.savings}%")
print(f"Total tokens: {chunk.usage.total_tokens}")
else:
print(chunk.content, end="", flush=True)
if __name__ == "__main__":
asyncio.run(main())