Skip to content

Commit

Permalink
fix: inputSchema as a function
Browse files Browse the repository at this point in the history
  • Loading branch information
grahamwhiteuk committed Aug 30, 2024
1 parent 7e8f4a0 commit 6b14b98
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 22 deletions.
12 changes: 7 additions & 5 deletions docs/tools.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@ Tools MUST do the following:

- Declare an input schema:

This is used to define the format of the input to your tool. The agent will formalise the natural language input(s) it has received and structure them into the fields described in the tool's input. The input schema can be specified using [Zod](https://github.com/colinhacks/zod) (recommended) or JSONSchema and must be a function (either sync or async). Zod effects (e.g. `z.object().transform(...)`) are not supported. The `inputSchema` definition must always be an object and pass validation by the `validateSchema()` function defined in [schema.ts](../src/internals/helpers/schema.ts).
This is used to define the format of the input to your tool. The agent will formalise the natural language input(s) it has received and structure them into the fields described in the tool's input. The input schema can be specified using [Zod](https://github.com/colinhacks/zod) (recommended) or JSONSchema. It must be a function (either sync or async). Zod effects (e.g. `z.object().transform(...)`) are not supported. The return value of `inputSchema` must always be an object and pass validation by the `validateSchema()` function defined in [schema.ts](../src/internals/helpers/schema.ts).

```typescript
// any Zod definition is good here, this is typical simple example
inputSchema = z.object({
// list of key-value pairs
});
inputSchema() {
// any Zod definition is good here, this is typical simple example
return z.object({
// list of key-value pairs
});
}
```

- Implement initialisation:
Expand Down
24 changes: 16 additions & 8 deletions examples/tools/helloWorld.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { BaseToolOptions, BaseToolRunOptions, StringToolOutput, Tool } from "@/tools/base.js";
import {
BaseToolOptions,
BaseToolRunOptions,
StringToolOutput,
Tool,
ToolInput,
} from "@/tools/base.js";
import { z } from "zod";

type ToolOptions = BaseToolOptions;
Expand All @@ -8,17 +14,19 @@ export class HelloWorldTool extends Tool<StringToolOutput, ToolOptions, ToolRunO
name = "HelloWorld";
description = "Says hello when asked for a special greeting.";

inputSchema = z.object({
identifier: z
.string()
.describe("The identifier (person, object, animal, etc.) used to when saying Hello"),
});
inputSchema() {
return z.object({
identifier: z
.string()
.describe("The identifier (person, object, animal, etc.) used to when saying Hello"),
});
}

static {
this.register();
}

protected async _run(input: string): Promise<StringToolOutput> {
return new StringToolOutput(`Hello, ${input}`);
protected async _run(input: ToolInput<this>): Promise<StringToolOutput> {
return new StringToolOutput(`Hello, ${input.identifier}`);
}
}
22 changes: 13 additions & 9 deletions examples/tools/openLibrary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,19 @@ export class OpenLibraryTool extends Tool<OpenLibraryToolOutput, ToolOptions, To
description =
"Provides access to a library of books with information about book titles, authors, contributors, publication dates, publisher and isbn.";

inputSchema = z.object({
title: z.string().optional(),
author: z.string().optional(),
isbn: z.string().optional(),
subject: z.string().optional(),
place: z.string().optional(),
person: z.string().optional(),
publisher: z.string().optional(),
});
inputSchema() {
return z
.object({
title: z.string(),
author: z.string(),
isbn: z.string(),
subject: z.string(),
place: z.string(),
person: z.string(),
publisher: z.string(),
})
.partial();
}

static {
this.register();
Expand Down

0 comments on commit 6b14b98

Please sign in to comment.