Skip to content

Commit

Permalink
Add object generator tool.
Browse files Browse the repository at this point in the history
  • Loading branch information
lgrammel committed Jan 29, 2024
1 parent 2e0afff commit 57a65a5
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 0 deletions.
File renamed without changes.
93 changes: 93 additions & 0 deletions examples/basic/src/tool/run-tool-example-2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import {
ObjectGeneratorTool,
createInstructionPrompt,
jsonObjectPrompt,
jsonToolCallPrompt,
llamacpp,
runTool,
zodSchema,
} from "modelfusion";
import { z } from "zod";

const enemyGenerator = new ObjectGeneratorTool({
name: "enemies",
description: "Generates a list of enemies.",

parameters: zodSchema(
z.object({
type: z
.string()
.describe("The type of the enemy, e.g. human, orc, elf..."),
numberOfOpponents: z.number(),
location: z.string().describe("The location of the encounter."),
})
),

objectSchema: zodSchema(
z.array(
z.object({
name: z.string(),
species: z
.string()
.describe(
"The species of the enemy, e.g. human, orc, elf, wolf, bear..."
),
class: z
.string()
.describe("Character class, e.g. warrior, mage, thief...")
.optional(),
description: z.string().describe("How the character looks like."),
weapon: z
.string()
.optional()
.describe("The weapon the character uses. Optional."),
})
)
),

model: llamacpp
.CompletionTextGenerator({
// run https://huggingface.co/TheBloke/Mixtral-8x7B-Instruct-v0.1-GGUF with llama.cpp
promptTemplate: llamacpp.prompt.ChatML,
temperature: 0.5,
})
.asObjectGenerationModel(jsonObjectPrompt.instruction()),

prompt: createInstructionPrompt(
async ({ type, numberOfOpponents, location }) => ({
system:
"You generate enemies for heroes in a fantasy role-playing game set in a medieval fantasy world. " +
"The list of enemies should be limited to a single encounter. " +
"The enemy group must be consistent, i.e. it must make sense for the enemies to appear together.",

instruction: `Generate ${numberOfOpponents} ${type} enemies that the heroes encounter in ${location}.`,
})
),
});

async function main() {
const { tool, toolCall, args, ok, result } = await runTool({
model: llamacpp
.CompletionTextGenerator({
// run https://huggingface.co/TheBloke/Mixtral-8x7B-Instruct-v0.1-GGUF with llama.cpp
promptTemplate: llamacpp.prompt.ChatML,
temperature: 0.7,
})
.withInstructionPrompt()
.asToolCallGenerationModel(jsonToolCallPrompt.text()),

tool: enemyGenerator,
prompt:
"The heros enter a dark cave. They hear a noise. They see something moving in the shadows.",
// "The heros enter the backroom of the tavern. They see a group of people sitting at a table.",
// "The heros are resting in the forest. They hear a noise. They see something moving between the trees.",
});

console.log(`Tool call:`, toolCall);
console.log(`Tool:`, tool);
console.log(`Arguments:`, args);
console.log(`Ok:`, ok);
console.log(`Result or Error:`, result);
}

main().catch(console.error);
48 changes: 48 additions & 0 deletions packages/modelfusion/src/tool/ObjectGeneratorTool.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import {
ObjectGenerationModel,
ObjectGenerationModelSettings,
} from "../model-function/generate-object/ObjectGenerationModel";
import { Tool } from "./Tool";
import { JsonSchemaProducer } from "../core/schema/JsonSchemaProducer";
import { PromptFunction } from "../core/PromptFunction";
import { Schema } from "../core/schema/Schema";
import { generateObject } from "../model-function/generate-object/generateObject";

/**
* A tool that generates an object. You can configure it with a model, an input, an output schema, and a prompt.
*/
export class ObjectGeneratorTool<
NAME extends string,
PROMPT,
PARAMETERS,
OBJECT,
> extends Tool<NAME, PARAMETERS, OBJECT> {
constructor({
name = "object-generator" as any, // eslint-disable-line @typescript-eslint/no-explicit-any
description,
model,
parameters,
objectSchema,
prompt,
}: {
name?: NAME;
description?: string;
model: ObjectGenerationModel<PROMPT, ObjectGenerationModelSettings>;
parameters: Schema<PARAMETERS> & JsonSchemaProducer;
objectSchema: Schema<OBJECT> & JsonSchemaProducer;
prompt: (input: PARAMETERS) => PromptFunction<PARAMETERS, PROMPT>;
}) {
super({
name,
description,
parameters,
execute: async (input, options) =>
generateObject({
model,
schema: objectSchema,
prompt: prompt(input),
...options,
}),
});
}
}
1 change: 1 addition & 0 deletions packages/modelfusion/src/tool/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from "./NoSuchToolDefinitionError";
export * from "./ObjectGeneratorTool";
export * from "./Tool";
export * from "./ToolCall";
export * from "./ToolCallArgumentsValidationError";
Expand Down

0 comments on commit 57a65a5

Please sign in to comment.