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] support uploading examples with attachments and running evals on examples with attachments #1209

Merged
merged 102 commits into from
Dec 10, 2024
Merged
Changes from 7 commits
Commits
Show all changes
102 commits
Select commit Hold shift + click to select a range
e9e2131
wip
isahers1 Nov 13, 2024
ff30541
unit test
isahers1 Nov 13, 2024
152ec59
integration test skeleton
isahers1 Nov 13, 2024
27b1546
integration test passing
isahers1 Nov 13, 2024
53a0f14
wip
isahers1 Nov 14, 2024
025aa6d
wip
isahers1 Nov 14, 2024
4208b6e
Update python/langsmith/client.py
isahers1 Nov 14, 2024
fd16baa
more edits
isahers1 Nov 14, 2024
28a4677
nit
isahers1 Nov 14, 2024
816302d
nit
isahers1 Nov 14, 2024
aa947a6
remove dev endpoint in test
isahers1 Nov 18, 2024
a82063b
typo
isahers1 Nov 18, 2024
b18df6b
Merge branch 'main' into isaac/multipartstuff
isahers1 Nov 18, 2024
ad19daf
fmt
isahers1 Nov 18, 2024
390ac66
yml changes
isahers1 Nov 18, 2024
523e5d1
fmt
isahers1 Nov 18, 2024
ed3aa1c
example search restoration
isahers1 Nov 18, 2024
ce73afc
fmt
isahers1 Nov 18, 2024
460b16b
list -> List
isahers1 Nov 18, 2024
4e9edf4
dict -> Dict
isahers1 Nov 18, 2024
b6b9d79
fmt
isahers1 Nov 18, 2024
bc9ec6f
undo yml changes
isahers1 Nov 18, 2024
15708dc
unit test fix
isahers1 Nov 18, 2024
527174a
unit test fix
isahers1 Nov 18, 2024
81f5249
unit test fix
isahers1 Nov 18, 2024
0b476e8
Merge branch 'main' into isaac/multipartstuff
isahers1 Nov 19, 2024
f36a0cb
make evaluate function compatible with attachments (#1218)
isahers1 Nov 19, 2024
ddbe2f5
file path update
isahers1 Nov 19, 2024
c1ba615
add benchmarks
jakerachleff Nov 19, 2024
3544171
better error message
jakerachleff Nov 19, 2024
3cc32c5
aevaluate
isahers1 Nov 19, 2024
161e0d1
Merge branch 'isaac/multipartstuff' of https://github.com/langchain-a…
isahers1 Nov 19, 2024
08a6f34
unit test for _include_attachments
isahers1 Nov 20, 2024
8e2e704
test that adding examples without attachments still lets you run evals
isahers1 Nov 20, 2024
cfa0e4c
fmt
isahers1 Nov 20, 2024
de38a37
fmt
isahers1 Nov 20, 2024
2e74735
fmt
isahers1 Nov 20, 2024
f26c996
attempt fix
isahers1 Nov 20, 2024
095aae9
fix test
isahers1 Nov 20, 2024
a99da23
add unit test
isahers1 Nov 20, 2024
ee9d968
Merge branch 'main' into isaac/multipartstuff
agola11 Nov 20, 2024
b9dd0f2
Bump version (rc)
hinthornw Nov 20, 2024
01ef4d0
repetitions
isahers1 Nov 27, 2024
3715c30
nit
isahers1 Nov 27, 2024
49442d7
added upload endpoint
isahers1 Dec 2, 2024
9a70f70
Merge branch 'main' into isaac/multipartstuff
isahers1 Dec 2, 2024
484f2a5
comments
isahers1 Dec 6, 2024
f57b4bd
Merge branch 'main' into isaac/multipartstuff
isahers1 Dec 6, 2024
28fe5d1
fmt
isahers1 Dec 6, 2024
1e5eebf
fmt
isahers1 Dec 6, 2024
e013d72
fmt
isahers1 Dec 6, 2024
bc2d4b6
fmt
isahers1 Dec 6, 2024
96f4246
fix test
isahers1 Dec 6, 2024
887782e
x
isahers1 Dec 6, 2024
66228e8
defaults
isahers1 Dec 9, 2024
a5ee599
refactor
isahers1 Dec 9, 2024
2f1e6be
fmt
isahers1 Dec 9, 2024
c9ade2e
fmt
isahers1 Dec 9, 2024
4576779
fmt
isahers1 Dec 9, 2024
578a715
changes
isahers1 Dec 9, 2024
e4e3068
fmt
isahers1 Dec 9, 2024
6e91e05
x
isahers1 Dec 9, 2024
020d074
fmt
isahers1 Dec 9, 2024
1abe4f9
flag
isahers1 Dec 9, 2024
39be3c7
flags in tests
isahers1 Dec 9, 2024
5c2c74d
attachment_urls -> attachments
isahers1 Dec 9, 2024
2b385b6
x
isahers1 Dec 9, 2024
0daf245
fmt
baskaryan Dec 9, 2024
04a5496
Merge branch 'isaac/multipartstuff' of github.com:langchain-ai/langsm…
baskaryan Dec 9, 2024
c8a2b01
undo
isahers1 Dec 9, 2024
8033b7e
undo
isahers1 Dec 9, 2024
114a79d
fix
isahers1 Dec 9, 2024
b524f72
fix
isahers1 Dec 9, 2024
23187f1
test fix
isahers1 Dec 9, 2024
5471e88
fmt
isahers1 Dec 9, 2024
49246d0
fmt
baskaryan Dec 9, 2024
9b7c36a
Merge branch 'isaac/multipartstuff' of github.com:langchain-ai/langsm…
baskaryan Dec 9, 2024
b0921e0
tests
isahers1 Dec 9, 2024
70c3f3c
tests
isahers1 Dec 10, 2024
8bb0826
update examples multipart (#1310)
isahers1 Dec 10, 2024
c841ec6
add attachments to evaluate (#1237)
isahers1 Dec 10, 2024
eeeb375
Merge branch 'main' into isaac/multipartstuff
agola11 Dec 10, 2024
f3cc56f
update to 0.2.2
agola11 Dec 10, 2024
bf00aa6
fix spelling
agola11 Dec 10, 2024
c63b92c
fix update_examples issue
agola11 Dec 10, 2024
76e003e
fix test
isahers1 Dec 10, 2024
cf9e58c
Merge branch 'isaac/multipartstuff' of https://github.com/langchain-a…
isahers1 Dec 10, 2024
ed73f1a
test fix
isahers1 Dec 10, 2024
4887a99
attempt to fix test_update_examples_multipart
agola11 Dec 10, 2024
87d2a33
Merge branch 'isaac/multipartstuff' of github.com:langchain-ai/langsm…
agola11 Dec 10, 2024
6b9a026
fix tests
isahers1 Dec 10, 2024
1f25f55
x
isahers1 Dec 10, 2024
7573691
x
isahers1 Dec 10, 2024
cf85e56
fix test
isahers1 Dec 10, 2024
5c74829
fix test_bulk_update_examples_with_attachments_operations
agola11 Dec 10, 2024
9795e6e
Merge branch 'isaac/multipartstuff' of github.com:langchain-ai/langsm…
agola11 Dec 10, 2024
266272d
lint and fmt
agola11 Dec 10, 2024
61b28f5
fix tests
isahers1 Dec 10, 2024
ca3ec28
Merge branch 'isaac/multipartstuff' of https://github.com/langchain-a…
isahers1 Dec 10, 2024
34e8bb9
fmt
isahers1 Dec 10, 2024
e043a7d
fmt
isahers1 Dec 10, 2024
d77bd0e
remove blanket try/except
agola11 Dec 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 10 additions & 16 deletions python/langsmith/_internal/_operations.py
Original file line number Diff line number Diff line change
@@ -3,7 +3,6 @@
import itertools
import logging
import uuid
from pathlib import Path
from typing import Literal, Optional, Union, cast

from langsmith import schemas as ls_schemas
@@ -214,7 +213,7 @@ def serialized_run_operation_to_multipart_parts_and_context(
op: SerializedRunOperation,
) -> MultipartPartsAndContext:
acc_parts: list[MultipartPart] = []
valb: Union[bytes, Path]

# this is main object, minus inputs/outputs/events/attachments
acc_parts.append(
(
@@ -257,22 +256,17 @@ def serialized_run_operation_to_multipart_parts_and_context(
)
continue

if isinstance(valb, Path):
# TODO: actually deal with this case
# This is just for speed of getting something out
continue
else:
acc_parts.append(
acc_parts.append(
(
f"attachment.{op.id}.{n}",
(
f"attachment.{op.id}.{n}",
(
None,
valb,
content_type,
{"Content-Length": str(len(valb))},
),
)
None,
valb,
content_type,
{"Content-Length": str(len(valb))},
),
)
)
return MultipartPartsAndContext(
acc_parts,
f"trace={op.trace_id},id={op.id}",
6 changes: 2 additions & 4 deletions python/langsmith/client.py
Original file line number Diff line number Diff line change
@@ -3843,10 +3843,9 @@ def read_example(
"presigned_url": value["presigned_url"],
"reader": reader,
}
del example["attachment_urls"]

return ls_schemas.Example(
**example,
**{k: v for k, v in example.items() if k != "attachment_urls"},
attachments=attachments,
_host_url=self._host_url,
_tenant_id=self._get_optional_tenant_id(),
@@ -3930,10 +3929,9 @@ def list_examples(
"presigned_url": value["presigned_url"],
"reader": reader,
}
del example["attachment_urls"]

yield ls_schemas.Example(
**example,
**{k: v for k, v in example.items() if k != "attachment_urls"},
attachments=attachments,
_host_url=self._host_url,
_tenant_id=self._get_optional_tenant_id(),
3 changes: 1 addition & 2 deletions python/langsmith/schemas.py
Original file line number Diff line number Diff line change
@@ -5,7 +5,6 @@
from datetime import datetime, timedelta, timezone
from decimal import Decimal
from enum import Enum
from pathlib import Path
from typing import (
Any,
Dict,
@@ -64,7 +63,7 @@ def my_function(bar: int, my_val: Attachment):
data: bytes


Attachments = Dict[str, Union[Tuple[str, bytes], Attachment, Tuple[str, Path]]]
Attachments = Dict[str, Union[Tuple[str, bytes], Attachment]]
"""Attachments associated with the run.
Each entry is a tuple of (mime_type, bytes), or (mime_type, file_path)"""

48 changes: 35 additions & 13 deletions python/tests/unit_tests/evaluation/test_runner.py
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@
import uuid
from datetime import datetime, timezone
from threading import Lock
from typing import Callable, List
from typing import Any, Callable, Dict, List, Tuple
from unittest import mock
from unittest.mock import MagicMock

@@ -53,7 +53,9 @@ def request(self, verb: str, endpoint: str, *args, **kwargs):
return res
elif endpoint == "http://localhost:1984/examples":
res = MagicMock()
res.json.return_value = [e.dict() for e in self.ds_examples]
res.json.return_value = [
e.dict() if not isinstance(e, dict) else e for e in self.ds_examples
]
return res
elif endpoint == "http://localhost:1984/sessions":
res = {} # type: ignore
@@ -143,14 +145,23 @@ def _wait_until(condition: Callable, timeout: int = 8):
raise TimeoutError("Condition not met")


def _create_example(idx: int) -> ls_schemas.Example:
def _create_example(idx: int) -> Tuple[ls_schemas.Example, Dict[str, Any]]:
_id = uuid.uuid4()
_created_at = datetime.now(timezone.utc)
return ls_schemas.Example(
id=uuid.uuid4(),
id=_id,
inputs={"in": idx},
outputs={"answer": idx + 1},
dataset_id="00886375-eb2a-4038-9032-efff60309896",
created_at=datetime.now(timezone.utc),
)
created_at=_created_at,
), {
"id": _id,
"dataset_id": "00886375-eb2a-4038-9032-efff60309896",
"created_at": _created_at,
"inputs": {"in": idx},
"outputs": {"answer": idx + 1},
"attachment_urls": None,
}


@pytest.mark.skipif(sys.version_info < (3, 9), reason="requires python3.9 or higher")
@@ -166,10 +177,13 @@ def test_evaluate_results(

SPLIT_SIZE = 3
NUM_REPETITIONS = 4
ds_examples = [_create_example(i) for i in range(10)]
ds_example_responses = [_create_example(i) for i in range(10)]
ds_examples = [e[0] for e in ds_example_responses]
dev_split = random.sample(ds_examples, SPLIT_SIZE)
tenant_id = str(uuid.uuid4())
fake_request = FakeRequest(ds_id, ds_name, ds_examples, tenant_id)
fake_request = FakeRequest(
ds_id, ds_name, [e[1] for e in ds_example_responses], tenant_id
)
session.request = fake_request.request
client = Client(
api_url="http://localhost:1984",
@@ -393,7 +407,12 @@ def eval2(x, y, inputs):
_normalize_evaluator_func(eval_)

with pytest.raises(ValueError, match="Invalid evaluator function."):
evaluate((lambda x: x), data=ds_examples, evaluators=[eval_], client=client)
evaluate(
(lambda inputs: inputs),
data=ds_examples,
evaluators=[eval_],
client=client,
)


def test_evaluate_raises_for_async():
@@ -437,10 +456,13 @@ async def test_aevaluate_results(

SPLIT_SIZE = 3
NUM_REPETITIONS = 4
ds_examples = [_create_example(i) for i in range(10)]
ds_example_responses = [_create_example(i) for i in range(10)]
ds_examples = [e[0] for e in ds_example_responses]
dev_split = random.sample(ds_examples, SPLIT_SIZE)
tenant_id = str(uuid.uuid4())
fake_request = FakeRequest(ds_id, ds_name, ds_examples, tenant_id)
fake_request = FakeRequest(
ds_id, ds_name, [e[1] for e in ds_example_responses], tenant_id
)
session.request = fake_request.request
client = Client(
api_url="http://localhost:1984",
@@ -664,8 +686,8 @@ async def eval2(x, y, inputs):

evaluators = [eval1, eval2]

async def atarget(x):
return x
async def atarget(inputs):
return inputs

for eval_ in evaluators:
with pytest.raises(ValueError, match="Invalid evaluator function."):
Loading