Skip to content

Commit 36f01bb

Browse files
Add lazy init for Quotient client (#118)
* lazy init * fix detections * working lazy decorator * Cleanup * fixup * Add lazyinit example * cleanup example * Remove comments * Fix client checks --------- Co-authored-by: Mike Goitia <[email protected]>
1 parent 7216ce0 commit 36f01bb

File tree

5 files changed

+259
-41
lines changed

5 files changed

+259
-41
lines changed

examples/tracing/quotient_trace_openai.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
from quotientai import QuotientAI
66

77
quotient = QuotientAI()
8+
89
quotient.tracer.init(
9-
app_name="openinference_test_openai",
10+
app_name="quotient-trace-openai",
1011
environment="local",
1112
instruments=[OpenAIInstrumentor()],
1213
)
1314

14-
1515
@quotient.trace()
16-
def test_openai():
16+
def main():
1717
client = openai.OpenAI()
1818
response = client.chat.completions.create(
1919
model="gpt-3.5-turbo",
@@ -29,4 +29,4 @@ def test_openai():
2929

3030

3131
if __name__ == "__main__":
32-
test_openai()
32+
main()
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import os
2+
import openai
3+
4+
from openinference.instrumentation.openai import OpenAIInstrumentor
5+
6+
from quotientai import QuotientAI
7+
8+
# Initialize with lazy_init=True to avoid errors if API key is not available at build time
9+
quotient = QuotientAI(lazy_init=True)
10+
11+
# Apply decorator at module level - it will be a no-op until client is configured
12+
@quotient.trace()
13+
def test_openai():
14+
client = openai.OpenAI()
15+
response = client.chat.completions.create(
16+
model="gpt-3.5-turbo",
17+
messages=[{"role": "user", "content": "Write a haiku."}],
18+
max_tokens=20,
19+
stream=True,
20+
stream_options={"include_usage": True},
21+
)
22+
23+
for chunk in response:
24+
if chunk.choices and (content := chunk.choices[0].delta.content):
25+
print(content, end="")
26+
27+
28+
def setup_quotient():
29+
"""Configure QuotientAI at runtime when API key is available."""
30+
# Get API key from environment
31+
quotient_api_key = os.environ.get("QUOTIENT_API_KEY")
32+
33+
if not quotient_api_key:
34+
print("Warning: QUOTIENT_API_KEY not found. Tracing will be disabled.")
35+
return False
36+
37+
# Configure the client with the API key
38+
quotient.configure(quotient_api_key)
39+
40+
# Initialize the tracer with instruments
41+
quotient.tracer.init(
42+
app_name="quotient-trace-openai-lazyinit",
43+
environment="dev",
44+
instruments=[OpenAIInstrumentor()],
45+
)
46+
47+
print("QuotientAI tracing configured successfully.")
48+
return True
49+
50+
51+
if __name__ == "__main__":
52+
tracing_enabled = setup_quotient()
53+
print(
54+
"Running OpenAI test with",
55+
"tracing enabled" if tracing_enabled else "tracing disabled",
56+
)
57+
print("=" * 50)
58+
59+
test_openai()

examples/tracing/quotient_trace_qdrant.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424

2525
# Initialize QuotientAI client
26-
quotient = QuotientAI()
26+
quotient = QuotientAI(lazy=True)
2727

2828
# Initialize tracing with Qdrant instrumentor
2929
quotient.tracer.init(

0 commit comments

Comments
 (0)