-
Notifications
You must be signed in to change notification settings - Fork 84
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
Showing
4 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
File renamed without changes.
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,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); |
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,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, | ||
}), | ||
}); | ||
} | ||
} |
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