@@ -127,23 +127,43 @@ def test_span_templates(sentry_init, capture_events):
127127
128128 @sentry_sdk .trace (template = SPANTEMPLATE .AI_TOOL )
129129 def my_tool (arg1 , arg2 ):
130- return "my_tool_result"
130+ mock_usage = mock .Mock ()
131+ mock_usage .input_tokens = 11
132+ mock_usage .output_tokens = 22
133+ mock_usage .total_tokens = 33
131134
132- @sentry_sdk .trace (template = SPANTEMPLATE .AI_CHAT )
133- def my_chat ():
134135 return {
135- "content" : "my_chat_result" ,
136- "usage" : {
137- "prompt_tokens" : 10 ,
138- "completion_tokens" : 20 ,
139- "total_tokens" : 30 ,
140- },
136+ "output" : "my_tool_result" ,
137+ "usage" : mock_usage ,
138+ }
139+
140+ @sentry_sdk .trace (template = SPANTEMPLATE .AI_CHAT )
141+ def my_chat (model = None , ** kwargs ):
142+ mock_result = mock .Mock ()
143+ mock_result .content = "my_chat_result"
144+ mock_result .usage = {
145+ "prompt_tokens" : 10 ,
146+ "completion_tokens" : 20 ,
147+ "total_tokens" : 30 ,
141148 }
149+ mock_result .model = f"{ model } -v123"
150+
151+ return mock_result
142152
143153 @sentry_sdk .trace (template = SPANTEMPLATE .AI_AGENT )
144154 def my_agent ():
145155 my_tool (1 , 2 )
146- my_chat ()
156+ my_chat (
157+ model = "my-gpt-4o-mini" ,
158+ prompt = "What is the weather in Tokyo?" ,
159+ system_prompt = "You are a helpful assistant that can answer questions about the weather." ,
160+ max_tokens = 100 ,
161+ temperature = 0.5 ,
162+ top_p = 0.9 ,
163+ top_k = 40 ,
164+ frequency_penalty = 1.0 ,
165+ presence_penalty = 2.0 ,
166+ )
147167
148168 with sentry_sdk .start_transaction (name = "test-transaction" ):
149169 my_agent ()
@@ -171,14 +191,26 @@ def my_agent():
171191 assert tool_span ["data" ] == {
172192 "gen_ai.tool.name" : "test_decorator.test_span_templates.<locals>.my_tool" ,
173193 "gen_ai.operation.name" : "execute_tool" ,
194+ "gen_ai.usage.input_tokens" : 11 ,
195+ "gen_ai.usage.output_tokens" : 22 ,
196+ "gen_ai.usage.total_tokens" : 33 ,
174197 "thread.id" : mock .ANY ,
175198 "thread.name" : mock .ANY ,
176199 }
177200
178201 assert chat_span ["op" ] == "gen_ai.chat"
179- assert chat_span ["description" ] == "chat"
202+ assert chat_span ["description" ] == "chat my-gpt-4o-mini "
180203 assert chat_span ["data" ] == {
181204 "gen_ai.operation.name" : "chat" ,
205+ "gen_ai.request.frequency_penalty" : 1.0 ,
206+ "gen_ai.request.max_tokens" : 100 ,
207+ "gen_ai.request.messages" : "[{'role': 'user', 'content': 'What is the weather in Tokyo?'}, {'role': 'system', 'content': 'You are a helpful assistant that can answer questions about the weather.'}]" ,
208+ "gen_ai.request.model" : "my-gpt-4o-mini" ,
209+ "gen_ai.request.presence_penalty" : 2.0 ,
210+ "gen_ai.request.temperature" : 0.5 ,
211+ "gen_ai.request.top_k" : 40 ,
212+ "gen_ai.request.top_p" : 0.9 ,
213+ "gen_ai.response.model" : "my-gpt-4o-mini-v123" ,
182214 "gen_ai.usage.input_tokens" : 10 ,
183215 "gen_ai.usage.output_tokens" : 20 ,
184216 "gen_ai.usage.total_tokens" : 30 ,
0 commit comments