Skip to content

Commit

Permalink
Remove placeholder/disabled tests and fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
aabmass committed Dec 5, 2024
1 parent b87ab01 commit 858c23a
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 330 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ def _set_span_attribute(span, name, value):
if value is not None:
if value != "":
span.set_attribute(name, value)
return


def _set_input_attributes(
Expand Down Expand Up @@ -215,8 +214,6 @@ def _set_input_attributes(
kwargs.get("frequency_penalty"),
)

return


@dont_throw
def _set_response_attributes(
Expand Down Expand Up @@ -347,12 +344,11 @@ async def _awrap(
return _build_from_streaming_response(
span, event_logger, response, llm_model
)
elif is_async_streaming_response(response):
if is_async_streaming_response(response):
return _abuild_from_streaming_response(
span, event_logger, response, llm_model
)
else:
_handle_response(span, event_logger, response, llm_model)
_handle_response(span, event_logger, response, llm_model)

span.end()
return response
Expand Down Expand Up @@ -395,12 +391,11 @@ def _wrap(
return _build_from_streaming_response(
span, event_logger, response, llm_model
)
elif is_async_streaming_response(response):
if is_async_streaming_response(response):
return _abuild_from_streaming_response(
span, event_logger, response, llm_model
)
else:
_handle_response(span, event_logger, response, llm_model)
_handle_response(span, event_logger, response, llm_model)

span.end()
return response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@

import logging
import traceback
from typing import Callable, Optional, ParamSpec, TypeVar

P = ParamSpec("P")
R = TypeVar("R")

def dont_throw(func):

def dont_throw(func: Callable[P, R]) -> Callable[P, Optional[R]]:
"""
A decorator that wraps the passed in function and logs exceptions instead of throwing them.
Expand All @@ -26,14 +30,15 @@ def dont_throw(func):
# Obtain a logger specific to the function's module
logger = logging.getLogger(func.__module__)

def wrapper(*args, **kwargs):
def wrapper(*args: P.args, **kwargs: P.kwargs) -> Optional[R]:
try:
return func(*args, **kwargs)
except Exception:
except Exception: # pylint: disable=broad-except
logger.debug(
"failed to trace in %s, error: %s",
func.__name__,
traceback.format_exc(),
)
return None

return wrapper
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,20 @@ interactions:
string: "{\n \"candidates\": [\n {\n \"content\": {\n \"role\":
\"model\",\n \"parts\": [\n {\n \"text\": \" The
image shows a table with a cup of coffee, a bowl of blueberries, and a plate
of scones with blueberries on it. There are also pink flowers on the table.\"\n
\ }\n ]\n },\n \"finishReason\": 1,\n \"safetyRatings\":
[\n {\n \"category\": 1,\n \"probability\": 1,\n
\ \"probabilityScore\": 0.02758789,\n \"severity\": 1,\n
\ \"severityScore\": 0.072753906\n },\n {\n \"category\":
2,\n \"probability\": 1,\n \"probabilityScore\": 0.026000977,\n
\ \"severity\": 1,\n \"severityScore\": 0.07080078\n },\n
\ {\n \"category\": 3,\n \"probability\": 1,\n \"probabilityScore\":
0.04272461,\n \"severity\": 1,\n \"severityScore\": 0.036865234\n
\ },\n {\n \"category\": 4,\n \"probability\":
1,\n \"probabilityScore\": 0.08496094,\n \"severity\": 1,\n
\ \"severityScore\": 0.09033203\n }\n ],\n \"avgLogprobs\":
-0.09576562472752162\n }\n ],\n \"usageMetadata\": {\n \"promptTokenCount\":
265,\n \"candidatesTokenCount\": 35,\n \"totalTokenCount\": 300\n },\n
\ \"modelVersion\": \"gemini-pro-vision\"\n}\n"
of scones. There are also some flowers on the table.\"\n }\n ]\n
\ },\n \"finishReason\": 1,\n \"safetyRatings\": [\n {\n
\ \"category\": 1,\n \"probability\": 1,\n \"probabilityScore\":
0.02331543,\n \"severity\": 1,\n \"severityScore\": 0.05493164\n
\ },\n {\n \"category\": 2,\n \"probability\":
1,\n \"probabilityScore\": 0.026367188,\n \"severity\":
1,\n \"severityScore\": 0.05493164\n },\n {\n \"category\":
3,\n \"probability\": 1,\n \"probabilityScore\": 0.046142578,\n
\ \"severity\": 1,\n \"severityScore\": 0.030639648\n },\n
\ {\n \"category\": 4,\n \"probability\": 1,\n \"probabilityScore\":
0.080566406,\n \"severity\": 1,\n \"severityScore\": 0.095214844\n
\ }\n ],\n \"avgLogprobs\": -0.11595650642148909\n }\n
\ ],\n \"usageMetadata\": {\n \"promptTokenCount\": 265,\n \"candidatesTokenCount\":
31,\n \"totalTokenCount\": 296\n },\n \"modelVersion\": \"gemini-pro-vision\"\n}\n"
headers:
Cache-Control:
- private
Expand All @@ -52,7 +51,7 @@ interactions:
- X-Origin
- Referer
content-length:
- '1296'
- '1275'
status:
code: 200
message: OK
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,25 @@

pytest_plugins = []

FAKE_PROJECT = "fake-project"


@pytest.fixture(scope="session")
def exporter():
exporter = InMemorySpanExporter()
processor = SimpleSpanProcessor(exporter)
span_exporter = InMemorySpanExporter()
processor = SimpleSpanProcessor(span_exporter)

provider = TracerProvider()
provider.add_span_processor(processor)
trace.set_tracer_provider(provider)

VertexAIInstrumentor().instrument()

return exporter
return span_exporter


@pytest.fixture(autouse=True)
def clear_exporter(exporter):
def clear_exporter(exporter): # pylint: disable=redefined-outer-name
exporter.clear()


Expand All @@ -50,10 +52,12 @@ def vertexai_init(vcr: VCR) -> None:
# When not recording (in CI), don't do any auth. That prevents trying to read application
# default credentials from the filesystem or metadata server and oauth token exchange. This
# is not the interesting part of our instrumentation to test.
if vcr.record_mode is RecordMode.NONE:
vertexai.init(credentials=AnonymousCredentials())
else:
vertexai.init()
print(f"VCR Mode is {vcr.record_mode=}, {RecordMode.NONE}")
vertex_init_kwargs = {"api_transport": "rest"}
if vcr.record_mode == RecordMode.NONE:
vertex_init_kwargs["credentials"] = AnonymousCredentials()
vertex_init_kwargs["project"] = FAKE_PROJECT
vertexai.init(**vertex_init_kwargs)


@pytest.fixture(scope="module")
Expand All @@ -79,7 +83,7 @@ def filter_headers(headers: Mapping[str, str]) -> Mapping[str, str]:
def before_record_cb(request: Request):
request.headers = filter_headers(request.headers)
request.uri = re.sub(
r"/projects/[^/]+/", "/projects/fake-project/", request.uri
r"/projects/[^/]+/", f"/projects/{FAKE_PROJECT}/", request.uri
)
return request

Expand Down
Loading

0 comments on commit 858c23a

Please sign in to comment.