Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 152 additions & 0 deletions examples/tracing/portkey/portkey_tracing.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/openlayer-ai/openlayer-python/blob/main/examples/tracing/portkey/portkey_tracing.ipynb)\n",
"\n",
"\n",
"# <a id=\"top\">Portkey monitoring quickstart</a>\n",
"\n",
"This notebook illustrates how to get started monitoring Portkey completions with Openlayer.\n",
"\n",
"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",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!pip install openlayer portkey-ai"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1. Set the environment variables\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"\n",
"from portkey_ai import Portkey\n",
"\n",
"\n",
"# Set your Portkey API keys\n",
"os.environ['PORTKEY_API_KEY'] = \"YOUR_PORTKEY_API_HERE\"\n",
"\n",
"# Openlayer env variables\n",
"os.environ[\"OPENLAYER_API_KEY\"] = \"YOUR_OPENLAYER_API_KEY_HERE\"\n",
"os.environ[\"OPENLAYER_INFERENCE_PIPELINE_ID\"] = \"YOUR_OPENLAYER_INFERENCE_PIPELINE_ID_HERE\"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2. Enable Portkey tracing\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"from openlayer.lib import trace_portkey\n",
"\n",
"# Enable openlayer tracing for all Portkey completions\n",
"trace_portkey()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3. Use Portkey normally - tracing happens automatically!\n",
"\n",
"### Basic completion with OpenAI\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Basic portkey client initialization\n",
"portkey = Portkey(\n",
" api_key = os.environ['PORTKEY_API_KEY'],\n",
" config = \"YOUR_PORTKEY_CONFIG_ID_HERE\", # optional your portkey config id\n",
")\n",
"\n",
"# Basic portkey LLM call\n",
"response = portkey.chat.completions.create(\n",
" #model = \"@YOUR_PORTKEY_SLUG/YOUR_MODEL_NAME\", # optional if giving config\n",
" messages = [\n",
" {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n",
" {\"role\": \"user\", \"content\": \"Write a poem on Argentina, least 500 words.\"}\n",
" ]\n",
")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3. View your traces\n",
"\n",
"Once you've run the examples above, you can:\n",
"\n",
"1. **Visit your OpenLayer dashboard** to see all the traced completions\n",
"2. **Analyze performance** across different models and providers\n",
"3. **Monitor costs** and token usage\n",
"4. **Debug issues** with detailed request/response logs\n",
"5. **Compare models** side-by-side\n",
"\n",
"The traces will include:\n",
"- **Request details**: Model, parameters, messages\n",
"- **Response data**: Generated content, token counts, latency\n",
"- **Provider information**: Which underlying service was used\n",
"- **Custom metadata**: Any additional context you provide\n",
"\n",
"For more information, check out:\n",
"- [OpenLayer Documentation](https://docs.openlayer.com/)\n",
"- [Portkey Documentation](https://portkey.ai/docs)\n",
"- [Portkey AI Gateway](https://portkey.ai/docs/product/ai-gateway)\n",
"- [Portkey Supported Providers](https://portkey.ai/docs/api-reference/inference-api/supported-providers)\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
36 changes: 36 additions & 0 deletions src/openlayer/lib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"trace_oci_genai",
"trace_oci", # Alias for backward compatibility
"trace_litellm",
"trace_portkey",
"trace_google_adk",
"unpatch_google_adk",
"update_current_trace",
Expand Down Expand Up @@ -192,6 +193,41 @@ def trace_litellm():
return litellm_tracer.trace_litellm()


# ---------------------------------- Portkey ---------------------------------- #
def trace_portkey():
"""Enable tracing for Portkey completions.

This function patches Portkey's chat.completions.create to automatically trace
all OpenAI-compatible completions routed via the Portkey AI Gateway.

Example:
>>> from portkey_ai import Portkey
>>> from openlayer.lib import trace_portkey
>>> # Enable openlayer tracing for all Portkey completions
>>> trace_portkey()
>>> # Basic portkey client initialization
>>> portkey = Portkey(
>>> api_key = os.environ['PORTKEY_API_KEY'],
>>> config = "YOUR_PORTKEY_CONFIG_ID", # optional your portkey config id
>>> )
>>> # use portkey normally - tracing happens automatically
>>> response = portkey.chat.completions.create(
>>> #model = "@YOUR_PORTKEY_SLUG/YOUR_MODEL_NAME", # optional if giving config
>>> messages = [
>>> {"role": "system", "content": "You are a helpful assistant."},
>>> {"role": "user", "content": "Write a poem on Argentina, least 100 words."}
>>> ]
>>> )
"""
# pylint: disable=import-outside-toplevel
try:
from portkey_ai import Portkey # noqa: F401
except ImportError:
raise ImportError("portkey-ai is required for Portkey tracing. Install with: pip install portkey-ai")

from .integrations import portkey_tracer

return portkey_tracer.trace_portkey()
# ------------------------------ Google ADK ---------------------------------- #
def trace_google_adk():
"""Enable tracing for Google Agent Development Kit (ADK).
Expand Down
Loading
Loading