Skip to content

Commit 7dc4cbb

Browse files
feat: azure export w/ async (#200)
* feat: azure export w/ async Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
1 parent 4cda646 commit 7dc4cbb

File tree

5 files changed

+54
-7
lines changed

5 files changed

+54
-7
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
2+
## 3.18.0 - 2025-02-28
3+
4+
1. Add support for Azure OpenAI.
5+
16
## 3.17.0 - 2025-02-27
27

38
1. The LangChain handler now captures tools in `$ai_generation` events, in property `$ai_tools`. This allows for displaying tools provided to the LLM call in PostHog UI. Note that support for `$ai_tools` in OpenAI and Anthropic SDKs is coming soon.

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())

posthog/ai/openai/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from .openai import OpenAI
22
from .openai_async import AsyncOpenAI
3+
from .openai_providers import AsyncAzureOpenAI, AzureOpenAI
34

4-
__all__ = ["OpenAI", "AsyncOpenAI"]
5+
__all__ = ["OpenAI", "AsyncOpenAI", "AzureOpenAI", "AsyncAzureOpenAI"]
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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)

posthog/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION = "3.17.0"
1+
VERSION = "3.18.0"
22

33
if __name__ == "__main__":
44
print(VERSION, end="") # noqa: T201

0 commit comments

Comments
 (0)