Skip to content

Commit

Permalink
Add custom fetch options (#538)
Browse files Browse the repository at this point in the history
  • Loading branch information
hinthornw authored Mar 21, 2024
1 parent 99d9688 commit 8e5d242
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 9 deletions.
34 changes: 34 additions & 0 deletions js/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ interface ClientConfig {
hideOutputs?: boolean;
autoBatchTracing?: boolean;
pendingAutoBatchedRunLimit?: number;
fetchOptions?: RequestInit;
}

/**
Expand Down Expand Up @@ -396,6 +397,8 @@ export class Client {

private serverInfo: Record<string, any> | undefined;

private fetchOptions: RequestInit;

constructor(config: ClientConfig = {}) {
const defaultConfig = Client.getDefaultClientConfig();

Expand All @@ -414,6 +417,7 @@ export class Client {
this.autoBatchTracing = config.autoBatchTracing ?? this.autoBatchTracing;
this.pendingAutoBatchedRunLimit =
config.pendingAutoBatchedRunLimit ?? this.pendingAutoBatchedRunLimit;
this.fetchOptions = config.fetchOptions || {};
}

public static getDefaultClientConfig(): {
Expand Down Expand Up @@ -522,6 +526,7 @@ export class Client {
method: "GET",
headers: this.headers,
signal: AbortSignal.timeout(this.timeout_ms),
...this.fetchOptions,
});
if (!response.ok) {
throw new Error(
Expand Down Expand Up @@ -553,6 +558,7 @@ export class Client {
method: "GET",
headers: this.headers,
signal: AbortSignal.timeout(this.timeout_ms),
...this.fetchOptions,
});
if (!response.ok) {
throw new Error(
Expand Down Expand Up @@ -584,6 +590,7 @@ export class Client {
method: requestMethod,
headers: { ...this.headers, "Content-Type": "application/json" },
signal: AbortSignal.timeout(this.timeout_ms),
...this.fetchOptions,
body: JSON.stringify(bodyParams),
});
const responseBody = await response.json();
Expand Down Expand Up @@ -693,6 +700,7 @@ export class Client {
method: "GET",
headers: { Accept: "application/json" },
signal: AbortSignal.timeout(this.timeout_ms),
...this.fetchOptions,
});
if (!response.ok) {
// consume the response body to release the connection
Expand Down Expand Up @@ -745,6 +753,7 @@ export class Client {
headers,
body: JSON.stringify(mergedRunCreateParams[0]),
signal: AbortSignal.timeout(this.timeout_ms),
...this.fetchOptions,
});
await raiseForStatus(response, "create run");
}
Expand Down Expand Up @@ -872,6 +881,7 @@ export class Client {
headers,
body: body,
signal: AbortSignal.timeout(this.timeout_ms),
...this.fetchOptions,
}
);
await raiseForStatus(response, "batch create run");
Expand Down Expand Up @@ -917,6 +927,7 @@ export class Client {
headers,
body: JSON.stringify(run),
signal: AbortSignal.timeout(this.timeout_ms),
...this.fetchOptions,
}
);
await raiseForStatus(response, "update run");
Expand Down Expand Up @@ -1161,6 +1172,7 @@ export class Client {
headers: this.headers,
body: JSON.stringify(data),
signal: AbortSignal.timeout(this.timeout_ms),
...this.fetchOptions,
}
);
const result = await response.json();
Expand All @@ -1179,6 +1191,7 @@ export class Client {
method: "DELETE",
headers: this.headers,
signal: AbortSignal.timeout(this.timeout_ms),
...this.fetchOptions,
}
);
await raiseForStatus(response, "unshare run");
Expand All @@ -1193,6 +1206,7 @@ export class Client {
method: "GET",
headers: this.headers,
signal: AbortSignal.timeout(this.timeout_ms),
...this.fetchOptions,
}
);
const result = await response.json();
Expand Down Expand Up @@ -1226,6 +1240,7 @@ export class Client {
method: "GET",
headers: this.headers,
signal: AbortSignal.timeout(this.timeout_ms),
...this.fetchOptions,
}
);
const runs = await response.json();
Expand All @@ -1251,6 +1266,7 @@ export class Client {
method: "GET",
headers: this.headers,
signal: AbortSignal.timeout(this.timeout_ms),
...this.fetchOptions,
}
);
const shareSchema = await response.json();
Expand Down Expand Up @@ -1283,6 +1299,7 @@ export class Client {
headers: this.headers,
body: JSON.stringify(data),
signal: AbortSignal.timeout(this.timeout_ms),
...this.fetchOptions,
}
);
const shareSchema = await response.json();
Expand All @@ -1301,6 +1318,7 @@ export class Client {
method: "DELETE",
headers: this.headers,
signal: AbortSignal.timeout(this.timeout_ms),
...this.fetchOptions,
}
);
await raiseForStatus(response, "unshare dataset");
Expand All @@ -1315,6 +1333,7 @@ export class Client {
method: "GET",
headers: this.headers,
signal: AbortSignal.timeout(this.timeout_ms),
...this.fetchOptions,
}
);
const dataset = await response.json();
Expand Down Expand Up @@ -1355,6 +1374,7 @@ export class Client {
headers: { ...this.headers, "Content-Type": "application/json" },
body: JSON.stringify(body),
signal: AbortSignal.timeout(this.timeout_ms),
...this.fetchOptions,
});
const result = await response.json();
if (!response.ok) {
Expand Down Expand Up @@ -1397,6 +1417,7 @@ export class Client {
headers: { ...this.headers, "Content-Type": "application/json" },
body: JSON.stringify(body),
signal: AbortSignal.timeout(this.timeout_ms),
...this.fetchOptions,
});
const result = await response.json();
if (!response.ok) {
Expand Down Expand Up @@ -1434,6 +1455,7 @@ export class Client {
method: "GET",
headers: this.headers,
signal: AbortSignal.timeout(this.timeout_ms),
...this.fetchOptions,
}
);
// consume the response body to release the connection
Expand Down Expand Up @@ -1583,6 +1605,7 @@ export class Client {
method: "DELETE",
headers: this.headers,
signal: AbortSignal.timeout(this.timeout_ms),
...this.fetchOptions,
}
);
await raiseForStatus(
Expand Down Expand Up @@ -1625,6 +1648,7 @@ export class Client {
headers: this.headers,
body: formData,
signal: AbortSignal.timeout(this.timeout_ms),
...this.fetchOptions,
});

if (!response.ok) {
Expand Down Expand Up @@ -1660,6 +1684,7 @@ export class Client {
headers: { ...this.headers, "Content-Type": "application/json" },
body: JSON.stringify(body),
signal: AbortSignal.timeout(this.timeout_ms),
...this.fetchOptions,
});

if (!response.ok) {
Expand Down Expand Up @@ -1829,6 +1854,7 @@ export class Client {
method: "DELETE",
headers: this.headers,
signal: AbortSignal.timeout(this.timeout_ms),
...this.fetchOptions,
});
if (!response.ok) {
throw new Error(
Expand Down Expand Up @@ -1867,6 +1893,7 @@ export class Client {
headers: { ...this.headers, "Content-Type": "application/json" },
body: JSON.stringify(data),
signal: AbortSignal.timeout(this.timeout_ms),
...this.fetchOptions,
});

if (!response.ok) {
Expand Down Expand Up @@ -1923,6 +1950,7 @@ export class Client {
headers: { ...this.headers, "Content-Type": "application/json" },
body: JSON.stringify(formattedExamples),
signal: AbortSignal.timeout(this.timeout_ms),
...this.fetchOptions,
}
);

Expand Down Expand Up @@ -2026,6 +2054,7 @@ export class Client {
method: "DELETE",
headers: this.headers,
signal: AbortSignal.timeout(this.timeout_ms),
...this.fetchOptions,
});
if (!response.ok) {
throw new Error(
Expand All @@ -2048,6 +2077,7 @@ export class Client {
headers: { ...this.headers, "Content-Type": "application/json" },
body: JSON.stringify(update),
signal: AbortSignal.timeout(this.timeout_ms),
...this.fetchOptions,
}
);
if (!response.ok) {
Expand Down Expand Up @@ -2163,6 +2193,7 @@ export class Client {
headers: { ...this.headers, "Content-Type": "application/json" },
body: JSON.stringify(feedback),
signal: AbortSignal.timeout(this.timeout_ms),
...this.fetchOptions,
});
await raiseForStatus(response, "create feedback");
return feedback as Feedback;
Expand Down Expand Up @@ -2204,6 +2235,7 @@ export class Client {
headers: { ...this.headers, "Content-Type": "application/json" },
body: JSON.stringify(feedbackUpdate),
signal: AbortSignal.timeout(this.timeout_ms),
...this.fetchOptions,
}
);
await raiseForStatus(response, "update feedback");
Expand All @@ -2223,6 +2255,7 @@ export class Client {
method: "DELETE",
headers: this.headers,
signal: AbortSignal.timeout(this.timeout_ms),
...this.fetchOptions,
});
if (!response.ok) {
throw new Error(
Expand Down Expand Up @@ -2314,6 +2347,7 @@ export class Client {
headers: { ...this.headers, "Content-Type": "application/json" },
body: JSON.stringify(body),
signal: AbortSignal.timeout(this.timeout_ms),
...this.fetchOptions,
}
);
const result = await response.json();
Expand Down
18 changes: 9 additions & 9 deletions js/src/tests/client.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,17 +279,13 @@ test.concurrent(
outputs: { generation: "hi there 2" },
end_time: new Date().getTime(),
});
await waitUntilRunFound(langchainClient, runId, true);
await waitUntilRunFound(langchainClient, runId, false);
const run1 = await langchainClient.readRun(runId);
expect(run1.inputs).toBeDefined();
expect(Object.keys(run1.inputs)).toHaveLength(0);
expect(run1.outputs).toBeDefined();
expect(Object.keys(run1.inputs ?? {})).toHaveLength(0);
expect(Object.keys(run1.outputs ?? {})).toHaveLength(0);
await waitUntilRunFound(langchainClient, runId2, true);
await waitUntilRunFound(langchainClient, runId2, false);
const run2 = await langchainClient.readRun(runId2);
expect(run2.inputs).toBeDefined();
expect(Object.keys(run2.inputs)).toHaveLength(0);
expect(run2.outputs).toBeDefined();
expect(Object.keys(run2.inputs ?? {})).toHaveLength(0);
expect(Object.keys(run2.outputs ?? {})).toHaveLength(0);
},
240_000
Expand Down Expand Up @@ -355,7 +351,11 @@ test.concurrent(

describe("createChatExample", () => {
it("should convert LangChainBaseMessage objects to examples", async () => {
const langchainClient = new Client({ autoBatchTracing: false });
const langchainClient = new Client({
autoBatchTracing: false,
// Test the fetch options option
fetchOptions: { cache: "no-store" },
});

const datasetName = "__createChatExample-test-dataset JS";
await deleteDataset(langchainClient, datasetName);
Expand Down

0 comments on commit 8e5d242

Please sign in to comment.