Skip to content

Commit

Permalink
feat: few shot example filtering in js (#975)
Browse files Browse the repository at this point in the history
  • Loading branch information
jakerachleff authored Sep 5, 2024
1 parent 97b6b9f commit ef88ac7
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
4 changes: 2 additions & 2 deletions js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "langsmith",
"version": "0.1.50",
"version": "0.1.51",
"description": "Client library to connect to the LangSmith LLM Tracing and Evaluation Platform.",
"packageManager": "[email protected]",
"files": [
Expand Down Expand Up @@ -276,4 +276,4 @@
},
"./package.json": "./package.json"
}
}
}
20 changes: 18 additions & 2 deletions js/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2226,6 +2226,13 @@ export class Client {
* similar examples in order of most similar to least similar. If no similar
* examples are found, random examples will be returned.
*
* @param filter A filter string to apply to the search. Only examples will be returned that
* match the filter string. Some examples of filters
*
* - eq(metadata.mykey, "value")
* - and(neq(metadata.my.nested.key, "value"), neq(metadata.mykey, "value"))
* - or(eq(metadata.mykey, "value"), eq(metadata.mykey, "othervalue"))
*
* @returns A list of similar examples.
*
*
Expand All @@ -2238,13 +2245,22 @@ export class Client {
public async similarExamples(
inputs: KVMap,
datasetId: string,
limit: number
limit: number,
{
filter,
}: {
filter?: string;
} = {}
): Promise<ExampleSearch[]> {
const data = {
const data: KVMap = {
limit: limit,
inputs: inputs,
};

if (filter !== undefined) {
data["filter"] = filter;
}

assertUuid(datasetId);
const response = await this.caller.call(
fetch,
Expand Down
2 changes: 1 addition & 1 deletion js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ export type {
export { RunTree, type RunTreeConfig } from "./run_trees.js";

// Update using yarn bump-version
export const __version__ = "0.1.50";
export const __version__ = "0.1.51";
15 changes: 14 additions & 1 deletion js/src/tests/few_shot.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { v4 as uuidv4 } from "uuid";

const TESTING_DATASET_NAME = `test_dataset_few_shot_js_${uuidv4()}`;

test("evaluate can evaluate", async () => {
test("few shot search", async () => {
const client = new Client();

const schema: KVMap = {
Expand Down Expand Up @@ -33,6 +33,7 @@ test("evaluate can evaluate", async () => {
const res = await client.createExamples({
inputs: [{ name: "foo" }, { name: "bar" }],
outputs: [{ output: 2 }, { output: 3 }],
metadata: [{ somekey: "somevalue" }, { somekey: "someothervalue" }],
datasetName: TESTING_DATASET_NAME,
});
if (res.length !== 2) {
Expand Down Expand Up @@ -62,4 +63,16 @@ test("evaluate can evaluate", async () => {
expect(examples.length).toBe(2);
expect(examples[0].inputs).toEqual({ name: "foo" });
expect(examples[1].inputs).toEqual({ name: "bar" });

const filtered_examples = await client.similarExamples(
{ name: "foo" },
dataset.id,
1,
{
filter: "eq(metadata.somekey, 'somevalue')",
}
);

expect(filtered_examples.length).toBe(1);
expect(filtered_examples[0].inputs).toEqual({ name: "foo" });
});

0 comments on commit ef88ac7

Please sign in to comment.