modelfusion 0.30.1 → 0.31.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 +7 -11
- package/model-function/SuccessfulModelCall.cjs +2 -9
- package/model-function/SuccessfulModelCall.d.ts +10 -7
- package/model-function/SuccessfulModelCall.js +2 -9
- package/model-function/generate-structure/StructureFromTextGenerationModel.cjs +2 -2
- package/model-function/generate-structure/StructureFromTextGenerationModel.d.ts +5 -9
- package/model-function/generate-structure/StructureFromTextGenerationModel.js +2 -2
- package/model-function/generate-structure/StructureGenerationModel.d.ts +3 -2
- package/model-function/generate-structure/StructureOrTextGenerationModel.d.ts +4 -5
- package/model-function/generate-structure/generateStructure.cjs +5 -2
- package/model-function/generate-structure/generateStructure.d.ts +1 -1
- package/model-function/generate-structure/generateStructure.js +5 -2
- package/model-function/generate-structure/generateStructureOrText.cjs +8 -5
- package/model-function/generate-structure/generateStructureOrText.d.ts +2 -2
- package/model-function/generate-structure/generateStructureOrText.js +8 -5
- package/model-function/index.cjs +2 -3
- package/model-function/index.d.ts +2 -3
- package/model-function/index.js +2 -3
- package/model-provider/openai/OpenAICostCalculator.cjs +6 -5
- package/model-provider/openai/OpenAICostCalculator.js +6 -5
- package/model-provider/openai/chat/OpenAIChatModel.cjs +47 -9
- package/model-provider/openai/chat/OpenAIChatModel.d.ts +15 -5
- package/model-provider/openai/chat/OpenAIChatModel.js +47 -9
- package/model-provider/openai/chat/OpenAIChatPromptFormat.cjs +78 -0
- package/model-provider/openai/chat/OpenAIChatPromptFormat.d.ts +12 -0
- package/model-provider/openai/chat/OpenAIChatPromptFormat.js +73 -0
- package/model-provider/openai/index.cjs +2 -3
- package/model-provider/openai/index.d.ts +1 -1
- package/model-provider/openai/index.js +1 -1
- package/package.json +1 -1
- package/prompt/AlpacaPromptFormat.cjs +23 -21
- package/prompt/AlpacaPromptFormat.d.ts +1 -1
- package/prompt/AlpacaPromptFormat.js +21 -19
- package/prompt/InstructionPrompt.d.ts +9 -0
- package/prompt/Llama2PromptFormat.cjs +44 -40
- package/prompt/Llama2PromptFormat.d.ts +2 -2
- package/prompt/Llama2PromptFormat.js +41 -37
- package/prompt/TextPromptFormat.cjs +5 -5
- package/prompt/TextPromptFormat.d.ts +2 -2
- package/prompt/TextPromptFormat.js +2 -2
- package/prompt/VicunaPromptFormat.cjs +39 -37
- package/prompt/VicunaPromptFormat.d.ts +1 -1
- package/prompt/VicunaPromptFormat.js +37 -35
- package/prompt/index.cjs +0 -1
- package/prompt/index.d.ts +0 -1
- package/prompt/index.js +0 -1
- package/tool/useTool.cjs +5 -1
- package/tool/useTool.d.ts +1 -1
- package/tool/useTool.js +5 -1
- package/tool/useToolOrGenerateText.cjs +5 -2
- package/tool/useToolOrGenerateText.d.ts +2 -2
- package/tool/useToolOrGenerateText.js +5 -2
- package/model-function/generate-structure/InstructionWithStructurePrompt.cjs +0 -17
- package/model-function/generate-structure/InstructionWithStructurePrompt.d.ts +0 -17
- package/model-function/generate-structure/InstructionWithStructurePrompt.js +0 -14
- package/model-provider/openai/chat/OpenAIChatPrompt.cjs +0 -135
- package/model-provider/openai/chat/OpenAIChatPrompt.d.ts +0 -96
- package/model-provider/openai/chat/OpenAIChatPrompt.js +0 -127
- package/prompt/OpenAIChatPromptFormat.cjs +0 -74
- package/prompt/OpenAIChatPromptFormat.d.ts +0 -12
- package/prompt/OpenAIChatPromptFormat.js +0 -69
@@ -4,14 +4,14 @@ import { ChatPrompt } from "./chat/ChatPrompt.js";
|
|
4
4
|
/**
|
5
5
|
* Formats an instruction prompt as a basic text prompt.
|
6
6
|
*/
|
7
|
-
export declare const
|
7
|
+
export declare const mapInstructionPromptToTextFormat: () => PromptFormat<InstructionPrompt, string>;
|
8
8
|
/**
|
9
9
|
* Formats a chat prompt as a basic text prompt.
|
10
10
|
*
|
11
11
|
* @param user The label of the user in the chat.
|
12
12
|
* @param ai The name of the AI in the chat.
|
13
13
|
*/
|
14
|
-
export declare const
|
14
|
+
export declare const mapChatPromptToTextFormat: ({ user, ai, }: {
|
15
15
|
user: string;
|
16
16
|
ai: string;
|
17
17
|
}) => PromptFormat<ChatPrompt, string>;
|
@@ -2,7 +2,7 @@ import { validateChatPrompt } from "./chat/validateChatPrompt.js";
|
|
2
2
|
/**
|
3
3
|
* Formats an instruction prompt as a basic text prompt.
|
4
4
|
*/
|
5
|
-
export const
|
5
|
+
export const mapInstructionPromptToTextFormat = () => ({
|
6
6
|
stopSequences: [],
|
7
7
|
format: (instruction) => {
|
8
8
|
let text = "";
|
@@ -22,7 +22,7 @@ export const TextInstructionPromptFormat = () => ({
|
|
22
22
|
* @param user The label of the user in the chat.
|
23
23
|
* @param ai The name of the AI in the chat.
|
24
24
|
*/
|
25
|
-
export const
|
25
|
+
export const mapChatPromptToTextFormat = ({ user, ai }) => ({
|
26
26
|
format: (chatPrompt) => {
|
27
27
|
validateChatPrompt(chatPrompt);
|
28
28
|
let text = "";
|
@@ -1,6 +1,6 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.
|
3
|
+
exports.mapChatPromptToVicunaFormat = void 0;
|
4
4
|
const validateChatPrompt_js_1 = require("./chat/validateChatPrompt.cjs");
|
5
5
|
const DEFAULT_SYSTEM_PROMPT = "A chat between a curious user and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the user's questions.";
|
6
6
|
/**
|
@@ -16,40 +16,42 @@ const DEFAULT_SYSTEM_PROMPT = "A chat between a curious user and an artificial i
|
|
16
16
|
* ASSISTANT:
|
17
17
|
* ```
|
18
18
|
*/
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
19
|
+
function mapChatPromptToVicunaFormat() {
|
20
|
+
return {
|
21
|
+
format: (chatPrompt) => {
|
22
|
+
(0, validateChatPrompt_js_1.validateChatPrompt)(chatPrompt);
|
23
|
+
let text = "";
|
24
|
+
for (let i = 0; i < chatPrompt.length; i++) {
|
25
|
+
const message = chatPrompt[i];
|
26
|
+
// system message:
|
27
|
+
if (i === 0 &&
|
28
|
+
"system" in message &&
|
29
|
+
typeof message.system === "string") {
|
30
|
+
text += `${message.system}\n\n`;
|
31
|
+
continue;
|
32
|
+
}
|
33
|
+
// first message was not a system message:
|
34
|
+
if (i === 0) {
|
35
|
+
text += `${DEFAULT_SYSTEM_PROMPT}\n\n`;
|
36
|
+
}
|
37
|
+
// user message
|
38
|
+
if ("user" in message) {
|
39
|
+
text += `USER: ${message.user}\n`;
|
40
|
+
continue;
|
41
|
+
}
|
42
|
+
// ai message:
|
43
|
+
if ("ai" in message) {
|
44
|
+
text += `ASSISTANT:\n${message.ai}\n`;
|
45
|
+
continue;
|
46
|
+
}
|
47
|
+
// unsupported message:
|
48
|
+
throw new Error(`Unsupported message: ${JSON.stringify(message)}`);
|
31
49
|
}
|
32
|
-
//
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
}
|
41
|
-
// ai message:
|
42
|
-
if ("ai" in message) {
|
43
|
-
text += `ASSISTANT:\n${message.ai}\n`;
|
44
|
-
continue;
|
45
|
-
}
|
46
|
-
// unsupported message:
|
47
|
-
throw new Error(`Unsupported message: ${JSON.stringify(message)}`);
|
48
|
-
}
|
49
|
-
// AI message prefix:
|
50
|
-
text += `ASSISTANT: `;
|
51
|
-
return text;
|
52
|
-
},
|
53
|
-
stopSequences: [`\nUSER:`],
|
54
|
-
});
|
55
|
-
exports.VicunaChatPromptFormat = VicunaChatPromptFormat;
|
50
|
+
// AI message prefix:
|
51
|
+
text += `ASSISTANT: `;
|
52
|
+
return text;
|
53
|
+
},
|
54
|
+
stopSequences: [`\nUSER:`],
|
55
|
+
};
|
56
|
+
}
|
57
|
+
exports.mapChatPromptToVicunaFormat = mapChatPromptToVicunaFormat;
|
@@ -13,39 +13,41 @@ const DEFAULT_SYSTEM_PROMPT = "A chat between a curious user and an artificial i
|
|
13
13
|
* ASSISTANT:
|
14
14
|
* ```
|
15
15
|
*/
|
16
|
-
export
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
16
|
+
export function mapChatPromptToVicunaFormat() {
|
17
|
+
return {
|
18
|
+
format: (chatPrompt) => {
|
19
|
+
validateChatPrompt(chatPrompt);
|
20
|
+
let text = "";
|
21
|
+
for (let i = 0; i < chatPrompt.length; i++) {
|
22
|
+
const message = chatPrompt[i];
|
23
|
+
// system message:
|
24
|
+
if (i === 0 &&
|
25
|
+
"system" in message &&
|
26
|
+
typeof message.system === "string") {
|
27
|
+
text += `${message.system}\n\n`;
|
28
|
+
continue;
|
29
|
+
}
|
30
|
+
// first message was not a system message:
|
31
|
+
if (i === 0) {
|
32
|
+
text += `${DEFAULT_SYSTEM_PROMPT}\n\n`;
|
33
|
+
}
|
34
|
+
// user message
|
35
|
+
if ("user" in message) {
|
36
|
+
text += `USER: ${message.user}\n`;
|
37
|
+
continue;
|
38
|
+
}
|
39
|
+
// ai message:
|
40
|
+
if ("ai" in message) {
|
41
|
+
text += `ASSISTANT:\n${message.ai}\n`;
|
42
|
+
continue;
|
43
|
+
}
|
44
|
+
// unsupported message:
|
45
|
+
throw new Error(`Unsupported message: ${JSON.stringify(message)}`);
|
28
46
|
}
|
29
|
-
//
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
continue;
|
37
|
-
}
|
38
|
-
// ai message:
|
39
|
-
if ("ai" in message) {
|
40
|
-
text += `ASSISTANT:\n${message.ai}\n`;
|
41
|
-
continue;
|
42
|
-
}
|
43
|
-
// unsupported message:
|
44
|
-
throw new Error(`Unsupported message: ${JSON.stringify(message)}`);
|
45
|
-
}
|
46
|
-
// AI message prefix:
|
47
|
-
text += `ASSISTANT: `;
|
48
|
-
return text;
|
49
|
-
},
|
50
|
-
stopSequences: [`\nUSER:`],
|
51
|
-
});
|
47
|
+
// AI message prefix:
|
48
|
+
text += `ASSISTANT: `;
|
49
|
+
return text;
|
50
|
+
},
|
51
|
+
stopSequences: [`\nUSER:`],
|
52
|
+
};
|
53
|
+
}
|
package/prompt/index.cjs
CHANGED
@@ -17,7 +17,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./AlpacaPromptFormat.cjs"), exports);
|
18
18
|
__exportStar(require("./InstructionPrompt.cjs"), exports);
|
19
19
|
__exportStar(require("./Llama2PromptFormat.cjs"), exports);
|
20
|
-
__exportStar(require("./OpenAIChatPromptFormat.cjs"), exports);
|
21
20
|
__exportStar(require("./PromptFormat.cjs"), exports);
|
22
21
|
__exportStar(require("./PromptFormatTextGenerationModel.cjs"), exports);
|
23
22
|
__exportStar(require("./TextPromptFormat.cjs"), exports);
|
package/prompt/index.d.ts
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
export * from "./AlpacaPromptFormat.js";
|
2
2
|
export * from "./InstructionPrompt.js";
|
3
3
|
export * from "./Llama2PromptFormat.js";
|
4
|
-
export * from "./OpenAIChatPromptFormat.js";
|
5
4
|
export * from "./PromptFormat.js";
|
6
5
|
export * from "./PromptFormatTextGenerationModel.js";
|
7
6
|
export * from "./TextPromptFormat.js";
|
package/prompt/index.js
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
export * from "./AlpacaPromptFormat.js";
|
2
2
|
export * from "./InstructionPrompt.js";
|
3
3
|
export * from "./Llama2PromptFormat.js";
|
4
|
-
export * from "./OpenAIChatPromptFormat.js";
|
5
4
|
export * from "./PromptFormat.js";
|
6
5
|
export * from "./PromptFormatTextGenerationModel.js";
|
7
6
|
export * from "./TextPromptFormat.js";
|
package/tool/useTool.cjs
CHANGED
@@ -14,11 +14,15 @@ const executeTool_js_1 = require("./executeTool.cjs");
|
|
14
14
|
* and the result of the tool execution (`result` property, typed).
|
15
15
|
*/
|
16
16
|
async function useTool(model, tool, prompt, options) {
|
17
|
+
// Note: PROMPT must not be a function.
|
18
|
+
const expandedPrompt = typeof prompt === "function"
|
19
|
+
? prompt(tool)
|
20
|
+
: prompt;
|
17
21
|
const { output: value } = await (0, generateStructure_js_1.generateStructure)(model, {
|
18
22
|
name: tool.name,
|
19
23
|
description: tool.description,
|
20
24
|
schema: tool.inputSchema,
|
21
|
-
},
|
25
|
+
}, expandedPrompt, options).asFullResponse();
|
22
26
|
return {
|
23
27
|
tool: tool.name,
|
24
28
|
parameters: value,
|
package/tool/useTool.d.ts
CHANGED
@@ -8,7 +8,7 @@ import { Tool } from "./Tool.js";
|
|
8
8
|
* the parameters (`parameters` property, typed),
|
9
9
|
* and the result of the tool execution (`result` property, typed).
|
10
10
|
*/
|
11
|
-
export declare function useTool<PROMPT, RESPONSE, SETTINGS extends StructureGenerationModelSettings, TOOL extends Tool<any, any, any>>(model: StructureGenerationModel<PROMPT, RESPONSE, SETTINGS>, tool: TOOL, prompt: (tool: TOOL) => PROMPT, options?: ModelFunctionOptions<SETTINGS>): Promise<{
|
11
|
+
export declare function useTool<PROMPT, RESPONSE, SETTINGS extends StructureGenerationModelSettings, TOOL extends Tool<any, any, any>>(model: StructureGenerationModel<PROMPT, RESPONSE, SETTINGS>, tool: TOOL, prompt: PROMPT | ((tool: TOOL) => PROMPT), options?: ModelFunctionOptions<SETTINGS>): Promise<{
|
12
12
|
tool: TOOL["name"];
|
13
13
|
parameters: TOOL["inputSchema"];
|
14
14
|
result: Awaited<ReturnType<TOOL["execute"]>>;
|
package/tool/useTool.js
CHANGED
@@ -11,11 +11,15 @@ import { executeTool } from "./executeTool.js";
|
|
11
11
|
* and the result of the tool execution (`result` property, typed).
|
12
12
|
*/
|
13
13
|
export async function useTool(model, tool, prompt, options) {
|
14
|
+
// Note: PROMPT must not be a function.
|
15
|
+
const expandedPrompt = typeof prompt === "function"
|
16
|
+
? prompt(tool)
|
17
|
+
: prompt;
|
14
18
|
const { output: value } = await generateStructure(model, {
|
15
19
|
name: tool.name,
|
16
20
|
description: tool.description,
|
17
21
|
schema: tool.inputSchema,
|
18
|
-
},
|
22
|
+
}, expandedPrompt, options).asFullResponse();
|
19
23
|
return {
|
20
24
|
tool: tool.name,
|
21
25
|
parameters: value,
|
@@ -5,12 +5,15 @@ const generateStructureOrText_js_1 = require("../model-function/generate-structu
|
|
5
5
|
const NoSuchToolError_js_1 = require("./NoSuchToolError.cjs");
|
6
6
|
const executeTool_js_1 = require("./executeTool.cjs");
|
7
7
|
async function useToolOrGenerateText(model, tools, prompt, options) {
|
8
|
-
|
8
|
+
// Note: PROMPT must not be a function.
|
9
|
+
const expandedPrompt = typeof prompt === "function"
|
10
|
+
? prompt(tools)
|
11
|
+
: prompt;
|
9
12
|
const modelResponse = await (0, generateStructureOrText_js_1.generateStructureOrText)(model, tools.map((tool) => ({
|
10
13
|
name: tool.name,
|
11
14
|
description: tool.description,
|
12
15
|
schema: tool.inputSchema,
|
13
|
-
})),
|
16
|
+
})), expandedPrompt, options);
|
14
17
|
const { structure, text } = modelResponse;
|
15
18
|
if (structure == null) {
|
16
19
|
return {
|
@@ -1,5 +1,5 @@
|
|
1
1
|
import { ModelFunctionOptions } from "../model-function/ModelFunctionOptions.js";
|
2
|
-
import { StructureOrTextGenerationModel, StructureOrTextGenerationModelSettings
|
2
|
+
import { StructureOrTextGenerationModel, StructureOrTextGenerationModelSettings } from "../model-function/generate-structure/StructureOrTextGenerationModel.js";
|
3
3
|
import { Tool } from "./Tool.js";
|
4
4
|
type ToolArray<T extends Tool<any, any, any>[]> = T;
|
5
5
|
type ToToolMap<T extends ToolArray<Tool<any, any, any>[]>> = {
|
@@ -14,7 +14,7 @@ type ToToolUnion<T> = {
|
|
14
14
|
} : never;
|
15
15
|
}[keyof T];
|
16
16
|
type ToOutputValue<TOOLS extends ToolArray<Tool<any, any, any>[]>> = ToToolUnion<ToToolMap<TOOLS>>;
|
17
|
-
export declare function useToolOrGenerateText<PROMPT, RESPONSE, SETTINGS extends StructureOrTextGenerationModelSettings, TOOLS extends Array<Tool<any, any, any>>>(model: StructureOrTextGenerationModel<PROMPT, RESPONSE, SETTINGS>, tools: TOOLS, prompt: (tools: TOOLS) => PROMPT
|
17
|
+
export declare function useToolOrGenerateText<PROMPT, RESPONSE, SETTINGS extends StructureOrTextGenerationModelSettings, TOOLS extends Array<Tool<any, any, any>>>(model: StructureOrTextGenerationModel<PROMPT, RESPONSE, SETTINGS>, tools: TOOLS, prompt: PROMPT | ((tools: TOOLS) => PROMPT), options?: ModelFunctionOptions<SETTINGS>): Promise<{
|
18
18
|
tool: null;
|
19
19
|
parameters: null;
|
20
20
|
result: null;
|
@@ -2,12 +2,15 @@ import { generateStructureOrText } from "../model-function/generate-structure/ge
|
|
2
2
|
import { NoSuchToolError } from "./NoSuchToolError.js";
|
3
3
|
import { executeTool } from "./executeTool.js";
|
4
4
|
export async function useToolOrGenerateText(model, tools, prompt, options) {
|
5
|
-
|
5
|
+
// Note: PROMPT must not be a function.
|
6
|
+
const expandedPrompt = typeof prompt === "function"
|
7
|
+
? prompt(tools)
|
8
|
+
: prompt;
|
6
9
|
const modelResponse = await generateStructureOrText(model, tools.map((tool) => ({
|
7
10
|
name: tool.name,
|
8
11
|
description: tool.description,
|
9
12
|
schema: tool.inputSchema,
|
10
|
-
})),
|
13
|
+
})), expandedPrompt, options);
|
11
14
|
const { structure, text } = modelResponse;
|
12
15
|
if (structure == null) {
|
13
16
|
return {
|
@@ -1,17 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.InstructionWithStructurePrompt = void 0;
|
4
|
-
exports.InstructionWithStructurePrompt = {
|
5
|
-
forStructure({ instruction, structure, }) {
|
6
|
-
return { structure, instruction };
|
7
|
-
},
|
8
|
-
forTool({ instruction, tool, }) {
|
9
|
-
return exports.InstructionWithStructurePrompt.forStructure({
|
10
|
-
instruction,
|
11
|
-
structure: tool.inputStructureDefinition,
|
12
|
-
});
|
13
|
-
},
|
14
|
-
forToolCurried(instruction) {
|
15
|
-
return (tool) => this.forTool({ instruction, tool });
|
16
|
-
},
|
17
|
-
};
|
@@ -1,17 +0,0 @@
|
|
1
|
-
import { Tool } from "../../tool/Tool.js";
|
2
|
-
import { StructureDefinition } from "../../core/structure/StructureDefinition.js";
|
3
|
-
export type InstructionWithStructure<NAME extends string, STRUCTURE> = {
|
4
|
-
instruction: string;
|
5
|
-
structure: StructureDefinition<NAME, STRUCTURE>;
|
6
|
-
};
|
7
|
-
export declare const InstructionWithStructurePrompt: {
|
8
|
-
forStructure<STRUCTURE>({ instruction, structure, }: {
|
9
|
-
instruction: string;
|
10
|
-
structure: StructureDefinition<string, STRUCTURE>;
|
11
|
-
}): InstructionWithStructure<string, STRUCTURE>;
|
12
|
-
forTool<INPUT, OUTPUT>({ instruction, tool, }: {
|
13
|
-
instruction: string;
|
14
|
-
tool: Tool<string, INPUT, OUTPUT>;
|
15
|
-
}): InstructionWithStructure<string, INPUT>;
|
16
|
-
forToolCurried<INPUT_1, OUTPUT_1>(instruction: string): (tool: Tool<string, INPUT_1, OUTPUT_1>) => InstructionWithStructure<string, INPUT_1>;
|
17
|
-
};
|
@@ -1,14 +0,0 @@
|
|
1
|
-
export const InstructionWithStructurePrompt = {
|
2
|
-
forStructure({ instruction, structure, }) {
|
3
|
-
return { structure, instruction };
|
4
|
-
},
|
5
|
-
forTool({ instruction, tool, }) {
|
6
|
-
return InstructionWithStructurePrompt.forStructure({
|
7
|
-
instruction,
|
8
|
-
structure: tool.inputStructureDefinition,
|
9
|
-
});
|
10
|
-
},
|
11
|
-
forToolCurried(instruction) {
|
12
|
-
return (tool) => this.forTool({ instruction, tool });
|
13
|
-
},
|
14
|
-
};
|
@@ -1,135 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
-
};
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
-
exports.OpenAIChatAutoFunctionPrompt = exports.OpenAIChatSingleFunctionPrompt = exports.OpenAIChatFunctionPrompt = void 0;
|
7
|
-
const secure_json_parse_1 = __importDefault(require("secure-json-parse"));
|
8
|
-
exports.OpenAIChatFunctionPrompt = {
|
9
|
-
forOpenAIFunctionDescription(options) {
|
10
|
-
return new OpenAIChatSingleFunctionPrompt(options);
|
11
|
-
},
|
12
|
-
forStructure({ messages, structure, }) {
|
13
|
-
return this.forOpenAIFunctionDescription({
|
14
|
-
messages,
|
15
|
-
fn: {
|
16
|
-
name: structure.name,
|
17
|
-
description: structure.description,
|
18
|
-
parameters: structure.schema,
|
19
|
-
},
|
20
|
-
});
|
21
|
-
},
|
22
|
-
forStructureCurried(messages) {
|
23
|
-
return (structure) => this.forStructure({ messages, structure });
|
24
|
-
},
|
25
|
-
forTool({ messages, tool, }) {
|
26
|
-
return this.forStructure({
|
27
|
-
messages,
|
28
|
-
structure: tool.inputStructureDefinition,
|
29
|
-
});
|
30
|
-
},
|
31
|
-
forToolCurried(messages) {
|
32
|
-
return (tool) => this.forTool({ messages, tool });
|
33
|
-
},
|
34
|
-
forOpenAIFunctionDescriptions(options) {
|
35
|
-
return new OpenAIChatAutoFunctionPrompt(options);
|
36
|
-
},
|
37
|
-
forStructures({ messages, structures, }) {
|
38
|
-
return this.forOpenAIFunctionDescriptions({
|
39
|
-
messages,
|
40
|
-
fns: structures.map((structure) => ({
|
41
|
-
name: structure.name,
|
42
|
-
description: structure.description,
|
43
|
-
parameters: structure.schema,
|
44
|
-
})),
|
45
|
-
});
|
46
|
-
},
|
47
|
-
forStructuresCurried(messages) {
|
48
|
-
return (structures) => this.forStructures({ messages, structures });
|
49
|
-
},
|
50
|
-
forTools({ messages, tools, }) {
|
51
|
-
return this.forStructures({
|
52
|
-
messages,
|
53
|
-
structures: tools.map((tool) => tool.inputStructureDefinition),
|
54
|
-
});
|
55
|
-
},
|
56
|
-
forToolsCurried(messages) {
|
57
|
-
return (tools) => this.forTools({ messages, tools });
|
58
|
-
},
|
59
|
-
};
|
60
|
-
class OpenAIChatSingleFunctionPrompt {
|
61
|
-
constructor({ messages, fn, }) {
|
62
|
-
Object.defineProperty(this, "messages", {
|
63
|
-
enumerable: true,
|
64
|
-
configurable: true,
|
65
|
-
writable: true,
|
66
|
-
value: void 0
|
67
|
-
});
|
68
|
-
Object.defineProperty(this, "fn", {
|
69
|
-
enumerable: true,
|
70
|
-
configurable: true,
|
71
|
-
writable: true,
|
72
|
-
value: void 0
|
73
|
-
});
|
74
|
-
this.messages = messages;
|
75
|
-
this.fn = fn;
|
76
|
-
}
|
77
|
-
get functionCall() {
|
78
|
-
return { name: this.fn.name };
|
79
|
-
}
|
80
|
-
get functions() {
|
81
|
-
return [
|
82
|
-
{
|
83
|
-
name: this.fn.name,
|
84
|
-
description: this.fn.description,
|
85
|
-
parameters: this.fn.parameters.getJsonSchema(),
|
86
|
-
},
|
87
|
-
];
|
88
|
-
}
|
89
|
-
}
|
90
|
-
exports.OpenAIChatSingleFunctionPrompt = OpenAIChatSingleFunctionPrompt;
|
91
|
-
class OpenAIChatAutoFunctionPrompt {
|
92
|
-
constructor({ messages, fns, }) {
|
93
|
-
Object.defineProperty(this, "messages", {
|
94
|
-
enumerable: true,
|
95
|
-
configurable: true,
|
96
|
-
writable: true,
|
97
|
-
value: void 0
|
98
|
-
});
|
99
|
-
Object.defineProperty(this, "fns", {
|
100
|
-
enumerable: true,
|
101
|
-
configurable: true,
|
102
|
-
writable: true,
|
103
|
-
value: void 0
|
104
|
-
});
|
105
|
-
this.messages = messages;
|
106
|
-
this.fns = fns;
|
107
|
-
}
|
108
|
-
extractStructureAndText(response) {
|
109
|
-
const message = response.choices[0].message;
|
110
|
-
const content = message.content;
|
111
|
-
const functionCall = message.function_call;
|
112
|
-
return functionCall == null
|
113
|
-
? {
|
114
|
-
structure: null,
|
115
|
-
value: null,
|
116
|
-
text: content ?? "",
|
117
|
-
}
|
118
|
-
: {
|
119
|
-
structure: functionCall.name,
|
120
|
-
value: secure_json_parse_1.default.parse(functionCall.arguments),
|
121
|
-
text: content,
|
122
|
-
};
|
123
|
-
}
|
124
|
-
get functionCall() {
|
125
|
-
return "auto";
|
126
|
-
}
|
127
|
-
get functions() {
|
128
|
-
return this.fns.map((fn) => ({
|
129
|
-
name: fn.name,
|
130
|
-
description: fn.description,
|
131
|
-
parameters: fn.parameters.getJsonSchema(),
|
132
|
-
}));
|
133
|
-
}
|
134
|
-
}
|
135
|
-
exports.OpenAIChatAutoFunctionPrompt = OpenAIChatAutoFunctionPrompt;
|
@@ -1,96 +0,0 @@
|
|
1
|
-
import { Schema } from "../../../core/structure/Schema.js";
|
2
|
-
import { StructureDefinition } from "../../../core/structure/StructureDefinition.js";
|
3
|
-
import { StructureOrTextGenerationPrompt } from "../../../model-function/generate-structure/StructureOrTextGenerationModel.js";
|
4
|
-
import { Tool } from "../../../tool/Tool.js";
|
5
|
-
import { OpenAIChatMessage } from "./OpenAIChatMessage.js";
|
6
|
-
import { OpenAIChatResponse } from "./OpenAIChatModel.js";
|
7
|
-
export type OpenAIFunctionDescription<T> = {
|
8
|
-
name: string;
|
9
|
-
description?: string;
|
10
|
-
parameters: Schema<T>;
|
11
|
-
};
|
12
|
-
export declare const OpenAIChatFunctionPrompt: {
|
13
|
-
forOpenAIFunctionDescription<T>(options: {
|
14
|
-
messages: OpenAIChatMessage[];
|
15
|
-
fn: OpenAIFunctionDescription<T>;
|
16
|
-
}): OpenAIChatSingleFunctionPrompt<T>;
|
17
|
-
forStructure<STRUCTURE>({ messages, structure, }: {
|
18
|
-
messages: OpenAIChatMessage[];
|
19
|
-
structure: StructureDefinition<any, STRUCTURE>;
|
20
|
-
}): OpenAIChatSingleFunctionPrompt<STRUCTURE>;
|
21
|
-
forStructureCurried<STRUCTURE_1>(messages: OpenAIChatMessage[]): (structure: StructureDefinition<any, STRUCTURE_1>) => OpenAIChatSingleFunctionPrompt<STRUCTURE_1>;
|
22
|
-
forTool<INPUT, OUTPUT>({ messages, tool, }: {
|
23
|
-
messages: OpenAIChatMessage[];
|
24
|
-
tool: Tool<any, INPUT, OUTPUT>;
|
25
|
-
}): OpenAIChatSingleFunctionPrompt<INPUT>;
|
26
|
-
forToolCurried<INPUT_1, OUTPUT_1>(messages: OpenAIChatMessage[]): (tool: Tool<any, INPUT_1, OUTPUT_1>) => OpenAIChatSingleFunctionPrompt<INPUT_1>;
|
27
|
-
forOpenAIFunctionDescriptions<FUNCTIONS extends OpenAIFunctionDescription<any>[]>(options: {
|
28
|
-
messages: OpenAIChatMessage[];
|
29
|
-
fns: FUNCTIONS;
|
30
|
-
}): OpenAIChatAutoFunctionPrompt<FUNCTIONS>;
|
31
|
-
forStructures<STRUCTURES extends StructureDefinition<any, any>[]>({ messages, structures, }: {
|
32
|
-
messages: OpenAIChatMessage[];
|
33
|
-
structures: STRUCTURES;
|
34
|
-
}): OpenAIChatAutoFunctionPrompt<{
|
35
|
-
name: any;
|
36
|
-
description: string | undefined;
|
37
|
-
parameters: Schema<any>;
|
38
|
-
}[]>;
|
39
|
-
forStructuresCurried<STRUCTURE_2 extends StructureDefinition<any, any>[]>(messages: OpenAIChatMessage[]): (structures: STRUCTURE_2) => OpenAIChatAutoFunctionPrompt<{
|
40
|
-
name: any;
|
41
|
-
description: string | undefined;
|
42
|
-
parameters: Schema<any>;
|
43
|
-
}[]>;
|
44
|
-
forTools<TOOLS extends Tool<any, any, any>[]>({ messages, tools, }: {
|
45
|
-
messages: OpenAIChatMessage[];
|
46
|
-
tools: TOOLS;
|
47
|
-
}): OpenAIChatAutoFunctionPrompt<{
|
48
|
-
name: any;
|
49
|
-
description: string | undefined;
|
50
|
-
parameters: Schema<any>;
|
51
|
-
}[]>;
|
52
|
-
forToolsCurried<TOOLS_1 extends Tool<any, any, any>[]>(messages: OpenAIChatMessage[]): (tools: TOOLS_1) => OpenAIChatAutoFunctionPrompt<{
|
53
|
-
name: any;
|
54
|
-
description: string | undefined;
|
55
|
-
parameters: Schema<any>;
|
56
|
-
}[]>;
|
57
|
-
};
|
58
|
-
export declare class OpenAIChatSingleFunctionPrompt<FUNCTION> {
|
59
|
-
readonly messages: OpenAIChatMessage[];
|
60
|
-
readonly fn: OpenAIFunctionDescription<FUNCTION>;
|
61
|
-
constructor({ messages, fn, }: {
|
62
|
-
messages: OpenAIChatMessage[];
|
63
|
-
fn: OpenAIFunctionDescription<FUNCTION>;
|
64
|
-
});
|
65
|
-
get functionCall(): {
|
66
|
-
name: string;
|
67
|
-
};
|
68
|
-
get functions(): {
|
69
|
-
name: string;
|
70
|
-
description: string | undefined;
|
71
|
-
parameters: unknown;
|
72
|
-
}[];
|
73
|
-
}
|
74
|
-
export declare class OpenAIChatAutoFunctionPrompt<FUNCTIONS extends Array<OpenAIFunctionDescription<any>>> implements StructureOrTextGenerationPrompt<OpenAIChatResponse> {
|
75
|
-
readonly messages: OpenAIChatMessage[];
|
76
|
-
readonly fns: FUNCTIONS;
|
77
|
-
constructor({ messages, fns, }: {
|
78
|
-
messages: OpenAIChatMessage[];
|
79
|
-
fns: FUNCTIONS;
|
80
|
-
});
|
81
|
-
extractStructureAndText(response: OpenAIChatResponse): {
|
82
|
-
structure: null;
|
83
|
-
value: null;
|
84
|
-
text: string;
|
85
|
-
} | {
|
86
|
-
structure: string;
|
87
|
-
value: any;
|
88
|
-
text: string | null;
|
89
|
-
};
|
90
|
-
get functionCall(): "auto";
|
91
|
-
get functions(): {
|
92
|
-
name: string;
|
93
|
-
description: string | undefined;
|
94
|
-
parameters: unknown;
|
95
|
-
}[];
|
96
|
-
}
|