Skip to content

Commit

Permalink
Fix URL Parse (#352)
Browse files Browse the repository at this point in the history
  • Loading branch information
hinthornw authored Jan 11, 2024
1 parent 37662f3 commit f0fbd3e
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 12 deletions.
2 changes: 1 addition & 1 deletion js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "langsmith",
"version": "0.0.57",
"version": "0.0.58",
"description": "Client library to connect to the LangSmith LLM Tracing and Evaluation Platform.",
"files": [
"dist/",
Expand Down
13 changes: 8 additions & 5 deletions js/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ export class Client {
const apiKey = getEnvironmentVariable("LANGCHAIN_API_KEY");
const apiUrl =
getEnvironmentVariable("LANGCHAIN_ENDPOINT") ??
(apiKey ? "https://api.smith.langchain.com" : "http://localhost:1984");
"https://api.smith.langchain.com";
return {
apiUrl: apiUrl,
apiKey: apiKey,
Expand All @@ -227,7 +227,10 @@ export class Client {
} else if (isLocalhost(this.apiUrl)) {
this.webUrl = "http://localhost";
return "http://localhost";
} else if (this.apiUrl.includes("/api")) {
} else if (
this.apiUrl.includes("/api") &&
!this.apiUrl.split(".", 1)[0].endsWith("api")
) {
this.webUrl = this.apiUrl.replace("/api", "");
return this.webUrl;
} else if (this.apiUrl.split(".", 1)[0].includes("dev")) {
Expand Down Expand Up @@ -310,10 +313,10 @@ export class Client {
private async *_getCursorPaginatedList<T>(
path: string,
body: Record<string, any> | null = null,
requestMethod: string = "POST",
dataKey: string = "runs"
requestMethod = "POST",
dataKey = "runs"
): AsyncIterable<T[]> {
let bodyParams = body ? { ...body } : {};
const bodyParams = body ? { ...body } : {};
while (true) {
const response = await this.caller.call(fetch, `${this.apiUrl}${path}`, {
method: requestMethod,
Expand Down
48 changes: 46 additions & 2 deletions js/src/tests/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Client } from "../client.js";
describe("Client", () => {
describe("createLLMExample", () => {
it("should create an example with the given input and generation", async () => {
const client = new Client();
const client = new Client({ apiKey: "test-api-key" });
const createExampleSpy = jest
.spyOn(client, "createExample")
.mockResolvedValue({
Expand Down Expand Up @@ -32,7 +32,7 @@ describe("Client", () => {

describe("createChatExample", () => {
it("should convert LangChainBaseMessage objects to examples", async () => {
const client = new Client();
const client = new Client({ apiKey: "test-api-key" });
const createExampleSpy = jest
.spyOn(client, "createExample")
.mockResolvedValue({
Expand Down Expand Up @@ -74,4 +74,48 @@ describe("Client", () => {
);
});
});

describe("getHostUrl", () => {
it("should return the webUrl if it exists", () => {
const client = new Client({
webUrl: "http://example.com",
apiKey: "test-api-key",
});
const result = (client as any).getHostUrl();
expect(result).toBe("http://example.com");
});

it("should return 'http://localhost' if apiUrl is localhost", () => {
const client = new Client({ apiUrl: "http://localhost/api" });
const result = (client as any).getHostUrl();
expect(result).toBe("http://localhost");
});

it("should return the webUrl without '/api' if apiUrl contains '/api'", () => {
const client = new Client({
webUrl: "https://example.com",
apiKey: "test-api-key",
});
const result = (client as any).getHostUrl();
expect(result).toBe("https://example.com");
});

it("should return 'https://dev.smith.langchain.com' if apiUrl contains 'dev'", () => {
const client = new Client({
apiUrl: "https://dev.smith.langchain.com/api",
apiKey: "test-api-key",
});
const result = (client as any).getHostUrl();
expect(result).toBe("https://dev.smith.langchain.com");
});

it("should return 'https://smith.langchain.com' for any other apiUrl", () => {
const client = new Client({
apiUrl: "https://smith.langchain.com/api",
apiKey: "test-api-key",
});
const result = (client as any).getHostUrl();
expect(result).toBe("https://smith.langchain.com");
});
});
});
4 changes: 2 additions & 2 deletions python/langsmith/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def _get_api_url(api_url: Optional[str], api_key: Optional[str]) -> str:
if api_url is not None
else os.getenv(
"LANGCHAIN_ENDPOINT",
"https://api.smith.langchain.com" if api_key else "http://localhost:1984",
"https://api.smith.langchain.com",
)
)
if not _api_url.strip():
Expand Down Expand Up @@ -275,7 +275,7 @@ def __init__(
----------
api_url : str or None, default=None
URL for the LangSmith API. Defaults to the LANGCHAIN_ENDPOINT
environment variable or http://localhost:1984 if not set.
environment variable or https://api.smith.langchain.com if not set.
api_key : str or None, default=None
API key for the LangSmith API. Defaults to the LANGCHAIN_API_KEY
environment variable.
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.0.79"
version = "0.0.80"
description = "Client library to connect to the LangSmith LLM Tracing and Evaluation Platform."
authors = ["LangChain <[email protected]>"]
license = "MIT"
Expand Down
2 changes: 1 addition & 1 deletion python/tests/unit_tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def test_get_api_url() -> None:
assert _get_api_url(None, "api_key") == "https://api.smith.langchain.com"

with patch.dict(os.environ, {}, clear=True):
assert _get_api_url(None, None) == "http://localhost:1984"
assert _get_api_url(None, None) == "https://api.smith.langchain.com"

with patch.dict(os.environ, {"LANGCHAIN_ENDPOINT": "http://env.url"}):
assert _get_api_url(None, None) == "http://env.url"
Expand Down

0 comments on commit f0fbd3e

Please sign in to comment.