LangChain-19 TokenUsage 通过回调函数 只需一行 统计当前调用API的花费情况 包含Tokens数量和Cost花费 原创
安装依赖
pip install -qU langchain-core langchain-openai
编写代码
我们将通过:get_openai_callback
函数,LangChain
已经帮我们封装好了。
此时我们通过该回调函数,变可以完成对消费的计算和统计。
from langchain.callbacks import get_openai_callback
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(
model="gpt-3.5-turbo",
)
with get_openai_callback() as cb:
result = llm.invoke("Tell me a joke")
print(cb)
运行结果
➜ python3 test19.py
/Users/wuzikang/Desktop/py/langchain_test/own_learn/env/lib/python3.12/site-packages/langchain/callbacks/__init__.py:37: LangChainDeprecationWarning: Importing this callback from langchain is deprecated. Importing it from langchain will no longer be supported as of langchain==0.2.0. Please import from langchain-community instead:
`from langchain_community.callbacks import get_openai_callback`.
To install langchain-community run `pip install -U langchain-community`.
warnings.warn(
Tokens Used: 26
Prompt Tokens: 12
Completion Tokens: 14
Successful Requests: 1
Total Cost (USD): $4.6e-05
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)