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

add upset examples multipart endpoint #1209

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
89 changes: 89 additions & 0 deletions python/langsmith/client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Client for interacting with the LangSmith API.

Check notice on line 1 in python/langsmith/client.py

View workflow job for this annotation

GitHub Actions / benchmark

Benchmark results

......................................... create_5_000_run_trees: Mean +- std dev: 617 ms +- 46 ms ......................................... create_10_000_run_trees: Mean +- std dev: 1.18 sec +- 0.06 sec ......................................... create_20_000_run_trees: Mean +- std dev: 1.19 sec +- 0.05 sec ......................................... dumps_class_nested_py_branch_and_leaf_200x400: Mean +- std dev: 704 us +- 15 us ......................................... dumps_class_nested_py_leaf_50x100: Mean +- std dev: 24.9 ms +- 0.2 ms ......................................... dumps_class_nested_py_leaf_100x200: Mean +- std dev: 104 ms +- 2 ms ......................................... dumps_dataclass_nested_50x100: Mean +- std dev: 25.3 ms +- 0.2 ms ......................................... WARNING: the benchmark result may be unstable * the standard deviation (16.4 ms) is 25% of the mean (66.4 ms) * the maximum (101 ms) is 52% greater than the mean (66.4 ms) Try to rerun the benchmark with more runs, values and/or loops. Run 'python -m pyperf system tune' command to reduce the system jitter. Use pyperf stats, pyperf dump and pyperf hist to analyze results. Use --quiet option to hide these warnings. dumps_pydantic_nested_50x100: Mean +- std dev: 66.4 ms +- 16.4 ms ......................................... WARNING: the benchmark result may be unstable * the standard deviation (32.3 ms) is 15% of the mean (221 ms) Try to rerun the benchmark with more runs, values and/or loops. Run 'python -m pyperf system tune' command to reduce the system jitter. Use pyperf stats, pyperf dump and pyperf hist to analyze results. Use --quiet option to hide these warnings. dumps_pydanticv1_nested_50x100: Mean +- std dev: 221 ms +- 32 ms

Check notice on line 1 in python/langsmith/client.py

View workflow job for this annotation

GitHub Actions / benchmark

Comparison against main

+------------------------------------+---------+-----------------------+ | Benchmark | main | changes | +====================================+=========+=======================+ | dumps_dataclass_nested_50x100 | 25.7 ms | 25.3 ms: 1.02x faster | +------------------------------------+---------+-----------------------+ | dumps_class_nested_py_leaf_50x100 | 25.2 ms | 24.9 ms: 1.01x faster | +------------------------------------+---------+-----------------------+ | dumps_class_nested_py_leaf_100x200 | 105 ms | 104 ms: 1.01x faster | +------------------------------------+---------+-----------------------+ | Geometric mean | (ref) | 1.00x faster | +------------------------------------+---------+-----------------------+ Benchmark hidden because not significant (6): dumps_pydantic_nested_50x100, create_10_000_run_trees, dumps_class_nested_py_branch_and_leaf_200x400, dumps_pydanticv1_nested_50x100, create_20_000_run_trees, create_5_000_run_trees

Use the client to customize API keys / workspace ocnnections, SSl certs,
etc. for tracing.
Expand Down Expand Up @@ -82,6 +82,7 @@
_SIZE_LIMIT_BYTES,
)
from langsmith._internal._multipart import (
MultipartPart,
MultipartPartsAndContext,
join_multipart_parts_and_context,
)
Expand Down Expand Up @@ -3369,6 +3370,94 @@
created_at=created_at,
)

def upsert_example_multipart(
self,
*,
upserts: List[ls_schemas.ExampleCreateWithAttachments] = None,
) -> None:
"""Upsert examples"""
parts = list[MultipartPart]

for example in upserts:

if example.id is not None:
example_id = str(example.id) # is the conversion to string neccessary?
else:
example_id = str(uuid.uuid4())

remaining_values = {
isahers1 marked this conversation as resolved.
Show resolved Hide resolved
"dataset_id": example.dataset_id,
"created_at": example.created_at,
"metadata": example.metadata,
"split": example.split
}
valb = _dumps_json(remaining_values)

parts.append(
f"{example_id}",
(
None,
valb,
"application/json",
{"Content-Length": str(len(valb))},
),
),

inputsb = example.inputs
outputsb = example.outputs

parts.append(
f"{example_id}.inputs",
(
None,
inputsb,
"application/json",
{"Content-Length": str(len(inputsb))},
),
),

parts.append(
f"{example_id}.outputs",
(
None,
outputsb,
"application/json",
{"Content-Length": str(len(outputsb))},
),
),

if example.attachments:
for attachment in example.attachments:
parts.append(
f"{example_id}.attachment.{attachment.mime_type}",
isahers1 marked this conversation as resolved.
Show resolved Hide resolved
(
None,
attachment.data,
"application/json", # I feel like this is wrong
isahers1 marked this conversation as resolved.
Show resolved Hide resolved
{"Content-Length": str(len(attachment.data))},
),
),

encoder = rqtb_multipart.MultipartEncoder(parts, boundary=BOUNDARY)
if encoder.len <= 20_000_000: # ~20 MB
data = encoder.to_string()
else:
data = encoder

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm assuming this is what we do for runs?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah this code was copy pasted


response = self.request_with_retries(
"POST",
"/v1/examples/multipart", # No clue what this is supposed to be
request_kwargs={
"data": data,
"headers": {
**self._headers,
"Content-Type": encoder.content_type,
},
},
)
ls_utils.raise_for_status_with_text(response)

def create_examples(
self,
*,
Expand Down
3 changes: 3 additions & 0 deletions python/langsmith/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ class ExampleCreate(ExampleBase):
created_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
split: Optional[Union[str, List[str]]] = None

class ExampleCreateWithAttachments(ExampleCreate):
"""Example create with attachments."""
attachments: Optional[List[Attachment]] = None
isahers1 marked this conversation as resolved.
Show resolved Hide resolved

class Example(ExampleBase):
"""Example model."""
Expand Down
Loading