|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "[](https://colab.research.google.com/github/openlayer-ai/openlayer-python/blob/main/examples/tracing/portkey/portkey_tracing.ipynb)\n", |
| 8 | + "\n", |
| 9 | + "\n", |
| 10 | + "# <a id=\"top\">Portkey monitoring quickstart</a>\n", |
| 11 | + "\n", |
| 12 | + "This notebook illustrates how to get started monitoring Portkey completions with Openlayer.\n", |
| 13 | + "\n", |
| 14 | + "Portkey provides a unified interface to call 100+ LLM APIs using the same input/output format. This integration allows you to trace and monitor completions across all supported providers through a single interface.\n", |
| 15 | + "\n" |
| 16 | + ] |
| 17 | + }, |
| 18 | + { |
| 19 | + "cell_type": "code", |
| 20 | + "execution_count": null, |
| 21 | + "metadata": {}, |
| 22 | + "outputs": [], |
| 23 | + "source": [ |
| 24 | + "!pip install openlayer portkey-ai" |
| 25 | + ] |
| 26 | + }, |
| 27 | + { |
| 28 | + "cell_type": "markdown", |
| 29 | + "metadata": {}, |
| 30 | + "source": [ |
| 31 | + "## 1. Set the environment variables\n" |
| 32 | + ] |
| 33 | + }, |
| 34 | + { |
| 35 | + "cell_type": "code", |
| 36 | + "execution_count": null, |
| 37 | + "metadata": {}, |
| 38 | + "outputs": [], |
| 39 | + "source": [ |
| 40 | + "import os\n", |
| 41 | + "\n", |
| 42 | + "from portkey_ai import Portkey\n", |
| 43 | + "\n", |
| 44 | + "# Set your Portkey API keys\n", |
| 45 | + "os.environ['PORTKEY_API_KEY'] = \"YOUR_PORTKEY_API_HERE\"\n", |
| 46 | + "\n", |
| 47 | + "# Openlayer env variables\n", |
| 48 | + "os.environ[\"OPENLAYER_API_KEY\"] = \"YOUR_OPENLAYER_API_KEY_HERE\"\n", |
| 49 | + "os.environ[\"OPENLAYER_INFERENCE_PIPELINE_ID\"] = \"YOUR_OPENLAYER_INFERENCE_PIPELINE_ID_HERE\"\n" |
| 50 | + ] |
| 51 | + }, |
| 52 | + { |
| 53 | + "cell_type": "markdown", |
| 54 | + "metadata": {}, |
| 55 | + "source": [ |
| 56 | + "## 2. Enable Portkey tracing\n" |
| 57 | + ] |
| 58 | + }, |
| 59 | + { |
| 60 | + "cell_type": "code", |
| 61 | + "execution_count": 5, |
| 62 | + "metadata": {}, |
| 63 | + "outputs": [], |
| 64 | + "source": [ |
| 65 | + "from openlayer.lib import trace_portkey\n", |
| 66 | + "\n", |
| 67 | + "# Enable openlayer tracing for all Portkey completions\n", |
| 68 | + "trace_portkey()" |
| 69 | + ] |
| 70 | + }, |
| 71 | + { |
| 72 | + "cell_type": "markdown", |
| 73 | + "metadata": {}, |
| 74 | + "source": [ |
| 75 | + "## 3. Use Portkey normally - tracing happens automatically!\n", |
| 76 | + "\n", |
| 77 | + "### Basic completion with OpenAI\n" |
| 78 | + ] |
| 79 | + }, |
| 80 | + { |
| 81 | + "cell_type": "code", |
| 82 | + "execution_count": null, |
| 83 | + "metadata": {}, |
| 84 | + "outputs": [], |
| 85 | + "source": [ |
| 86 | + "# Basic portkey client initialization\n", |
| 87 | + "portkey = Portkey(\n", |
| 88 | + " api_key = os.environ['PORTKEY_API_KEY'],\n", |
| 89 | + " config = \"YOUR_PORTKEY_CONFIG_ID_HERE\", # optional your portkey config id\n", |
| 90 | + ")\n", |
| 91 | + "\n", |
| 92 | + "# Basic portkey LLM call\n", |
| 93 | + "response = portkey.chat.completions.create(\n", |
| 94 | + " #model = \"@YOUR_PORTKEY_SLUG/YOUR_MODEL_NAME\", # optional if giving config\n", |
| 95 | + " messages = [\n", |
| 96 | + " {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n", |
| 97 | + " {\"role\": \"user\", \"content\": \"Write a poem on Argentina, least 500 words.\"}\n", |
| 98 | + " ]\n", |
| 99 | + ")\n" |
| 100 | + ] |
| 101 | + }, |
| 102 | + { |
| 103 | + "cell_type": "markdown", |
| 104 | + "metadata": {}, |
| 105 | + "source": [ |
| 106 | + "## 3. View your traces\n", |
| 107 | + "\n", |
| 108 | + "Once you've run the examples above, you can:\n", |
| 109 | + "\n", |
| 110 | + "1. **Visit your OpenLayer dashboard** to see all the traced completions\n", |
| 111 | + "2. **Analyze performance** across different models and providers\n", |
| 112 | + "3. **Monitor costs** and token usage\n", |
| 113 | + "4. **Debug issues** with detailed request/response logs\n", |
| 114 | + "5. **Compare models** side-by-side\n", |
| 115 | + "\n", |
| 116 | + "The traces will include:\n", |
| 117 | + "- **Request details**: Model, parameters, messages\n", |
| 118 | + "- **Response data**: Generated content, token counts, latency\n", |
| 119 | + "- **Provider information**: Which underlying service was used\n", |
| 120 | + "- **Custom metadata**: Any additional context you provide\n", |
| 121 | + "\n", |
| 122 | + "For more information, check out:\n", |
| 123 | + "- [OpenLayer Documentation](https://docs.openlayer.com/)\n", |
| 124 | + "- [Portkey Documentation](https://portkey.ai/docs)\n", |
| 125 | + "- [Portkey AI Gateway](https://portkey.ai/docs/product/ai-gateway)\n", |
| 126 | + "- [Portkey Supported Providers](https://portkey.ai/docs/api-reference/inference-api/supported-providers)\n" |
| 127 | + ] |
| 128 | + } |
| 129 | + ], |
| 130 | + "metadata": { |
| 131 | + "kernelspec": { |
| 132 | + "display_name": "Python 3", |
| 133 | + "language": "python", |
| 134 | + "name": "python3" |
| 135 | + }, |
| 136 | + "language_info": { |
| 137 | + "codemirror_mode": { |
| 138 | + "name": "ipython", |
| 139 | + "version": 3 |
| 140 | + }, |
| 141 | + "file_extension": ".py", |
| 142 | + "mimetype": "text/x-python", |
| 143 | + "name": "python", |
| 144 | + "nbconvert_exporter": "python", |
| 145 | + "pygments_lexer": "ipython3", |
| 146 | + "version": "3.9.6" |
| 147 | + } |
| 148 | + }, |
| 149 | + "nbformat": 4, |
| 150 | + "nbformat_minor": 2 |
| 151 | +} |
0 commit comments