Skip to content

Commit

Permalink
Read Share Dataset (#314)
Browse files Browse the repository at this point in the history
In shared method
  • Loading branch information
hinthornw authored Dec 1, 2023
1 parent b8b7a19 commit 2001f6b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
19 changes: 11 additions & 8 deletions python/langsmith/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,14 +316,17 @@ def _host_url(self) -> str:
"""The web host url."""
if self._web_url:
link = self._web_url
elif _is_localhost(self.api_url):
link = "http://localhost"
elif "/api" in self.api_url:
link = self.api_url.replace("/api", "")
elif "dev" in self.api_url.split(".", maxsplit=1)[0]:
link = "https://dev.smith.langchain.com"
else:
link = "https://smith.langchain.com"
parsed_url = urllib_parse.urlparse(self.api_url)
if _is_localhost(self.api_url):
link = "http://localhost"
elif parsed_url.path.endswith("/api"):
new_path = parsed_url.path.rsplit("/api", 1)[0]
link = urllib_parse.urlunparse(parsed_url._replace(path=new_path))
elif parsed_url.netloc.startswith("dev."):
link = "https://dev.smith.langchain.com"
else:
link = "https://smith.langchain.com"
return link

@property
Expand Down Expand Up @@ -1039,7 +1042,7 @@ def read_shared_dataset(
return ls_schemas.Dataset(
**response.json(),
_host_url=self._host_url,
_tenant_id=self._get_tenant_id(),
_public_path=f"/public/{share_token}/d",
)

def list_shared_examples(
Expand Down
5 changes: 5 additions & 0 deletions python/langsmith/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,22 +138,27 @@ class Dataset(DatasetBase):
last_session_start_time: Optional[datetime] = None
_host_url: Optional[str] = PrivateAttr(default=None)
_tenant_id: Optional[UUID] = PrivateAttr(default=None)
_public_path: Optional[str] = PrivateAttr(default=None)

def __init__(
self,
_host_url: Optional[str] = None,
_tenant_id: Optional[UUID] = None,
_public_path: Optional[str] = None,
**kwargs: Any,
) -> None:
"""Initialize a Dataset object."""
super().__init__(**kwargs)
self._host_url = _host_url
self._tenant_id = _tenant_id
self._public_path = _public_path

@property
def url(self) -> Optional[str]:
"""URL of this run within the app."""
if self._host_url:
if self._public_path:
return f"{self._host_url}{self._public_path}"
if self._tenant_id:
return f"{self._host_url}/o/{str(self._tenant_id)}/datasets/{self.id}"
return f"{self._host_url}/datasets/{self.id}"
Expand Down
21 changes: 21 additions & 0 deletions python/tests/unit_tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,24 @@ class MyPydantic(BaseModel):
obj2 = {"output": obj}
res2 = json.loads(json.dumps(obj2, default=_serialize_json))
assert res2 == {"output": expected}


def test_host_url() -> None:
client = Client(api_url="https://api.foobar.com/api", api_key="API_KEY")
assert client._host_url == "https://api.foobar.com"

client = Client(
api_url="https://api.langsmith.com",
api_key="API_KEY",
web_url="https://web.langsmith.com",
)
assert client._host_url == "https://web.langsmith.com"

client = Client(api_url="http://localhost:8000", api_key="API_KEY")
assert client._host_url == "http://localhost"

client = Client(api_url="https://dev.api.smith.langchain.com", api_key="API_KEY")
assert client._host_url == "https://dev.smith.langchain.com"

client = Client(api_url="https://api.smith.langchain.com", api_key="API_KEY")
assert client._host_url == "https://smith.langchain.com"

0 comments on commit 2001f6b

Please sign in to comment.