-
Notifications
You must be signed in to change notification settings - Fork 2.6k
feat[langsmith]: transparent handoff between @traceable HOF and LangChain #5339
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
Changes from all commits
1afc6c2
3de61b5
7fba45a
ec4c02a
c48394a
94d952a
f27a2a0
7e1d17a
b849f2d
3c81e05
45ab4a5
b16042d
24a27e1
7d88168
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
import { Client } from "langsmith"; | ||
import { RunTree } from "langsmith/run_trees"; | ||
import { getCurrentRunTree } from "langsmith/singletons/traceable"; | ||
|
||
import { | ||
BaseRun, | ||
RunCreate, | ||
|
@@ -57,6 +60,40 @@ export class LangChainTracer | |
getEnvironmentVariable("LANGCHAIN_SESSION"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hey there! I noticed that the recent code changes in
dqbd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 = this.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; | ||
} | ||
} | ||
|
||
private async _convertToCreate( | ||
|
@@ -102,4 +139,12 @@ export class LangChainTracer | |
getRun(id: string): Run | undefined { | ||
return this.runMap.get(id); | ||
} | ||
|
||
getTraceableRunTree(): RunTree | undefined { | ||
try { | ||
return getCurrentRunTree(); | ||
} catch { | ||
return undefined; | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.