Skip to content

Commit

Permalink
add config support for hiding inputs and outputs (#378)
Browse files Browse the repository at this point in the history
Currently it's not possible to specify these options for specific
invocations. This will allow better customization.

Please let me know your thoughts.

Example Usage:
```
import { Client } from "langsmith";
import { LangChainTracer } from "@langchain/core/tracers/tracer_langchain";
import { ChatOpenAI } from "@langchain/openai";

const callbacks = [
  new LangChainTracer({
    projectName: process.env.LANGCHAIN_PROJECT,
    client: new Client({
      hideInputs: false,
      hideOutputs: true,
    }),
  }),
];
const llm = new ChatOpenAI({});
await llm.invoke("Hello, world!", { callbacks });

Co-authored-by: William FH <[email protected]>
  • Loading branch information
h4r5h4 and hinthornw authored Feb 1, 2024
1 parent 0386d55 commit dc8ed85
Showing 1 changed file with 34 additions and 18 deletions.
52 changes: 34 additions & 18 deletions js/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ interface ClientConfig {
callerOptions?: AsyncCallerParams;
timeout_ms?: number;
webUrl?: string;
hideInputs?: boolean;
hideOutputs?: boolean;
}

interface ListRunsParams {
Expand Down Expand Up @@ -158,20 +160,6 @@ function trimQuotes(str?: string): string | undefined {
.replace(/^'(.*)'$/, "$1");
}

function hideInputs(inputs: KVMap): KVMap {
if (getEnvironmentVariable("LANGCHAIN_HIDE_INPUTS") === "true") {
return {};
}
return inputs;
}

function hideOutputs(outputs: KVMap): KVMap {
if (getEnvironmentVariable("LANGCHAIN_HIDE_OUTPUTS") === "true") {
return {};
}
return outputs;
}

function assertUuid(str: string): void {
if (!uuid.validate(str)) {
throw new Error(`Invalid UUID: ${str}`);
Expand All @@ -191,6 +179,10 @@ export class Client {

private _tenantId: string | null = null;

private hideInputs?: boolean;

private hideOutputs?: boolean;

constructor(config: ClientConfig = {}) {
const defaultConfig = Client.getDefaultClientConfig();

Expand All @@ -200,21 +192,31 @@ export class Client {
this.validateApiKeyIfHosted();
this.timeout_ms = config.timeout_ms ?? 12_000;
this.caller = new AsyncCaller(config.callerOptions ?? {});
this.hideInputs = config.hideInputs ?? defaultConfig.hideInputs;
this.hideOutputs = config.hideOutputs ?? defaultConfig.hideOutputs;
}

public static getDefaultClientConfig(): {
apiUrl: string;
apiKey?: string;
webUrl?: string;
hideInputs?: boolean;
hideOutputs?: boolean;
} {
const apiKey = getEnvironmentVariable("LANGCHAIN_API_KEY");
const apiUrl =
getEnvironmentVariable("LANGCHAIN_ENDPOINT") ??
"https://api.smith.langchain.com";
const hideInputs =
getEnvironmentVariable("LANGCHAIN_HIDE_INPUTS") === "true";
const hideOutputs =
getEnvironmentVariable("LANGCHAIN_HIDE_OUTPUTS") === "true";
return {
apiUrl: apiUrl,
apiKey: apiKey,
webUrl: undefined,
hideInputs: hideInputs,
hideOutputs: hideOutputs,
};
}

Expand Down Expand Up @@ -258,6 +260,20 @@ export class Client {
return headers;
}

private processInputs(inputs: KVMap): KVMap {
if (this.hideInputs) {
return {};
}
return inputs;
}

private processOutputs(outputs: KVMap): KVMap {
if (this.hideOutputs) {
return {};
}
return outputs;
}

private async _getResponse(
path: string,
queryParams?: URLSearchParams
Expand Down Expand Up @@ -377,9 +393,9 @@ export class Client {
},
},
};
runCreate.inputs = hideInputs(runCreate.inputs);
runCreate.inputs = this.processInputs(runCreate.inputs);
if (runCreate.outputs) {
runCreate.outputs = hideOutputs(runCreate.outputs);
runCreate.outputs = this.processOutputs(runCreate.outputs);
}

const response = await this.caller.call(fetch, `${this.apiUrl}/runs`, {
Expand All @@ -394,11 +410,11 @@ export class Client {
public async updateRun(runId: string, run: RunUpdate): Promise<void> {
assertUuid(runId);
if (run.inputs) {
run.inputs = hideInputs(run.inputs);
run.inputs = this.processInputs(run.inputs);
}

if (run.outputs) {
run.outputs = hideOutputs(run.outputs);
run.outputs = this.processOutputs(run.outputs);
}
const headers = { ...this.headers, "Content-Type": "application/json" };
const response = await this.caller.call(
Expand Down

0 comments on commit dc8ed85

Please sign in to comment.