Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions examples/tracing/quotient_trace_openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
from quotientai import QuotientAI

quotient = QuotientAI()

quotient.tracer.init(
app_name="openinference_test_openai",
app_name="quotient-trace-openai",
environment="local",
instruments=[OpenAIInstrumentor()],
)


@quotient.trace()
def test_openai():
def main():
client = openai.OpenAI()
response = client.chat.completions.create(
model="gpt-3.5-turbo",
Expand All @@ -29,4 +29,4 @@ def test_openai():


if __name__ == "__main__":
test_openai()
main()
59 changes: 59 additions & 0 deletions examples/tracing/quotient_trace_openai_lazyinit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import os
import openai

from openinference.instrumentation.openai import OpenAIInstrumentor

from quotientai import QuotientAI

# Initialize with lazy_init=True to avoid errors if API key is not available at build time
quotient = QuotientAI(lazy_init=True)

# Apply decorator at module level - it will be a no-op until client is configured
@quotient.trace()
def test_openai():
client = openai.OpenAI()
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": "Write a haiku."}],
max_tokens=20,
stream=True,
stream_options={"include_usage": True},
)

for chunk in response:
if chunk.choices and (content := chunk.choices[0].delta.content):
print(content, end="")


def setup_quotient():
"""Configure QuotientAI at runtime when API key is available."""
# Get API key from environment
quotient_api_key = os.environ.get("QUOTIENT_API_KEY")

if not quotient_api_key:
print("Warning: QUOTIENT_API_KEY not found. Tracing will be disabled.")
return False

# Configure the client with the API key
quotient.configure(quotient_api_key)

# Initialize the tracer with instruments
quotient.tracer.init(
app_name="freddie-trace-openai-test",
environment="dev",
instruments=[OpenAIInstrumentor()],
)

print("QuotientAI tracing configured successfully.")
return True


if __name__ == "__main__":
tracing_enabled = setup_quotient()
print(
"Running OpenAI test with",
"tracing enabled" if tracing_enabled else "tracing disabled",
)
print("=" * 50)

test_openai()
2 changes: 1 addition & 1 deletion examples/tracing/quotient_trace_qdrant.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@


# Initialize QuotientAI client
quotient = QuotientAI()
quotient = QuotientAI(lazy=True)

# Initialize tracing with Qdrant instrumentor
quotient.tracer.init(
Expand Down
Loading
Loading