完成令牌使用量与成本
默认情况下,LiteLLM 在所有完成请求中返回令牌使用量(参见此处)。
LiteLLM 在所有调用中返回 response_cost。
from litellm import completion
response = litellm.completion(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": "Hey, how's it going?"}],
mock_response="Hello world",
)
print(response._hidden_params["response_cost"])
LiteLLM 还提供了一些辅助函数:
encode:使用特定于模型的分词器对传入的文本进行编码。跳转到代码decode:使用特定于模型的分词器对传入的令牌进行解码。跳转到代码token_counter:返回给定输入的令牌数量——它根据模型使用的分词器,如果没有特定于模型的分词器,则默认使用 tiktoken。跳转到代码create_pretrained_tokenizer和create_tokenizer:LiteLLM 为 OpenAI、Cohere、Anthropic、Llama2 和 Llama3 模型提供了默认分词器支持。如果你使用的是其他模型,可以创建自定义分词器并将其作为custom_tokenizer传递给encode、decode和token_counter方法。跳转到代码cost_per_token:返回提示(输入)和完成(输出)令牌的成本(以美元计)。使用来自api.litellm.ai的实时列表。跳转到代码completion_cost:返回给定 LLM API 调用的总成本(以美元计)。它结合了token_counter和cost_per_token,返回该查询的成本(计算输入和输出的成本)。跳转到代码get_max_tokens:返回给定模型允许的最大令牌数量。跳转到代码model_cost:返回所有模型的字典,包含其 max_tokens、input_cost_per_token 和 output_cost_per_token。它使用下面显示的api.litellm.ai调用。跳转到代码register_model:在新模型或覆盖现有模型(及其定价详情)在模型成本字典中注册。跳转到代码
📣 这是一个社区维护的列表。欢迎贡献!❤️
示例用法
1. encode
编码具有特定于模型的分词器,适用于 anthropic、cohere、llama2 和 openai。如果传入了不支持的模型,它将默认使用 tiktoken(openai 的分词器)。
from litellm import encode, decode
sample_text = "Hellö World, this is my input string!"
# openai 编码和解码
openai_tokens = encode(model="gpt-3.5-turbo", text=sample_text)
print(openai_tokens)
2. decode
解码支持 anthropic、cohere、llama2 和 openai。
from litellm import encode, decode
sample_text = "Hellö World, this is my input string!"
# openai 编码和解码
openai_tokens = encode(model="gpt-3.5-turbo", text=sample_text)
openai_text = decode(model="gpt-3.5-turbo", tokens=openai_tokens)
print(openai_text)
3. token_counter
from litellm import token_counter
messages = [{"user": "role", "content": "Hey, how's it going"}]
print(token_counter(model="gpt-3.5-turbo", messages=messages))
4. create_pretrained_tokenizer 和 create_tokenizer
from litellm import create_pretrained_tokenizer, create_tokenizer
# 从 huggingface 仓库获取分词器
custom_tokenizer_1 = create_pretrained_tokenizer("Xenova/llama-3-tokenizer")
# 使用 json 文件中的分词器
with open("tokenizer.json") as f:
json_data = json.load(f)
json_str = json.dumps(json_data)
custom_tokenizer_2 = create_tokenizer(json_str)
5. cost_per_token
from litellm import cost_per_token
prompt_tokens = 5
completion_tokens = 10
prompt_tokens_cost_usd_dollar, completion_tokens_cost_usd_dollar = cost_per_token(model="gpt-3.5-turbo", prompt_tokens=prompt_tokens, completion_tokens=completion_tokens))
print(prompt_tokens_cost_usd_dollar, completion_tokens_cost_usd_dollar)
6. completion_cost
- 输入:接受
litellm.completion()响应 或 提示 + 完成字符串 - 输出:返回
completion调用的成本float
litellm.completion()
from litellm import completion, completion_cost
response = completion(
model="bedrock/anthropic.claude-v2",
messages=messages,
request_timeout=200,
)
# 将completion的响应传递给completion_cost
cost = completion_cost(completion_response=response)
formatted_string = f"${float(cost):.10f}"
print(formatted_string)
提示 + 完成字符串
from litellm import completion_cost
cost = completion_cost(model="bedrock/anthropic.claude-v2", prompt="Hey!", completion="How's it going?")
formatted_string = f"${float(cost):.10f}"
print(formatted_string)
7. get_max_tokens
输入:接受一个模型名称 - 例如,gpt-3.5-turbo(要获取完整列表,请调用litellm.model_list)。 输出:返回给定模型允许的最大令牌数
from litellm import get_max_tokens
model = "gpt-3.5-turbo"
print(get_max_tokens(model)) # 输出: 4097
8. model_cost
- 输出:返回一个包含所有模型在社区维护列表上的max_tokens、input_cost_per_token、output_cost_per_token的dict对象
from litellm import model_cost
print(model_cost) # {'gpt-3.5-turbo': {'max_tokens': 4000, 'input_cost_per_token': 1.5e-06, 'output_cost_per_token': 2e-06}, ...}
9. register_model
- 输入:提供一个模型成本字典或一个托管json blob的URL
- 输出:返回更新后的model_cost字典 + 更新litellm.model_cost的模型详情。
字典
from litellm import register_model
litellm.register_model({
"gpt-4": {
"max_tokens": 8192,
"input_cost_per_token": 0.00002,
"output_cost_per_token": 0.00006,
"litellm_provider": "openai",
"mode": "chat"
},
})
URL for json blob
import litellm
litellm.register_model(model_cost=
"https://raw.githubusercontent.com/BerriAI/litellm/main/model_prices_and_context_window.json")
不拉取托管的model_cost_map
如果你有防火墙,并且只想使用本地版本的模型成本映射,你可以这样做:
export LITELLM_LOCAL_MODEL_COST_MAP="True"
注意:这意味着你需要手动更新以获取更新的定价和新模型。