Skip to content

Commit

Permalink
Adding litellm example
Browse files Browse the repository at this point in the history
  • Loading branch information
HowieG committed Aug 2, 2024
1 parent a8a3a34 commit e55058f
Showing 1 changed file with 76 additions and 22 deletions.
98 changes: 76 additions & 22 deletions examples/litellm/litellm_example.ipynb
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### NOTE:\n",
"AgentOps requires that you import and call LiteLLM differently than LiteLLM's documentation. \n",
"Instead of\n",
"``` python\n",
"from litellm import completion\n",
"completion()\n",
"```\n",
"You should import and call like this:\n",
"``` python\n",
"import litellm\n",
"litellm.completion()\n",
"```\n",
"\n",
"Please see examples below\n",
"\n",
"[See our LiteLLM docs](https://docs.agentops.ai/v1/integrations/litellm)"
]
},
{
"cell_type": "code",
"execution_count": 1,
Expand Down Expand Up @@ -52,7 +74,7 @@
"Requirement already satisfied: packaging>=20.9 in /Users/howardgil/Desktop/agentops/AgentOps-AI/agentops/env/lib/python3.12/site-packages (from huggingface-hub<1.0,>=0.16.4->tokenizers->litellm) (23.2)\n",
"Requirement already satisfied: pyyaml>=5.1 in /Users/howardgil/Desktop/agentops/AgentOps-AI/agentops/env/lib/python3.12/site-packages (from huggingface-hub<1.0,>=0.16.4->tokenizers->litellm) (6.0.1)\n",
"Note: you may need to restart the kernel to use updated packages.\n",
"Requirement already satisfied: agentops in /Users/howardgil/Desktop/agentops/AgentOps-AI/agentops/env/lib/python3.12/site-packages (0.3.5)\n",
"Requirement already satisfied: agentops in /Users/howardgil/Desktop/agentops/AgentOps-AI/agentops/env/lib/python3.12/site-packages (0.3.6)\n",
"Requirement already satisfied: requests==2.31.0 in /Users/howardgil/Desktop/agentops/AgentOps-AI/agentops/env/lib/python3.12/site-packages (from agentops) (2.31.0)\n",
"Requirement already satisfied: psutil==5.9.8 in /Users/howardgil/Desktop/agentops/AgentOps-AI/agentops/env/lib/python3.12/site-packages (from agentops) (5.9.8)\n",
"Requirement already satisfied: packaging==23.2 in /Users/howardgil/Desktop/agentops/AgentOps-AI/agentops/env/lib/python3.12/site-packages (from agentops) (23.2)\n",
Expand Down Expand Up @@ -80,6 +102,13 @@
"import agentops"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"LiteLLM allows you to use several models including from OpenAI, Llama, Mistral, Claude, Gemini, Gemma, Dall-E, Whisper, and more all using the OpenAI format. To use a different model all you need to change are the API KEY and model (litellm.completion(model=\"...\"))."
]
},
{
"cell_type": "code",
"execution_count": 3,
Expand All @@ -88,45 +117,70 @@
"source": [
"import os\n",
"\n",
"os.environ[\"OPENAI_API_KEY\"] = (\n",
" \"openai_api_key\"\n",
")\n",
"os.environ[\"OPENAI_API_KEY\"] = \"openai_api_key\" # or the provider of your choosing\n",
"os.environ[\"AGENTOPS_API_KEY\"] = \"agentops_api_key\""
]
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"🖇 AgentOps: \u001b[34m\u001b[34mSession Replay: https://app.agentops.ai/drilldown?session_id=ee207031-879d-42bc-8d99-f30521c58541\u001b[0m\u001b[0m\n"
"🖇 AgentOps: API Key is invalid: {c9114273-2e84-41cc-bd54-499bc165c7ba1}.\n",
"\t Find your API key at https://app.agentops.ai/settings/projects\n",
"🖇 AgentOps: API Key is invalid: {c9114273-2e84-41cc-bd54-499bc165c7ba1}.\n",
"\t Find your API key at https://app.agentops.ai/settings/projects\n",
"\u001b[31;1m🖇 AgentOps: Could not initialize AgentOps client - API Key is missing.\n",
"\t Find your API key at https://app.agentops.ai/settings/projects\u001b[0m\n"
]
},
}
],
"source": [
"agentops.init(default_tags=[\"litellm-example\"])"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"ename": "FileNotFoundError",
"evalue": "[Errno 2] No such file or directory: '/Users/howardgil/Desktop/agentops/AgentOps-AI/agentops/env/lib/python3.12/site-packages/.agentops_time_travel.yaml'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mFileNotFoundError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[4], line 5\u001b[0m\n\u001b[1;32m 1\u001b[0m agentops\u001b[38;5;241m.\u001b[39minit(default_tags\u001b[38;5;241m=\u001b[39m[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mlitellm-example\u001b[39m\u001b[38;5;124m\"\u001b[39m])\n\u001b[1;32m 3\u001b[0m messages \u001b[38;5;241m=\u001b[39m [{\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mrole\u001b[39m\u001b[38;5;124m\"\u001b[39m: \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124muser\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcontent\u001b[39m\u001b[38;5;124m\"\u001b[39m: \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mWrite a 12 word poem about secret agents.\u001b[39m\u001b[38;5;124m\"\u001b[39m}]\n\u001b[0;32m----> 5\u001b[0m response \u001b[38;5;241m=\u001b[39m \u001b[43mlitellm\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcompletion\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmodel\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mclaude-3\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mmessages\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mmessages\u001b[49m\u001b[43m)\u001b[49m\n",
"File \u001b[0;32m~/Desktop/agentops/AgentOps-AI/agentops/env/lib/python3.12/site-packages/agentops/llm_tracker.py:747\u001b[0m, in \u001b[0;36mLlmTracker.override_litellm_completion.<locals>.patched_function\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 744\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124msession\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;129;01min\u001b[39;00m kwargs\u001b[38;5;241m.\u001b[39mkeys():\n\u001b[1;32m 745\u001b[0m \u001b[38;5;28;01mdel\u001b[39;00m kwargs[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124msession\u001b[39m\u001b[38;5;124m\"\u001b[39m]\n\u001b[0;32m--> 747\u001b[0m completion_override \u001b[38;5;241m=\u001b[39m \u001b[43mfetch_completion_override_from_time_travel_cache\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 748\u001b[0m \u001b[43m \u001b[49m\u001b[43mkwargs\u001b[49m\n\u001b[1;32m 749\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 750\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m completion_override:\n\u001b[1;32m 751\u001b[0m result_model \u001b[38;5;241m=\u001b[39m ChatCompletion\u001b[38;5;241m.\u001b[39mmodel_validate_json(completion_override)\n",
"File \u001b[0;32m~/Desktop/agentops/AgentOps-AI/agentops/env/lib/python3.12/site-packages/agentops/time_travel.py:64\u001b[0m, in \u001b[0;36mfetch_completion_override_from_time_travel_cache\u001b[0;34m(kwargs)\u001b[0m\n\u001b[1;32m 63\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mfetch_completion_override_from_time_travel_cache\u001b[39m(kwargs):\n\u001b[0;32m---> 64\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[43mcheck_time_travel_active\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m:\n\u001b[1;32m 65\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m\n\u001b[1;32m 67\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m TimeTravel()\u001b[38;5;241m.\u001b[39m_completion_overrides_map:\n",
"File \u001b[0;32m~/Desktop/agentops/AgentOps-AI/agentops/env/lib/python3.12/site-packages/agentops/time_travel.py:88\u001b[0m, in \u001b[0;36mcheck_time_travel_active\u001b[0;34m()\u001b[0m\n\u001b[1;32m 85\u001b[0m parent_dir \u001b[38;5;241m=\u001b[39m os\u001b[38;5;241m.\u001b[39mpath\u001b[38;5;241m.\u001b[39mdirname(script_dir)\n\u001b[1;32m 86\u001b[0m config_file_path \u001b[38;5;241m=\u001b[39m os\u001b[38;5;241m.\u001b[39mpath\u001b[38;5;241m.\u001b[39mjoin(parent_dir, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m.agentops_time_travel.yaml\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m---> 88\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m \u001b[38;5;28;43mopen\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mconfig_file_path\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mr\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m \u001b[38;5;28;01mas\u001b[39;00m config_file:\n\u001b[1;32m 89\u001b[0m config \u001b[38;5;241m=\u001b[39m yaml\u001b[38;5;241m.\u001b[39msafe_load(config_file)\n\u001b[1;32m 90\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m config\u001b[38;5;241m.\u001b[39mget(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mTime_Travel_Debugging_Active\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28;01mTrue\u001b[39;00m):\n",
"\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: '/Users/howardgil/Desktop/agentops/AgentOps-AI/agentops/env/lib/python3.12/site-packages/.agentops_time_travel.yaml'"
"name": "stdout",
"output_type": "stream",
"text": [
"Silently lurking, shadows’ friends.\n",
"Danger dances.\n",
"Unseen heroes, their missions never ends.\n"
]
}
],
"source": [
"agentops.init(default_tags=[\"litellm-example\"])\n",
"\n",
"messages = [{\"role\": \"user\", \"content\": \"Write a 12 word poem about secret agents.\"}]\n",
"\n",
"response = litellm.completion(model=\"claude-3\", messages=messages)"
"response = litellm.completion(model=\"gpt-4\", messages=messages) # or the model of your choosing\n",
"print(response.choices[0].message.content)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"🖇 AgentOps: API Key is invalid: {c9114273-2e84-41cc-bd54-499bc165c7ba1}.\n",
"\t Find your API key at https://app.agentops.ai/settings/projects\n",
"🖇 AgentOps: Could not end session - no sessions detected\n"
]
}
],
"source": [
"agentops.end_session(\"Success\")"
]
}
],
Expand Down

0 comments on commit e55058f

Please sign in to comment.