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

traceable: Add option to configure which args are sent as inputs to langsmith #643

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion js/src/tests/traceable.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ test.concurrent("Test traceable wrapper with async error thrown", async () => {

expect(collectedRun).not.toBeNull();
expect(collectedRun!.error).toEqual("Error: I am bad");
expect(collectedRun!.inputs).toEqual({ args: ["testing", 9] });
expect(collectedRun!.inputs).toEqual({ 0: "testing", 1: 9 });
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this is likely a breaking change for runOnDataset in LangChain

await waitUntilRunFound(langchainClient, runId);
const storedRun = await langchainClient.readRun(runId);
expect(storedRun.id).toEqual(runId);
Expand Down
16 changes: 16 additions & 0 deletions js/src/tests/wrapped_openai.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,22 @@ test.concurrent("chat completions with tool calling", async () => {
expect(JSON.parse((call[2] as any).body).extra.metadata).toEqual({
thing1: "thing2",
});
if ((call[2] as any)["method"] === "POST") {
expect(JSON.parse((call[2] as any).body).inputs).toEqual({
messages: [
{ role: "user", content: `What is the current weather in SF?` },
],
temperature: 0,
seed: 42,
model: "gpt-3.5-turbo",
tools: toolDefinition,
tool_choice: {
type: "function",
function: { name: "get_current_weather" },
},
stream: true,
});
}
}
callSpy.mockClear();
});
Expand Down
23 changes: 20 additions & 3 deletions js/src/traceable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,19 @@ export function traceable<Func extends (...args: any[]) => any>(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
aggregator?: (args: any[]) => any;
argsConfigPath?: [number] | [number, string];
argsInputPath?: [number];
tracingEnabled?: boolean;
}
) {
type Inputs = Parameters<Func>;
type Output = ReturnType<Func>;
const { aggregator, argsConfigPath, tracingEnabled, ...runTreeConfig } =
config ?? {};
const {
aggregator,
argsConfigPath,
argsInputPath,
tracingEnabled,
...runTreeConfig
} = config ?? {};

const traceableFunc = (
...args: Inputs | [RunTreeLike, ...Inputs] | [RunnableConfigLike, ...Inputs]
Expand Down Expand Up @@ -228,8 +234,18 @@ export function traceable<Func extends (...args: any[]) => any>(
const firstInput = rawInputs[0];
if (firstInput == null) {
inputs = {};
} else if (argsInputPath) {
const [idx] = argsInputPath;
if (idx <= rawInputs.length && isKVMap(rawInputs[idx])) {
inputs = rawInputs[idx];
} else {
inputs = { input: rawInputs[idx] };
}
} else if (rawInputs.length > 1) {
inputs = { args: rawInputs };
inputs = {};
for (let i = 0; i < rawInputs.length; i++) {
inputs[i] = rawInputs[i];
}
} else if (isKVMap(firstInput)) {
inputs = firstInput;
} else {
Expand Down Expand Up @@ -469,6 +485,7 @@ function isKVMap(x: unknown): x is Record<string, unknown> {
}

export function wrapFunctionAndEnsureTraceable<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Func extends (...args: any[]) => any
>(target: Func, options: Partial<RunTreeConfig>, name = "target") {
if (typeof target === "function") {
Expand Down
2 changes: 2 additions & 0 deletions js/src/wrappers/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ export const wrapOpenAI = <T extends OpenAIType>(
name: "ChatOpenAI",
run_type: "llm",
aggregator: chatAggregator,
argsInputPath: [0],
argsConfigPath: [1, "langsmithExtra"],
...options,
}
Expand All @@ -228,6 +229,7 @@ export const wrapOpenAI = <T extends OpenAIType>(
name: "OpenAI",
run_type: "llm",
aggregator: textAggregator,
argsInputPath: [0],
argsConfigPath: [1, "langsmithExtra"],
...options,
}
Expand Down