11import os
22import uuid
33
4+ from pydantic import BaseModel
5+
46import posthog
57from posthog .ai .openai import AsyncOpenAI , OpenAI
68
79# Example credentials - replace these with your own or use environment variables
810posthog .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" )
1011posthog .host = os .getenv ("POSTHOG_HOST" , "http://localhost:8000" ) # Or https://app.posthog.com
1112posthog .debug = True
1213# change this to False to see usage events
@@ -31,10 +32,11 @@ def main_sync():
3132 groups = {"company" : "test_company" }
3233
3334 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 ()
35+ # basic_openai_call(distinct_id, trace_id, properties, groups)
36+ # streaming_openai_call(distinct_id, trace_id, properties, groups)
37+ # embedding_openai_call(distinct_id, trace_id, properties, groups)
38+ # image_openai_call()
39+ beta_openai_call (distinct_id , trace_id , properties , groups )
3840 except Exception as e :
3941 print ("Error during OpenAI call:" , str (e ))
4042
@@ -187,10 +189,32 @@ async def embedding_async_openai_call(posthog_distinct_id, posthog_trace_id, pos
187189 return response
188190
189191
192+ class CalendarEvent (BaseModel ):
193+ name : str
194+ date : str
195+ participants : list [str ]
196+
197+
198+ def beta_openai_call (distinct_id , trace_id , properties , groups ):
199+ response = openai_client .beta .chat .completions .parse (
200+ model = "gpt-4o-mini" ,
201+ messages = [
202+ {"role" : "system" , "content" : "Extract the event information." },
203+ {"role" : "user" , "content" : "Alice and Bob are going to a science fair on Friday." },
204+ ],
205+ response_format = CalendarEvent ,
206+ posthog_distinct_id = distinct_id ,
207+ posthog_trace_id = trace_id ,
208+ posthog_properties = properties ,
209+ posthog_groups = groups ,
210+ )
211+ print (response )
212+ return response
213+
214+
190215# HOW TO RUN:
191216# comment out one of these to run the other
192217
193218if __name__ == "__main__" :
194219 main_sync ()
195-
196220# asyncio.run(main_async())
0 commit comments