Skip to content

Commit 5bbfb9d

Browse files
committed
feat: add beta parse method support
1 parent 4155080 commit 5bbfb9d

File tree

3 files changed

+114
-7
lines changed

3 files changed

+114
-7
lines changed

llm_observability_examples.py

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33

44
import posthog
55
from posthog.ai.openai import AsyncOpenAI, OpenAI
6+
from pydantic import BaseModel
67

78
# Example credentials - replace these with your own or use environment variables
89
posthog.project_api_key = os.getenv("POSTHOG_PROJECT_API_KEY", "your-project-api-key")
9-
posthog.personal_api_key = os.getenv("POSTHOG_PERSONAL_API_KEY", "your-personal-api-key")
1010
posthog.host = os.getenv("POSTHOG_HOST", "http://localhost:8000") # Or https://app.posthog.com
1111
posthog.debug = True
1212
# change this to False to see usage events
@@ -22,7 +22,6 @@
2222
posthog_client=posthog,
2323
)
2424

25-
2625
def main_sync():
2726
trace_id = str(uuid.uuid4())
2827
print("Trace ID:", trace_id)
@@ -31,10 +30,11 @@ def main_sync():
3130
groups = {"company": "test_company"}
3231

3332
try:
34-
basic_openai_call(distinct_id, trace_id, properties, groups)
35-
streaming_openai_call(distinct_id, trace_id, properties, groups)
36-
embedding_openai_call(distinct_id, trace_id, properties, groups)
37-
image_openai_call()
33+
# basic_openai_call(distinct_id, trace_id, properties, groups)
34+
# streaming_openai_call(distinct_id, trace_id, properties, groups)
35+
# embedding_openai_call(distinct_id, trace_id, properties, groups)
36+
# image_openai_call()
37+
beta_openai_call(distinct_id, trace_id, properties, groups)
3838
except Exception as e:
3939
print("Error during OpenAI call:", str(e))
4040

@@ -187,10 +187,32 @@ async def embedding_async_openai_call(posthog_distinct_id, posthog_trace_id, pos
187187
return response
188188

189189

190+
# TODO: add beta client
191+
192+
class CalendarEvent(BaseModel):
193+
name: str
194+
date: str
195+
participants: list[str]
196+
197+
def beta_openai_call(distinct_id, trace_id, properties, groups):
198+
response = openai_client.beta.chat.completions.parse(
199+
model="gpt-4o-mini",
200+
messages=[
201+
{"role": "system", "content": "Extract the event information."},
202+
{"role": "user", "content": "Alice and Bob are going to a science fair on Friday."},
203+
],
204+
response_format=CalendarEvent,
205+
posthog_distinct_id=distinct_id,
206+
posthog_trace_id=trace_id,
207+
posthog_properties=properties,
208+
posthog_groups=groups,
209+
)
210+
print(response)
211+
return response
212+
190213
# HOW TO RUN:
191214
# comment out one of these to run the other
192215

193216
if __name__ == "__main__":
194217
main_sync()
195-
196218
# asyncio.run(main_async())

posthog/ai/openai/openai.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def __init__(self, posthog_client: PostHogClient, **kwargs):
3131
self._ph_client = posthog_client
3232
self.chat = WrappedChat(self)
3333
self.embeddings = WrappedEmbeddings(self)
34+
self.beta = WrappedBeta(self)
3435

3536

3637
class WrappedChat(openai.resources.chat.Chat):
@@ -249,3 +250,46 @@ def create(
249250
)
250251

251252
return response
253+
254+
255+
class WrappedBeta(openai.resources.beta.Beta):
256+
_client: OpenAI
257+
258+
@property
259+
def chat(self):
260+
return WrappedBetaChat(self._client)
261+
262+
263+
class WrappedBetaChat(openai.resources.beta.chat.Chat):
264+
_client: OpenAI
265+
266+
@property
267+
def completions(self):
268+
return WrappedBetaCompletions(self._client)
269+
270+
271+
class WrappedBetaCompletions(openai.resources.beta.chat.completions.Completions):
272+
_client: OpenAI
273+
274+
def parse(
275+
self,
276+
posthog_distinct_id: Optional[str] = None,
277+
posthog_trace_id: Optional[str] = None,
278+
posthog_properties: Optional[Dict[str, Any]] = None,
279+
posthog_privacy_mode: bool = False,
280+
posthog_groups: Optional[Dict[str, Any]] = None,
281+
**kwargs: Any,
282+
):
283+
print("kwargs", kwargs)
284+
return call_llm_and_track_usage(
285+
posthog_distinct_id,
286+
self._client._ph_client,
287+
"openai",
288+
posthog_trace_id,
289+
posthog_properties,
290+
posthog_privacy_mode,
291+
posthog_groups,
292+
self._client.base_url,
293+
super().parse,
294+
**kwargs,
295+
)

posthog/ai/openai/openai_async.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,3 +248,44 @@ async def create(
248248
)
249249

250250
return response
251+
252+
class WrappedBeta(openai.resources.beta.Beta):
253+
_client: AsyncOpenAI
254+
255+
@property
256+
def chat(self):
257+
return WrappedBetaChat(self._client)
258+
259+
260+
class WrappedBetaChat(openai.resources.beta.chat.Chat):
261+
_client: AsyncOpenAI
262+
263+
@property
264+
def completions(self):
265+
return WrappedBetaCompletions(self._client)
266+
267+
268+
class WrappedBetaCompletions(openai.resources.beta.chat.completions.Completions):
269+
_client: AsyncOpenAI
270+
271+
def parse(
272+
self,
273+
posthog_distinct_id: Optional[str] = None,
274+
posthog_trace_id: Optional[str] = None,
275+
posthog_properties: Optional[Dict[str, Any]] = None,
276+
posthog_privacy_mode: bool = False,
277+
posthog_groups: Optional[Dict[str, Any]] = None,
278+
**kwargs: Any,
279+
):
280+
return call_llm_and_track_usage_async(
281+
posthog_distinct_id,
282+
self._client._ph_client,
283+
"openai",
284+
posthog_trace_id,
285+
posthog_properties,
286+
posthog_privacy_mode,
287+
posthog_groups,
288+
self._client.base_url,
289+
super().parse,
290+
**kwargs,
291+
)

0 commit comments

Comments
 (0)