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): throw when wrapping same client twice, allow passing name in argsConfigExtra #663

Merged
merged 4 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions js/src/run_trees.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ export interface RunTreeConfig {
}

export interface RunnableConfigLike {
/**
* Name of the traced run, overriding the name provided by the traceable
* function.
*/
name?: string;
dqbd marked this conversation as resolved.
Show resolved Hide resolved
/**
* Tags for this call and any sub-calls (eg. a Chain calling an LLM).
* You can use these to filter calls.
Expand Down
70 changes: 68 additions & 2 deletions js/src/tests/traceable.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { RunTree } from "../run_trees.js";
import { ROOT, traceable } from "../traceable.js";
import type { RunTree, RunnableConfigLike } from "../run_trees.js";
import { ROOT, RunTreeLike, traceable } from "../traceable.js";
import { getAssumedTreeFromCalls } from "./utils/tree.js";
import { mockClient } from "./utils/mock_client.js";
import { FakeChatModel } from "@langchain/core/utils/testing";
Expand Down Expand Up @@ -441,3 +441,69 @@ describe("langchain", () => {
});
});
});

test("metadata", async () => {
const { client, callSpy } = mockClient();
const main = traceable(async (): Promise<number> => 42, {
client,
name: "main",
metadata: { customValue: "hello" },
tracingEnabled: true,
});

await main();

expect(getAssumedTreeFromCalls(callSpy.mock.calls)).toMatchObject({
nodes: ["main:0"],
edges: [],
data: {
"main:0": {
extra: { metadata: { customValue: "hello" } },
outputs: { outputs: 42 },
},
},
});
});

test("argsConfigPath", async () => {
const { client, callSpy } = mockClient();
const main = traceable(
async (
value: number,
options: {
suffix: string;
langsmithExtra?: RunTreeLike | RunnableConfigLike;
}
): Promise<string> => `${value}${options.suffix}`,
{
client,
name: "main",
argsConfigPath: [1, "langsmithExtra"],
tracingEnabled: true,
}
);

await main(1, {
suffix: "hello",
langsmithExtra: {
name: "renamed",
tags: ["tag1", "tag2"],
metadata: { customValue: "hello" },
},
});

expect(getAssumedTreeFromCalls(callSpy.mock.calls)).toMatchObject({
nodes: ["renamed:0"],
edges: [],
data: {
"renamed:0": {
extra: { metadata: { customValue: "hello" } },
tags: ["tag1", "tag2"],
inputs: {
args: [1, { suffix: "hello" }],
},
outputs: { outputs: "1hello" },
},
},
});
});
68 changes: 68 additions & 0 deletions js/src/tests/wrapped_openai.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { jest } from "@jest/globals";
import { OpenAI } from "openai";
import { wrapOpenAI } from "../wrappers/index.js";
import { Client } from "../client.js";
import { mockClient } from "./utils/mock_client.js";
import { getAssumedTreeFromCalls } from "./utils/tree.js";

test("wrapOpenAI should return type compatible with OpenAI", async () => {
let originalClient = new OpenAI();
Expand Down Expand Up @@ -462,3 +464,69 @@ test.skip("no tracing with env var unset", async () => {
expect(patched).toBeDefined();
console.log(patched);
});

test("wrapping same instance", async () => {
const wrapped = wrapOpenAI(new OpenAI());
expect(() => wrapOpenAI(wrapped)).toThrowError(
"This instance of OpenAI client has been already wrapped once."
);
});

test("chat.concurrent extra name", async () => {
const { client, callSpy } = mockClient();

const openai = wrapOpenAI(new OpenAI(), {
client,
});

await openai.chat.completions.create(
{
messages: [{ role: "user", content: `Say 'red'` }],
temperature: 0,
seed: 42,
model: "gpt-3.5-turbo",
},
{ langsmithExtra: { name: "red", metadata: { customKey: "red" } } }
);

const stream = await openai.chat.completions.create(
{
messages: [{ role: "user", content: `Say 'green'` }],
temperature: 0,
seed: 42,
model: "gpt-3.5-turbo",
stream: true,
},
{ langsmithExtra: { name: "green", metadata: { customKey: "green" } } }
);

// eslint-disable-next-line @typescript-eslint/no-unused-vars
for await (const _ of stream) {
// pass
}

expect(getAssumedTreeFromCalls(callSpy.mock.calls)).toMatchObject({
nodes: ["red:0", "green:1"],
edges: [],
data: {
"red:0": {
name: "red",
extra: { metadata: { customKey: "red" } },
outputs: {
choices: [
{ index: 0, message: { role: "assistant", content: "Red" } },
],
},
},
"green:1": {
name: "green",
extra: { metadata: { customKey: "green" } },
outputs: {
choices: [
{ index: 0, message: { role: "assistant", content: "Green" } },
],
},
},
},
});
});
15 changes: 14 additions & 1 deletion js/src/wrappers/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import { OpenAI } from "openai";
import type { APIPromise } from "openai/core";
import type { Client, RunTreeConfig } from "../index.js";
import { type RunnableConfigLike } from "../run_trees.js";
import { traceable, type RunTreeLike } from "../traceable.js";
import {
isTraceableFunction,
traceable,
type RunTreeLike,
} from "../traceable.js";

// Extra leniency around types in case multiple OpenAI SDK versions get installed
type OpenAIType = {
Expand Down Expand Up @@ -211,6 +215,15 @@ export const wrapOpenAI = <T extends OpenAIType>(
openai: T,
options?: Partial<RunTreeConfig>
): PatchedOpenAIClient<T> => {
if (
isTraceableFunction(openai.chat.completions.create) ||
isTraceableFunction(openai.completions.create)
) {
throw new Error(
"This instance of OpenAI client has already been wrapped once."
);
}

openai.chat.completions.create = traceable(
openai.chat.completions.create.bind(openai.chat.completions),
{
Expand Down
Loading