You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
To ensure that the usage in the OpenAI compatible API's LLM update matches the response data when using tiktoken in a self-hosted Dify version 0.10.1 setup, you need to accurately calculate the number of tokens used in the request and response. This involves using the _num_tokens_from_string and _num_tokens_from_messages methods in the DeepSeekLargeLanguageModel class, which is a subclass of OpenAILargeLanguageModel.
Token Calculation for Text Completion:
Use the _num_tokens_from_string method to calculate the number of tokens for text completion models. This method uses the tiktoken package with the cl100k_base encoding to encode the text and count the tokens.
Token Calculation for Chat Models:
Use the _num_tokens_from_messages method to calculate the number of tokens for chat models like GPT-3.5-turbo and GPT-4. This method also uses the cl100k_base encoding and accounts for additional tokens per message and per name.
By using these methods, you can ensure that the token usage in your API requests matches the response data, as both are calculated using the same encoding and logic [1][2].
Self Checks
Dify version
0.10.1
Cloud or Self Hosted
Self Hosted (Source)
Steps to reproduce
usage in openai compatiable api' llm count by tiktoken, but response data may include field usage
✔️ Expected Behavior
usage is same with response
❌ Actual Behavior
usage is not same with response
The text was updated successfully, but these errors were encountered: