Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Anthropic tracer #242

Merged
merged 1 commit into from
Jun 25, 2024
Merged
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
133 changes: 133 additions & 0 deletions examples/tracing/anthropic/anthropic_tracing.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "2722b419",
"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/anthropic/anthropic_tracing.ipynb)\n",
"\n",
"\n",
"# <a id=\"top\">Anthropic tracing</a>\n",
"\n",
"This notebook illustrates how to get started tracing Anthropic LLMs with Openlayer."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "020c8f6a",
"metadata": {},
"outputs": [],
"source": [
"!pip install openlayer"
]
},
{
"cell_type": "markdown",
"id": "75c2a473",
"metadata": {},
"source": [
"## 1. Set the environment variables"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f3f4fa13",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import anthropic\n",
"\n",
"# OpenAI env variables\n",
"os.environ[\"ANTHROPIC_API_KEY\"] = \"YOUR_ANTHROPIC_API_KEY_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\""
]
},
{
"cell_type": "markdown",
"id": "9758533f",
"metadata": {},
"source": [
"## 2. Import the `trace_anthropic` function"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c35d9860-dc41-4f7c-8d69-cc2ac7e5e485",
"metadata": {},
"outputs": [],
"source": [
"from openlayer.lib import trace_anthropic\n",
"\n",
"anthropic_client = trace_anthropic(anthropic.Anthropic())"
]
},
{
"cell_type": "markdown",
"id": "72a6b954",
"metadata": {},
"source": [
"## 3. Use the traced Anthropic client normally"
]
},
{
"cell_type": "markdown",
"id": "76a350b4",
"metadata": {},
"source": [
"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!"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e00c1c79",
"metadata": {},
"outputs": [],
"source": [
"response = anthropic_client.messages.create(\n",
" model=\"claude-3-opus-20240229\",\n",
" max_tokens=1024,\n",
" messages=[\n",
" {\"role\": \"user\", \"content\": \"How are you doing today?\"}],\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d5093b5b-539c-4119-b5d3-dda6524edaa9",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.18"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
13 changes: 13 additions & 0 deletions src/openlayer/lib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

__all__ = [
"trace",
"trace_anthropic",
"trace_openai",
"trace_openai_assistant_thread_run",
]
Expand All @@ -13,6 +14,18 @@
trace = tracer.trace


def trace_anthropic(client):
"""Trace Anthropic chat completions."""
# pylint: disable=import-outside-toplevel
import anthropic

from .integrations import anthropic_tracer

if not isinstance(client, anthropic.Anthropic):
raise ValueError("Invalid client. Please provide an Anthropic client.")
return anthropic_tracer.trace_anthropic(client)


def trace_openai(client):
"""Trace OpenAI chat completions."""
# pylint: disable=import-outside-toplevel
Expand Down
Loading