22
33import pytest
44import vertexai
5+ from vertexai .language_models import (
6+ ChatModel ,
7+ InputOutputTextPair ,
8+ TextGenerationModel ,
9+ )
10+
511from opentelemetry .semconv_ai import SpanAttributes
6- from vertexai .language_models import TextGenerationModel , ChatModel , InputOutputTextPair
712
813vertexai .init ()
914
@@ -31,14 +36,17 @@ def test_vertexai_predict(exporter):
3136
3237 vertexai_span = spans [0 ]
3338 assert (
34- vertexai_span .attributes [SpanAttributes .LLM_REQUEST_MODEL ] == "text-bison@001"
39+ vertexai_span .attributes [SpanAttributes .LLM_REQUEST_MODEL ]
40+ == "text-bison@001"
3541 )
3642 assert (
3743 "Give me ten interview questions for the role of program manager."
3844 in vertexai_span .attributes [f"{ SpanAttributes .LLM_PROMPTS } .0.user" ]
3945 )
4046 assert vertexai_span .attributes [SpanAttributes .LLM_REQUEST_TOP_P ] == 0.8
41- assert vertexai_span .attributes [SpanAttributes .LLM_REQUEST_MAX_TOKENS ] == 256
47+ assert (
48+ vertexai_span .attributes [SpanAttributes .LLM_REQUEST_MAX_TOKENS ] == 256
49+ )
4250 assert vertexai_span .attributes [SpanAttributes .LLM_TOP_K ] == 40
4351 assert (
4452 vertexai_span .attributes [f"{ SpanAttributes .LLM_COMPLETIONS } .0.content" ]
@@ -74,14 +82,17 @@ async def async_predict_text() -> str:
7482
7583 vertexai_span = spans [0 ]
7684 assert (
77- vertexai_span .attributes [SpanAttributes .LLM_REQUEST_MODEL ] == "text-bison@001"
85+ vertexai_span .attributes [SpanAttributes .LLM_REQUEST_MODEL ]
86+ == "text-bison@001"
7887 )
7988 assert (
8089 "Give me ten interview questions for the role of program manager."
8190 in vertexai_span .attributes [f"{ SpanAttributes .LLM_PROMPTS } .0.user" ]
8291 )
8392 assert vertexai_span .attributes [SpanAttributes .LLM_REQUEST_TOP_P ] == 0.8
84- assert vertexai_span .attributes [SpanAttributes .LLM_REQUEST_MAX_TOKENS ] == 256
93+ assert (
94+ vertexai_span .attributes [SpanAttributes .LLM_REQUEST_MAX_TOKENS ] == 256
95+ )
8596 assert vertexai_span .attributes [SpanAttributes .LLM_TOP_K ] == 40
8697 assert (
8798 vertexai_span .attributes [f"{ SpanAttributes .LLM_COMPLETIONS } .0.content" ]
@@ -111,13 +122,18 @@ def test_vertexai_stream(exporter):
111122 ]
112123
113124 vertexai_span = spans [0 ]
114- assert vertexai_span .attributes [SpanAttributes .LLM_REQUEST_MODEL ] == "text-bison"
125+ assert (
126+ vertexai_span .attributes [SpanAttributes .LLM_REQUEST_MODEL ]
127+ == "text-bison"
128+ )
115129 assert (
116130 "Give me ten interview questions for the role of program manager."
117131 in vertexai_span .attributes [f"{ SpanAttributes .LLM_PROMPTS } .0.user" ]
118132 )
119133 assert vertexai_span .attributes [SpanAttributes .LLM_REQUEST_TOP_P ] == 0.8
120- assert vertexai_span .attributes [SpanAttributes .LLM_REQUEST_MAX_TOKENS ] == 256
134+ assert (
135+ vertexai_span .attributes [SpanAttributes .LLM_REQUEST_MAX_TOKENS ] == 256
136+ )
121137 assert vertexai_span .attributes [SpanAttributes .LLM_TOP_K ] == 40
122138 assert vertexai_span .attributes [
123139 f"{ SpanAttributes .LLM_COMPLETIONS } .0.content"
@@ -129,7 +145,9 @@ def test_vertexai_stream_async(exporter):
129145 async def async_streaming_prediction () -> list :
130146 """Streaming Text Example with a Large Language Model"""
131147
132- text_generation_model = TextGenerationModel .from_pretrained ("text-bison" )
148+ text_generation_model = TextGenerationModel .from_pretrained (
149+ "text-bison"
150+ )
133151 parameters = {
134152 "max_output_tokens" : 256 ,
135153 "top_p" : 0.8 ,
@@ -151,13 +169,18 @@ async def async_streaming_prediction() -> list:
151169 ]
152170
153171 vertexai_span = spans [0 ]
154- assert vertexai_span .attributes [SpanAttributes .LLM_REQUEST_MODEL ] == "text-bison"
172+ assert (
173+ vertexai_span .attributes [SpanAttributes .LLM_REQUEST_MODEL ]
174+ == "text-bison"
175+ )
155176 assert (
156177 "Give me ten interview questions for the role of program manager."
157178 in vertexai_span .attributes [f"{ SpanAttributes .LLM_PROMPTS } .0.user" ]
158179 )
159180 assert vertexai_span .attributes [SpanAttributes .LLM_REQUEST_TOP_P ] == 0.8
160- assert vertexai_span .attributes [SpanAttributes .LLM_REQUEST_MAX_TOKENS ] == 256
181+ assert (
182+ vertexai_span .attributes [SpanAttributes .LLM_REQUEST_MAX_TOKENS ] == 256
183+ )
161184 assert vertexai_span .attributes [SpanAttributes .LLM_TOP_K ] == 40
162185 assert vertexai_span .attributes [
163186 f"{ SpanAttributes .LLM_COMPLETIONS } .0.content"
@@ -197,14 +220,17 @@ def test_vertexai_chat(exporter):
197220
198221 vertexai_span = spans [0 ]
199222 assert (
200- vertexai_span .attributes [SpanAttributes .LLM_REQUEST_MODEL ] == "chat-bison@001"
223+ vertexai_span .attributes [SpanAttributes .LLM_REQUEST_MODEL ]
224+ == "chat-bison@001"
201225 )
202226 assert (
203227 "How many planets are there in the solar system?"
204228 in vertexai_span .attributes [f"{ SpanAttributes .LLM_PROMPTS } .0.user" ]
205229 )
206230 assert vertexai_span .attributes [SpanAttributes .LLM_REQUEST_TOP_P ] == 0.95
207- assert vertexai_span .attributes [SpanAttributes .LLM_REQUEST_MAX_TOKENS ] == 256
231+ assert (
232+ vertexai_span .attributes [SpanAttributes .LLM_REQUEST_MAX_TOKENS ] == 256
233+ )
208234 assert vertexai_span .attributes [SpanAttributes .LLM_TOP_K ] == 40
209235 assert (
210236 vertexai_span .attributes [f"{ SpanAttributes .LLM_COMPLETIONS } .0.content" ]
@@ -247,11 +273,16 @@ def test_vertexai_chat_stream(exporter):
247273
248274 vertexai_span = spans [0 ]
249275 assert (
250- vertexai_span .attributes [SpanAttributes .LLM_REQUEST_MODEL ] == "chat-bison@001"
276+ vertexai_span .attributes [SpanAttributes .LLM_REQUEST_MODEL ]
277+ == "chat-bison@001"
251278 )
252279 assert vertexai_span .attributes [SpanAttributes .LLM_REQUEST_TOP_P ] == 0.95
253- assert vertexai_span .attributes [SpanAttributes .LLM_REQUEST_TEMPERATURE ] == 0.8
254- assert vertexai_span .attributes [SpanAttributes .LLM_REQUEST_MAX_TOKENS ] == 256
280+ assert (
281+ vertexai_span .attributes [SpanAttributes .LLM_REQUEST_TEMPERATURE ] == 0.8
282+ )
283+ assert (
284+ vertexai_span .attributes [SpanAttributes .LLM_REQUEST_MAX_TOKENS ] == 256
285+ )
255286 assert vertexai_span .attributes [SpanAttributes .LLM_TOP_K ] == 40
256287 assert vertexai_span .attributes [
257288 f"{ SpanAttributes .LLM_COMPLETIONS } .0.content"
0 commit comments