modelfusion 0.121.2 → 0.123.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/CHANGELOG.md +78 -1
- package/README.md +127 -85
- package/index.cjs +0 -1
- package/index.d.ts +0 -1
- package/index.js +0 -1
- package/model-function/ModelCallEvent.d.ts +6 -2
- package/model-function/classify/Classifier.cjs +2 -0
- package/model-function/classify/Classifier.d.ts +10 -0
- package/model-function/classify/Classifier.js +1 -0
- package/model-function/classify/ClassifyEvent.cjs +2 -0
- package/model-function/classify/ClassifyEvent.d.ts +20 -0
- package/model-function/classify/ClassifyEvent.js +1 -0
- package/model-function/classify/EmbeddingSimilarityClassifier.cjs +97 -0
- package/model-function/classify/EmbeddingSimilarityClassifier.d.ts +40 -0
- package/model-function/classify/EmbeddingSimilarityClassifier.js +93 -0
- package/model-function/classify/classify.cjs +27 -0
- package/model-function/classify/classify.d.ts +17 -0
- package/model-function/classify/classify.js +23 -0
- package/{classifier → model-function/classify}/index.cjs +4 -1
- package/model-function/classify/index.d.ts +4 -0
- package/model-function/classify/index.js +4 -0
- package/model-function/embed/embed.cjs +14 -14
- package/model-function/embed/embed.d.ts +24 -18
- package/model-function/embed/embed.js +14 -14
- package/model-function/generate-image/generateImage.cjs +6 -6
- package/model-function/generate-image/generateImage.d.ts +12 -9
- package/model-function/generate-image/generateImage.js +6 -6
- package/model-function/generate-speech/generateSpeech.cjs +7 -7
- package/model-function/generate-speech/generateSpeech.d.ts +12 -9
- package/model-function/generate-speech/generateSpeech.js +7 -7
- package/model-function/generate-speech/streamSpeech.cjs +6 -6
- package/model-function/generate-speech/streamSpeech.d.ts +12 -8
- package/model-function/generate-speech/streamSpeech.js +6 -6
- package/model-function/generate-structure/StructureFromTextGenerationModel.cjs +5 -3
- package/model-function/generate-structure/StructureFromTextGenerationModel.d.ts +1 -1
- package/model-function/generate-structure/StructureFromTextGenerationModel.js +5 -3
- package/model-function/generate-structure/StructureFromTextStreamingModel.cjs +5 -1
- package/model-function/generate-structure/StructureFromTextStreamingModel.js +5 -1
- package/model-function/generate-structure/StructureGenerationModel.d.ts +1 -1
- package/model-function/generate-structure/generateStructure.cjs +8 -8
- package/model-function/generate-structure/generateStructure.d.ts +17 -10
- package/model-function/generate-structure/generateStructure.js +8 -8
- package/model-function/generate-structure/streamStructure.cjs +6 -6
- package/model-function/generate-structure/streamStructure.d.ts +16 -10
- package/model-function/generate-structure/streamStructure.js +6 -6
- package/model-function/generate-text/generateText.cjs +6 -6
- package/model-function/generate-text/generateText.d.ts +12 -9
- package/model-function/generate-text/generateText.js +6 -6
- package/model-function/generate-text/streamText.cjs +6 -6
- package/model-function/generate-text/streamText.d.ts +12 -8
- package/model-function/generate-text/streamText.js +6 -6
- package/model-function/generate-transcription/generateTranscription.cjs +3 -3
- package/model-function/generate-transcription/generateTranscription.d.ts +12 -9
- package/model-function/generate-transcription/generateTranscription.js +3 -3
- package/model-function/index.cjs +1 -0
- package/model-function/index.d.ts +1 -0
- package/model-function/index.js +1 -0
- package/model-provider/cohere/CohereTextGenerationModel.d.ts +12 -12
- package/model-provider/cohere/CohereTextGenerationModel.test.cjs +7 -4
- package/model-provider/cohere/CohereTextGenerationModel.test.js +7 -4
- package/model-provider/llamacpp/LlamaCppCompletionModel.d.ts +10 -10
- package/model-provider/llamacpp/LlamaCppCompletionModel.test.cjs +4 -1
- package/model-provider/llamacpp/LlamaCppCompletionModel.test.js +4 -1
- package/model-provider/mistral/MistralChatModel.test.cjs +15 -8
- package/model-provider/mistral/MistralChatModel.test.js +15 -8
- package/model-provider/mistral/MistralTextEmbeddingModel.d.ts +13 -13
- package/model-provider/ollama/OllamaChatModel.d.ts +9 -9
- package/model-provider/ollama/OllamaChatModel.test.cjs +6 -1
- package/model-provider/ollama/OllamaChatModel.test.js +6 -1
- package/model-provider/ollama/OllamaCompletionModel.test.cjs +31 -16
- package/model-provider/ollama/OllamaCompletionModel.test.js +31 -16
- package/model-provider/openai/OpenAIChatFunctionCallStructureGenerationModel.cjs +4 -4
- package/model-provider/openai/OpenAIChatFunctionCallStructureGenerationModel.d.ts +1 -1
- package/model-provider/openai/OpenAIChatFunctionCallStructureGenerationModel.js +4 -4
- package/model-provider/openai/OpenAIChatModel.test.cjs +21 -14
- package/model-provider/openai/OpenAIChatModel.test.js +21 -14
- package/model-provider/openai/OpenAICompletionModel.test.cjs +15 -9
- package/model-provider/openai/OpenAICompletionModel.test.js +15 -9
- package/model-provider/openai/OpenAITextEmbeddingModel.d.ts +12 -12
- package/package.json +1 -1
- package/tool/execute-tool/executeTool.cjs +5 -5
- package/tool/execute-tool/executeTool.d.ts +8 -4
- package/tool/execute-tool/executeTool.js +5 -5
- package/tool/execute-tool/safeExecuteToolCall.cjs +1 -1
- package/tool/execute-tool/safeExecuteToolCall.js +1 -1
- package/tool/generate-tool-call/TextGenerationToolCallModel.cjs +4 -2
- package/tool/generate-tool-call/TextGenerationToolCallModel.js +4 -2
- package/tool/generate-tool-call/generateToolCall.cjs +7 -7
- package/tool/generate-tool-call/generateToolCall.d.ts +11 -5
- package/tool/generate-tool-call/generateToolCall.js +7 -7
- package/tool/generate-tool-calls/TextGenerationToolCallsModel.cjs +4 -2
- package/tool/generate-tool-calls/TextGenerationToolCallsModel.js +4 -2
- package/tool/generate-tool-calls/generateToolCalls.cjs +3 -3
- package/tool/generate-tool-calls/generateToolCalls.d.ts +11 -5
- package/tool/generate-tool-calls/generateToolCalls.js +3 -3
- package/tool/use-tool/useTool.cjs +2 -2
- package/tool/use-tool/useTool.d.ts +5 -1
- package/tool/use-tool/useTool.js +2 -2
- package/tool/use-tools/useTools.cjs +8 -2
- package/tool/use-tools/useTools.d.ts +5 -1
- package/tool/use-tools/useTools.js +8 -2
- package/vector-index/VectorIndexRetriever.cjs +5 -1
- package/vector-index/VectorIndexRetriever.js +5 -1
- package/vector-index/upsertIntoVectorIndex.cjs +5 -1
- package/vector-index/upsertIntoVectorIndex.js +5 -1
- package/classifier/SemanticClassifier.cjs +0 -75
- package/classifier/SemanticClassifier.d.ts +0 -25
- package/classifier/SemanticClassifier.js +0 -71
- package/classifier/index.d.ts +0 -1
- package/classifier/index.js +0 -1
@@ -7,13 +7,13 @@ import { getRun } from "../../core/getRun.js";
|
|
7
7
|
import { startDurationMeasurement } from "../../util/DurationMeasurement.js";
|
8
8
|
import { runSafe } from "../../util/runSafe.js";
|
9
9
|
import { ToolExecutionError } from "../ToolExecutionError.js";
|
10
|
-
|
11
|
-
tool, args, options) {
|
12
|
-
const
|
13
|
-
return
|
10
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
11
|
+
export async function executeTool({ tool, args, fullResponse, ...options }) {
|
12
|
+
const callResponse = await doExecuteTool({ tool, args, ...options });
|
13
|
+
return fullResponse ? callResponse : callResponse.output;
|
14
14
|
}
|
15
15
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
16
|
-
async function doExecuteTool(tool, args, options) {
|
16
|
+
async function doExecuteTool({ tool, args, ...options }) {
|
17
17
|
const run = await getRun(options?.run);
|
18
18
|
const eventSource = new FunctionEventSource({
|
19
19
|
observers: [
|
@@ -11,7 +11,7 @@ async function safeExecuteToolCall(tool, toolCall, options) {
|
|
11
11
|
toolCall,
|
12
12
|
args: toolCall.args,
|
13
13
|
ok: true,
|
14
|
-
result: await (0, executeTool_js_1.executeTool)(tool, toolCall.args, options),
|
14
|
+
result: await (0, executeTool_js_1.executeTool)({ tool, args: toolCall.args, ...options }),
|
15
15
|
};
|
16
16
|
}
|
17
17
|
catch (error) {
|
@@ -8,7 +8,7 @@ export async function safeExecuteToolCall(tool, toolCall, options) {
|
|
8
8
|
toolCall,
|
9
9
|
args: toolCall.args,
|
10
10
|
ok: true,
|
11
|
-
result: await executeTool(tool, toolCall.args, options),
|
11
|
+
result: await executeTool({ tool, args: toolCall.args, ...options }),
|
12
12
|
};
|
13
13
|
}
|
14
14
|
catch (error) {
|
@@ -30,9 +30,11 @@ class TextGenerationToolCallModel {
|
|
30
30
|
return this.model.settingsForEvent;
|
31
31
|
}
|
32
32
|
async doGenerateToolCall(tool, prompt, options) {
|
33
|
-
const { rawResponse, text, metadata } = await (0, generateText_js_1.generateText)(
|
34
|
-
|
33
|
+
const { rawResponse, text, metadata } = await (0, generateText_js_1.generateText)({
|
34
|
+
model: this.model,
|
35
|
+
prompt: this.format.createPrompt(prompt, tool),
|
35
36
|
fullResponse: true,
|
37
|
+
...options,
|
36
38
|
});
|
37
39
|
try {
|
38
40
|
return {
|
@@ -27,9 +27,11 @@ export class TextGenerationToolCallModel {
|
|
27
27
|
return this.model.settingsForEvent;
|
28
28
|
}
|
29
29
|
async doGenerateToolCall(tool, prompt, options) {
|
30
|
-
const { rawResponse, text, metadata } = await generateText(
|
31
|
-
|
30
|
+
const { rawResponse, text, metadata } = await generateText({
|
31
|
+
model: this.model,
|
32
|
+
prompt: this.format.createPrompt(prompt, tool),
|
32
33
|
fullResponse: true,
|
34
|
+
...options,
|
33
35
|
});
|
34
36
|
try {
|
35
37
|
return {
|
@@ -4,12 +4,12 @@ exports.generateToolCall = void 0;
|
|
4
4
|
const executeStandardCall_js_1 = require("../../model-function/executeStandardCall.cjs");
|
5
5
|
const ToolCallArgumentsValidationError_js_1 = require("../ToolCallArgumentsValidationError.cjs");
|
6
6
|
const ToolCallGenerationError_js_1 = require("../ToolCallGenerationError.cjs");
|
7
|
-
async function generateToolCall(model, tool, prompt, options) {
|
7
|
+
async function generateToolCall({ model, tool, prompt, fullResponse, ...options }) {
|
8
8
|
// Note: PROMPT must not be a function.
|
9
9
|
const expandedPrompt = typeof prompt === "function"
|
10
10
|
? prompt(tool)
|
11
11
|
: prompt;
|
12
|
-
const
|
12
|
+
const callResponse = await (0, executeStandardCall_js_1.executeStandardCall)({
|
13
13
|
functionType: "generate-tool-call",
|
14
14
|
input: expandedPrompt,
|
15
15
|
model,
|
@@ -54,12 +54,12 @@ async function generateToolCall(model, tool, prompt, options) {
|
|
54
54
|
}
|
55
55
|
},
|
56
56
|
});
|
57
|
-
return
|
57
|
+
return fullResponse
|
58
58
|
? {
|
59
|
-
toolCall:
|
60
|
-
|
61
|
-
metadata:
|
59
|
+
toolCall: callResponse.value,
|
60
|
+
rawResponse: callResponse.rawResponse,
|
61
|
+
metadata: callResponse.metadata,
|
62
62
|
}
|
63
|
-
:
|
63
|
+
: callResponse.value;
|
64
64
|
}
|
65
65
|
exports.generateToolCall = generateToolCall;
|
@@ -3,13 +3,19 @@ import { ModelCallMetadata } from "../../model-function/ModelCallMetadata.js";
|
|
3
3
|
import { ToolCall } from "../ToolCall.js";
|
4
4
|
import { ToolDefinition } from "../ToolDefinition.js";
|
5
5
|
import { ToolCallGenerationModel, ToolCallGenerationModelSettings } from "./ToolCallGenerationModel.js";
|
6
|
-
export declare function generateToolCall<PARAMETERS, PROMPT, NAME extends string, SETTINGS extends ToolCallGenerationModelSettings>(
|
6
|
+
export declare function generateToolCall<PARAMETERS, PROMPT, NAME extends string, SETTINGS extends ToolCallGenerationModelSettings>(params: {
|
7
|
+
model: ToolCallGenerationModel<PROMPT, SETTINGS>;
|
8
|
+
tool: ToolDefinition<NAME, PARAMETERS>;
|
9
|
+
prompt: PROMPT | ((tool: ToolDefinition<NAME, PARAMETERS>) => PROMPT);
|
7
10
|
fullResponse?: false;
|
8
|
-
}): Promise<ToolCall<NAME, PARAMETERS>>;
|
9
|
-
export declare function generateToolCall<PARAMETERS, PROMPT, NAME extends string, SETTINGS extends ToolCallGenerationModelSettings>(
|
11
|
+
} & FunctionOptions): Promise<ToolCall<NAME, PARAMETERS>>;
|
12
|
+
export declare function generateToolCall<PARAMETERS, PROMPT, NAME extends string, SETTINGS extends ToolCallGenerationModelSettings>(params: {
|
13
|
+
model: ToolCallGenerationModel<PROMPT, SETTINGS>;
|
14
|
+
tool: ToolDefinition<NAME, PARAMETERS>;
|
15
|
+
prompt: PROMPT | ((tool: ToolDefinition<NAME, PARAMETERS>) => PROMPT);
|
10
16
|
fullResponse: true;
|
11
|
-
}): Promise<{
|
17
|
+
} & FunctionOptions): Promise<{
|
12
18
|
toolCall: ToolCall<NAME, PARAMETERS>;
|
13
|
-
|
19
|
+
rawResponse: unknown;
|
14
20
|
metadata: ModelCallMetadata;
|
15
21
|
}>;
|
@@ -1,12 +1,12 @@
|
|
1
1
|
import { executeStandardCall } from "../../model-function/executeStandardCall.js";
|
2
2
|
import { ToolCallArgumentsValidationError } from "../ToolCallArgumentsValidationError.js";
|
3
3
|
import { ToolCallGenerationError } from "../ToolCallGenerationError.js";
|
4
|
-
export async function generateToolCall(model, tool, prompt, options) {
|
4
|
+
export async function generateToolCall({ model, tool, prompt, fullResponse, ...options }) {
|
5
5
|
// Note: PROMPT must not be a function.
|
6
6
|
const expandedPrompt = typeof prompt === "function"
|
7
7
|
? prompt(tool)
|
8
8
|
: prompt;
|
9
|
-
const
|
9
|
+
const callResponse = await executeStandardCall({
|
10
10
|
functionType: "generate-tool-call",
|
11
11
|
input: expandedPrompt,
|
12
12
|
model,
|
@@ -51,11 +51,11 @@ export async function generateToolCall(model, tool, prompt, options) {
|
|
51
51
|
}
|
52
52
|
},
|
53
53
|
});
|
54
|
-
return
|
54
|
+
return fullResponse
|
55
55
|
? {
|
56
|
-
toolCall:
|
57
|
-
|
58
|
-
metadata:
|
56
|
+
toolCall: callResponse.value,
|
57
|
+
rawResponse: callResponse.rawResponse,
|
58
|
+
metadata: callResponse.metadata,
|
59
59
|
}
|
60
|
-
:
|
60
|
+
: callResponse.value;
|
61
61
|
}
|
@@ -30,9 +30,11 @@ class TextGenerationToolCallsModel {
|
|
30
30
|
return this.model.settingsForEvent;
|
31
31
|
}
|
32
32
|
async doGenerateToolCalls(tools, prompt, options) {
|
33
|
-
const { rawResponse, text: generatedText, metadata, } = await (0, generateText_js_1.generateText)(
|
34
|
-
|
33
|
+
const { rawResponse, text: generatedText, metadata, } = await (0, generateText_js_1.generateText)({
|
34
|
+
model: this.model,
|
35
|
+
prompt: this.template.createPrompt(prompt, tools),
|
35
36
|
fullResponse: true,
|
37
|
+
...options,
|
36
38
|
});
|
37
39
|
try {
|
38
40
|
const { text, toolCalls } = this.template.extractToolCallsAndText(generatedText);
|
@@ -27,9 +27,11 @@ export class TextGenerationToolCallsModel {
|
|
27
27
|
return this.model.settingsForEvent;
|
28
28
|
}
|
29
29
|
async doGenerateToolCalls(tools, prompt, options) {
|
30
|
-
const { rawResponse, text: generatedText, metadata, } = await generateText(
|
31
|
-
|
30
|
+
const { rawResponse, text: generatedText, metadata, } = await generateText({
|
31
|
+
model: this.model,
|
32
|
+
prompt: this.template.createPrompt(prompt, tools),
|
32
33
|
fullResponse: true,
|
34
|
+
...options,
|
33
35
|
});
|
34
36
|
try {
|
35
37
|
const { text, toolCalls } = this.template.extractToolCallsAndText(generatedText);
|
@@ -4,12 +4,12 @@ exports.generateToolCalls = void 0;
|
|
4
4
|
const executeStandardCall_js_1 = require("../../model-function/executeStandardCall.cjs");
|
5
5
|
const NoSuchToolDefinitionError_js_1 = require("../NoSuchToolDefinitionError.cjs");
|
6
6
|
const ToolCallArgumentsValidationError_js_1 = require("../ToolCallArgumentsValidationError.cjs");
|
7
|
-
async function generateToolCalls(model, tools, prompt, options) {
|
7
|
+
async function generateToolCalls({ model, tools, prompt, fullResponse, ...options }) {
|
8
8
|
// Note: PROMPT must not be a function.
|
9
9
|
const expandedPrompt = typeof prompt === "function"
|
10
10
|
? prompt(tools)
|
11
11
|
: prompt;
|
12
|
-
const
|
12
|
+
const callResponse = await (0, executeStandardCall_js_1.executeStandardCall)({
|
13
13
|
functionType: "generate-tool-calls",
|
14
14
|
input: expandedPrompt,
|
15
15
|
model,
|
@@ -58,6 +58,6 @@ async function generateToolCalls(model, tools, prompt, options) {
|
|
58
58
|
};
|
59
59
|
},
|
60
60
|
});
|
61
|
-
return
|
61
|
+
return fullResponse ? callResponse : callResponse.value;
|
62
62
|
}
|
63
63
|
exports.generateToolCalls = generateToolCalls;
|
@@ -14,15 +14,21 @@ type ToToolCallUnion<T> = {
|
|
14
14
|
} : never;
|
15
15
|
}[keyof T];
|
16
16
|
type ToOutputValue<TOOL_CALLS extends ToolCallDefinitionArray<ToolDefinition<any, any>[]>> = ToToolCallUnion<ToToolCallDefinitionMap<TOOL_CALLS>>;
|
17
|
-
export declare function generateToolCalls<TOOLS extends Array<ToolDefinition<any, any>>, PROMPT>(
|
17
|
+
export declare function generateToolCalls<TOOLS extends Array<ToolDefinition<any, any>>, PROMPT>(params: {
|
18
|
+
model: ToolCallsGenerationModel<PROMPT, ToolCallsGenerationModelSettings>;
|
19
|
+
tools: TOOLS;
|
20
|
+
prompt: PROMPT | ((tools: TOOLS) => PROMPT);
|
18
21
|
fullResponse?: false;
|
19
|
-
}): Promise<{
|
22
|
+
} & FunctionOptions): Promise<{
|
20
23
|
text: string | null;
|
21
24
|
toolCalls: Array<ToOutputValue<TOOLS>> | null;
|
22
25
|
}>;
|
23
|
-
export declare function generateToolCalls<TOOLS extends ToolDefinition<any, any>[], PROMPT>(
|
24
|
-
|
25
|
-
|
26
|
+
export declare function generateToolCalls<TOOLS extends ToolDefinition<any, any>[], PROMPT>(params: {
|
27
|
+
model: ToolCallsGenerationModel<PROMPT, ToolCallsGenerationModelSettings>;
|
28
|
+
tools: TOOLS;
|
29
|
+
prompt: PROMPT | ((tools: TOOLS) => PROMPT);
|
30
|
+
fullResponse: true;
|
31
|
+
} & FunctionOptions): Promise<{
|
26
32
|
value: {
|
27
33
|
text: string | null;
|
28
34
|
toolCalls: Array<ToOutputValue<TOOLS>>;
|
@@ -1,12 +1,12 @@
|
|
1
1
|
import { executeStandardCall } from "../../model-function/executeStandardCall.js";
|
2
2
|
import { NoSuchToolDefinitionError } from "../NoSuchToolDefinitionError.js";
|
3
3
|
import { ToolCallArgumentsValidationError } from "../ToolCallArgumentsValidationError.js";
|
4
|
-
export async function generateToolCalls(model, tools, prompt, options) {
|
4
|
+
export async function generateToolCalls({ model, tools, prompt, fullResponse, ...options }) {
|
5
5
|
// Note: PROMPT must not be a function.
|
6
6
|
const expandedPrompt = typeof prompt === "function"
|
7
7
|
? prompt(tools)
|
8
8
|
: prompt;
|
9
|
-
const
|
9
|
+
const callResponse = await executeStandardCall({
|
10
10
|
functionType: "generate-tool-calls",
|
11
11
|
input: expandedPrompt,
|
12
12
|
model,
|
@@ -55,5 +55,5 @@ export async function generateToolCalls(model, tools, prompt, options) {
|
|
55
55
|
};
|
56
56
|
},
|
57
57
|
});
|
58
|
-
return
|
58
|
+
return fullResponse ? callResponse : callResponse.value;
|
59
59
|
}
|
@@ -15,7 +15,7 @@ const generateToolCall_js_1 = require("../generate-tool-call/generateToolCall.cj
|
|
15
15
|
* @see {@link generateToolCall}
|
16
16
|
* @see {@link executeTool}
|
17
17
|
*/
|
18
|
-
async function useTool(model, tool, prompt, options) {
|
18
|
+
async function useTool({ model, tool, prompt, ...options }) {
|
19
19
|
// Note: PROMPT must not be a function.
|
20
20
|
const expandedPrompt = typeof prompt === "function"
|
21
21
|
? prompt(tool)
|
@@ -24,7 +24,7 @@ async function useTool(model, tool, prompt, options) {
|
|
24
24
|
options,
|
25
25
|
input: expandedPrompt,
|
26
26
|
functionType: "use-tool",
|
27
|
-
execute: async (options) => (0, safeExecuteToolCall_js_1.safeExecuteToolCall)(tool, await (0, generateToolCall_js_1.generateToolCall)(model, tool, expandedPrompt, options), options),
|
27
|
+
execute: async (options) => (0, safeExecuteToolCall_js_1.safeExecuteToolCall)(tool, await (0, generateToolCall_js_1.generateToolCall)({ model, tool, prompt: expandedPrompt, ...options }), options),
|
28
28
|
});
|
29
29
|
}
|
30
30
|
exports.useTool = useTool;
|
@@ -13,4 +13,8 @@ import { ToolCallGenerationModel, ToolCallGenerationModelSettings } from "../gen
|
|
13
13
|
* @see {@link generateToolCall}
|
14
14
|
* @see {@link executeTool}
|
15
15
|
*/
|
16
|
-
export declare function useTool<PROMPT, TOOL extends Tool<string, any, any>>(model
|
16
|
+
export declare function useTool<PROMPT, TOOL extends Tool<string, any, any>>({ model, tool, prompt, ...options }: {
|
17
|
+
model: ToolCallGenerationModel<PROMPT, ToolCallGenerationModelSettings>;
|
18
|
+
tool: TOOL;
|
19
|
+
prompt: PROMPT | ((tool: TOOL) => PROMPT);
|
20
|
+
} & FunctionOptions): Promise<ToolCallResult<TOOL["name"], TOOL["parameters"], Awaited<ReturnType<TOOL["execute"]>>>>;
|
package/tool/use-tool/useTool.js
CHANGED
@@ -12,7 +12,7 @@ import { generateToolCall } from "../generate-tool-call/generateToolCall.js";
|
|
12
12
|
* @see {@link generateToolCall}
|
13
13
|
* @see {@link executeTool}
|
14
14
|
*/
|
15
|
-
export async function useTool(model, tool, prompt, options) {
|
15
|
+
export async function useTool({ model, tool, prompt, ...options }) {
|
16
16
|
// Note: PROMPT must not be a function.
|
17
17
|
const expandedPrompt = typeof prompt === "function"
|
18
18
|
? prompt(tool)
|
@@ -21,6 +21,6 @@ export async function useTool(model, tool, prompt, options) {
|
|
21
21
|
options,
|
22
22
|
input: expandedPrompt,
|
23
23
|
functionType: "use-tool",
|
24
|
-
execute: async (options) => safeExecuteToolCall(tool, await generateToolCall(model, tool, expandedPrompt, options), options),
|
24
|
+
execute: async (options) => safeExecuteToolCall(tool, await generateToolCall({ model, tool, prompt: expandedPrompt, ...options }), options),
|
25
25
|
});
|
26
26
|
}
|
@@ -5,7 +5,7 @@ const executeFunctionCall_js_1 = require("../../core/executeFunctionCall.cjs");
|
|
5
5
|
const ToolCallError_js_1 = require("../ToolCallError.cjs");
|
6
6
|
const safeExecuteToolCall_js_1 = require("../execute-tool/safeExecuteToolCall.cjs");
|
7
7
|
const generateToolCalls_js_1 = require("../generate-tool-calls/generateToolCalls.cjs");
|
8
|
-
async function useTools(model, tools, prompt, options) {
|
8
|
+
async function useTools({ model, tools, prompt, ...options }) {
|
9
9
|
// Note: PROMPT must not be a function.
|
10
10
|
const expandedPrompt = typeof prompt === "function"
|
11
11
|
? prompt(tools)
|
@@ -15,7 +15,13 @@ async function useTools(model, tools, prompt, options) {
|
|
15
15
|
input: expandedPrompt,
|
16
16
|
functionType: "use-tools",
|
17
17
|
execute: async (options) => {
|
18
|
-
const modelResponse = await (0, generateToolCalls_js_1.generateToolCalls)(
|
18
|
+
const modelResponse = await (0, generateToolCalls_js_1.generateToolCalls)({
|
19
|
+
model,
|
20
|
+
tools,
|
21
|
+
prompt: expandedPrompt,
|
22
|
+
fullResponse: false,
|
23
|
+
...options,
|
24
|
+
});
|
19
25
|
const { toolCalls, text } = modelResponse;
|
20
26
|
// no tool calls:
|
21
27
|
if (toolCalls == null) {
|
@@ -11,7 +11,11 @@ type ToToolCallUnion<T> = {
|
|
11
11
|
[KEY in StringKeys<T>]: T[KEY] extends Tool<any, infer PARAMETERS, infer OUTPUT> ? ToolCallResult<KEY, PARAMETERS, OUTPUT> : never;
|
12
12
|
}[StringKeys<T>];
|
13
13
|
type ToOutputValue<TOOLS extends ToolArray<Tool<any, any, any>[]>> = ToToolCallUnion<ToToolMap<TOOLS>>;
|
14
|
-
export declare function useTools<PROMPT, TOOLS extends Array<Tool<any, any, any>>>(model
|
14
|
+
export declare function useTools<PROMPT, TOOLS extends Array<Tool<any, any, any>>>({ model, tools, prompt, ...options }: {
|
15
|
+
model: ToolCallsGenerationModel<PROMPT, ToolCallsGenerationModelSettings>;
|
16
|
+
tools: TOOLS;
|
17
|
+
prompt: PROMPT | ((tools: TOOLS) => PROMPT);
|
18
|
+
} & FunctionOptions): Promise<{
|
15
19
|
text: string | null;
|
16
20
|
toolResults: Array<ToOutputValue<TOOLS>> | null;
|
17
21
|
}>;
|
@@ -2,7 +2,7 @@ import { executeFunctionCall } from "../../core/executeFunctionCall.js";
|
|
2
2
|
import { ToolCallError } from "../ToolCallError.js";
|
3
3
|
import { safeExecuteToolCall } from "../execute-tool/safeExecuteToolCall.js";
|
4
4
|
import { generateToolCalls } from "../generate-tool-calls/generateToolCalls.js";
|
5
|
-
export async function useTools(model, tools, prompt, options) {
|
5
|
+
export async function useTools({ model, tools, prompt, ...options }) {
|
6
6
|
// Note: PROMPT must not be a function.
|
7
7
|
const expandedPrompt = typeof prompt === "function"
|
8
8
|
? prompt(tools)
|
@@ -12,7 +12,13 @@ export async function useTools(model, tools, prompt, options) {
|
|
12
12
|
input: expandedPrompt,
|
13
13
|
functionType: "use-tools",
|
14
14
|
execute: async (options) => {
|
15
|
-
const modelResponse = await generateToolCalls(
|
15
|
+
const modelResponse = await generateToolCalls({
|
16
|
+
model,
|
17
|
+
tools,
|
18
|
+
prompt: expandedPrompt,
|
19
|
+
fullResponse: false,
|
20
|
+
...options,
|
21
|
+
});
|
16
22
|
const { toolCalls, text } = modelResponse;
|
17
23
|
// no tool calls:
|
18
24
|
if (toolCalls == null) {
|
@@ -31,7 +31,11 @@ class VectorIndexRetriever {
|
|
31
31
|
};
|
32
32
|
}
|
33
33
|
async retrieve(query, options) {
|
34
|
-
const embedding = await (0, embed_js_1.embed)(
|
34
|
+
const embedding = await (0, embed_js_1.embed)({
|
35
|
+
model: this.embeddingModel,
|
36
|
+
value: query,
|
37
|
+
...options,
|
38
|
+
});
|
35
39
|
const queryResult = await this.vectorIndex.queryByVector({
|
36
40
|
queryVector: embedding,
|
37
41
|
maxResults: this.settings.maxResults ?? 1,
|
@@ -28,7 +28,11 @@ export class VectorIndexRetriever {
|
|
28
28
|
};
|
29
29
|
}
|
30
30
|
async retrieve(query, options) {
|
31
|
-
const embedding = await embed(
|
31
|
+
const embedding = await embed({
|
32
|
+
model: this.embeddingModel,
|
33
|
+
value: query,
|
34
|
+
...options,
|
35
|
+
});
|
32
36
|
const queryResult = await this.vectorIndex.queryByVector({
|
33
37
|
queryVector: embedding,
|
34
38
|
maxResults: this.settings.maxResults ?? 1,
|
@@ -12,7 +12,11 @@ async function upsertIntoVectorIndex({ vectorIndex, embeddingModel, generateId =
|
|
12
12
|
inputPropertyName: "objects",
|
13
13
|
execute: async (options) => {
|
14
14
|
// many embedding models support bulk embedding, so we first embed all texts:
|
15
|
-
const embeddings = await (0, embed_js_1.embedMany)(
|
15
|
+
const embeddings = await (0, embed_js_1.embedMany)({
|
16
|
+
model: embeddingModel,
|
17
|
+
values: objects.map(getValueToEmbed),
|
18
|
+
...options,
|
19
|
+
});
|
16
20
|
await vectorIndex.upsertMany(objects.map((object, i) => ({
|
17
21
|
id: getId?.(object, i) ?? generateId(),
|
18
22
|
vector: embeddings[i],
|
@@ -9,7 +9,11 @@ export async function upsertIntoVectorIndex({ vectorIndex, embeddingModel, gener
|
|
9
9
|
inputPropertyName: "objects",
|
10
10
|
execute: async (options) => {
|
11
11
|
// many embedding models support bulk embedding, so we first embed all texts:
|
12
|
-
const embeddings = await embedMany(
|
12
|
+
const embeddings = await embedMany({
|
13
|
+
model: embeddingModel,
|
14
|
+
values: objects.map(getValueToEmbed),
|
15
|
+
...options,
|
16
|
+
});
|
13
17
|
await vectorIndex.upsertMany(objects.map((object, i) => ({
|
14
18
|
id: getId?.(object, i) ?? generateId(),
|
15
19
|
vector: embeddings[i],
|
@@ -1,75 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.SemanticClassifier = void 0;
|
4
|
-
const embed_js_1 = require("../model-function/embed/embed.cjs");
|
5
|
-
const cosineSimilarity_js_1 = require("../util/cosineSimilarity.cjs");
|
6
|
-
class SemanticClassifier {
|
7
|
-
constructor({ clusters, embeddingModel, similarityThreshold, }) {
|
8
|
-
Object.defineProperty(this, "clusters", {
|
9
|
-
enumerable: true,
|
10
|
-
configurable: true,
|
11
|
-
writable: true,
|
12
|
-
value: void 0
|
13
|
-
});
|
14
|
-
Object.defineProperty(this, "embeddingModel", {
|
15
|
-
enumerable: true,
|
16
|
-
configurable: true,
|
17
|
-
writable: true,
|
18
|
-
value: void 0
|
19
|
-
});
|
20
|
-
Object.defineProperty(this, "similarityThreshold", {
|
21
|
-
enumerable: true,
|
22
|
-
configurable: true,
|
23
|
-
writable: true,
|
24
|
-
value: void 0
|
25
|
-
});
|
26
|
-
Object.defineProperty(this, "embeddings", {
|
27
|
-
enumerable: true,
|
28
|
-
configurable: true,
|
29
|
-
writable: true,
|
30
|
-
value: void 0
|
31
|
-
});
|
32
|
-
this.clusters = clusters;
|
33
|
-
this.embeddingModel = embeddingModel;
|
34
|
-
this.similarityThreshold = similarityThreshold;
|
35
|
-
}
|
36
|
-
async getEmbeddings() {
|
37
|
-
if (this.embeddings != null) {
|
38
|
-
return this.embeddings;
|
39
|
-
}
|
40
|
-
const embeddings = [];
|
41
|
-
for (const cluster of this.clusters) {
|
42
|
-
const clusterEmbeddings = await (0, embed_js_1.embedMany)(this.embeddingModel, cluster.values);
|
43
|
-
for (let i = 0; i < clusterEmbeddings.length; i++) {
|
44
|
-
embeddings.push({
|
45
|
-
embedding: clusterEmbeddings[i],
|
46
|
-
clusterValue: cluster.values[i],
|
47
|
-
clusterName: cluster.name,
|
48
|
-
});
|
49
|
-
}
|
50
|
-
}
|
51
|
-
this.embeddings = embeddings; // lazy caching
|
52
|
-
return embeddings;
|
53
|
-
}
|
54
|
-
async classify(value) {
|
55
|
-
const valueEmbedding = await (0, embed_js_1.embed)(this.embeddingModel, value);
|
56
|
-
const clusterEmbeddings = await this.getEmbeddings();
|
57
|
-
const allMatches = [];
|
58
|
-
for (const embedding of clusterEmbeddings) {
|
59
|
-
const similarity = (0, cosineSimilarity_js_1.cosineSimilarity)(valueEmbedding, embedding.embedding);
|
60
|
-
if (similarity >= this.similarityThreshold) {
|
61
|
-
allMatches.push({
|
62
|
-
similarity,
|
63
|
-
clusterValue: embedding.clusterValue,
|
64
|
-
clusterName: embedding.clusterName,
|
65
|
-
});
|
66
|
-
}
|
67
|
-
}
|
68
|
-
// sort (highest similarity first)
|
69
|
-
allMatches.sort((a, b) => b.similarity - a.similarity);
|
70
|
-
return allMatches.length > 0
|
71
|
-
? allMatches[0].clusterName
|
72
|
-
: null;
|
73
|
-
}
|
74
|
-
}
|
75
|
-
exports.SemanticClassifier = SemanticClassifier;
|
@@ -1,25 +0,0 @@
|
|
1
|
-
import { Vector } from "../core/Vector.js";
|
2
|
-
import { EmbeddingModel } from "../model-function/embed/EmbeddingModel.js";
|
3
|
-
export interface SemanticCluster<VALUE, NAME extends string> {
|
4
|
-
name: NAME;
|
5
|
-
values: VALUE[];
|
6
|
-
}
|
7
|
-
export declare class SemanticClassifier<VALUE, CLUSTERS extends Array<SemanticCluster<VALUE, string>>> {
|
8
|
-
readonly clusters: CLUSTERS;
|
9
|
-
readonly embeddingModel: EmbeddingModel<VALUE>;
|
10
|
-
readonly similarityThreshold: number;
|
11
|
-
private embeddings;
|
12
|
-
constructor({ clusters, embeddingModel, similarityThreshold, }: {
|
13
|
-
clusters: CLUSTERS;
|
14
|
-
embeddingModel: EmbeddingModel<VALUE>;
|
15
|
-
similarityThreshold: number;
|
16
|
-
});
|
17
|
-
getEmbeddings(): Promise<{
|
18
|
-
embedding: Vector;
|
19
|
-
clusterValue: VALUE;
|
20
|
-
clusterName: string;
|
21
|
-
}[]>;
|
22
|
-
classify(value: VALUE): Promise<ClusterNames<CLUSTERS> | null>;
|
23
|
-
}
|
24
|
-
type ClusterNames<CLUSTERS> = CLUSTERS extends Array<SemanticCluster<unknown, infer NAME>> ? NAME : never;
|
25
|
-
export {};
|
@@ -1,71 +0,0 @@
|
|
1
|
-
import { embed, embedMany } from "../model-function/embed/embed.js";
|
2
|
-
import { cosineSimilarity } from "../util/cosineSimilarity.js";
|
3
|
-
export class SemanticClassifier {
|
4
|
-
constructor({ clusters, embeddingModel, similarityThreshold, }) {
|
5
|
-
Object.defineProperty(this, "clusters", {
|
6
|
-
enumerable: true,
|
7
|
-
configurable: true,
|
8
|
-
writable: true,
|
9
|
-
value: void 0
|
10
|
-
});
|
11
|
-
Object.defineProperty(this, "embeddingModel", {
|
12
|
-
enumerable: true,
|
13
|
-
configurable: true,
|
14
|
-
writable: true,
|
15
|
-
value: void 0
|
16
|
-
});
|
17
|
-
Object.defineProperty(this, "similarityThreshold", {
|
18
|
-
enumerable: true,
|
19
|
-
configurable: true,
|
20
|
-
writable: true,
|
21
|
-
value: void 0
|
22
|
-
});
|
23
|
-
Object.defineProperty(this, "embeddings", {
|
24
|
-
enumerable: true,
|
25
|
-
configurable: true,
|
26
|
-
writable: true,
|
27
|
-
value: void 0
|
28
|
-
});
|
29
|
-
this.clusters = clusters;
|
30
|
-
this.embeddingModel = embeddingModel;
|
31
|
-
this.similarityThreshold = similarityThreshold;
|
32
|
-
}
|
33
|
-
async getEmbeddings() {
|
34
|
-
if (this.embeddings != null) {
|
35
|
-
return this.embeddings;
|
36
|
-
}
|
37
|
-
const embeddings = [];
|
38
|
-
for (const cluster of this.clusters) {
|
39
|
-
const clusterEmbeddings = await embedMany(this.embeddingModel, cluster.values);
|
40
|
-
for (let i = 0; i < clusterEmbeddings.length; i++) {
|
41
|
-
embeddings.push({
|
42
|
-
embedding: clusterEmbeddings[i],
|
43
|
-
clusterValue: cluster.values[i],
|
44
|
-
clusterName: cluster.name,
|
45
|
-
});
|
46
|
-
}
|
47
|
-
}
|
48
|
-
this.embeddings = embeddings; // lazy caching
|
49
|
-
return embeddings;
|
50
|
-
}
|
51
|
-
async classify(value) {
|
52
|
-
const valueEmbedding = await embed(this.embeddingModel, value);
|
53
|
-
const clusterEmbeddings = await this.getEmbeddings();
|
54
|
-
const allMatches = [];
|
55
|
-
for (const embedding of clusterEmbeddings) {
|
56
|
-
const similarity = cosineSimilarity(valueEmbedding, embedding.embedding);
|
57
|
-
if (similarity >= this.similarityThreshold) {
|
58
|
-
allMatches.push({
|
59
|
-
similarity,
|
60
|
-
clusterValue: embedding.clusterValue,
|
61
|
-
clusterName: embedding.clusterName,
|
62
|
-
});
|
63
|
-
}
|
64
|
-
}
|
65
|
-
// sort (highest similarity first)
|
66
|
-
allMatches.sort((a, b) => b.similarity - a.similarity);
|
67
|
-
return allMatches.length > 0
|
68
|
-
? allMatches[0].clusterName
|
69
|
-
: null;
|
70
|
-
}
|
71
|
-
}
|
package/classifier/index.d.ts
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
export * from "./SemanticClassifier.js";
|
package/classifier/index.js
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
export * from "./SemanticClassifier.js";
|