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

Use onRunCreate #4405

Merged
merged 4 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
36 changes: 35 additions & 1 deletion langchain-core/src/tracers/tests/langchain_tracer.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import * as uuid from "uuid";
import { test } from "@jest/globals";

import { LangChainTracer } from "../tracer_langchain.js";
import { LangChainTracer, Run } from "../tracer_langchain.js";
import { Serialized } from "../../load/serializable.js";
import { HumanMessage } from "../../messages/index.js";

Expand Down Expand Up @@ -41,3 +41,37 @@ test("LangChain V2 tracer does not throw errors for its methods", async () => {
await tracer.handleLLMStart(serialized, ["test"], llmRunId3);
await tracer.handleLLMEnd({ generations: [[]] }, llmRunId3);
});

class FakeTracer extends LangChainTracer {
createOperations: { [id: string]: Run } = {};

updateOperations: { [id: string]: Run } = {};

async onRunCreate(run: Run): Promise<void> {
this.createOperations[run.id] = run;
}

async onRunUpdate(run: Run): Promise<void> {
this.updateOperations[run.id] = run;
}
}

test("LangChain V2 tracer creates and updates runs with trace_id and dotted_order", async () => {
const tracer = new FakeTracer({
projectName: `JS Int Test - ${uuid.v4()}`,
});
const chainRunId = uuid.v4();
const llmRunId = uuid.v4();
await tracer.handleChainStart(serialized, { foo: "bar" }, chainRunId);

await tracer.handleLLMStart(serialized, ["test"], llmRunId, chainRunId);
await tracer.handleLLMEnd({ generations: [[]] }, llmRunId);
await tracer.handleChainEnd({ foo: "bar" }, chainRunId);

expect(tracer.createOperations[chainRunId].trace_id).toBeDefined();
expect(tracer.createOperations[chainRunId].trace_id).toEqual(chainRunId);
expect(tracer.createOperations[chainRunId].dotted_order).toBeDefined();
expect(tracer.updateOperations[llmRunId].trace_id).toBeDefined();
expect(tracer.updateOperations[llmRunId].trace_id).toEqual(chainRunId);
expect(tracer.updateOperations[llmRunId].dotted_order).toBeDefined();
});
66 changes: 15 additions & 51 deletions langchain-core/src/tracers/tracer_langchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,20 @@ export interface Run extends BaseRun {
id: string;
child_runs: this[];
child_execution_order: number;
dotted_order?: string;
trace_id?: string;
}

export interface RunCreate2 extends RunCreate {
trace_id?: string;
dotted_order?: string;
}

export interface RunUpdate extends BaseRunUpdate {
events: BaseRun["events"];
inputs: KVMap;
trace_id?: string;
dotted_order?: string;
}

export interface LangChainTracerFields extends BaseCallbackHandlerInput {
Expand Down Expand Up @@ -68,70 +77,25 @@ export class LangChainTracer

protected async persistRun(_run: Run): Promise<void> {}

protected async _persistRunSingle(run: Run): Promise<void> {
const persistedRun: RunCreate = await this._convertToCreate(
async onRunCreate(run: Run): Promise<void> {
const persistedRun: RunCreate2 = await this._convertToCreate(
run,
this.exampleId
);
await this.client.createRun(persistedRun);
}

protected async _updateRunSingle(run: Run): Promise<void> {
async onRunUpdate(run: Run): Promise<void> {
const runUpdate: RunUpdate = {
end_time: run.end_time,
error: run.error,
outputs: run.outputs,
events: run.events,
inputs: run.inputs,
trace_id: run.trace_id,
dotted_order: run.dotted_order,
parent_run_id: run.parent_run_id,
};
await this.client.updateRun(run.id, runUpdate);
}

async onRetrieverStart(run: Run): Promise<void> {
await this._persistRunSingle(run);
}

async onRetrieverEnd(run: Run): Promise<void> {
await this._updateRunSingle(run);
}

async onRetrieverError(run: Run): Promise<void> {
await this._updateRunSingle(run);
}

async onLLMStart(run: Run): Promise<void> {
await this._persistRunSingle(run);
}

async onLLMEnd(run: Run): Promise<void> {
await this._updateRunSingle(run);
}

async onLLMError(run: Run): Promise<void> {
await this._updateRunSingle(run);
}

async onChainStart(run: Run): Promise<void> {
await this._persistRunSingle(run);
}

async onChainEnd(run: Run): Promise<void> {
await this._updateRunSingle(run);
}

async onChainError(run: Run): Promise<void> {
await this._updateRunSingle(run);
}

async onToolStart(run: Run): Promise<void> {
await this._persistRunSingle(run);
}

async onToolEnd(run: Run): Promise<void> {
await this._updateRunSingle(run);
}

async onToolError(run: Run): Promise<void> {
await this._updateRunSingle(run);
}
}
Loading