Skip to content

Commit 25792c5

Browse files
author
Stainless Bot
committed
feat: feat: Add Anthropic tracer
1 parent 24057f9 commit 25792c5

File tree

4 files changed

+456
-1
lines changed

4 files changed

+456
-1
lines changed
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "2722b419",
6+
"metadata": {},
7+
"source": [
8+
"[![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/anthropic/anthropic_tracing.ipynb)\n",
9+
"\n",
10+
"\n",
11+
"# <a id=\"top\">Anthropic tracing</a>\n",
12+
"\n",
13+
"This notebook illustrates how to get started tracing Anthropic LLMs with Openlayer."
14+
]
15+
},
16+
{
17+
"cell_type": "code",
18+
"execution_count": null,
19+
"id": "020c8f6a",
20+
"metadata": {},
21+
"outputs": [],
22+
"source": [
23+
"!pip install openlayer"
24+
]
25+
},
26+
{
27+
"cell_type": "markdown",
28+
"id": "75c2a473",
29+
"metadata": {},
30+
"source": [
31+
"## 1. Set the environment variables"
32+
]
33+
},
34+
{
35+
"cell_type": "code",
36+
"execution_count": null,
37+
"id": "f3f4fa13",
38+
"metadata": {},
39+
"outputs": [],
40+
"source": [
41+
"import os\n",
42+
"import anthropic\n",
43+
"\n",
44+
"# OpenAI env variables\n",
45+
"os.environ[\"ANTHROPIC_API_KEY\"] = \"YOUR_ANTHROPIC_API_KEY_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\""
50+
]
51+
},
52+
{
53+
"cell_type": "markdown",
54+
"id": "9758533f",
55+
"metadata": {},
56+
"source": [
57+
"## 2. Import the `trace_anthropic` function"
58+
]
59+
},
60+
{
61+
"cell_type": "code",
62+
"execution_count": null,
63+
"id": "c35d9860-dc41-4f7c-8d69-cc2ac7e5e485",
64+
"metadata": {},
65+
"outputs": [],
66+
"source": [
67+
"from openlayer.lib import trace_anthropic\n",
68+
"\n",
69+
"anthropic_client = trace_anthropic(anthropic.Anthropic())"
70+
]
71+
},
72+
{
73+
"cell_type": "markdown",
74+
"id": "72a6b954",
75+
"metadata": {},
76+
"source": [
77+
"## 3. Use the traced Anthropic client normally"
78+
]
79+
},
80+
{
81+
"cell_type": "markdown",
82+
"id": "76a350b4",
83+
"metadata": {},
84+
"source": [
85+
"That's it! Now you can continue using the traced Anthropic client normally. The data is automatically published to Openlayer and you can start creating tests around it!"
86+
]
87+
},
88+
{
89+
"cell_type": "code",
90+
"execution_count": null,
91+
"id": "e00c1c79",
92+
"metadata": {},
93+
"outputs": [],
94+
"source": [
95+
"response = anthropic_client.messages.create(\n",
96+
" model=\"claude-3-opus-20240229\",\n",
97+
" max_tokens=1024,\n",
98+
" messages=[\n",
99+
" {\"role\": \"user\", \"content\": \"How are you doing today?\"}],\n",
100+
")"
101+
]
102+
},
103+
{
104+
"cell_type": "code",
105+
"execution_count": null,
106+
"id": "d5093b5b-539c-4119-b5d3-dda6524edaa9",
107+
"metadata": {},
108+
"outputs": [],
109+
"source": []
110+
}
111+
],
112+
"metadata": {
113+
"kernelspec": {
114+
"display_name": "Python 3 (ipykernel)",
115+
"language": "python",
116+
"name": "python3"
117+
},
118+
"language_info": {
119+
"codemirror_mode": {
120+
"name": "ipython",
121+
"version": 3
122+
},
123+
"file_extension": ".py",
124+
"mimetype": "text/x-python",
125+
"name": "python",
126+
"nbconvert_exporter": "python",
127+
"pygments_lexer": "ipython3",
128+
"version": "3.9.18"
129+
}
130+
},
131+
"nbformat": 4,
132+
"nbformat_minor": 5
133+
}

src/openlayer/lib/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
__all__ = [
55
"trace",
6+
"trace_anthropic",
67
"trace_openai",
78
"trace_openai_assistant_thread_run",
89
]
@@ -13,6 +14,18 @@
1314
trace = tracer.trace
1415

1516

17+
def trace_anthropic(client):
18+
"""Trace Anthropic chat completions."""
19+
# pylint: disable=import-outside-toplevel
20+
import anthropic
21+
22+
from .integrations import anthropic_tracer
23+
24+
if not isinstance(client, anthropic.Anthropic):
25+
raise ValueError("Invalid client. Please provide an Anthropic client.")
26+
return anthropic_tracer.trace_anthropic(client)
27+
28+
1629
def trace_openai(client):
1730
"""Trace OpenAI chat completions."""
1831
# pylint: disable=import-outside-toplevel

0 commit comments

Comments
 (0)