Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(js): Further reduce blocking behavior, try naive stringification instead of circular detection #1133

Merged
merged 6 commits into from
Oct 26, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 18 additions & 27 deletions js/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@
return false;
};

export class Queue {
export class AutoBatchQueue {
items: {
action: "create" | "update";
payload: RunCreate | RunUpdate;
Expand Down Expand Up @@ -419,7 +419,7 @@
// If there is an item on the queue we were unable to pop,
// just return it as a single batch.
if (popped.length === 0 && this.items.length > 0) {
const item = this.items.shift()!;

Check warning on line 422 in js/src/client.ts

View workflow job for this annotation

GitHub Actions / Check linting

Forbidden non-null assertion
popped.push(item);
poppedSizeBytes += item.size;
this.sizeBytes -= item.size;
Expand Down Expand Up @@ -461,7 +461,7 @@

private autoBatchTracing = true;

private autoBatchQueue = new Queue();
private autoBatchQueue = new AutoBatchQueue();

private autoBatchTimeout: ReturnType<typeof setTimeout> | undefined;

Expand Down Expand Up @@ -755,7 +755,7 @@
}
}

private async _getBatchSizeLimitBytes() {
private async _getBatchSizeLimitBytes(): Promise<number> {
const serverInfo = await this._ensureServerInfo();
return (
this.batchSizeBytesLimit ??
Expand All @@ -764,21 +764,18 @@
);
}

private async drainAutoBatchQueue() {
const batchSizeLimit = await this._getBatchSizeLimitBytes();
private drainAutoBatchQueue(batchSizeLimit: number) {
while (this.autoBatchQueue.items.length > 0) {
for (let i = 0; i < this.traceBatchConcurrency; i++) {
const [batch, done] = this.autoBatchQueue.pop(batchSizeLimit);
if (!batch.length) {
done();
break;
}
await this.processBatch(batch, done);
const [batch, done] = this.autoBatchQueue.pop(batchSizeLimit);
if (!batch.length) {
done();
break;
}
void this._processBatch(batch, done).catch(console.error);
}
}

private async processBatch(batch: AutoBatchQueueItem[], done: () => void) {
private async _processBatch(batch: AutoBatchQueueItem[], done: () => void) {
if (!batch.length) {
done();
return;
Expand All @@ -803,10 +800,7 @@
}
}

private async processRunOperation(
item: AutoBatchQueueItem,
immediatelyTriggerBatch?: boolean
) {
private async processRunOperation(item: AutoBatchQueueItem) {
const oldTimeout = this.autoBatchTimeout;
clearTimeout(this.autoBatchTimeout);
this.autoBatchTimeout = undefined;
Expand All @@ -815,19 +809,14 @@
}
const itemPromise = this.autoBatchQueue.push(item);
const sizeLimitBytes = await this._getBatchSizeLimitBytes();
if (
immediatelyTriggerBatch ||
this.autoBatchQueue.sizeBytes > sizeLimitBytes
) {
await this.drainAutoBatchQueue().catch(console.error);
if (this.autoBatchQueue.sizeBytes > sizeLimitBytes) {
this.drainAutoBatchQueue(sizeLimitBytes);
}
if (this.autoBatchQueue.items.length > 0) {
this.autoBatchTimeout = setTimeout(
() => {
this.autoBatchTimeout = undefined;
// This error would happen in the background and is uncatchable
// from the outside. So just log instead.
void this.drainAutoBatchQueue().catch(console.error);
this.drainAutoBatchQueue(sizeLimitBytes);
},
oldTimeout
? this.autoBatchAggregationDelayMs
Expand All @@ -854,7 +843,7 @@
if (this._serverInfo === undefined) {
try {
this._serverInfo = await this._getServerInfo();
} catch (e) {

Check warning on line 846 in js/src/client.ts

View workflow job for this annotation

GitHub Actions / Check linting

'e' is defined but never used. Allowed unused args must match /^_/u
console.warn(
`[WARNING]: LangSmith failed to fetch info on supported operations. Falling back to single calls and default limits.`
);
Expand Down Expand Up @@ -1232,9 +1221,11 @@
data.parent_run_id === undefined &&
this.blockOnRootRunFinalization
) {
// Trigger a batch as soon as a root trace ends and block to ensure trace finishes
// Trigger batches as soon as a root trace ends and wait to ensure trace finishes
// in serverless environments.
await this.processRunOperation({ action: "update", item: data }, true);
await this.processRunOperation({ action: "update", item: data }).catch(
console.error
);
return;
} else {
void this.processRunOperation({ action: "update", item: data }).catch(
Expand Down Expand Up @@ -1562,7 +1553,7 @@
treeFilter?: string;
isRoot?: boolean;
dataSourceType?: string;
}): Promise<any> {

Check warning on line 1556 in js/src/client.ts

View workflow job for this annotation

GitHub Actions / Check linting

Unexpected any. Specify a different type
let projectIds_ = projectIds || [];
if (projectNames) {
projectIds_ = [
Expand Down Expand Up @@ -1850,7 +1841,7 @@
`Failed to list shared examples: ${response.status} ${response.statusText}`
);
}
return result.map((example: any) => ({

Check warning on line 1844 in js/src/client.ts

View workflow job for this annotation

GitHub Actions / Check linting

Unexpected any. Specify a different type
...example,
_hostUrl: this.getHostUrl(),
}));
Expand Down Expand Up @@ -1987,7 +1978,7 @@
}
// projectId querying
return true;
} catch (e) {

Check warning on line 1981 in js/src/client.ts

View workflow job for this annotation

GitHub Actions / Check linting

'e' is defined but never used. Allowed unused args must match /^_/u
return false;
}
}
Expand Down Expand Up @@ -3327,7 +3318,7 @@
async _logEvaluationFeedback(
evaluatorResponse: EvaluationResult | EvaluationResults,
run?: Run,
sourceInfo?: { [key: string]: any }

Check warning on line 3321 in js/src/client.ts

View workflow job for this annotation

GitHub Actions / Check linting

Unexpected any. Specify a different type
): Promise<[results: EvaluationResult[], feedbacks: Feedback[]]> {
const evalResults: Array<EvaluationResult> =
this._selectEvalResults(evaluatorResponse);
Expand Down Expand Up @@ -3366,7 +3357,7 @@
public async logEvaluationFeedback(
evaluatorResponse: EvaluationResult | EvaluationResults,
run?: Run,
sourceInfo?: { [key: string]: any }

Check warning on line 3360 in js/src/client.ts

View workflow job for this annotation

GitHub Actions / Check linting

Unexpected any. Specify a different type
): Promise<EvaluationResult[]> {
const [results] = await this._logEvaluationFeedback(
evaluatorResponse,
Expand Down Expand Up @@ -3816,7 +3807,7 @@

public async createCommit(
promptIdentifier: string,
object: any,

Check warning on line 3810 in js/src/client.ts

View workflow job for this annotation

GitHub Actions / Check linting

Unexpected any. Specify a different type
options?: {
parentCommitHash?: string;
}
Expand Down Expand Up @@ -3867,7 +3858,7 @@
isPublic?: boolean;
isArchived?: boolean;
}
): Promise<Record<string, any>> {

Check warning on line 3861 in js/src/client.ts

View workflow job for this annotation

GitHub Actions / Check linting

Unexpected any. Specify a different type
if (!(await this.promptExists(promptIdentifier))) {
throw new Error("Prompt does not exist, you must create it first.");
}
Expand All @@ -3878,7 +3869,7 @@
throw await this._ownerConflictError("update a prompt", owner);
}

const payload: Record<string, any> = {};

Check warning on line 3872 in js/src/client.ts

View workflow job for this annotation

GitHub Actions / Check linting

Unexpected any. Specify a different type

if (options?.description !== undefined)
payload.description = options.description;
Expand Down
Loading