安装依赖
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