Skip to content

Commit

Permalink
Include agent schemas in deployment manifests
Browse files Browse the repository at this point in the history
  • Loading branch information
xiv-bazed-ai committed Feb 26, 2024
1 parent 064f242 commit 079f7eb
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
27 changes: 24 additions & 3 deletions bin/bazed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import FormData from "form-data";
import { SingleBar } from "cli-progress";
import tar from "tar";
import moment from "moment";
import { getToolInterface } from "../src/tool";
import zodToJsonSchema from "zod-to-json-schema";

dotenv.config();

Expand Down Expand Up @@ -352,7 +354,22 @@ const scanForAgents = async (path: string): Promise<Record<string, any>> => {
const constructors = constructorsFromModule(module);
const agents: Record<string, any> = {};
for (const constructor of constructors) {
agents[constructor.name] = { input: {}, output: {}, state: {} };
const toolInterface = getToolInterface(constructor);
if (toolInterface) {
agents[constructor.name] = {
description: toolInterface.description,
input: zodToJsonSchema(toolInterface.args),
output: zodToJsonSchema(toolInterface.returns),
state: {},
};
} else {
agents[constructor.name] = {
description: "",
input: {},
output: {},
state: {},
};
}
}
return agents;
};
Expand Down Expand Up @@ -555,14 +572,18 @@ program
console.log("\tCost per model:");
for (const model in response.data.session.cost) {
console.log(
`\t\t${model}: $${response.data.session.cost[model as ModelType].toFixed(2)}`,
`\t\t${model}: $${response.data.session.cost[
model as ModelType
].toFixed(2)}`,
chalk.gray(`(${response.data.session.cost[model as ModelType]})`)
);
}
console.log("\tTokens per model:");
for (const model in response.data.session.tokens) {
console.log(
`\t\t${model}: ${response.data.session.tokens[model as ModelType]}`
`\t\t${model}: ${
response.data.session.tokens[model as ModelType]
}`
);
}
}
Expand Down
33 changes: 32 additions & 1 deletion src/tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,21 @@ export class FunctionTool<Args, Returns> implements Tool {
}
}

interface ToolInterface {
name: string;
description: string;
args: z.ZodType<any>;
returns: z.ZodType<any>;
}

interface ToolableAgentConstructor extends Constructor<Agent> {
toolInterface?: ToolInterface;
asTool?: Tool;
}

/** Toolable is a mixin for agent classes that can be used as tools. */
export interface Toolable {
toolInterface?: ToolInterface;
asTool?: Tool;
}

Expand All @@ -122,6 +131,21 @@ export const toTool = (toolLike: ToolLike): Tool => {
}
};

/** Get the tool interface of a toolable.
* @param toolLike
* @returns Tool interface or undefined
*/
export const getToolInterface = (
toolLike: ToolLike
): ToolInterface | undefined => {
if ("toolInterface" in toolLike) {
if (toolLike.toolInterface) {
return toolLike.toolInterface;
}
}
return undefined;
};

/** Decorator for agent classes that can be used as tools.
* @param name Name of the tool
* @param args Zod schema for parameters of the tool
Expand All @@ -134,7 +158,14 @@ export const isTool = <A, R>(
returns: z.ZodType<R>
) =>
function (constructor: Constructor<Agent>, _context: any) {
(constructor as ToolableAgentConstructor).asTool = new FunctionTool(
const c = constructor as ToolableAgentConstructor;
c.toolInterface = {
name,
description,
args,
returns,
};
c.asTool = new FunctionTool(
name,
description,
args,
Expand Down

0 comments on commit 079f7eb

Please sign in to comment.