Skip to content

Commit

Permalink
Expose method for updating tracer from run tree
Browse files Browse the repository at this point in the history
  • Loading branch information
dqbd committed Jul 16, 2024
1 parent 1746136 commit d348779
Showing 1 changed file with 31 additions and 31 deletions.
62 changes: 31 additions & 31 deletions langchain-core/src/tracers/tracer_langchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,38 +61,9 @@ export class LangChainTracer
this.exampleId = exampleId;
this.client = client ?? new Client({});

// if we're inside traceable, we can obtain the traceable tree
// and populate the run map, which is used to correctly
// infer dotted order and execution order
const traceableTree = LangChainTracer.getTraceableRunTree();
if (traceableTree) {
let rootRun: RunTree = traceableTree;
const visited = new Set<string>();
while (rootRun.parent_run) {
if (visited.has(rootRun.id)) break;
visited.add(rootRun.id);

if (!rootRun.parent_run) break;
rootRun = rootRun.parent_run as RunTree;
}
visited.clear();

const queue = [rootRun];
while (queue.length > 0) {
const current = queue.shift();
if (!current || visited.has(current.id)) continue;
visited.add(current.id);

// @ts-expect-error Types of property 'events' are incompatible.
this.runMap.set(current.id, current);
if (current.child_runs) {
queue.push(...current.child_runs);
}
}

this.client = traceableTree.client ?? this.client;
this.projectName = traceableTree.project_name ?? this.projectName;
this.exampleId = traceableTree.reference_example_id ?? this.exampleId;
this.updateFromRunTree(traceableTree);
}
}

Expand Down Expand Up @@ -140,8 +111,37 @@ export class LangChainTracer
return this.runMap.get(id);
}

updateFromRunTree(runTree: RunTree) {
let rootRun: RunTree = runTree;
const visited = new Set<string>();
while (rootRun.parent_run) {
if (visited.has(rootRun.id)) break;
visited.add(rootRun.id);

if (!rootRun.parent_run) break;
rootRun = rootRun.parent_run as RunTree;
}
visited.clear();

const queue = [rootRun];
while (queue.length > 0) {
const current = queue.shift();
if (!current || visited.has(current.id)) continue;
visited.add(current.id);

// @ts-expect-error Types of property 'events' are incompatible.
this.runMap.set(current.id, current);
if (current.child_runs) {
queue.push(...current.child_runs);
}
}

this.client = runTree.client ?? this.client;
this.projectName = runTree.project_name ?? this.projectName;
this.exampleId = runTree.reference_example_id ?? this.exampleId;
}

convertToRunTree(id: string): RunTree | undefined {
// create a run tree from a run map
const runTreeMap: Record<string, RunTree> = {};
const runTreeList: [id: string, dotted_order: string | undefined][] = [];
for (const [id, run] of this.runMap) {
Expand Down

0 comments on commit d348779

Please sign in to comment.