Skip to content

Commit

Permalink
python[patch]: fix example link [LS-2764] (#1473)
Browse files Browse the repository at this point in the history
  • Loading branch information
baskaryan authored Jan 29, 2025
1 parent b832f1f commit 157ab3f
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
5 changes: 5 additions & 0 deletions python/langsmith/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -770,10 +770,15 @@ def get_host_url(web_url: Optional[str], api_url: str):
elif str(parsed_url.path).endswith("/api"):
new_path = str(parsed_url.path).rsplit("/api", 1)[0]
link = urllib_parse.urlunparse(parsed_url._replace(path=new_path))
elif str(parsed_url.path).endswith("/api/v1"):
new_path = str(parsed_url.path).rsplit("/api/v1", 1)[0]
link = urllib_parse.urlunparse(parsed_url._replace(path=new_path))
elif str(parsed_url.netloc).startswith("eu."):
link = "https://eu.smith.langchain.com"
elif str(parsed_url.netloc).startswith("dev."):
link = "https://dev.smith.langchain.com"
elif str(parsed_url.netloc).startswith("beta."):
link = "https://beta.smith.langchain.com"
else:
link = "https://smith.langchain.com"
return link
Expand Down
59 changes: 59 additions & 0 deletions python/tests/unit_tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,3 +409,62 @@ class BarClass:
assert ls_utils._get_function_name(print) == "print"

assert ls_utils._get_function_name("not_a_function") == "not_a_function"


def test_get_host_url():
# If web_url is explicitly provided, it takes precedence over api_url.
assert (
ls_utils.get_host_url(
"https://my-custom-web.com", "https://api.smith.langchain.com"
)
== "https://my-custom-web.com"
)

# When web_url is None and api_url is localhost.
assert ls_utils.get_host_url(None, "http://localhost:5000") == "http://localhost"
# A port variation on localhost.
assert (
ls_utils.get_host_url(None, "http://127.0.0.1:8080") == "http://localhost"
), "Should recognize 127.x.x.x as localhost."

# If api_url path ends with /api, trimmed back to netloc.
assert (
ls_utils.get_host_url(None, "https://my-awesome-domain.com/api")
== "https://my-awesome-domain.com"
)

# If api_url path ends with /api/v1, trimmed back to netloc.
assert (
ls_utils.get_host_url(None, "https://my-other-domain.com/api/v1")
== "https://my-other-domain.com"
)

# If netloc begins with dev.
assert (
ls_utils.get_host_url(None, "https://dev.smith.langchain.com/api/v1")
== "https://dev.smith.langchain.com"
)

# If netloc begins with eu.
assert (
ls_utils.get_host_url(None, "https://eu.smith.langchain.com/api")
== "https://eu.smith.langchain.com"
)

# If netloc begins with beta.
assert (
ls_utils.get_host_url(None, "https://beta.smith.langchain.com")
== "https://beta.smith.langchain.com"
)

# If netloc begins with api.
assert (
ls_utils.get_host_url(None, "https://api.smith.langchain.com")
== "https://smith.langchain.com"
)

# Otherwise, returns https://smith.langchain.com for unknown host.
assert (
ls_utils.get_host_url(None, "https://unknownhost.com")
== "https://smith.langchain.com"
)

0 comments on commit 157ab3f

Please sign in to comment.