99posthog .personal_api_key = os .getenv ("POSTHOG_PERSONAL_API_KEY" , "your-personal-api-key" )
1010posthog .host = os .getenv ("POSTHOG_HOST" , "http://localhost:8000" ) # Or https://app.posthog.com
1111posthog .debug = True
12+ # change this to False to see usage events
13+ # posthog.privacy_mode = True
1214
1315openai_client = OpenAI (
1416 api_key = os .getenv ("OPENAI_API_KEY" , "your-openai-api-key" ),
@@ -26,11 +28,12 @@ def main_sync():
2628 print ("Trace ID:" , trace_id )
2729 distinct_id = "test2_distinct_id"
2830 properties = {"test_property" : "test_value" }
31+ groups = {"company" : "test_company" }
2932
3033 try :
31- basic_openai_call (distinct_id , trace_id , properties )
32- streaming_openai_call (distinct_id , trace_id , properties )
33- embedding_openai_call (distinct_id , trace_id , properties )
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 )
3437 image_openai_call ()
3538 except Exception as e :
3639 print ("Error during OpenAI call:" , str (e ))
@@ -41,17 +44,18 @@ async def main_async():
4144 print ("Trace ID:" , trace_id )
4245 distinct_id = "test_distinct_id"
4346 properties = {"test_property" : "test_value" }
47+ groups = {"company" : "test_company" }
4448
4549 try :
46- await basic_async_openai_call (distinct_id , trace_id , properties )
47- await streaming_async_openai_call (distinct_id , trace_id , properties )
48- await embedding_async_openai_call (distinct_id , trace_id , properties )
50+ await basic_async_openai_call (distinct_id , trace_id , properties , groups )
51+ await streaming_async_openai_call (distinct_id , trace_id , properties , groups )
52+ await embedding_async_openai_call (distinct_id , trace_id , properties , groups )
4953 await image_async_openai_call ()
5054 except Exception as e :
5155 print ("Error during OpenAI call:" , str (e ))
5256
5357
54- def basic_openai_call (distinct_id , trace_id , properties ):
58+ def basic_openai_call (distinct_id , trace_id , properties , groups ):
5559 response = openai_client .chat .completions .create (
5660 model = "gpt-4o-mini" ,
5761 messages = [
@@ -63,6 +67,7 @@ def basic_openai_call(distinct_id, trace_id, properties):
6367 posthog_distinct_id = distinct_id ,
6468 posthog_trace_id = trace_id ,
6569 posthog_properties = properties ,
70+ posthog_groups = groups ,
6671 )
6772 print (response )
6873 if response and response .choices :
@@ -72,7 +77,7 @@ def basic_openai_call(distinct_id, trace_id, properties):
7277 return response
7378
7479
75- async def basic_async_openai_call (distinct_id , trace_id , properties ):
80+ async def basic_async_openai_call (distinct_id , trace_id , properties , groups ):
7681 response = await async_openai_client .chat .completions .create (
7782 model = "gpt-4o-mini" ,
7883 messages = [
@@ -84,6 +89,7 @@ async def basic_async_openai_call(distinct_id, trace_id, properties):
8489 posthog_distinct_id = distinct_id ,
8590 posthog_trace_id = trace_id ,
8691 posthog_properties = properties ,
92+ posthog_groups = groups ,
8793 )
8894 if response and hasattr (response , "choices" ):
8995 print ("OpenAI response:" , response .choices [0 ].message .content )
@@ -92,7 +98,7 @@ async def basic_async_openai_call(distinct_id, trace_id, properties):
9298 return response
9399
94100
95- def streaming_openai_call (distinct_id , trace_id , properties ):
101+ def streaming_openai_call (distinct_id , trace_id , properties , groups ):
96102
97103 response = openai_client .chat .completions .create (
98104 model = "gpt-4o-mini" ,
@@ -106,6 +112,7 @@ def streaming_openai_call(distinct_id, trace_id, properties):
106112 posthog_distinct_id = distinct_id ,
107113 posthog_trace_id = trace_id ,
108114 posthog_properties = properties ,
115+ posthog_groups = groups ,
109116 )
110117
111118 for chunk in response :
@@ -115,7 +122,7 @@ def streaming_openai_call(distinct_id, trace_id, properties):
115122 return response
116123
117124
118- async def streaming_async_openai_call (distinct_id , trace_id , properties ):
125+ async def streaming_async_openai_call (distinct_id , trace_id , properties , groups ):
119126 response = await async_openai_client .chat .completions .create (
120127 model = "gpt-4o-mini" ,
121128 messages = [
@@ -128,6 +135,7 @@ async def streaming_async_openai_call(distinct_id, trace_id, properties):
128135 posthog_distinct_id = distinct_id ,
129136 posthog_trace_id = trace_id ,
130137 posthog_properties = properties ,
138+ posthog_groups = groups ,
131139 )
132140
133141 async for chunk in response :
@@ -153,25 +161,27 @@ async def image_async_openai_call():
153161 return response
154162
155163
156- def embedding_openai_call (posthog_distinct_id , posthog_trace_id , posthog_properties ):
164+ def embedding_openai_call (posthog_distinct_id , posthog_trace_id , posthog_properties , posthog_groups ):
157165 response = openai_client .embeddings .create (
158166 input = "The hedgehog is cute" ,
159167 model = "text-embedding-3-small" ,
160168 posthog_distinct_id = posthog_distinct_id ,
161169 posthog_trace_id = posthog_trace_id ,
162170 posthog_properties = posthog_properties ,
171+ posthog_groups = posthog_groups ,
163172 )
164173 print (response )
165174 return response
166175
167176
168- async def embedding_async_openai_call (posthog_distinct_id , posthog_trace_id , posthog_properties ):
177+ async def embedding_async_openai_call (posthog_distinct_id , posthog_trace_id , posthog_properties , posthog_groups ):
169178 response = await async_openai_client .embeddings .create (
170179 input = "The hedgehog is cute" ,
171180 model = "text-embedding-3-small" ,
172181 posthog_distinct_id = posthog_distinct_id ,
173182 posthog_trace_id = posthog_trace_id ,
174183 posthog_properties = posthog_properties ,
184+ posthog_groups = posthog_groups ,
175185 )
176186 print (response )
177187 return response
0 commit comments