Skip to content

Commit d1fa606

Browse files
committed
chore: reorganize imports
1 parent f629591 commit d1fa606

File tree

9 files changed

+29
-23
lines changed

9 files changed

+29
-23
lines changed

llm_observability_examples.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import uuid
33

44
import posthog
5-
from posthog.ai import AsyncOpenAI, OpenAI
5+
from posthog.ai.openai import AsyncOpenAI, OpenAI
66

77
# Example credentials - replace these with your own or use environment variables
88
posthog.project_api_key = os.getenv("POSTHOG_PROJECT_API_KEY", "your-project-api-key")

posthog/ai/__init__.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +0,0 @@
1-
from .providers.openai.openai import OpenAI
2-
from .providers.openai.openai_async import AsyncOpenAI
3-
4-
__all__ = ["OpenAI", "AsyncOpenAI"]

posthog/ai/langchain/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .callbacks import PosthogCallbackHandler
2+
3+
__all__ = ["PosthogCallbackHandler"]
Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
try:
2-
import langchain
2+
import langchain # noqa: F401
33
except ImportError:
44
raise ModuleNotFoundError("Please install LangChain to use this feature: 'pip install langchain'")
55

@@ -42,6 +42,10 @@ class RunMetadata(TypedDict, total=False):
4242

4343

4444
class PosthogCallbackHandler(BaseCallbackHandler):
45+
"""
46+
A callback handler for LangChain that sends events to PostHog LLM Observability.
47+
"""
48+
4549
_client: Client
4650
"""PostHog client instance."""
4751
_distinct_id: Optional[str]
@@ -371,20 +375,19 @@ def _parse_usage(response: LLMResult):
371375
message_chunk = getattr(generation_chunk, "message", {})
372376
response_metadata = getattr(message_chunk, "response_metadata", {})
373377

374-
chunk_usage = (
375-
(
376-
response_metadata.get("usage", None) # for Bedrock-Anthropic
377-
if isinstance(response_metadata, dict)
378-
else None
379-
)
380-
or (
381-
response_metadata.get("amazon-bedrock-invocationMetrics", None) # for Bedrock-Titan
382-
if isinstance(response_metadata, dict)
383-
else None
384-
)
385-
or getattr(message_chunk, "usage_metadata", None) # for Ollama
378+
bedrock_anthropic_usage = (
379+
response_metadata.get("usage", None) # for Bedrock-Anthropic
380+
if isinstance(response_metadata, dict)
381+
else None
382+
)
383+
bedrock_titan_usage = (
384+
response_metadata.get("amazon-bedrock-invocationMetrics", None) # for Bedrock-Titan
385+
if isinstance(response_metadata, dict)
386+
else None
386387
)
388+
ollama_usage = getattr(message_chunk, "usage_metadata", None) # for Ollama
387389

390+
chunk_usage = bedrock_anthropic_usage or bedrock_titan_usage or ollama_usage
388391
if chunk_usage:
389392
llm_usage = _parse_usage_model(chunk_usage)
390393
break

posthog/ai/openai/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from .openai import OpenAI
2+
from .openai_async import AsyncOpenAI
3+
4+
__all__ = ["OpenAI", "AsyncOpenAI"]
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
import uuid
33
from typing import Any, Dict, Optional
44

5-
import openai.resources
6-
75
try:
86
import openai
97
except ImportError:
108
raise ModuleNotFoundError("Please install the OpenAI SDK to use this feature: 'pip install openai'")
119

10+
import openai.resources
11+
1212
from posthog.ai.utils import call_llm_and_track_usage, get_model_params
1313
from posthog.client import Client as PostHogClient
1414

posthog/ai/providers/openai/openai_async.py renamed to posthog/ai/openai/openai_async.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
import uuid
33
from typing import Any, Dict, Optional
44

5-
import openai.resources
6-
75
try:
86
import openai
97
except ImportError:
108
raise ModuleNotFoundError("Please install the OpenAI SDK to use this feature: 'pip install openai'")
119

10+
import openai.resources
11+
1212
from posthog.ai.utils import call_llm_and_track_usage_async, get_model_params
1313
from posthog.client import Client as PostHogClient
1414

posthog/test/ai/providers/test_langchain.py renamed to posthog/test/ai/langchain/test_callbacks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from langchain_openai.chat_models import ChatOpenAI
1313
import pytest
1414

15-
from posthog.ai.providers.langchain import PosthogCallbackHandler
15+
from posthog.ai.langchain import PosthogCallbackHandler
1616

1717

1818
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")

0 commit comments

Comments
 (0)