-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
27fe0ff
commit 63ada8e
Showing
2 changed files
with
85 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { z } from "zod"; | ||
import { zodToJsonSchema } from "zod-to-json-schema"; | ||
import { RunnableLambda, RunnableToolLike } from "../base.js"; | ||
|
||
test("Runnable asTool works", async () => { | ||
const schema = z.object({ | ||
foo: z.string(), | ||
}); | ||
const runnable = RunnableLambda.from<z.infer<typeof schema>, string>( | ||
(input, config) => { | ||
return `${input.foo}${config?.configurable.foo}`; | ||
} | ||
); | ||
const tool = runnable.asTool({ | ||
schema, | ||
}); | ||
|
||
expect(tool).toBeInstanceOf(RunnableToolLike); | ||
expect(tool.schema).toBe(schema); | ||
expect(tool.description).toBe( | ||
`Takes ${JSON.stringify(zodToJsonSchema(schema), null, 2)}` | ||
); | ||
expect(tool.name).toBe(runnable.getName()); | ||
}); | ||
|
||
test("Runnable asTool works with all populated fields", async () => { | ||
const schema = z.object({ | ||
foo: z.string(), | ||
}); | ||
const runnable = RunnableLambda.from<z.infer<typeof schema>, string>( | ||
(input, config) => { | ||
return `${input.foo}${config?.configurable.foo}`; | ||
} | ||
); | ||
const tool = runnable.asTool({ | ||
schema, | ||
name: "test", | ||
description: "test", | ||
}); | ||
|
||
expect(tool).toBeInstanceOf(RunnableToolLike); | ||
expect(tool.schema).toBe(schema); | ||
expect(tool.description).toBe("test"); | ||
expect(tool.name).toBe("test"); | ||
}); | ||
|
||
test("Runnable asTool can invoke", async () => { | ||
const schema = z.object({ | ||
foo: z.string(), | ||
}); | ||
const runnable = RunnableLambda.from<z.infer<typeof schema>, string>( | ||
(input, config) => { | ||
console.log("I am invoked."); | ||
return `${input.foo}${config?.configurable.foo}`; | ||
} | ||
); | ||
const tool = runnable.asTool({ | ||
schema, | ||
}); | ||
|
||
const toolResponse = await tool.invoke( | ||
{ | ||
foo: "bar", | ||
}, | ||
{ | ||
configurable: { | ||
foo: "bar", | ||
}, | ||
} | ||
); | ||
|
||
expect(toolResponse).toBe("barbar"); | ||
}); |