|
| 1 | +try: |
| 2 | + import openai |
| 3 | + import openai.resources |
| 4 | +except ImportError: |
| 5 | + raise ModuleNotFoundError("Please install the Open AI SDK to use this feature: 'pip install openai'") |
| 6 | + |
| 7 | +from posthog.ai.openai.openai import WrappedBeta, WrappedChat, WrappedEmbeddings |
| 8 | +from posthog.ai.openai.openai_async import WrappedBeta as AsyncWrappedBeta |
| 9 | +from posthog.ai.openai.openai_async import WrappedChat as AsyncWrappedChat |
| 10 | +from posthog.ai.openai.openai_async import WrappedEmbeddings as AsyncWrappedEmbeddings |
| 11 | +from posthog.client import Client as PostHogClient |
| 12 | + |
| 13 | + |
| 14 | +class AzureOpenAI(openai.AzureOpenAI): |
| 15 | + """ |
| 16 | + A wrapper around the Azure OpenAI SDK that automatically sends LLM usage events to PostHog. |
| 17 | + """ |
| 18 | + |
| 19 | + _ph_client: PostHogClient |
| 20 | + |
| 21 | + def __init__(self, posthog_client: PostHogClient, **kwargs): |
| 22 | + super().__init__(**kwargs) |
| 23 | + self._ph_client = posthog_client |
| 24 | + self.chat = WrappedChat(self) |
| 25 | + self.embeddings = WrappedEmbeddings(self) |
| 26 | + self.beta = WrappedBeta(self) |
| 27 | + |
| 28 | + |
| 29 | +class AsyncAzureOpenAI(openai.AsyncAzureOpenAI): |
| 30 | + """ |
| 31 | + A wrapper around the Azure OpenAI SDK that automatically sends LLM usage events to PostHog. |
| 32 | + """ |
| 33 | + |
| 34 | + _ph_client: PostHogClient |
| 35 | + |
| 36 | + def __init__(self, posthog_client: PostHogClient, **kwargs): |
| 37 | + super().__init__(**kwargs) |
| 38 | + self._ph_client = posthog_client |
| 39 | + self.chat = AsyncWrappedChat(self) |
| 40 | + self.embeddings = AsyncWrappedEmbeddings(self) |
| 41 | + self.beta = AsyncWrappedBeta(self) |
0 commit comments