Skip to content

Commit f06c646

Browse files
committed
feat: azure export w/ async
1 parent 4cda646 commit f06c646

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

llm_observability_examples.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ def main_sync():
3333
groups = {"company": "test_company"}
3434

3535
try:
36-
# basic_openai_call(distinct_id, trace_id, properties, groups)
36+
basic_openai_call(distinct_id, trace_id, properties, groups)
3737
# streaming_openai_call(distinct_id, trace_id, properties, groups)
3838
# embedding_openai_call(distinct_id, trace_id, properties, groups)
3939
# image_openai_call()
40-
beta_openai_call(distinct_id, trace_id, properties, groups)
40+
# beta_openai_call(distinct_id, trace_id, properties, groups)
4141
except Exception as e:
4242
print("Error during OpenAI call:", str(e))
4343

@@ -216,6 +216,6 @@ def beta_openai_call(distinct_id, trace_id, properties, groups):
216216
# HOW TO RUN:
217217
# comment out one of these to run the other
218218

219-
# if __name__ == "__main__":
220-
# main_sync()
221-
asyncio.run(main_async())
219+
if __name__ == "__main__":
220+
main_sync()
221+
# asyncio.run(main_async())
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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 anthropic'")
6+
7+
from posthog.ai.openai.openai import WrappedBeta, WrappedChat, WrappedEmbeddings
8+
from posthog.ai.openai.openai_async import AsyncWrappedBeta, AsyncWrappedChat, AsyncWrappedEmbeddings
9+
from posthog.client import Client as PostHogClient
10+
11+
12+
class AzureOpenAI(openai.AzureOpenAI):
13+
"""
14+
A wrapper around the Azure OpenAI SDK that automatically sends LLM usage events to PostHog.
15+
"""
16+
17+
_ph_client: PostHogClient
18+
19+
def __init__(self, posthog_client: PostHogClient, **kwargs):
20+
super().__init__(**kwargs)
21+
self._ph_client = posthog_client
22+
self.chat = WrappedChat(self)
23+
self.embeddings = WrappedEmbeddings(self)
24+
self.beta = WrappedBeta(self)
25+
26+
27+
class AsyncAzureOpenAI(openai.AsyncAzureOpenAI):
28+
"""
29+
A wrapper around the Azure OpenAI SDK that automatically sends LLM usage events to PostHog.
30+
"""
31+
32+
_ph_client: PostHogClient
33+
34+
def __init__(self, posthog_client: PostHogClient, **kwargs):
35+
super().__init__(**kwargs)
36+
self._ph_client = posthog_client
37+
self.chat = AsyncWrappedChat(self)
38+
self.embeddings = AsyncWrappedEmbeddings(self)
39+
self.beta = AsyncWrappedBeta(self)

0 commit comments

Comments
 (0)