litellm/cookbook/benchmark/benchmark.py

91 lines
2.9 KiB
Python
Raw Normal View History

2023-09-08 12:43:13 +08:00
from litellm import completion, completion_cost
import time
import click
from tqdm import tqdm
from tabulate import tabulate
2023-09-08 13:07:42 +08:00
from termcolor import colored
import os
2023-09-08 12:43:13 +08:00
# Define the list of models to benchmark
2023-09-08 13:07:42 +08:00
# select any LLM listed here: https://docs.litellm.ai/docs/providers
2023-12-25 16:40:38 +08:00
models = ["gpt-3.5-turbo", "claude-2"]
2023-09-08 12:43:13 +08:00
2023-09-08 13:07:42 +08:00
# Enter LLM API keys
# https://docs.litellm.ai/docs/providers
2023-12-25 16:40:38 +08:00
os.environ["OPENAI_API_KEY"] = ""
os.environ["ANTHROPIC_API_KEY"] = ""
2023-09-08 13:07:42 +08:00
2023-09-08 12:43:13 +08:00
# List of questions to benchmark (replace with your questions)
2023-12-25 16:40:38 +08:00
questions = ["When will BerriAI IPO?", "When will LiteLLM hit $100M ARR?"]
2023-09-08 12:43:13 +08:00
2023-12-25 16:40:38 +08:00
# Enter your system prompt here
2023-09-08 13:07:42 +08:00
system_prompt = """
You are LiteLLMs helpful assistant
"""
2023-12-25 16:40:38 +08:00
2023-09-08 12:43:13 +08:00
@click.command()
2023-12-25 16:40:38 +08:00
@click.option(
"--system-prompt",
default="You are a helpful assistant that can answer questions.",
help="System prompt for the conversation.",
)
2023-09-08 12:43:13 +08:00
def main(system_prompt):
for question in questions:
data = [] # Data for the current question
with tqdm(total=len(models)) as pbar:
for model in models:
2023-12-25 16:40:38 +08:00
colored_description = colored(
f"Running question: {question} for model: {model}", "green"
)
2023-09-08 12:43:13 +08:00
pbar.set_description(colored_description)
start_time = time.time()
response = completion(
model=model,
max_tokens=500,
messages=[
{"role": "system", "content": system_prompt},
2023-12-25 16:40:38 +08:00
{"role": "user", "content": question},
2023-09-08 12:43:13 +08:00
],
)
end = time.time()
total_time = end - start_time
2023-09-14 02:20:12 +08:00
cost = completion_cost(completion_response=response)
2023-12-25 16:40:38 +08:00
raw_response = response["choices"][0]["message"]["content"]
2023-09-08 12:43:13 +08:00
2023-12-25 16:40:38 +08:00
data.append(
{
"Model": colored(model, "light_blue"),
"Response": raw_response, # Colorize the response
"ResponseTime": colored(f"{total_time:.2f} seconds", "red"),
"Cost": colored(f"${cost:.6f}", "green"), # Colorize the cost
}
)
2023-09-08 12:43:13 +08:00
pbar.update(1)
# Separate headers from the data
2023-12-25 16:40:38 +08:00
headers = ["Model", "Response", "Response Time (seconds)", "Cost ($)"]
2023-09-08 12:43:13 +08:00
colwidths = [15, 80, 15, 10]
# Create a nicely formatted table for the current question
2023-12-25 16:40:38 +08:00
table = tabulate(
[list(d.values()) for d in data],
headers,
tablefmt="grid",
maxcolwidths=colwidths,
)
2023-09-08 12:43:13 +08:00
# Print the table for the current question
2023-12-25 16:40:38 +08:00
colored_question = colored(question, "green")
2023-09-08 12:43:13 +08:00
click.echo(f"\nBenchmark Results for '{colored_question}':")
click.echo(table) # Display the formatted table
2023-12-25 16:40:38 +08:00
if __name__ == "__main__":
2023-09-08 12:43:13 +08:00
main()