{ "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", "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Questiongpt-3.5-turboclaude-2
0how do i call completion() using LiteLLMTo call the `completion()` function using Lite...Here is how you can call the completion() met...
1does LiteLLM support VertexAIYes, LiteLLM does support Google Cloud Vertex ...Unfortunately, LiteLLM does not currently sup...
2how do I set my keys on replicate llama2?To set your keys on Replicate Llama2, follow t...Here are the steps to set your API keys on Re...
\n", "
\n", "
\n", "\n", "
\n", " \n", "\n", " \n", "\n", " \n", "
\n", "\n", "\n", "
\n", " \n", "\n", "\n", "\n", " \n", "
\n", "
\n", "
\n" ], "application/vnd.google.colaboratory.module+javascript": "\n import \"https://ssl.gstatic.com/colaboratory/data_table/881c4a0d49046431/data_table.js\";\n\n const table = window.createDataTable({\n data: [[{\n 'v': 0,\n 'f': \"0\",\n },\n\"how do i call completion() using LiteLLM\",\n\"To call the `completion()` function using LiteLLM, you need to follow these steps:\\n\\n1. Install the `litellm` package by running `pip install litellm` in your terminal.\\n2. Import the `Completion` class from the `litellm` module.\\n3. Initialize an instance of the `Completion` class by providing the required parameters like the API endpoint URL and your API key.\\n4. Call the `complete()` method on the `Completion` instance and pass the text prompt as a string.\\n5. Retrieve the generated completion from the response object and use it as desired.\\n\\nHere's an example:\\n\\n```python\\nfrom litellm.completion import Completion\\n\\n# Initialize the Completion client\\ncompletion_client = Completion(\\n model_name='gpt-3.5-turbo',\\n api_key='your_api_key',\\n endpoint='https://your_endpoint_url'\\n)\\n\\n# Call the completion() method\\nresponse = completion_client.complete(\\\"Once upon a time\\\")\\n\\n# Retrieve the generated completion\\ncompletion = response['choices'][0]['text']\\n\\nprint(completion)\\n```\\n\\nMake sure to replace `'gpt-3.5-turbo'` with the desired model name, `'your_api_key'` with your actual API key, and `'https://your_endpoint_url'` with the correct API endpoint URL provided by your service provider.\\n\\nNote: The above example assumes you have a valid API key and endpoint URL for the OpenAI GPT-3.5-turbo model. Make sure to obtain the necessary credentials according to the API you are using.\",\n\" Here is how you can call the completion() method using LiteLLM:\\n\\nFirst, import LiteLLM:\\n\\n```python\\nimport litellm as lm\\n```\\n\\nThen create a LiteLLM object, specifying the API you want to use (e.g. \\\"openai\\\"):\\n\\n```python \\nai = lm.LiteLLM(\\\"openai\\\")\\n```\\n\\nNow you can call the completion() method on the ai object:\\n\\n```python\\nresponse = ai.completion(\\n prompt=\\\"Hello\\\", \\n model=\\\"text-davinci-003\\\",\\n max_tokens=100\\n)\\n```\\n\\nThe completion() method takes parameters like:\\n\\n- prompt (str): The prompt text to complete \\n- model (str): The AI model to use\\n- max_tokens (int): The maximum number of tokens to generate\\n\\nIt returns a Python dictionary with the AI's response.\\n\\nYou can then access the generated text using:\\n\\n```python\\nprint(response[\\\"choices\\\"][0][\\\"text\\\"]) \\n```\\n\\nSo LiteLLM provides a simple unified interface to call the underlying AI APIs. The completion() method works similarly for OpenAI\"],\n [{\n 'v': 1,\n 'f': \"1\",\n },\n\"does LiteLLM support VertexAI\",\n\"Yes, LiteLLM does support Google Cloud Vertex AI. It provides convenient wrappers and simplified functions to call Vertex AI API endpoints for natural language processing tasks such as text classification, entity extraction, sentiment analysis, etc. You can easily integrate LiteLLM with Vertex AI in your code to leverage its capabilities.\",\n\" Unfortunately, LiteLLM does not currently support VertexAI. LiteLLM focuses on providing a simple interface to call the APIs of services like OpenAI, Azure, Cohere, Anthropic, and Hugging Face. \\n\\nVertexAI is Google's managed machine learning platform. Integrating VertexAI would require additional development work to wrap the VertexAI SDK in a simple interface like LiteLLM provides for other services. \\n\\nHowever, LiteLLM is open source, so it is possible for someone to contribute support for VertexAI. The maintainers would likely welcome a pull request to add VertexAI as an option if implemented well. But out of the box, LiteLLM does not have built-in support for calling VertexAI APIs.\\n\\nThe key services LiteLLM supports are:\\n\\n- OpenAI (GPT, Codex, DALL-E)\\n- Azure Cognitive Services (Text Analytics, Computer Vision, Speech) \\n- Cohere\\n- Anthropic AI\\n- Hugging Face Transformers\\n\\nSo while it doesn't cover every ML API provider, it does make it easy to use the most popular natural language, speech, and vision APIs through a simple interface. Adding VertexAI\"],\n [{\n 'v': 2,\n 'f': \"2\",\n },\n\"how do I set my keys on replicate llama2?\",\n\"To set your keys on Replicate Llama2, follow these steps:\\n\\n1. Open the Llama2 dashboard in your browser.\\n2. Click on the \\\"Settings\\\" tab in the top menu.\\n3. Scroll down to the \\\"API Keys\\\" section.\\n4. Click on the \\\"Add a Key\\\" button.\\n5. Enter a name for your API key to help you identify it later.\\n6. Select the provider for your API key from the dropdown menu. For example, you can select \\\"OpenAI\\\" for OpenAI GPT-3 access.\\n7. Enter your API key in the provided input field. Make sure to copy it correctly.\\n8. Click on the \\\"Save\\\" button to save your API key.\\n\\nNote: The actual steps may vary slightly depending on the platform or interface you are using to access Llama2.\",\n\" Here are the steps to set your API keys on Replicate for litellm:\\n\\n1. Go to your Replicate project settings and select the Environment tab.\\n\\n2. Under Environment Variables, click Add Variable.\\n\\n3. Add variables for the API keys you want to use. The variable names should match the ones used in litellm:\\n\\n- `OPENAI_API_KEY` for OpenAI \\n- `AZURE_API_KEY` for Azure Cognitive Services\\n- `COHERE_API_KEY` for Cohere\\n- `ANTHROPIC_API_KEY` for Anthropic\\n- `HUGGINGFACE_API_KEY` for Hugging Face\\n\\n4. Set the value to your actual API key for each service. Make sure to treat the values as secrets.\\n\\n5. Make sure your litellm code is referencing the environment variable names, for example:\\n\\n```python\\nimport litellm as lm\\n\\nlm.auth(openai_key=os.getenv(\\\"OPENAI_API_KEY\\\")) \\n```\\n\\n6. Restart your Replicate runtime to load the new environment variables.\\n\\nNow litellm will use your\"]],\n columns: [[\"number\", \"index\"], [\"string\", \"Question\"], [\"string\", \"gpt-3.5-turbo\"], [\"string\", \"claude-2\"]],\n columnOptions: [{\"width\": \"1px\", \"className\": \"index_column\"}],\n rowsPerPage: 25,\n helpUrl: \"https://colab.research.google.com/notebooks/data_table.ipynb\",\n suppressOutputScrolling: true,\n minimumWidth: undefined,\n });\n\n function appendQuickchartButton(parentElement) {\n let quickchartButtonContainerElement = document.createElement('div');\n quickchartButtonContainerElement.innerHTML = `\n
\n \n \n\n\n \n
`;\n parentElement.appendChild(quickchartButtonContainerElement);\n }\n\n appendQuickchartButton(table);\n " }, "metadata": {}, "execution_count": 15 } ] } ] }