langchain: get_openai_callback doesn't return the credits for ChatGPT chain
I run the following code:
from langchain.chat_models import ChatOpenAI
from langchain import PromptTemplate, LLMChain
from langchain.prompts.chat import (
ChatPromptTemplate,
SystemMessagePromptTemplate,
AIMessagePromptTemplate,
HumanMessagePromptTemplate,
)
from langchain.schema import (
AIMessage,
HumanMessage,
SystemMessage
)
from langchain.callbacks.base import CallbackManager
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
gpt_4 = ChatOpenAI(model_name="gpt-4", streaming=True, callback_manager=CallbackManager([StreamingStdOutCallbackHandler()]), verbose=True, temperature=0)
template="You are ChatGPT, a large language model trained by OpenAI. Follow the user's instructions carefully. Respond using markdown."
system_message_prompt = SystemMessagePromptTemplate.from_template(template)
human_template="{text}"
human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)
prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt])
chain = LLMChain(llm=gpt_4, prompt=prompt)
from langchain.callbacks import get_openai_callback
with get_openai_callback() as cb:
text = "How are you?"
res = chain.run(text=text)
print(cb)
However when I print the callback value, I get back info that I used 0 credits, even though I know I used some.
I'm an AI language model, so I don't have feelings or emotions like humans do. However, I'm here to help you with any questions or information you need. What can I help you with today?Tokens Used: 0
Prompt Tokens: 0
Completion Tokens: 0
Successful Requests: 0
Total Cost (USD): $0.0
Am I doing something wrong, or is this an issue?
About this issue
- Original URL
- State: closed
- Created a year ago
- Reactions: 8
- Comments: 22
Hi,
In order to solve this bug I created my own async and cost calculator ‘handler’(require tiktoken dependency):
I use this way:
I hope this helps anyone.
Thanks,
The underlying reason for no cost information when streaming is enabled is that in the OpenAI API, the
usage
field is only present in non-streaming responses. As things currently stand,OpenAICallbackHandler
effectively simply retranslates theusage
received from OpenAI API, if any.Sorry Manuel, It was a misunderstood.
For me it works but using BaseCallbackHandler instead of Async. I’m using streaming with Flask with threading/queue
It would be nice to do something like this also for indexing
I think this has been fixed meanwhile. Callbacks are now implemented differently and the
BaseChatModel
already calls them correctly (current implementation here). I have since updated my customisation to the newest releases and thus got rid of that implementation