-
Notifications
You must be signed in to change notification settings - Fork 95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
python[minor]: Release 0.3.0 #1439
Conversation
Co-authored-by: Predrag Gruevski <2348618+obi1kenobi@users.noreply.github.com> Co-authored-by: Erick Friis <erick@langchain.dev>
Co-authored-by: Bagatur <baskaryan@gmail.com>
Log pytest tests to LangSmith. Useful for: - Evaluations where the eval logic is different for each datapoint, making it difficult to use generic evaluators on a whole dataset - Unit tests where you want both the local pytest experience and the ability to trace and share results ### Install ```bash pip install "langsmith[pytest]==0.2.11rc14" ``` ### Simple usage ```python # tests/test_simple.py import pytest from langsmith import testing as t @pytest.mark.langsmith def test_addition_single(): x = 3 y = 4 # directly log example inputs if you don't want to use fixtures t.log_inputs({"x": x, "y": y}) expected = 7 # directly log example outputs if you don't want to use fixtures t.log_reference_outputs({"sum": expected}) actual = x + y # directly log run outputs t.log_outputs({"sum": actual}) # test pass/fail automatically logged to langsmith assert actual == expected ``` Run ```bash pytest --outputs='ls' tests/test_foo.py ``` Results <img width="952" alt="Screenshot 2025-01-08 at 2 53 04 AM" src="https://github.com/user-attachments/assets/d695747b-cfdf-4248-b5fd-c5c420aa92ec" /> ### Advanced usage ```python #tests/test_advanced.py import openai import pytest from langsmith import wrappers from langsmith import testing as t oai_client = wrappers.wrap_openai(openai.Client()) @pytest.mark.langsmith def test_openai_says_hello(): # Traced code will be included in the test case text = "Say hello!" response = oai_client.chat.completions.create( model="gpt-4o-mini", messages=[ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": text}, ], ) t.log_inputs({"text": text}) t.log_outputs({"response": response.choices[0].message.content}) t.log_reference_outputs({"response": "hello!"}) # Use this context manager to trace any steps used for generating evaluation # feedback separately from the main application logic with t.trace_feedback(): grade = oai_client.chat.completions.create( model="gpt-4o-mini", messages=[ { "role": "system", "content": "Return 1 if 'hello' is in the user message and 0 otherwise.", }, {"role": "user", "content": response.choices[0].message.content}, ], ) t.log_feedback( key="llm_judge", score=float(grade.choices[0].message.content) ) assert "hello" in response.choices[0].message.content.lower() @pytest.mark.langsmith(output_keys=["expected"]) @pytest.mark.parametrize( "a, b, expected", [(1, 2, 3), (3, 4, 7)], ) def test_addition_parametrized(a: int, b: int, expected: int): t.log_outputs({"sum": a + b}) assert a + b == expected ``` Run using pytest-xdist to parallelize (`pip install pytest-xdist first`) ```bash LANGSMITH_TEST_SUITE="Test suite name" LANGSMITH_EXPERIMENT="Experiment name" pytest --outputs='ls' tests ``` Results: https://dev.smith.langchain.com/public/cea0e7fd-2d27-47d1-8ada-141069acdf0d/d <img width="1030" alt="Screenshot 2025-01-08 at 3 07 51 AM" src="https://github.com/user-attachments/assets/db770d00-67b3-4b53-8e33-289c4f0edfeb" /> --------- Co-authored-by: isaac hershenson <ihershenson@hmc.edu>
Co-authored-by: Bagatur <baskaryan@gmail.com> Co-authored-by: Bagatur <22008038+baskaryan@users.noreply.github.com> Co-authored-by: isaac hershenson <ihershenson@hmc.edu>
if isinstance(data, BufferedReader): | ||
encoded_data = data.read() | ||
else: | ||
encoded_data = str(data).encode() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good
I think you can optimize this more if you create a case that streams the reader's content into the compressor_writer, but I didn't do the compressor_writer work
No description provided.