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
We'd like to add a simple token usage tracking feature to our FastMLX application. This will help users understand how many tokens their requests are consuming.
Objective:
Implement a function that counts the number of tokens in the input and output of our AI models.
Tasks:
Create a new function count_tokens(text: str) -> int in the utils.py file.
Use the appropriate tokenizer from our AI model to count tokens.
Integrate this function into the main request processing flow in main.py.
Update the response structure to include token counts.
Example Implementation:
fromtransformersimportAutoTokenizerdefcount_tokens(text: str) ->int:
tokenizer=AutoTokenizer.from_pretrained("gpt2") # or use our model's tokenizerreturnlen(tokenizer.encode(text))
# In main request processing:input_tokens=count_tokens(user_input)
output_tokens=count_tokens(model_output)
total_tokens=input_tokens+output_tokensresponse= {
"output": model_output,
"usage": {
"prompt_tokens": input_tokens,
"completion_tokens": output_tokens,
"total_tokens": total_tokens
}
}
Guidelines:
Focus on basic functionality first. We can optimize later.
Make sure to handle potential errors, like invalid inputs.
Add comments to explain your code.
If you're unsure about anything, feel free to ask questions in the comments!
Hi @Blaizzy . Perhaps this issue could be an opportunity to implement LLM tracking using AgentOps [https://github.com/AgentOps-AI/agentops] (for example). Or do you see this as a step for the future, or is that not the right approach? What are your thoughts?
Description:
We'd like to add a simple token usage tracking feature to our FastMLX application. This will help users understand how many tokens their requests are consuming.
Objective:
Implement a function that counts the number of tokens in the input and output of our AI models.
Tasks:
count_tokens(text: str) -> int
in theutils.py
file.main.py
.Example Implementation:
Guidelines:
Resources:
Definition of Done:
We're excited to see your contribution! This feature will help our users better understand and manage their token usage. Good luck!
The text was updated successfully, but these errors were encountered: