Skip to content

Commit

Permalink
Make tenant call optional (#477)
Browse files Browse the repository at this point in the history
  • Loading branch information
hinthornw authored Feb 26, 2024
1 parent 5520b70 commit 13902a5
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 19 deletions.
61 changes: 43 additions & 18 deletions python/langsmith/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,9 @@ def upload_csv(
file_name = file_name.split("/")[-1]
raise ValueError(f"Dataset {file_name} already exists")
return ls_schemas.Dataset(
**result, _host_url=self._host_url, _tenant_id=self._get_tenant_id()
**result,
_host_url=self._host_url,
_tenant_id=self._get_optional_tenant_id(),
)

@staticmethod
Expand Down Expand Up @@ -1758,18 +1760,29 @@ def update_project(
ls_utils.raise_for_status_with_text(response)
return ls_schemas.TracerSession(**response.json(), _host_url=self._host_url)

def _get_tenant_id(self) -> uuid.UUID:
def _get_optional_tenant_id(self) -> Optional[uuid.UUID]:
if self._tenant_id is not None:
return self._tenant_id
response = self._get_with_retries("/sessions", params={"limit": 1})
result = response.json()
if isinstance(result, list):
tracer_session = ls_schemas.TracerSessionResult(
**result[0], _host_url=self._host_url
try:
response = self._get_with_retries("/sessions", params={"limit": 1})
result = response.json()
if isinstance(result, list) and len(result) > 0:
tracer_session = ls_schemas.TracerSessionResult(
**result[0], _host_url=self._host_url
)
self._tenant_id = tracer_session.tenant_id
return self._tenant_id
except Exception as e:
logger.warning(
"Failed to get tenant ID from LangSmith: %s", repr(e), exc_info=True
)
self._tenant_id = tracer_session.tenant_id
return self._tenant_id
raise ls_utils.LangSmithError("No projects found")
return None

def _get_tenant_id(self) -> uuid.UUID:
tenant_id = self._get_optional_tenant_id()
if tenant_id is None:
raise ls_utils.LangSmithError("No tenant ID found")
return tenant_id

@ls_utils.xor_args(("project_id", "project_name"))
def read_project(
Expand Down Expand Up @@ -2026,7 +2039,7 @@ def create_dataset(
return ls_schemas.Dataset(
**response.json(),
_host_url=self._host_url,
_tenant_id=self._get_tenant_id(),
_tenant_id=self._get_optional_tenant_id(),
)

def has_dataset(
Expand Down Expand Up @@ -2092,10 +2105,14 @@ def read_dataset(
f"Dataset {dataset_name} not found"
)
return ls_schemas.Dataset(
**result[0], _host_url=self._host_url, _tenant_id=self._get_tenant_id()
**result[0],
_host_url=self._host_url,
_tenant_id=self._get_optional_tenant_id(),
)
return ls_schemas.Dataset(
**result, _host_url=self._host_url, _tenant_id=self._get_tenant_id()
**result,
_host_url=self._host_url,
_tenant_id=self._get_optional_tenant_id(),
)

def read_dataset_openai_finetuning(
Expand Down Expand Up @@ -2155,7 +2172,9 @@ def list_datasets(

yield from (
ls_schemas.Dataset(
**dataset, _host_url=self._host_url, _tenant_id=self._get_tenant_id()
**dataset,
_host_url=self._host_url,
_tenant_id=self._get_optional_tenant_id(),
)
for dataset in self._get_paginated_list("/datasets", params=params)
)
Expand Down Expand Up @@ -2498,7 +2517,9 @@ def create_example(
ls_utils.raise_for_status_with_text(response)
result = response.json()
return ls_schemas.Example(
**result, _host_url=self._host_url, _tenant_id=self._get_tenant_id()
**result,
_host_url=self._host_url,
_tenant_id=self._get_optional_tenant_id(),
)

def read_example(self, example_id: ID_TYPE) -> ls_schemas.Example:
Expand All @@ -2516,7 +2537,7 @@ def read_example(self, example_id: ID_TYPE) -> ls_schemas.Example:
return ls_schemas.Example(
**response.json(),
_host_url=self._host_url,
_tenant_id=self._get_tenant_id(),
_tenant_id=self._get_optional_tenant_id(),
)

def list_examples(
Expand Down Expand Up @@ -2554,7 +2575,9 @@ def list_examples(
params["inline_s3_urls"] = inline_s3_urls
yield from (
ls_schemas.Example(
**example, _host_url=self._host_url, _tenant_id=self._get_tenant_id()
**example,
_host_url=self._host_url,
_tenant_id=self._get_optional_tenant_id(),
)
for example in self._get_paginated_list("/examples", params=params)
)
Expand Down Expand Up @@ -2667,7 +2690,9 @@ def _resolve_example_id(
reference_example_ = example
elif isinstance(example, dict):
reference_example_ = ls_schemas.Example(
**example, _host_url=self._host_url, _tenant_id=self._get_tenant_id()
**example,
_host_url=self._host_url,
_tenant_id=self._get_optional_tenant_id(),
)
elif run.reference_example_id is not None:
reference_example_ = self.read_example(run.reference_example_id)
Expand Down
2 changes: 1 addition & 1 deletion python/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "langsmith"
version = "0.1.8"
version = "0.1.9"
description = "Client library to connect to the LangSmith LLM Tracing and Evaluation Platform."
authors = ["LangChain <[email protected]>"]
license = "MIT"
Expand Down

0 comments on commit 13902a5

Please sign in to comment.