modelfusion 0.71.0 → 0.73.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +57 -45
- package/core/FunctionEvent.d.ts +5 -5
- package/core/api/ApiCallError.cjs +2 -1
- package/core/api/ApiCallError.d.ts +3 -3
- package/core/api/ApiCallError.js +2 -1
- package/core/api/postToApi.cjs +10 -3
- package/core/api/postToApi.js +10 -3
- package/guard/guard.cjs +5 -4
- package/guard/guard.d.ts +1 -1
- package/guard/guard.js +5 -4
- package/model-function/ModelCallEvent.d.ts +2 -2
- package/model-function/generate-structure/StructureGenerationEvent.d.ts +2 -2
- package/model-function/generate-structure/StructureParseError.cjs +10 -0
- package/model-function/generate-structure/StructureParseError.d.ts +8 -0
- package/model-function/generate-structure/StructureParseError.js +10 -0
- package/model-function/generate-structure/index.cjs +0 -3
- package/model-function/generate-structure/index.d.ts +0 -3
- package/model-function/generate-structure/index.js +0 -3
- package/model-function/index.cjs +0 -1
- package/model-function/index.d.ts +0 -1
- package/model-function/index.js +0 -1
- package/model-provider/automatic1111/Automatic1111ImageGenerationModel.d.ts +3 -3
- package/model-provider/ollama/OllamaError.cjs +21 -7
- package/model-provider/ollama/OllamaError.d.ts +10 -3
- package/model-provider/ollama/OllamaError.js +21 -7
- package/model-provider/ollama/OllamaTextGenerationModel.cjs +2 -2
- package/model-provider/ollama/OllamaTextGenerationModel.d.ts +5 -5
- package/model-provider/ollama/OllamaTextGenerationModel.js +2 -2
- package/model-provider/openai/chat/OpenAIChatMessage.cjs +16 -25
- package/model-provider/openai/chat/OpenAIChatMessage.d.ts +10 -28
- package/model-provider/openai/chat/OpenAIChatMessage.js +16 -25
- package/model-provider/openai/chat/OpenAIChatModel.cjs +2 -48
- package/model-provider/openai/chat/OpenAIChatModel.d.ts +15 -107
- package/model-provider/openai/chat/OpenAIChatModel.js +2 -48
- package/package.json +1 -1
- package/tool/Tool.cjs +5 -16
- package/tool/Tool.d.ts +9 -14
- package/tool/Tool.js +5 -16
- package/tool/ToolCall.d.ts +15 -0
- package/tool/ToolCallArgumentsValidationError.cjs +49 -0
- package/tool/ToolCallArgumentsValidationError.d.ts +23 -0
- package/tool/ToolCallArgumentsValidationError.js +45 -0
- package/tool/ToolCallError.cjs +34 -0
- package/tool/ToolCallError.d.ts +17 -0
- package/tool/ToolCallError.js +30 -0
- package/{model-function/generate-tool-call → tool}/ToolCallGenerationError.cjs +4 -4
- package/{model-function/generate-tool-call → tool}/ToolCallGenerationError.d.ts +1 -1
- package/{model-function/generate-tool-call → tool}/ToolCallGenerationError.js +2 -2
- package/{model-function/generate-tool-call/ToolCallParametersValidationError.cjs → tool/ToolCallParseError.cjs} +13 -13
- package/{model-function/generate-tool-call/ToolCallParametersValidationError.d.ts → tool/ToolCallParseError.d.ts} +6 -6
- package/{model-function/generate-tool-call/ToolCallParametersValidationError.js → tool/ToolCallParseError.js} +11 -11
- package/tool/ToolCallResult.d.ts +13 -0
- package/{model-function/generate-tool-call → tool}/ToolDefinition.d.ts +2 -2
- package/tool/ToolExecutionError.cjs +5 -4
- package/tool/ToolExecutionError.d.ts +4 -4
- package/tool/ToolExecutionError.js +5 -4
- package/tool/WebSearchTool.cjs +4 -4
- package/tool/WebSearchTool.d.ts +2 -2
- package/tool/WebSearchTool.js +4 -4
- package/tool/{ExecuteToolEvent.d.ts → execute-tool/ExecuteToolEvent.d.ts} +1 -1
- package/tool/{executeTool.cjs → execute-tool/executeTool.cjs} +15 -15
- package/tool/{executeTool.d.ts → execute-tool/executeTool.d.ts} +6 -6
- package/tool/{executeTool.js → execute-tool/executeTool.js} +15 -15
- package/tool/execute-tool/index.cjs +18 -0
- package/tool/execute-tool/index.d.ts +2 -0
- package/tool/execute-tool/index.js +2 -0
- package/tool/execute-tool/safeExecuteToolCall.cjs +34 -0
- package/tool/execute-tool/safeExecuteToolCall.d.ts +5 -0
- package/tool/execute-tool/safeExecuteToolCall.js +30 -0
- package/tool/generate-tool-call/TextGenerationToolCallModel.cjs +59 -0
- package/tool/generate-tool-call/TextGenerationToolCallModel.d.ts +35 -0
- package/tool/generate-tool-call/TextGenerationToolCallModel.js +55 -0
- package/{model-function → tool}/generate-tool-call/ToolCallGenerationEvent.d.ts +1 -1
- package/{model-function → tool}/generate-tool-call/ToolCallGenerationModel.d.ts +3 -3
- package/{model-function → tool}/generate-tool-call/generateToolCall.cjs +11 -11
- package/{model-function → tool}/generate-tool-call/generateToolCall.d.ts +3 -3
- package/{model-function → tool}/generate-tool-call/generateToolCall.js +11 -11
- package/tool/generate-tool-call/index.cjs +20 -0
- package/tool/generate-tool-call/index.d.ts +4 -0
- package/tool/generate-tool-call/index.js +4 -0
- package/{model-function/generate-tool-call → tool/generate-tool-calls-or-text}/ToolCallsOrTextGenerationEvent.d.ts +1 -1
- package/{model-function/generate-tool-call → tool/generate-tool-calls-or-text}/ToolCallsOrTextGenerationModel.d.ts +3 -3
- package/{model-function/generate-tool-call → tool/generate-tool-calls-or-text}/generateToolCallsOrText.cjs +8 -8
- package/{model-function/generate-tool-call → tool/generate-tool-calls-or-text}/generateToolCallsOrText.d.ts +3 -3
- package/{model-function/generate-tool-call → tool/generate-tool-calls-or-text}/generateToolCallsOrText.js +8 -8
- package/{model-function/generate-tool-call → tool/generate-tool-calls-or-text}/index.cjs +0 -7
- package/tool/generate-tool-calls-or-text/index.d.ts +3 -0
- package/tool/generate-tool-calls-or-text/index.js +3 -0
- package/tool/index.cjs +12 -7
- package/tool/index.d.ts +12 -7
- package/tool/index.js +12 -7
- package/tool/{UseToolEvent.d.ts → use-tool/UseToolEvent.d.ts} +1 -1
- package/tool/use-tool/index.cjs +18 -0
- package/tool/use-tool/index.d.ts +2 -0
- package/tool/use-tool/index.js +2 -0
- package/tool/use-tool/useTool.cjs +30 -0
- package/tool/use-tool/useTool.d.ts +16 -0
- package/tool/use-tool/useTool.js +26 -0
- package/tool/use-tools-or-generate-text/UseToolsOrGenerateTextEvent.d.ts +7 -0
- package/tool/use-tools-or-generate-text/index.cjs +18 -0
- package/tool/use-tools-or-generate-text/index.d.ts +2 -0
- package/tool/use-tools-or-generate-text/index.js +2 -0
- package/tool/use-tools-or-generate-text/useToolsOrGenerateText.cjs +48 -0
- package/tool/use-tools-or-generate-text/useToolsOrGenerateText.d.ts +18 -0
- package/tool/use-tools-or-generate-text/useToolsOrGenerateText.js +44 -0
- package/util/getErrorMessage.cjs +3 -0
- package/util/getErrorMessage.d.ts +1 -1
- package/util/getErrorMessage.js +3 -0
- package/model-function/generate-structure/NoSuchStructureError.cjs +0 -17
- package/model-function/generate-structure/NoSuchStructureError.d.ts +0 -4
- package/model-function/generate-structure/NoSuchStructureError.js +0 -13
- package/model-function/generate-structure/StructureOrTextGenerationModel.d.ts +0 -26
- package/model-function/generate-structure/generateStructureOrText.cjs +0 -54
- package/model-function/generate-structure/generateStructureOrText.d.ts +0 -97
- package/model-function/generate-structure/generateStructureOrText.js +0 -50
- package/model-function/generate-tool-call/ToolCall.d.ts +0 -5
- package/model-function/generate-tool-call/index.d.ts +0 -10
- package/model-function/generate-tool-call/index.js +0 -10
- package/tool/NoSuchToolError.cjs +0 -25
- package/tool/NoSuchToolError.d.ts +0 -10
- package/tool/NoSuchToolError.js +0 -21
- package/tool/UseToolOrGenerateTextEvent.d.ts +0 -7
- package/tool/useTool.cjs +0 -40
- package/tool/useTool.d.ts +0 -15
- package/tool/useTool.js +0 -36
- package/tool/useToolOrGenerateText.cjs +0 -50
- package/tool/useToolOrGenerateText.d.ts +0 -23
- package/tool/useToolOrGenerateText.js +0 -46
- /package/{model-function/generate-tool-call → tool}/NoSuchToolDefinitionError.cjs +0 -0
- /package/{model-function/generate-tool-call → tool}/NoSuchToolDefinitionError.d.ts +0 -0
- /package/{model-function/generate-tool-call → tool}/NoSuchToolDefinitionError.js +0 -0
- /package/{model-function/generate-tool-call → tool}/ToolCall.cjs +0 -0
- /package/{model-function/generate-tool-call → tool}/ToolCall.js +0 -0
- /package/{model-function/generate-structure/StructureOrTextGenerationModel.cjs → tool/ToolCallResult.cjs} +0 -0
- /package/{model-function/generate-structure/StructureOrTextGenerationModel.js → tool/ToolCallResult.js} +0 -0
- /package/{model-function/generate-tool-call → tool}/ToolDefinition.cjs +0 -0
- /package/{model-function/generate-tool-call → tool}/ToolDefinition.js +0 -0
- /package/tool/{ExecuteToolEvent.cjs → execute-tool/ExecuteToolEvent.cjs} +0 -0
- /package/tool/{ExecuteToolEvent.js → execute-tool/ExecuteToolEvent.js} +0 -0
- /package/{model-function → tool}/generate-tool-call/ToolCallGenerationEvent.cjs +0 -0
- /package/{model-function → tool}/generate-tool-call/ToolCallGenerationEvent.js +0 -0
- /package/{model-function → tool}/generate-tool-call/ToolCallGenerationModel.cjs +0 -0
- /package/{model-function → tool}/generate-tool-call/ToolCallGenerationModel.js +0 -0
- /package/{model-function/generate-tool-call → tool/generate-tool-calls-or-text}/ToolCallsOrTextGenerationEvent.cjs +0 -0
- /package/{model-function/generate-tool-call → tool/generate-tool-calls-or-text}/ToolCallsOrTextGenerationEvent.js +0 -0
- /package/{model-function/generate-tool-call → tool/generate-tool-calls-or-text}/ToolCallsOrTextGenerationModel.cjs +0 -0
- /package/{model-function/generate-tool-call → tool/generate-tool-calls-or-text}/ToolCallsOrTextGenerationModel.js +0 -0
- /package/tool/{UseToolEvent.cjs → use-tool/UseToolEvent.cjs} +0 -0
- /package/tool/{UseToolEvent.js → use-tool/UseToolEvent.js} +0 -0
- /package/tool/{UseToolOrGenerateTextEvent.cjs → use-tools-or-generate-text/UseToolsOrGenerateTextEvent.cjs} +0 -0
- /package/tool/{UseToolOrGenerateTextEvent.js → use-tools-or-generate-text/UseToolsOrGenerateTextEvent.js} +0 -0
@@ -1,97 +0,0 @@
|
|
1
|
-
import { FunctionOptions } from "../../core/FunctionOptions.js";
|
2
|
-
import { StructureDefinition } from "../../core/schema/StructureDefinition.js";
|
3
|
-
import { ModelCallMetadata } from "../ModelCallMetadata.js";
|
4
|
-
import { StructureOrTextGenerationModel, StructureOrTextGenerationModelSettings } from "./StructureOrTextGenerationModel.js";
|
5
|
-
type StructureDefinitionArray<T extends StructureDefinition<any, any>[]> = T;
|
6
|
-
type ToStructureDefinitionMap<T extends StructureDefinitionArray<StructureDefinition<any, any>[]>> = {
|
7
|
-
[K in T[number]["name"]]: Extract<T[number], StructureDefinition<K, any>>;
|
8
|
-
};
|
9
|
-
type ToStructureUnion<T> = {
|
10
|
-
[KEY in keyof T]: T[KEY] extends StructureDefinition<any, infer STRUCTURE> ? {
|
11
|
-
structure: KEY;
|
12
|
-
value: STRUCTURE;
|
13
|
-
text: string | null;
|
14
|
-
} : never;
|
15
|
-
}[keyof T];
|
16
|
-
type ToOutputValue<STRUCTURES extends StructureDefinitionArray<StructureDefinition<any, any>[]>> = ToStructureUnion<ToStructureDefinitionMap<STRUCTURES>>;
|
17
|
-
/**
|
18
|
-
* Generates a typed object or plain text based on the given prompt and structure definitions.
|
19
|
-
* The structure definition is used as part of the final prompt.
|
20
|
-
*
|
21
|
-
* This function interacts with a specified model to either return a structured object conforming to one of the provided
|
22
|
-
* structure definitions or a plain text response if the model decides not to return a structured object.
|
23
|
-
*
|
24
|
-
* For the OpenAI chat model, this generates and parses a function call with automatic function selection.
|
25
|
-
*
|
26
|
-
* @see https://modelfusion.dev/guide/function/generate-structure-or-text
|
27
|
-
*
|
28
|
-
* @example
|
29
|
-
* const { structure, value, text } = await generateStructureOrText(
|
30
|
-
* new OpenAIChatModel(...),
|
31
|
-
* [
|
32
|
-
* new ZodStructureDefinition({
|
33
|
-
* name: "getCurrentWeather" as const, // mark 'as const' for type inference
|
34
|
-
* description: "Get the current weather in a given location",
|
35
|
-
* schema: z.object({
|
36
|
-
* location: z
|
37
|
-
* .string()
|
38
|
-
* .describe("The city and state, e.g. San Francisco, CA"),
|
39
|
-
* unit: z.enum(["celsius", "fahrenheit"]).optional(),
|
40
|
-
* }),
|
41
|
-
* }),
|
42
|
-
* new ZodStructureDefinition({
|
43
|
-
* name: "getContactInformation" as const,
|
44
|
-
* description: "Get the contact information for a given person",
|
45
|
-
* schema: z.object({
|
46
|
-
* name: z.string().describe("The name of the person"),
|
47
|
-
* }),
|
48
|
-
* }),
|
49
|
-
* ],
|
50
|
-
* [OpenAIChatMessage.user(query)]
|
51
|
-
* );
|
52
|
-
*
|
53
|
-
* switch (structure) {
|
54
|
-
* case "getCurrentWeather": {
|
55
|
-
* const { location, unit } = value;
|
56
|
-
* console.log("getCurrentWeather", location, unit);
|
57
|
-
* break;
|
58
|
-
* }
|
59
|
-
*
|
60
|
-
* case "getContactInformation": {
|
61
|
-
* const { name } = value;
|
62
|
-
* console.log("getContactInformation", name);
|
63
|
-
* break;
|
64
|
-
* }
|
65
|
-
*
|
66
|
-
* case null: {
|
67
|
-
* console.log("No function call. Generated text: ", text);
|
68
|
-
* }
|
69
|
-
* }
|
70
|
-
*
|
71
|
-
* @param {StructureOrTextGenerationModel<PROMPT, SETTINGS>} model - The model responsible for generating structured data or text.
|
72
|
-
* @param {STRUCTURES} structureDefinitions - An array of StructureDefinition instances defining the possible structures of the expected response.
|
73
|
-
* @param {PROMPT | ((structureDefinitions: STRUCTURES) => PROMPT)} prompt - The prompt used to generate the structure or text.
|
74
|
-
* You can also pass a function that takes the array of structure definitions as an argument and returns the prompt.
|
75
|
-
* @param {FunctionOptions} [options] - Additional options to control the function's execution behavior.
|
76
|
-
*
|
77
|
-
* @returns {Promise<{ structure: null; value: null; text: string } | ToOutputValue<STRUCTURES>>} - Returns a promise that resolves to an object containing either a structured response conforming to one of the provided definitions or a plain text response.
|
78
|
-
*/
|
79
|
-
export declare function generateStructureOrText<STRUCTURES extends StructureDefinition<any, any>[], PROMPT>(model: StructureOrTextGenerationModel<PROMPT, StructureOrTextGenerationModelSettings>, structureDefinitions: STRUCTURES, prompt: PROMPT | ((structureDefinitions: STRUCTURES) => PROMPT), options?: FunctionOptions & {
|
80
|
-
returnType?: "structure";
|
81
|
-
}): Promise<{
|
82
|
-
structure: null;
|
83
|
-
value: null;
|
84
|
-
text: string;
|
85
|
-
} | ToOutputValue<STRUCTURES>>;
|
86
|
-
export declare function generateStructureOrText<STRUCTURES extends StructureDefinition<any, any>[], PROMPT>(model: StructureOrTextGenerationModel<PROMPT, StructureOrTextGenerationModelSettings>, structureDefinitions: STRUCTURES, prompt: PROMPT | ((structureDefinitions: STRUCTURES) => PROMPT), options: FunctionOptions & {
|
87
|
-
returnType?: "full";
|
88
|
-
}): Promise<{
|
89
|
-
value: {
|
90
|
-
structure: null;
|
91
|
-
value: null;
|
92
|
-
text: string;
|
93
|
-
} | ToOutputValue<STRUCTURES>;
|
94
|
-
response: unknown;
|
95
|
-
metadata: ModelCallMetadata;
|
96
|
-
}>;
|
97
|
-
export {};
|
@@ -1,50 +0,0 @@
|
|
1
|
-
import { executeStandardCall } from "../executeStandardCall.js";
|
2
|
-
import { NoSuchStructureError } from "./NoSuchStructureError.js";
|
3
|
-
import { StructureValidationError } from "./StructureValidationError.js";
|
4
|
-
export async function generateStructureOrText(model, structureDefinitions, prompt, options) {
|
5
|
-
// Note: PROMPT must not be a function.
|
6
|
-
const expandedPrompt = typeof prompt === "function"
|
7
|
-
? prompt(structureDefinitions)
|
8
|
-
: prompt;
|
9
|
-
const fullResponse = await executeStandardCall({
|
10
|
-
functionType: "generate-structure-or-text",
|
11
|
-
input: expandedPrompt,
|
12
|
-
model,
|
13
|
-
options,
|
14
|
-
generateResponse: async (options) => {
|
15
|
-
const result = await model.doGenerateStructureOrText(structureDefinitions, expandedPrompt, options);
|
16
|
-
const { structure, value, text } = result.structureAndText;
|
17
|
-
// text generation:
|
18
|
-
if (structure == null) {
|
19
|
-
return {
|
20
|
-
response: result.response,
|
21
|
-
extractedValue: { structure, value, text },
|
22
|
-
usage: result.usage,
|
23
|
-
};
|
24
|
-
}
|
25
|
-
const definition = structureDefinitions.find((d) => d.name === structure);
|
26
|
-
if (definition == undefined) {
|
27
|
-
throw new NoSuchStructureError(structure);
|
28
|
-
}
|
29
|
-
const parseResult = definition.schema.validate(value);
|
30
|
-
if (!parseResult.success) {
|
31
|
-
throw new StructureValidationError({
|
32
|
-
structureName: structure,
|
33
|
-
value,
|
34
|
-
valueText: result.structureAndText.valueText,
|
35
|
-
cause: parseResult.error,
|
36
|
-
});
|
37
|
-
}
|
38
|
-
return {
|
39
|
-
response: result.response,
|
40
|
-
extractedValue: {
|
41
|
-
structure: structure,
|
42
|
-
value: parseResult.data,
|
43
|
-
text: text, // text is string | null, which is part of the response for schema values
|
44
|
-
},
|
45
|
-
usage: result.usage,
|
46
|
-
};
|
47
|
-
},
|
48
|
-
});
|
49
|
-
return options?.returnType === "full" ? fullResponse : fullResponse.value;
|
50
|
-
}
|
@@ -1,10 +0,0 @@
|
|
1
|
-
export * from "./ToolCall.js";
|
2
|
-
export * from "./ToolCallGenerationError.js";
|
3
|
-
export * from "./ToolCallGenerationEvent.js";
|
4
|
-
export * from "./ToolCallGenerationModel.js";
|
5
|
-
export * from "./ToolCallParametersValidationError.js";
|
6
|
-
export * from "./ToolCallsOrTextGenerationEvent.js";
|
7
|
-
export * from "./ToolCallsOrTextGenerationModel.js";
|
8
|
-
export * from "./ToolDefinition.js";
|
9
|
-
export * from "./generateToolCall.js";
|
10
|
-
export * from "./generateToolCallsOrText.js";
|
@@ -1,10 +0,0 @@
|
|
1
|
-
export * from "./ToolCall.js";
|
2
|
-
export * from "./ToolCallGenerationError.js";
|
3
|
-
export * from "./ToolCallGenerationEvent.js";
|
4
|
-
export * from "./ToolCallGenerationModel.js";
|
5
|
-
export * from "./ToolCallParametersValidationError.js";
|
6
|
-
export * from "./ToolCallsOrTextGenerationEvent.js";
|
7
|
-
export * from "./ToolCallsOrTextGenerationModel.js";
|
8
|
-
export * from "./ToolDefinition.js";
|
9
|
-
export * from "./generateToolCall.js";
|
10
|
-
export * from "./generateToolCallsOrText.js";
|
package/tool/NoSuchToolError.cjs
DELETED
@@ -1,25 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.NoSuchToolError = void 0;
|
4
|
-
class NoSuchToolError extends Error {
|
5
|
-
constructor(toolName) {
|
6
|
-
super(`No such tool: ${toolName}`);
|
7
|
-
Object.defineProperty(this, "toolName", {
|
8
|
-
enumerable: true,
|
9
|
-
configurable: true,
|
10
|
-
writable: true,
|
11
|
-
value: void 0
|
12
|
-
});
|
13
|
-
this.name = "NoSuchToolError";
|
14
|
-
this.toolName = toolName;
|
15
|
-
}
|
16
|
-
toJSON() {
|
17
|
-
return {
|
18
|
-
name: this.name,
|
19
|
-
toolName: this.toolName,
|
20
|
-
message: this.message,
|
21
|
-
stack: this.stack,
|
22
|
-
};
|
23
|
-
}
|
24
|
-
}
|
25
|
-
exports.NoSuchToolError = NoSuchToolError;
|
package/tool/NoSuchToolError.js
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
export class NoSuchToolError extends Error {
|
2
|
-
constructor(toolName) {
|
3
|
-
super(`No such tool: ${toolName}`);
|
4
|
-
Object.defineProperty(this, "toolName", {
|
5
|
-
enumerable: true,
|
6
|
-
configurable: true,
|
7
|
-
writable: true,
|
8
|
-
value: void 0
|
9
|
-
});
|
10
|
-
this.name = "NoSuchToolError";
|
11
|
-
this.toolName = toolName;
|
12
|
-
}
|
13
|
-
toJSON() {
|
14
|
-
return {
|
15
|
-
name: this.name,
|
16
|
-
toolName: this.toolName,
|
17
|
-
message: this.message,
|
18
|
-
stack: this.stack,
|
19
|
-
};
|
20
|
-
}
|
21
|
-
}
|
@@ -1,7 +0,0 @@
|
|
1
|
-
import { BaseFunctionFinishedEvent, BaseFunctionStartedEvent } from "../core/FunctionEvent.js";
|
2
|
-
export interface UseToolOrGenerateTextStartedEvent extends BaseFunctionStartedEvent {
|
3
|
-
functionType: "use-tool-or-generate-text";
|
4
|
-
}
|
5
|
-
export interface UseToolOrGenerateTextFinishedEvent extends BaseFunctionFinishedEvent {
|
6
|
-
functionType: "use-tool-or-generate-text";
|
7
|
-
}
|
package/tool/useTool.cjs
DELETED
@@ -1,40 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.useTool = void 0;
|
4
|
-
const executeFunctionCall_js_1 = require("../core/executeFunctionCall.cjs");
|
5
|
-
const generateStructure_js_1 = require("../model-function/generate-structure/generateStructure.cjs");
|
6
|
-
const executeTool_js_1 = require("./executeTool.cjs");
|
7
|
-
/**
|
8
|
-
* `useTool` uses `generateStructure` to generate parameters for a tool and then executes the tool with the parameters.
|
9
|
-
*
|
10
|
-
* @returns The result contains the name of the tool (`tool` property),
|
11
|
-
* the parameters (`parameters` property, typed),
|
12
|
-
* and the result of the tool execution (`result` property, typed).
|
13
|
-
*/
|
14
|
-
async function useTool(model, tool, prompt, options) {
|
15
|
-
// Note: PROMPT must not be a function.
|
16
|
-
const expandedPrompt = typeof prompt === "function"
|
17
|
-
? prompt(tool)
|
18
|
-
: prompt;
|
19
|
-
return (0, executeFunctionCall_js_1.executeFunctionCall)({
|
20
|
-
options,
|
21
|
-
input: expandedPrompt,
|
22
|
-
functionType: "use-tool",
|
23
|
-
execute: async (options) => {
|
24
|
-
const { value } = await (0, generateStructure_js_1.generateStructure)(model, {
|
25
|
-
name: tool.name,
|
26
|
-
description: tool.description,
|
27
|
-
schema: tool.inputSchema,
|
28
|
-
}, expandedPrompt, {
|
29
|
-
...options,
|
30
|
-
returnType: "full",
|
31
|
-
});
|
32
|
-
return {
|
33
|
-
tool: tool.name,
|
34
|
-
parameters: value,
|
35
|
-
result: await (0, executeTool_js_1.executeTool)(tool, value, options),
|
36
|
-
};
|
37
|
-
},
|
38
|
-
});
|
39
|
-
}
|
40
|
-
exports.useTool = useTool;
|
package/tool/useTool.d.ts
DELETED
@@ -1,15 +0,0 @@
|
|
1
|
-
import { FunctionOptions } from "../core/FunctionOptions.js";
|
2
|
-
import { StructureGenerationModel, StructureGenerationModelSettings } from "../model-function/generate-structure/StructureGenerationModel.js";
|
3
|
-
import { Tool } from "./Tool.js";
|
4
|
-
/**
|
5
|
-
* `useTool` uses `generateStructure` to generate parameters for a tool and then executes the tool with the parameters.
|
6
|
-
*
|
7
|
-
* @returns The result contains the name of the tool (`tool` property),
|
8
|
-
* the parameters (`parameters` property, typed),
|
9
|
-
* and the result of the tool execution (`result` property, typed).
|
10
|
-
*/
|
11
|
-
export declare function useTool<PROMPT, TOOL extends Tool<any, any, any>>(model: StructureGenerationModel<PROMPT, StructureGenerationModelSettings>, tool: TOOL, prompt: PROMPT | ((tool: TOOL) => PROMPT), options?: FunctionOptions): Promise<{
|
12
|
-
tool: TOOL["name"];
|
13
|
-
parameters: TOOL["inputSchema"];
|
14
|
-
result: Awaited<ReturnType<TOOL["execute"]>>;
|
15
|
-
}>;
|
package/tool/useTool.js
DELETED
@@ -1,36 +0,0 @@
|
|
1
|
-
import { executeFunctionCall } from "../core/executeFunctionCall.js";
|
2
|
-
import { generateStructure } from "../model-function/generate-structure/generateStructure.js";
|
3
|
-
import { executeTool } from "./executeTool.js";
|
4
|
-
/**
|
5
|
-
* `useTool` uses `generateStructure` to generate parameters for a tool and then executes the tool with the parameters.
|
6
|
-
*
|
7
|
-
* @returns The result contains the name of the tool (`tool` property),
|
8
|
-
* the parameters (`parameters` property, typed),
|
9
|
-
* and the result of the tool execution (`result` property, typed).
|
10
|
-
*/
|
11
|
-
export async function useTool(model, tool, prompt, options) {
|
12
|
-
// Note: PROMPT must not be a function.
|
13
|
-
const expandedPrompt = typeof prompt === "function"
|
14
|
-
? prompt(tool)
|
15
|
-
: prompt;
|
16
|
-
return executeFunctionCall({
|
17
|
-
options,
|
18
|
-
input: expandedPrompt,
|
19
|
-
functionType: "use-tool",
|
20
|
-
execute: async (options) => {
|
21
|
-
const { value } = await generateStructure(model, {
|
22
|
-
name: tool.name,
|
23
|
-
description: tool.description,
|
24
|
-
schema: tool.inputSchema,
|
25
|
-
}, expandedPrompt, {
|
26
|
-
...options,
|
27
|
-
returnType: "full",
|
28
|
-
});
|
29
|
-
return {
|
30
|
-
tool: tool.name,
|
31
|
-
parameters: value,
|
32
|
-
result: await executeTool(tool, value, options),
|
33
|
-
};
|
34
|
-
},
|
35
|
-
});
|
36
|
-
}
|
@@ -1,50 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.useToolOrGenerateText = void 0;
|
4
|
-
const executeFunctionCall_js_1 = require("../core/executeFunctionCall.cjs");
|
5
|
-
const generateStructureOrText_js_1 = require("../model-function/generate-structure/generateStructureOrText.cjs");
|
6
|
-
const NoSuchToolError_js_1 = require("./NoSuchToolError.cjs");
|
7
|
-
const executeTool_js_1 = require("./executeTool.cjs");
|
8
|
-
async function useToolOrGenerateText(model, tools, prompt, options) {
|
9
|
-
// Note: PROMPT must not be a function.
|
10
|
-
const expandedPrompt = typeof prompt === "function"
|
11
|
-
? prompt(tools)
|
12
|
-
: prompt;
|
13
|
-
return (0, executeFunctionCall_js_1.executeFunctionCall)({
|
14
|
-
options,
|
15
|
-
input: expandedPrompt,
|
16
|
-
functionType: "use-tool-or-generate-text",
|
17
|
-
execute: async (options) => {
|
18
|
-
const modelResponse = await (0, generateStructureOrText_js_1.generateStructureOrText)(model, tools.map((tool) => ({
|
19
|
-
name: tool.name,
|
20
|
-
description: tool.description,
|
21
|
-
schema: tool.inputSchema,
|
22
|
-
})), expandedPrompt, {
|
23
|
-
...options,
|
24
|
-
returnType: "structure",
|
25
|
-
});
|
26
|
-
const { structure, text } = modelResponse;
|
27
|
-
if (structure == null) {
|
28
|
-
return {
|
29
|
-
tool: null,
|
30
|
-
parameters: null,
|
31
|
-
result: null,
|
32
|
-
text,
|
33
|
-
};
|
34
|
-
}
|
35
|
-
const tool = tools.find((tool) => tool.name === structure);
|
36
|
-
if (tool == null) {
|
37
|
-
throw new NoSuchToolError_js_1.NoSuchToolError(structure.toString());
|
38
|
-
}
|
39
|
-
const toolParameters = modelResponse.value;
|
40
|
-
const result = await (0, executeTool_js_1.executeTool)(tool, toolParameters, options);
|
41
|
-
return {
|
42
|
-
tool: structure,
|
43
|
-
result,
|
44
|
-
parameters: toolParameters,
|
45
|
-
text: text, // string | null is the expected value here
|
46
|
-
};
|
47
|
-
},
|
48
|
-
});
|
49
|
-
}
|
50
|
-
exports.useToolOrGenerateText = useToolOrGenerateText;
|
@@ -1,23 +0,0 @@
|
|
1
|
-
import { FunctionOptions } from "../core/FunctionOptions.js";
|
2
|
-
import { StructureOrTextGenerationModel, StructureOrTextGenerationModelSettings } from "../model-function/generate-structure/StructureOrTextGenerationModel.js";
|
3
|
-
import { Tool } from "./Tool.js";
|
4
|
-
type ToolArray<T extends Tool<any, any, any>[]> = T;
|
5
|
-
type ToToolMap<T extends ToolArray<Tool<any, any, any>[]>> = {
|
6
|
-
[K in T[number]["name"]]: Extract<T[number], Tool<K, any, any>>;
|
7
|
-
};
|
8
|
-
type ToToolUnion<T> = {
|
9
|
-
[KEY in keyof T]: T[KEY] extends Tool<any, infer INPUT, infer OUTPUT> ? {
|
10
|
-
tool: KEY;
|
11
|
-
parameters: INPUT;
|
12
|
-
result: OUTPUT;
|
13
|
-
text: string | null;
|
14
|
-
} : never;
|
15
|
-
}[keyof T];
|
16
|
-
type ToOutputValue<TOOLS extends ToolArray<Tool<any, any, any>[]>> = ToToolUnion<ToToolMap<TOOLS>>;
|
17
|
-
export declare function useToolOrGenerateText<PROMPT, SETTINGS extends StructureOrTextGenerationModelSettings, TOOLS extends Array<Tool<any, any, any>>>(model: StructureOrTextGenerationModel<PROMPT, SETTINGS>, tools: TOOLS, prompt: PROMPT | ((tools: TOOLS) => PROMPT), options?: FunctionOptions): Promise<{
|
18
|
-
tool: null;
|
19
|
-
parameters: null;
|
20
|
-
result: null;
|
21
|
-
text: string;
|
22
|
-
} | ToOutputValue<TOOLS>>;
|
23
|
-
export {};
|
@@ -1,46 +0,0 @@
|
|
1
|
-
import { executeFunctionCall } from "../core/executeFunctionCall.js";
|
2
|
-
import { generateStructureOrText } from "../model-function/generate-structure/generateStructureOrText.js";
|
3
|
-
import { NoSuchToolError } from "./NoSuchToolError.js";
|
4
|
-
import { executeTool } from "./executeTool.js";
|
5
|
-
export async function useToolOrGenerateText(model, tools, prompt, options) {
|
6
|
-
// Note: PROMPT must not be a function.
|
7
|
-
const expandedPrompt = typeof prompt === "function"
|
8
|
-
? prompt(tools)
|
9
|
-
: prompt;
|
10
|
-
return executeFunctionCall({
|
11
|
-
options,
|
12
|
-
input: expandedPrompt,
|
13
|
-
functionType: "use-tool-or-generate-text",
|
14
|
-
execute: async (options) => {
|
15
|
-
const modelResponse = await generateStructureOrText(model, tools.map((tool) => ({
|
16
|
-
name: tool.name,
|
17
|
-
description: tool.description,
|
18
|
-
schema: tool.inputSchema,
|
19
|
-
})), expandedPrompt, {
|
20
|
-
...options,
|
21
|
-
returnType: "structure",
|
22
|
-
});
|
23
|
-
const { structure, text } = modelResponse;
|
24
|
-
if (structure == null) {
|
25
|
-
return {
|
26
|
-
tool: null,
|
27
|
-
parameters: null,
|
28
|
-
result: null,
|
29
|
-
text,
|
30
|
-
};
|
31
|
-
}
|
32
|
-
const tool = tools.find((tool) => tool.name === structure);
|
33
|
-
if (tool == null) {
|
34
|
-
throw new NoSuchToolError(structure.toString());
|
35
|
-
}
|
36
|
-
const toolParameters = modelResponse.value;
|
37
|
-
const result = await executeTool(tool, toolParameters, options);
|
38
|
-
return {
|
39
|
-
tool: structure,
|
40
|
-
result,
|
41
|
-
parameters: toolParameters,
|
42
|
-
text: text, // string | null is the expected value here
|
43
|
-
};
|
44
|
-
},
|
45
|
-
});
|
46
|
-
}
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|