-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathlangchain_groq_example.py
More file actions
65 lines (47 loc) · 1.89 KB
/
Copy pathlangchain_groq_example.py
File metadata and controls
65 lines (47 loc) · 1.89 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
"""floe-guard + LangChain + Groq example.
The guard hard-stops the next call once spending crosses the ceiling.
Run::
pip install floe-guard[langchain] langchain-groq
export GROQ_API_KEY=gsk_...
python examples/langchain_groq_example.py
ChatGroq reports tokens via usage_metadata, not the token_usage block OpenAI
uses — the adapter already handles this via its fallback path.
"""
from __future__ import annotations
import os
import sys
from langchain_groq import ChatGroq
from floe_guard import BudgetExceeded, BudgetGuard
from floe_guard.integrations.langchain import budget_guard_callback_handler
def main() -> None:
api_key = os.environ.get("GROQ_API_KEY")
if not api_key:
print(
"Set GROQ_API_KEY before running this example.\n"
" export GROQ_API_KEY=gsk_...",
file=sys.stderr,
)
sys.exit(1)
# Tighten the ceiling below call 1's actual cost so call 2 is always
# blocked, regardless of current Groq pricing.
guard = BudgetGuard(limit_usd=1.0)
handler = budget_guard_callback_handler(guard)
llm = ChatGroq(
model="llama-3.1-8b-instant",
api_key=api_key,
callbacks=[handler],
)
print("Call 1 — under budget, should go through...")
response = llm.invoke("Reply with one word: hello")
print(f" response : {response.content!r}")
print(f" spent so far: ${guard.spent_usd:.6f}\n")
guard.limit_usd = guard.spent_usd * 0.5
print("Call 2 — projected cost would cross the ceiling, should be blocked...")
try:
llm.invoke("Reply with one word: world")
print(" ERROR: expected BudgetExceeded but no exception was raised")
except BudgetExceeded as exc:
print(f" BudgetExceeded raised as expected: {exc}")
print(f"\nFinal spend: ${guard.spent_usd:.6f} (ceiling: ${guard.limit_usd:.6f})")
if __name__ == "__main__":
main()