1
1
import os
2
2
import uuid
3
3
4
+ from pydantic import BaseModel
5
+
4
6
import posthog
5
7
from posthog .ai .openai import AsyncOpenAI , OpenAI
6
8
7
9
# Example credentials - replace these with your own or use environment variables
8
10
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" )
10
11
posthog .host = os .getenv ("POSTHOG_HOST" , "http://localhost:8000" ) # Or https://app.posthog.com
11
12
posthog .debug = True
12
13
# change this to False to see usage events
@@ -31,10 +32,11 @@ def main_sync():
31
32
groups = {"company" : "test_company" }
32
33
33
34
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 )
38
40
except Exception as e :
39
41
print ("Error during OpenAI call:" , str (e ))
40
42
@@ -187,10 +189,32 @@ async def embedding_async_openai_call(posthog_distinct_id, posthog_trace_id, pos
187
189
return response
188
190
189
191
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
+
190
215
# HOW TO RUN:
191
216
# comment out one of these to run the other
192
217
193
218
if __name__ == "__main__" :
194
219
main_sync ()
195
-
196
220
# asyncio.run(main_async())
0 commit comments