11from opentelemetry .semconv_ai import SpanAttributes
22import pytest
33from openai import OpenAI
4- from traceloop .sdk .tracing .manual import LLMMessage , track_llm_call
4+ from traceloop .sdk .tracing .manual import LLMMessage , LLMUsage , track_llm_call
55
66
77@pytest .fixture
88def openai_client ():
99 return OpenAI ()
1010
1111
12- @pytest .mark .vcr
1312def test_manual_report (exporter , openai_client ):
1413 with track_llm_call (vendor = "openai" , type = "chat" ) as span :
1514 span .report_request (
@@ -19,14 +18,21 @@ def test_manual_report(exporter, openai_client):
1918 ],
2019 )
2120
22- res = openai_client .chat .completions .create (
23- model = "gpt-3.5-turbo" ,
24- messages = [
25- {"role" : "user" , "content" : "Tell me a joke about opentelemetry" }
26- ],
27- )
21+ res = [
22+ "Why did the opentelemetry developer break up with their partner? Because they were tired"
23+ + " of constantly tracing their every move!" ,
24+ ]
2825
29- span .report_response (res .model , [text .message .content for text in res .choices ])
26+ span .report_response ("gpt-3.5-turbo-0125" , res )
27+ span .report_usage (
28+ LLMUsage (
29+ prompt_tokens = 15 ,
30+ completion_tokens = 24 ,
31+ total_tokens = 39 ,
32+ cache_creation_input_tokens = 15 ,
33+ cache_read_input_tokens = 18 ,
34+ )
35+ )
3036
3137 spans = exporter .get_finished_spans ()
3238 open_ai_span = spans [0 ]
@@ -46,3 +52,13 @@ def test_manual_report(exporter, openai_client):
4652 + " of constantly tracing their every move!"
4753 )
4854 assert open_ai_span .end_time > open_ai_span .start_time
55+ assert open_ai_span .attributes [SpanAttributes .LLM_USAGE_PROMPT_TOKENS ] == 15
56+ assert open_ai_span .attributes [SpanAttributes .LLM_USAGE_COMPLETION_TOKENS ] == 24
57+ assert open_ai_span .attributes [SpanAttributes .LLM_USAGE_TOTAL_TOKENS ] == 39
58+ assert (
59+ open_ai_span .attributes [SpanAttributes .LLM_USAGE_CACHE_CREATION_INPUT_TOKENS ]
60+ == 15
61+ )
62+ assert (
63+ open_ai_span .attributes [SpanAttributes .LLM_USAGE_CACHE_READ_INPUT_TOKENS ] == 18
64+ )
0 commit comments