From d8d3262987bd1048ef173384c82201d6b568b611 Mon Sep 17 00:00:00 2001 From: Eugene Yurtsev Date: Mon, 13 Nov 2023 11:04:02 -0500 Subject: [PATCH] Do not enable langsmith tracer globally (#205) Fixes test to not enable langsmith tracer globally --- .github/workflows/langserve_ci.yml | 1 + tests/unit_tests/test_server_client.py | 134 ++++++++++++------------- 2 files changed, 67 insertions(+), 68 deletions(-) diff --git a/.github/workflows/langserve_ci.yml b/.github/workflows/langserve_ci.yml index 080e83cf..7556ae8a 100644 --- a/.github/workflows/langserve_ci.yml +++ b/.github/workflows/langserve_ci.yml @@ -11,6 +11,7 @@ on: - '.github/workflows/_test.yml' - '.github/workflows/langserve_ci.yml' - 'langserve/**' + - 'tests/**' - 'examples/**' - 'pyproject.toml' - 'poetry.lock' diff --git a/tests/unit_tests/test_server_client.py b/tests/unit_tests/test_server_client.py index 2b4e3f33..8f4281f4 100644 --- a/tests/unit_tests/test_server_client.py +++ b/tests/unit_tests/test_server_client.py @@ -2,7 +2,6 @@ import asyncio import datetime import json -import os from asyncio import AbstractEventLoop from contextlib import asynccontextmanager, contextmanager from enum import Enum @@ -113,7 +112,6 @@ async def add_one_or_passthrough( else: return x - os.environ["LANGCHAIN_TRACING_V2"] = "true" runnable_lambda = RunnableLambda(func=add_one_or_passthrough) app = FastAPI() try: @@ -1555,88 +1553,88 @@ async def test_feedback_succeeds_when_langsmith_enabled() -> None: """Tests that the feedback endpoint can accept feedback to langsmith.""" with patch("langserve.server.ls_client") as mocked_ls_client_package: - mocked_client = MagicMock(return_value=None) - mocked_ls_client_package.Client.return_value = mocked_client - - mocked_client.create_feedback.return_value = ls_schemas.Feedback( - id="5484c6b3-5a1a-4a87-b2c7-2e39e7a7e4ac", - created_at=datetime.datetime(1994, 9, 19, 9, 19), - modified_at=datetime.datetime(1994, 9, 19, 9, 19), - run_id="f47ac10b-58cc-4372-a567-0e02b2c3d479", - key="silliness", - score=1000, - ) + with patch("langserve.server.tracing_is_enabled") as tracing_is_enabled: + tracing_is_enabled.return_value = True + mocked_client = MagicMock(return_value=None) + mocked_ls_client_package.Client.return_value = mocked_client + mocked_client.create_feedback.return_value = ls_schemas.Feedback( + id="5484c6b3-5a1a-4a87-b2c7-2e39e7a7e4ac", + created_at=datetime.datetime(1994, 9, 19, 9, 19), + modified_at=datetime.datetime(1994, 9, 19, 9, 19), + run_id="f47ac10b-58cc-4372-a567-0e02b2c3d479", + key="silliness", + score=1000, + ) - local_app = FastAPI() - add_routes( - local_app, - RunnableLambda(lambda foo: "hello"), - enable_feedback_endpoint=True, - ) + local_app = FastAPI() + add_routes( + local_app, + RunnableLambda(lambda foo: "hello"), + enable_feedback_endpoint=True, + ) - async with get_async_test_client( - local_app, raise_app_exceptions=True - ) as async_client: - response = await async_client.post( - "/feedback", - json={ + async with get_async_test_client( + local_app, raise_app_exceptions=True + ) as async_client: + response = await async_client.post( + "/feedback", + json={ + "run_id": "f47ac10b-58cc-4372-a567-0e02b2c3d479", + "key": "silliness", + "score": 1000, + }, + ) + + expected_response_json = { "run_id": "f47ac10b-58cc-4372-a567-0e02b2c3d479", "key": "silliness", "score": 1000, - }, - ) - - expected_response_json = { - "run_id": "f47ac10b-58cc-4372-a567-0e02b2c3d479", - "key": "silliness", - "score": 1000, - "created_at": datetime.datetime(1994, 9, 19, 9, 19).strftime( - "%Y-%m-%dT%H:%M:%S" - ), - "modified_at": datetime.datetime(1994, 9, 19, 9, 19).strftime( - "%Y-%m-%dT%H:%M:%S" - ), - "comment": None, - "correction": None, - "value": None, - } + "created_at": "1994-09-19T09:19:00", + "modified_at": "1994-09-19T09:19:00", + "comment": None, + "correction": None, + "value": None, + } - json_response = response.json() + json_response = response.json() - assert "id" in json_response - del json_response["id"] + assert "id" in json_response + del json_response["id"] - assert json_response == expected_response_json + assert json_response == expected_response_json @pytest.mark.asyncio async def test_feedback_fails_when_run_doesnt_exist() -> None: - """Tests that the feedback endpoint can't accept feedback for a non existent run.""" + """Tests that the feedback endpoint can't accept feedback for a non-existent run.""" with patch("langserve.server.ls_client") as mocked_ls_client_package: - mocked_client = MagicMock(return_value=None) - mocked_ls_client_package.Client.return_value = mocked_client - mocked_client.create_feedback.side_effect = LangSmithNotFoundError("no run :/") - local_app = FastAPI() - add_routes( - local_app, - RunnableLambda(lambda foo: "hello"), - enable_feedback_endpoint=True, - ) - - async with get_async_test_client( - local_app, raise_app_exceptions=True - ) as async_client: - response = await async_client.post( - "/feedback", - json={ - "run_id": "f47ac10b-58cc-4372-a567-0e02b2c3d479", - "key": "silliness", - "score": 1000, - }, + with patch("langserve.server.tracing_is_enabled") as tracing_is_enabled: + tracing_is_enabled.return_value = True + mocked_client = MagicMock(return_value=None) + mocked_ls_client_package.Client.return_value = mocked_client + mocked_client.create_feedback.side_effect = LangSmithNotFoundError( + "no run :/" + ) + local_app = FastAPI() + add_routes( + local_app, + RunnableLambda(lambda foo: "hello"), + enable_feedback_endpoint=True, ) - assert response.status_code == 404 + async with get_async_test_client( + local_app, raise_app_exceptions=True + ) as async_client: + response = await async_client.post( + "/feedback", + json={ + "run_id": "f47ac10b-58cc-4372-a567-0e02b2c3d479", + "key": "silliness", + "score": 1000, + }, + ) + assert response.status_code == 404 @pytest.mark.asyncio