|
| 1 | +import os |
| 2 | + |
| 3 | +from openai import OpenAI |
| 4 | + |
| 5 | +import sentry_sdk |
| 6 | +from sentry_sdk.ai.monitoring import ai_track |
| 7 | +from sentry_sdk.integrations.openai import OpenAIIntegration |
| 8 | +from sentry_sdk.integrations.stdlib import StdlibIntegration |
| 9 | + |
| 10 | + |
| 11 | +@ai_track("Streamed OpenAI workflow") |
| 12 | +def streamed_workflow(client): |
| 13 | + with sentry_sdk.start_transaction(name="openai-streamed"): |
| 14 | + # Chat Completions API with streaming |
| 15 | + print("Starting streamed chat completion...") |
| 16 | + stream = client.chat.completions.create( |
| 17 | + messages=[ |
| 18 | + { |
| 19 | + "role": "system", |
| 20 | + "content": "You are a helpful assistant that explains things concisely.", |
| 21 | + }, |
| 22 | + { |
| 23 | + "role": "user", |
| 24 | + "content": "Explain how photosynthesis works in 3 sentences.", |
| 25 | + } |
| 26 | + ], |
| 27 | + model="gpt-4o-mini", |
| 28 | + max_tokens=200, |
| 29 | + temperature=0.7, |
| 30 | + stream=True, |
| 31 | + ) |
| 32 | + |
| 33 | + print("--------------------------------") |
| 34 | + print("Streamed Response:") |
| 35 | + full_response = "" |
| 36 | + for chunk in stream: |
| 37 | + if chunk.choices[0].delta.content: |
| 38 | + content = chunk.choices[0].delta.content |
| 39 | + full_response += content |
| 40 | + print(content, end="", flush=True) |
| 41 | + print("\n--------------------------------") |
| 42 | + print(f"Full response length: {len(full_response)} chars") |
| 43 | + |
| 44 | + # Responses API with streaming |
| 45 | + print("\nStarting streamed responses API...") |
| 46 | + response = client.responses.create( |
| 47 | + model="gpt-4o-mini", |
| 48 | + instructions="You are a helpful assistant.", |
| 49 | + input="What are the three primary colors?", |
| 50 | + temperature=0.5, |
| 51 | + stream=True, |
| 52 | + ) |
| 53 | + print("--------------------------------") |
| 54 | + print("Streamed Responses API:") |
| 55 | + for chunk in response: |
| 56 | + print(f"Chunk type: {chunk.type}") |
| 57 | + if hasattr(chunk, 'delta') and chunk.delta: |
| 58 | + print(f" Delta: {chunk.delta}") |
| 59 | + print("--------------------------------") |
| 60 | + |
| 61 | + |
| 62 | +def before_send_transaction(event, hint): |
| 63 | + """Print gen_ai attributes from all spans before sending transaction.""" |
| 64 | + print("\n" + "=" * 80) |
| 65 | + print("TRANSACTION EVENT - Printing all spans with gen_ai attributes") |
| 66 | + print("=" * 80) |
| 67 | + |
| 68 | + spans = event.get("spans", []) |
| 69 | + for span in spans: |
| 70 | + op = span.get("op", "unknown") |
| 71 | + description = span.get("description", "no description") |
| 72 | + span_id = span.get("span_id", "unknown") |
| 73 | + data = span.get("data", {}) |
| 74 | + |
| 75 | + # Filter gen_ai prefixed attributes |
| 76 | + gen_ai_attrs = {k: v for k, v in data.items() if k.startswith("gen_ai")} |
| 77 | + |
| 78 | + if gen_ai_attrs: |
| 79 | + print(f"\nSPAN: {op} - {description}") |
| 80 | + print(f"Span ID: {span_id}") |
| 81 | + print("gen_ai attributes:") |
| 82 | + for key, value in sorted(gen_ai_attrs.items()): |
| 83 | + print(f" {key}: {value}") |
| 84 | + |
| 85 | + print("=" * 80 + "\n") |
| 86 | + return event |
| 87 | + |
| 88 | + |
| 89 | +def main(): |
| 90 | + sentry_sdk.init( |
| 91 | + dsn=os.getenv("SENTRY_DSN", None), |
| 92 | + environment=os.getenv("ENV", "openai-test-streamed"), |
| 93 | + traces_sample_rate=1.0, |
| 94 | + profiles_sample_rate=1.0, |
| 95 | + send_default_pii=True, |
| 96 | + debug=True, |
| 97 | + before_send_transaction=before_send_transaction, |
| 98 | + integrations=[ |
| 99 | + OpenAIIntegration( |
| 100 | + include_prompts=True, |
| 101 | + tiktoken_encoding_name="cl100k_base", |
| 102 | + ), |
| 103 | + ], |
| 104 | + disabled_integrations=[ |
| 105 | + StdlibIntegration(), |
| 106 | + ], |
| 107 | + ) |
| 108 | + |
| 109 | + client = OpenAI( |
| 110 | + api_key=os.environ.get("OPENAI_API_KEY"), |
| 111 | + ) |
| 112 | + |
| 113 | + streamed_workflow(client) |
| 114 | + |
| 115 | + print("--------------------------------") |
| 116 | + print("Done!") |
| 117 | + |
| 118 | + |
| 119 | +if __name__ == "__main__": |
| 120 | + main() |
0 commit comments