Skip to content

Commit

Permalink
Filter for ts
Browse files Browse the repository at this point in the history
  • Loading branch information
hinthornw committed Nov 3, 2023
1 parent 6d53e81 commit cb3333c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
33 changes: 33 additions & 0 deletions js/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,13 @@ function hideOutputs(outputs: KVMap): KVMap {
}
return outputs;
}

function assertUuid(str: string): void {
if (!uuid.validate(str)) {
throw new Error(`Invalid UUID: ${str}`);
}
}

export class Client {
private apiKey?: string;

Expand Down Expand Up @@ -331,6 +338,7 @@ export class Client {
}

public async updateRun(runId: string, run: RunUpdate): Promise<void> {
assertUuid(runId);
if (run.inputs) {
run.inputs = hideInputs(run.inputs);
}
Expand All @@ -356,6 +364,7 @@ export class Client {
runId: string,
{ loadChildRuns }: { loadChildRuns: boolean } = { loadChildRuns: false }
): Promise<Run> {
assertUuid(runId);
let run = await this._get<Run>(`/runs/${runId}`);
if (loadChildRuns && run.child_run_ids) {
run = await this._loadChildRuns(run);
Expand Down Expand Up @@ -509,6 +518,7 @@ export class Client {
run_id: runId,
share_token: shareId || uuid.v4(),
};
assertUuid(runId);
const response = await this.caller.call(
fetch,
`${this.apiUrl}/runs/${runId}/share`,
Expand All @@ -527,6 +537,7 @@ export class Client {
}

public async unshareRun(runId: string): Promise<void> {
assertUuid(runId);
const response = await this.caller.call(
fetch,
`${this.apiUrl}/runs/${runId}/share`,
Expand All @@ -540,6 +551,7 @@ export class Client {
}

public async readRunSharedLink(runId: string): Promise<string | undefined> {
assertUuid(runId);
const response = await this.caller.call(
fetch,
`${this.apiUrl}/runs/${runId}/share`,
Expand Down Expand Up @@ -572,6 +584,7 @@ export class Client {
queryParams.append("id", runId);
}
}
assertUuid(shareToken);
const response = await this.caller.call(
fetch,
`${this.apiUrl}/public/${shareToken}/runs${queryParams}`,
Expand All @@ -596,6 +609,7 @@ export class Client {
const dataset = await this.readDataset({ datasetName });
datasetId = dataset.id;
}
assertUuid(datasetId);
const response = await this.caller.call(
fetch,
`${this.apiUrl}/datasets/${datasetId}/share`,
Expand Down Expand Up @@ -626,6 +640,7 @@ export class Client {
const data = {
dataset_id: datasetId,
};
assertUuid(datasetId);
const response = await this.caller.call(
fetch,
`${this.apiUrl}/datasets/${datasetId}/share`,
Expand All @@ -644,6 +659,7 @@ export class Client {
}

public async unshareDataset(datasetId: string): Promise<void> {
assertUuid(datasetId);
const response = await this.caller.call(
fetch,
`${this.apiUrl}/datasets/${datasetId}/share`,
Expand All @@ -657,6 +673,7 @@ export class Client {
}

public async readSharedDataset(shareToken: string): Promise<Dataset> {
assertUuid(shareToken);
const response = await this.caller.call(
fetch,
`${this.apiUrl}/public/${shareToken}/datasets`,
Expand Down Expand Up @@ -719,6 +736,7 @@ export class Client {
if (projectId !== undefined && projectName !== undefined) {
throw new Error("Must provide either projectName or projectId, not both");
} else if (projectId !== undefined) {
assertUuid(projectId);
path += `/${projectId}`;
} else if (projectName !== undefined) {
params.append("name", projectName);
Expand Down Expand Up @@ -822,6 +840,7 @@ export class Client {
} else {
projectId_ = projectId;
}
assertUuid(projectId_);
const response = await this.caller.call(
fetch,
`${this.apiUrl}/sessions/${projectId_}`,
Expand Down Expand Up @@ -935,6 +954,7 @@ export class Client {
if (datasetId !== undefined && datasetName !== undefined) {
throw new Error("Must provide either datasetName or datasetId, not both");
} else if (datasetId !== undefined) {
assertUuid(datasetId);
path += `/${datasetId}`;
} else if (datasetName !== undefined) {
params.append("name", datasetName);
Expand Down Expand Up @@ -1030,6 +1050,7 @@ export class Client {
datasetId_ = dataset.id;
}
if (datasetId_ !== undefined) {
assertUuid(datasetId_);
path += `/${datasetId_}`;
} else {
throw new Error("Must provide datasetName or datasetId");
Expand Down Expand Up @@ -1118,6 +1139,7 @@ export class Client {
}

public async readExample(exampleId: string): Promise<Example> {
assertUuid(exampleId);
const path = `/examples/${exampleId}`;
return await this._get<Example>(path);
}
Expand Down Expand Up @@ -1157,6 +1179,7 @@ export class Client {
}

public async deleteExample(exampleId: string): Promise<void> {
assertUuid(exampleId);
const path = `/examples/${exampleId}`;
const response = await this.caller.call(fetch, this.apiUrl + path, {
method: "DELETE",
Expand All @@ -1175,6 +1198,7 @@ export class Client {
exampleId: string,
update: ExampleUpdate
): Promise<object> {
assertUuid(exampleId);
const response = await this.caller.call(
fetch,
`${this.apiUrl}/examples/${exampleId}`,
Expand Down Expand Up @@ -1268,6 +1292,12 @@ export class Client {
) {
feedback_source.metadata["__run"] = { run_id: sourceRunId };
}
if (
feedback_source?.metadata !== undefined &&
feedback_source.metadata["__run"]?.run_id !== undefined
) {
assertUuid(feedback_source.metadata["__run"].run_id);
}
const feedback: FeedbackCreate = {
id: feedbackId ?? uuid.v4(),
run_id: runId,
Expand Down Expand Up @@ -1316,6 +1346,7 @@ export class Client {
if (comment !== undefined && comment !== null) {
feedbackUpdate["comment"] = comment;
}
assertUuid(feedbackId);
const response = await this.caller.call(
fetch,
`${this.apiUrl}/feedback/${feedbackId}`,
Expand All @@ -1330,12 +1361,14 @@ export class Client {
}

public async readFeedback(feedbackId: string): Promise<Feedback> {
assertUuid(feedbackId);
const path = `/feedback/${feedbackId}`;
const response = await this._get<Feedback>(path);
return response;
}

public async deleteFeedback(feedbackId: string): Promise<void> {
assertUuid(feedbackId);
const path = `/feedback/${feedbackId}`;
const response = await this.caller.call(fetch, this.apiUrl + path, {
method: "DELETE",
Expand Down
8 changes: 5 additions & 3 deletions js/src/tests/client.int.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Client } from "../client.js";
import { Dataset, Feedback, Run } from "../schemas.js";
import { FunctionMessage, HumanMessage } from "langchain/schema";
import { RunTree, RunTreeConfig } from "../run_trees.js";

import { Client } from "../client.js";
import { StringEvaluator } from "../evaluation/string_evaluator.js";
import { Dataset, Feedback, Run } from "../schemas.js";
import { v4 as uuidv4 } from "uuid";
import { HumanMessage, FunctionMessage } from "langchain/schema";

async function toArray<T>(iterable: AsyncIterable<T>): Promise<T[]> {
const result: T[] = [];
for await (const item of iterable) {
Expand Down
5 changes: 5 additions & 0 deletions python/langsmith/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2034,6 +2034,11 @@ def create_feedback(
)
if source_run_id is not None and "__run" not in feedback_source.metadata:
feedback_source.metadata["__run"] = {"run_id": str(source_run_id)}
if feedback_source.metadata and "__run" in feedback_source.metadata:
# Validate that the linked run ID is a valid UUID
feedback_source.metadata["__run"]["run_id"] = str(
_as_uuid(feedback_source.metadata["__run"]["run_id"])
)
feedback = ls_schemas.FeedbackCreate(
id=feedback_id or uuid.uuid4(),
run_id=run_id,
Expand Down

0 comments on commit cb3333c

Please sign in to comment.