{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "markdown", "source": [ "## Comparing LLMs on a Test Set using LiteLLM\n", "LiteLLM allows you to use any LLM as a drop in replacement for `gpt-3.5-turbo`\n", "\n", "This notebook walks through how you can compare GPT-4 vs Claude-2 on a given test set using litellm" ], "metadata": { "id": "L-W4C3SgClxl" } }, { "cell_type": "code", "source": [ "!pip install litellm" ], "metadata": { "id": "fBkbl4Qo9pvz" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "execution_count": 16, "metadata": { "id": "tzS-AXWK8lJC" }, "outputs": [], "source": [ "from litellm import completion\n", "import litellm\n", "\n", "# init your test set questions\n", "questions = [\n", " \"how do i call completion() using LiteLLM\",\n", " \"does LiteLLM support VertexAI\",\n", " \"how do I set my keys on replicate llama2?\",\n", "]\n", "\n", "\n", "# set your prompt\n", "prompt = \"\"\"\n", "You are a coding assistant helping users using litellm.\n", "litellm is a light package to simplify calling OpenAI, Azure, Cohere, Anthropic, Huggingface API Endpoints. It manages:\n", "\n", "\"\"\"" ] }, { "cell_type": "code", "source": [ "import os\n", "os.environ['OPENAI_API_KEY'] = \"\"\n", "os.environ['ANTHROPIC_API_KEY'] = \"\"" ], "metadata": { "id": "vMlqi40x-KAA" }, "execution_count": 18, "outputs": [] }, { "cell_type": "markdown", "source": [], "metadata": { "id": "-HOzUfpK-H8J" } }, { "cell_type": "markdown", "source": [ "## Calling gpt-3.5-turbo and claude-2 on the same questions\n", "\n", "## LiteLLM `completion()` allows you to call all LLMs in the same format\n" ], "metadata": { "id": "Ktn25dfKEJF1" } }, { "cell_type": "code", "source": [ "results = [] # for storing results\n", "\n", "models = ['gpt-3.5-turbo', 'claude-2'] # define what models you're testing, see: https://docs.litellm.ai/docs/providers\n", "for question in questions:\n", " row = [question]\n", " for model in models:\n", " print(\"Calling:\", model, \"question:\", question)\n", " response = completion( # using litellm.completion\n", " model=model,\n", " messages=[\n", " {'role': 'system', 'content': prompt},\n", " {'role': 'user', 'content': question}\n", " ]\n", " )\n", " answer = response.choices[0].message['content']\n", " row.append(answer)\n", " print(print(\"Calling:\", model, \"answer:\", answer))\n", "\n", " results.append(row) # save results\n", "\n" ], "metadata": { "id": "DhXwRlc-9DED" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "## Visualizing Results" ], "metadata": { "id": "RkEXhXxCDN77" } }, { "cell_type": "code", "source": [ "# Create a table to visualize results\n", "import pandas as pd\n", "\n", "columns = ['Question'] + models\n", "df = pd.DataFrame(results, columns=columns)\n", "\n", "df" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 761 }, "id": "42hrmW6q-n4s", "outputId": "b763bf39-72b9-4bea-caf6-de6b2412f86d" }, "execution_count": 15, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " Question \\\n", "0 how do i call completion() using LiteLLM \n", "1 does LiteLLM support VertexAI \n", "2 how do I set my keys on replicate llama2? \n", "\n", " gpt-3.5-turbo \\\n", "0 To call the `completion()` function using Lite... \n", "1 Yes, LiteLLM does support Google Cloud Vertex ... \n", "2 To set your keys on Replicate Llama2, follow t... \n", "\n", " claude-2 \n", "0 Here is how you can call the completion() met... \n", "1 Unfortunately, LiteLLM does not currently sup... \n", "2 Here are the steps to set your API keys on Re... " ], "text/html": [ "\n", "
\n", " | Question | \n", "gpt-3.5-turbo | \n", "claude-2 | \n", "
---|---|---|---|
0 | \n", "how do i call completion() using LiteLLM | \n", "To call the `completion()` function using Lite... | \n", "Here is how you can call the completion() met... | \n", "
1 | \n", "does LiteLLM support VertexAI | \n", "Yes, LiteLLM does support Google Cloud Vertex ... | \n", "Unfortunately, LiteLLM does not currently sup... | \n", "
2 | \n", "how do I set my keys on replicate llama2? | \n", "To set your keys on Replicate Llama2, follow t... | \n", "Here are the steps to set your API keys on Re... | \n", "