From ed83c81d9ee996b3b486edecf5a61bfa4f0fe833 Mon Sep 17 00:00:00 2001 From: Ian Vanagas <34755028+ivanagas@users.noreply.github.com> Date: Fri, 17 Jan 2025 13:32:03 -0800 Subject: [PATCH] anthropic observability --- .../ai-engineering/_snippets/anthropic.mdx | 49 +++++++++++++++++++ .../docs/ai-engineering/_snippets/openai.mdx | 6 +-- .../docs/ai-engineering/observability.mdx | 17 ++++++- 3 files changed, 67 insertions(+), 5 deletions(-) create mode 100644 contents/docs/ai-engineering/_snippets/anthropic.mdx diff --git a/contents/docs/ai-engineering/_snippets/anthropic.mdx b/contents/docs/ai-engineering/_snippets/anthropic.mdx new file mode 100644 index 000000000000..9fa61842250a --- /dev/null +++ b/contents/docs/ai-engineering/_snippets/anthropic.mdx @@ -0,0 +1,49 @@ +Start by installing the Anthropic Python SDK: + +```bash +pip install anthropic +``` + +In the spot where you initialize the Anthropic SDK, import PostHog and our Anthropic wrapper, initialize PostHog with your project API key and host (from [your project settings](https://us.posthog.com/settings/project)), and pass it to our Anthropic wrapper. + +```python +from posthog.ai.anthropic import Anthropic +import posthog + +posthog.project_api_key = "" +posthog.host = "" + +client = Anthropic( + api_key="sk-ant-api...", # Replace with your Anthropic API key + posthog_client=posthog +) +``` + +> **Note:** This also works with the `AsyncAnthropic` client as well as `AnthropicBedrock`, `AnthropicVertex`, and the async versions of those. + +Now, when you use the Anthropic SDK, it automatically captures many properties into PostHog including `$ai_input`, `$ai_input_tokens`, `$ai_latency`, `$ai_model`, `$ai_model_parameters`, `$ai_output_choices`, and `$ai_output_tokens`. + +You can also capture additional properties like `posthog_distinct_id`, `posthog_trace_id`, `posthog_properties`, `posthog_groups`, and `posthog_privacy_mode`. + +```python +response = client.messages.create( + model="claude-3-opus-20240229", + messages=[ + { + "role": "user", + "content": "Tell me a fun fact about hedgehogs" + } + ], + posthog_distinct_id="user_123", # optional + posthog_trace_id="trace_123", # optional + posthog_properties={"conversation_id": "abc123", "paid": True}, # optional + posthog_groups={"company": "company_id_in_your_db"}, # optional + posthog_privacy_mode=False # optional +) + +print(response.content[0].text) +``` + +> **Notes:** +> - This also works when message streams are used (e.g. `stream=True` or `client.messages.stream(...)`). +> - If you want to capture LLM events anonymously, **don't** pass a distinct ID to the request. See our docs on [anonymous vs identified events](/docs/data/anonymous-vs-identified-events) to learn more. \ No newline at end of file diff --git a/contents/docs/ai-engineering/_snippets/openai.mdx b/contents/docs/ai-engineering/_snippets/openai.mdx index c46db5a78631..076696edd2f0 100644 --- a/contents/docs/ai-engineering/_snippets/openai.mdx +++ b/contents/docs/ai-engineering/_snippets/openai.mdx @@ -21,7 +21,7 @@ client = OpenAI( > **Note:** This also works with the `AsyncOpenAI` client. -Now, when you use the OpenAI SDK, it automatically captures many properties into PostHog including `$ai_input`, `$ai_input_tokens`, `$ai_latency`, `$ai_model`, `$ai_model_parameters`, `$ai_output`, and `$ai_output_tokens`. +Now, when you use the OpenAI SDK, it automatically captures many properties into PostHog including `$ai_input`, `$ai_input_tokens`, `$ai_latency`, `$ai_model`, `$ai_model_parameters`, `$ai_output_choices`, and `$ai_output_tokens`. You can also capture additional properties like `posthog_distinct_id`, `posthog_trace_id`, `posthog_properties`, `posthog_groups`, and `posthog_privacy_mode`. @@ -33,8 +33,8 @@ response = client.chat.completions.create( ], posthog_distinct_id="user_123", # optional posthog_trace_id="trace_123", # optional - posthog_properties={"conversation_id": "abc123", "paid": True} # optional - posthog_groups={"company": "company_id_in_your_db"} # optional + posthog_properties={"conversation_id": "abc123", "paid": True}, # optional + posthog_groups={"company": "company_id_in_your_db"}, # optional posthog_privacy_mode=False # optional ) diff --git a/contents/docs/ai-engineering/observability.mdx b/contents/docs/ai-engineering/observability.mdx index 0f5b927bf6e6..b0540416405e 100644 --- a/contents/docs/ai-engineering/observability.mdx +++ b/contents/docs/ai-engineering/observability.mdx @@ -32,18 +32,23 @@ The rest of the setup depends on the LLM platform you're using. These SDKs _do n import Tab from "components/Tab" import OpenAIInstall from "./_snippets/openai.mdx" +import AnthropicInstall from "./_snippets/anthropic.mdx" import LangChainInstall from "./_snippets/langchain.mdx" - + OpenAI + Anthropic LangChain + + + @@ -52,7 +57,7 @@ import LangChainInstall from "./_snippets/langchain.mdx" ## Privacy mode -To avoid storing potentially sensitive prompt and completion data, you can enable privacy mode. This excludes the `$ai_input` and `$ai_output` properties from being captured. +To avoid storing potentially sensitive prompt and completion data, you can enable privacy mode. This excludes the `$ai_input` and `$ai_output_choices` properties from being captured. This can be done either by setting the `privacy_mode` config option in the Python SDK like this: @@ -75,6 +80,14 @@ client.chat.completions.create( ) ``` +```python file=Anthropic +response = client.messages.create( + model="claude-3-opus-20240229", + messages=[...], + posthog_privacy_mode=True +) +``` + ```python file=LangChain callback_handler = PosthogCallbackHandler( client,