mirror of
https://github.com/hanzoai/insights-python.git
synced 2026-10-30 18:33:17 +00:00
- Remove Posthog = Insights alias from __init__.py - Remove PosthogContextMiddleware alias from django.py - Remove PostHogTracingProcessor alias from processor.py - Change $lib from "posthog-python" to "insights-python" - Change ingestion URLs from posthog.com to insights.hanzo.ai - Rename all posthog_* kwargs to insights_* across AI wrappers - Rename __posthog_exception_captured to __insights_exception_captured - Rename posthog_context_stack contextvar to insights_context_stack - Rename posthog🎏 Redis prefix to insights🎏 - Rename $$_posthog_redacted_* sentinels to $$_insights_redacted_* - Remove POSTHOG_MW_* Django settings fallback, X-POSTHOG-* headers - Rename Prompts(posthog=) param to Prompts(client=) - Update APP_ENDPOINT to us.insights.hanzo.ai - Update all tests, examples, docs, mypy config
99 lines
3.9 KiB
Python
99 lines
3.9 KiB
Python
try:
|
|
import openai
|
|
except ImportError:
|
|
raise ModuleNotFoundError(
|
|
"Please install the Open AI SDK to use this feature: 'pip install openai'"
|
|
)
|
|
|
|
from hanzo_insights.ai.openai.openai import (
|
|
WrappedBeta,
|
|
WrappedChat,
|
|
WrappedEmbeddings,
|
|
WrappedResponses,
|
|
)
|
|
from hanzo_insights.ai.openai.openai_async import WrappedBeta as AsyncWrappedBeta
|
|
from hanzo_insights.ai.openai.openai_async import WrappedChat as AsyncWrappedChat
|
|
from hanzo_insights.ai.openai.openai_async import WrappedEmbeddings as AsyncWrappedEmbeddings
|
|
from hanzo_insights.ai.openai.openai_async import WrappedResponses as AsyncWrappedResponses
|
|
from typing import Optional
|
|
|
|
from hanzo_insights.client import Client as InsightsClient
|
|
from hanzo_insights import setup
|
|
|
|
|
|
class AzureOpenAI(openai.AzureOpenAI):
|
|
"""
|
|
A wrapper around the Azure OpenAI SDK that automatically sends LLM usage events to Insights.
|
|
"""
|
|
|
|
_ph_client: InsightsClient
|
|
|
|
def __init__(self, insights_client: Optional[InsightsClient] = None, **kwargs):
|
|
"""
|
|
Args:
|
|
api_key: Azure OpenAI API key.
|
|
insights_client: If provided, events will be captured via this client instead
|
|
of the global hanzo_insights.
|
|
**openai_config: Any additional keyword args to set on Azure OpenAI (e.g. azure_endpoint="xxx").
|
|
"""
|
|
super().__init__(**kwargs)
|
|
self._ph_client = insights_client or setup()
|
|
|
|
# Store original objects after parent initialization (only if they exist)
|
|
self._original_chat = getattr(self, "chat", None)
|
|
self._original_embeddings = getattr(self, "embeddings", None)
|
|
self._original_beta = getattr(self, "beta", None)
|
|
self._original_responses = getattr(self, "responses", None)
|
|
|
|
# Replace with wrapped versions (only if originals exist)
|
|
if self._original_chat is not None:
|
|
self.chat = WrappedChat(self, self._original_chat)
|
|
|
|
if self._original_embeddings is not None:
|
|
self.embeddings = WrappedEmbeddings(self, self._original_embeddings)
|
|
|
|
if self._original_beta is not None:
|
|
self.beta = WrappedBeta(self, self._original_beta)
|
|
|
|
if self._original_responses is not None:
|
|
self.responses = WrappedResponses(self, self._original_responses)
|
|
|
|
|
|
class AsyncAzureOpenAI(openai.AsyncAzureOpenAI):
|
|
"""
|
|
An async wrapper around the Azure OpenAI SDK that automatically sends LLM usage events to Insights.
|
|
"""
|
|
|
|
_ph_client: InsightsClient
|
|
|
|
def __init__(self, insights_client: Optional[InsightsClient] = None, **kwargs):
|
|
"""
|
|
Args:
|
|
api_key: Azure OpenAI API key.
|
|
insights_client: If provided, events will be captured via this client instead
|
|
of the global hanzo_insights.
|
|
**openai_config: Any additional keyword args to set on Azure OpenAI (e.g. azure_endpoint="xxx").
|
|
"""
|
|
super().__init__(**kwargs)
|
|
self._ph_client = insights_client or setup()
|
|
|
|
# Store original objects after parent initialization (only if they exist)
|
|
self._original_chat = getattr(self, "chat", None)
|
|
self._original_embeddings = getattr(self, "embeddings", None)
|
|
self._original_beta = getattr(self, "beta", None)
|
|
self._original_responses = getattr(self, "responses", None)
|
|
|
|
# Replace with wrapped versions (only if originals exist)
|
|
if self._original_chat is not None:
|
|
self.chat = AsyncWrappedChat(self, self._original_chat)
|
|
|
|
if self._original_embeddings is not None:
|
|
self.embeddings = AsyncWrappedEmbeddings(self, self._original_embeddings)
|
|
|
|
if self._original_beta is not None:
|
|
self.beta = AsyncWrappedBeta(self, self._original_beta)
|
|
|
|
# Only add responses if available (newer OpenAI versions)
|
|
if self._original_responses is not None:
|
|
self.responses = AsyncWrappedResponses(self, self._original_responses)
|