Comments (6)
I believe we're missing the usage details.
from litellm.
What is the expected behavior from litellm? expose a spend calculating function?
from litellm import cost_calculator_completion
cost = cost_calculator_completion(llm_provider, input_tokens, output_tokens)
??
from litellm.
I think what feels more natural is if litellm
actually returned the number of tokens
- i believe the package currently returns just the text, this makes it hard to calculate what the tokens even are (different for different providers). I believe the openai response object does return this, so it would be natural to me for this library to map to that.
from litellm.
I did something like this of my own, but it's obviously not ideal when dealing with multiple providers (if it's not openai then i'm just assuming num tokens = length of string)
def logging_fn(self, model, scenario, messages, response, logger_fn=None):
try:
self.print_verbose(f"the model is: {model}")
status = "success"
if logger_fn:
logger_fn(model=model, messages=messages, response=response)
# POST this data to the supabase logs
llm_provider = "azure" # assume azure by default
if model in model_provider_map.keys():
llm_provider = model_provider_map["model"]
if scenario == Scenario.generate:
if response:
input_text = " ".join([message["content"] for message in messages])
output_text = response['choices'][0]['message']['content'] if response else ""
number_of_input_tokens = len(input_text)
number_of_output_tokens = len(output_text)
if llm_provider == "openai" or llm_provider == "azure":
number_of_input_tokens = len(self.openai_encoding.encode(input_text))
number_of_output_tokens = len(self.openai_encoding.encode(output_text))
else:
number_of_input_tokens = 0
number_of_output_tokens = 0
elif scenario == Scenario.embed:
self.print_verbose(f"input_text: {messages}")
if isinstance(messages, str):
input_text = messages
elif isinstance(messages, list):
input_text = " ".join([message for message in messages])
number_of_input_tokens = len(self.openai_encoding.encode(input_text))
number_of_output_tokens = 0 # embedding is priced on input
data = {"user_email": "[email protected]", "model": {"llm_provider": llm_provider, "model_name": model}, "input_text": {"number_of_tokens": number_of_input_tokens}, "output_text": {"number_of_tokens": number_of_output_tokens}}
supabase.table('plaid_logs').upsert(data).execute()
except:
self.print_verbose(f"An error occurred: {traceback.format_exc()}")
```
from litellm.
OpenAI response object:
{
"id": "chatcmpl-123",
"object": "chat.completion",
"created": 1677652288,
"choices": [{
"index": 0,
"message": {
"role": "assistant",
"content": "\n\nHello there, how may I assist you today?",
},
"finish_reason": "stop"
}],
"usage": {
"prompt_tokens": 9,
"completion_tokens": 12,
"total_tokens": 21
}
}
from litellm.
Damn - both of us just had the same request, see #11
😱💘
from litellm.
Related Issues (20)
- [Bug]: Remove cache_hit tag in Langfuse trace
- [Feature]: Add various security headers HOT 2
- [Bug]: AI Studio (Gemini API) returns invalid `1` index instead of `0` when `"stream": false`
- [Feature]: Expose `/tokenize` endpoint -> return num tokens used for given input
- [Feature]: Anthropic - Support new anthropic-beta: tools-2024-05-16 HOT 5
- [Feature]: S3 Scratch Bucket + 303 Redirect for responses
- Add GPT-4o to vision docs HOT 1
- [Bug]: Azure OpenAI with azure_ad_token not using correct key HOT 5
- [Feature]: Add cost tracking for fine tuned OpenAI models HOT 1
- [Bug]: Replicate -> 'ModelResponse' object has no attribute 'ended' HOT 7
- [Feature]: Vertex AI Gemini - use `role` in requests by converting `messages` to chat history instead of concatenating HOT 2
- This feature doesn't meet my needs because... HOT 2
- [Bug]: [cloudflare] - config.yaml / how can I config CLOUDFLARE_ACCOUNT_ID ? HOT 1
- [Feature]: Add Premp AI provider HOT 1
- [Feature]: map 'functions' to 'tools' for groq HOT 4
- [Bug]: mistral-embed 'Extra inputs are not permitted' HOT 4
- [Bug]: Anthropic Function Streaming doesn't pass "stream" parameter.
- [Bug]: surrogate pair response handling
- [Bug]: Re-add/fix upstream Langfuse support HOT 1
- [Feature]: Cloudflare AI Gateway support for Google Vertex AI
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from litellm.