Skip to content
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]: manually set test case inputs/outputs #1362

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

baskaryan
Copy link
Contributor

example usage

@unit
def test_example():
    x = 0
    y = 1
    add_test_inputs({"x": x, "y": y})
    add_test_reference_outputs({"product": 0})
    add_test_outputs({"product": x * y})
    assert x * y == 0

@hinthornw
Copy link
Collaborator

I like the functionality. I kinda like using a single module/object to access the methods, like

@unit
def test_example(captest):
    x = 0
    y = 1
    captest.log_inputs({"x": x, "y": y})
    captest.log_reference({"product": 0})
    captest.log_outputs({"product": x * y})
    assert x * y == 0

or something

@baskaryan
Copy link
Contributor Author

Updated

from langsmith import testing

@testing.test
def test_example():
    x = 0
    y = 1
    testing.log_inputs({"x": x, "y": y})
    testing.log_reference_outputs({"product": 0})
    testing.log_outputs({"product": x * y})
    testing.log_feedback(key="correct", value=1.0)
    assert x * y == 0

@baskaryan baskaryan marked this pull request as ready for review January 3, 2025 14:15
@baskaryan baskaryan changed the title rfc: manually set test case inputs/outputs python[minor]: manually set test case inputs/outputs Jan 3, 2025
@hinthornw
Copy link
Collaborator

I like

@baskaryan
Copy link
Contributor Author

updated usage

LANGSMITH_TEST_SUITE="foo" pytest -n auto tests/test_foo.py
# test_foo.py
import openai
import pytest

from langsmith import testing, wrappers

oai_client = wrappers.wrap_openai(openai.Client())


@testing.test
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},
        ],
    )
    testing.log_inputs({"text": text})
    testing.log_outputs({"response": response.choices[0].message.content})
    testing.log_reference_outputs({"response": "hello!"})
    with testing.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},
            ],
        )
        testing.log_feedback(
            key="llm_judge", score=float(grade.choices[0].message.content)
        )

    assert "hello" in response.choices[0].message.content.lower()


@testing.test(output_keys=["expected"])
@pytest.mark.parametrize(
    "a, b, expected",
    [
        (1, 2, 3),
        (3, 4, 7),
    ],
)
def test_addition_with_multiple_inputs(a: int, b: int, expected: int):
    testing.log_outputs({"sum": a + b})
    assert a + b == expected

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants