|
| 1 | +import os |
| 2 | +import uuid |
| 3 | + |
| 4 | +import posthog |
| 5 | +from posthog.ai import AsyncOpenAI, OpenAI |
| 6 | + |
| 7 | +# Example credentials - replace these with your own or use environment variables |
| 8 | +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 | +posthog.host = os.getenv("POSTHOG_HOST", "http://localhost:8000") # Or https://app.posthog.com |
| 11 | +posthog.debug = True |
| 12 | + |
| 13 | +openai_client = OpenAI( |
| 14 | + api_key=os.getenv("OPENAI_API_KEY", "your-openai-api-key"), |
| 15 | + posthog_client=posthog, |
| 16 | +) |
| 17 | + |
| 18 | +async_openai_client = AsyncOpenAI( |
| 19 | + api_key=os.getenv("OPENAI_API_KEY", "your-openai-api-key"), |
| 20 | + posthog_client=posthog, |
| 21 | +) |
| 22 | + |
| 23 | + |
| 24 | +def main_sync(): |
| 25 | + trace_id = str(uuid.uuid4()) |
| 26 | + print("Trace ID:", trace_id) |
| 27 | + distinct_id = "test_distinct_id" |
| 28 | + properties = {"test_property": "test_value"} |
| 29 | + |
| 30 | + try: |
| 31 | + basic_openai_call(distinct_id, trace_id, properties) |
| 32 | + streaming_openai_call(distinct_id, trace_id, properties) |
| 33 | + non_instrumented_openai_call() |
| 34 | + except Exception as e: |
| 35 | + print("Error during OpenAI call:", str(e)) |
| 36 | + |
| 37 | + |
| 38 | +async def main_async(): |
| 39 | + trace_id = str(uuid.uuid4()) |
| 40 | + print("Trace ID:", trace_id) |
| 41 | + distinct_id = "test_distinct_id" |
| 42 | + properties = {"test_property": "test_value"} |
| 43 | + |
| 44 | + try: |
| 45 | + await basic_async_openai_call(distinct_id, trace_id, properties) |
| 46 | + await streaming_async_openai_call(distinct_id, trace_id, properties) |
| 47 | + except Exception as e: |
| 48 | + print("Error during OpenAI call:", str(e)) |
| 49 | + |
| 50 | + |
| 51 | +def basic_openai_call(distinct_id, trace_id, properties): |
| 52 | + response = openai_client.chat.completions.create( |
| 53 | + model="gpt-4o-mini", |
| 54 | + messages=[ |
| 55 | + {"role": "system", "content": "You are a complex problem solver."}, |
| 56 | + {"role": "user", "content": "Explain quantum computing in simple terms."}, |
| 57 | + ], |
| 58 | + max_tokens=100, |
| 59 | + temperature=0.7, |
| 60 | + posthog_distinct_id=distinct_id, |
| 61 | + posthog_trace_id=trace_id, |
| 62 | + posthog_properties=properties, |
| 63 | + ) |
| 64 | + print(response) |
| 65 | + if response and response.choices: |
| 66 | + print("OpenAI response:", response.choices[0].message.content) |
| 67 | + else: |
| 68 | + print("No response or unexpected format returned.") |
| 69 | + return response |
| 70 | + |
| 71 | + |
| 72 | +async def basic_async_openai_call(distinct_id, trace_id, properties): |
| 73 | + response = await async_openai_client.chat.completions.create( |
| 74 | + model="gpt-4o-mini", |
| 75 | + messages=[ |
| 76 | + {"role": "system", "content": "You are a complex problem solver."}, |
| 77 | + {"role": "user", "content": "Explain quantum computing in simple terms."}, |
| 78 | + ], |
| 79 | + max_tokens=100, |
| 80 | + temperature=0.7, |
| 81 | + posthog_distinct_id=distinct_id, |
| 82 | + posthog_trace_id=trace_id, |
| 83 | + posthog_properties=properties, |
| 84 | + ) |
| 85 | + if response and hasattr(response, "choices"): |
| 86 | + print("OpenAI response:", response.choices[0].message.content) |
| 87 | + else: |
| 88 | + print("No response or unexpected format returned.") |
| 89 | + return response |
| 90 | + |
| 91 | + |
| 92 | +def streaming_openai_call(distinct_id, trace_id, properties): |
| 93 | + |
| 94 | + response = openai_client.chat.completions.create( |
| 95 | + model="gpt-4o-mini", |
| 96 | + messages=[ |
| 97 | + {"role": "system", "content": "You are a complex problem solver."}, |
| 98 | + {"role": "user", "content": "Explain quantum computing in simple terms."}, |
| 99 | + ], |
| 100 | + max_tokens=100, |
| 101 | + temperature=0.7, |
| 102 | + stream=True, |
| 103 | + posthog_distinct_id=distinct_id, |
| 104 | + posthog_trace_id=trace_id, |
| 105 | + posthog_properties=properties, |
| 106 | + ) |
| 107 | + |
| 108 | + for chunk in response: |
| 109 | + if hasattr(chunk, "choices") and chunk.choices and len(chunk.choices) > 0: |
| 110 | + print(chunk.choices[0].delta.content or "", end="") |
| 111 | + |
| 112 | + return response |
| 113 | + |
| 114 | + |
| 115 | +async def streaming_async_openai_call(distinct_id, trace_id, properties): |
| 116 | + response = await async_openai_client.chat.completions.create( |
| 117 | + model="gpt-4o-mini", |
| 118 | + messages=[ |
| 119 | + {"role": "system", "content": "You are a complex problem solver."}, |
| 120 | + {"role": "user", "content": "Explain quantum computing in simple terms."}, |
| 121 | + ], |
| 122 | + max_tokens=100, |
| 123 | + temperature=0.7, |
| 124 | + stream=True, |
| 125 | + posthog_distinct_id=distinct_id, |
| 126 | + posthog_trace_id=trace_id, |
| 127 | + posthog_properties=properties, |
| 128 | + ) |
| 129 | + |
| 130 | + async for chunk in response: |
| 131 | + if hasattr(chunk, "choices") and chunk.choices and len(chunk.choices) > 0: |
| 132 | + print(chunk.choices[0].delta.content or "", end="") |
| 133 | + |
| 134 | + return response |
| 135 | + |
| 136 | + |
| 137 | +def non_instrumented_openai_call(): |
| 138 | + response = openai_client.images.generate(model="dall-e-3", prompt="A cute baby hedgehog", n=1, size="1024x1024") |
| 139 | + print(response) |
| 140 | + return response |
| 141 | + |
| 142 | + |
| 143 | +# HOW TO RUN: |
| 144 | +# comment out one of these to run the other |
| 145 | + |
| 146 | +if __name__ == "__main__": |
| 147 | + main_sync() |
| 148 | + |
| 149 | +# asyncio.run(main_async()) |
0 commit comments