Skip to content

Commit 03c36e1

Browse files
portkey integration code and tests
1 parent b4ba138 commit 03c36e1

File tree

5 files changed

+1522
-0
lines changed

5 files changed

+1522
-0
lines changed
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"[![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",
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+
}

src/openlayer/lib/__init__.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"trace_oci_genai",
1515
"trace_oci", # Alias for backward compatibility
1616
"trace_litellm",
17+
"trace_portkey",
1718
"trace_google_adk",
1819
"unpatch_google_adk",
1920
"trace_gemini",
@@ -193,6 +194,43 @@ def trace_litellm():
193194
return litellm_tracer.trace_litellm()
194195

195196

197+
# ---------------------------------- Portkey ---------------------------------- #
198+
def trace_portkey():
199+
"""Enable tracing for Portkey completions.
200+
201+
This function patches Portkey's chat.completions.create to automatically trace
202+
all OpenAI-compatible completions routed via the Portkey AI Gateway.
203+
204+
Example:
205+
>>> from portkey_ai import Portkey
206+
>>> from openlayer.lib import trace_portkey
207+
>>> # Enable openlayer tracing for all Portkey completions
208+
>>> trace_portkey()
209+
>>> # Basic portkey client initialization
210+
>>> portkey = Portkey(
211+
>>> api_key = os.environ['PORTKEY_API_KEY'],
212+
>>> config = "YOUR_PORTKEY_CONFIG_ID", # optional your portkey config id
213+
>>> )
214+
>>> # use portkey normally - tracing happens automatically
215+
>>> response = portkey.chat.completions.create(
216+
>>> #model = "@YOUR_PORTKEY_SLUG/YOUR_MODEL_NAME", # optional if giving config
217+
>>> messages = [
218+
>>> {"role": "system", "content": "You are a helpful assistant."},
219+
>>> {"role": "user", "content": "Write a poem on Argentina, least 100 words."}
220+
>>> ]
221+
>>> )
222+
"""
223+
# pylint: disable=import-outside-toplevel
224+
try:
225+
from portkey_ai import Portkey # noqa: F401
226+
except ImportError:
227+
raise ImportError("portkey-ai is required for Portkey tracing. Install with: pip install portkey-ai")
228+
229+
from .integrations import portkey_tracer
230+
231+
return portkey_tracer.trace_portkey()
232+
233+
196234
# ------------------------------ Google ADK ---------------------------------- #
197235
def trace_google_adk(disable_adk_otel: bool = False):
198236
"""Enable tracing for Google Agent Development Kit (ADK).

0 commit comments

Comments
 (0)