modelfusion 0.30.0 → 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.
Files changed (66) hide show
  1. package/README.md +7 -11
  2. package/model-function/SuccessfulModelCall.cjs +2 -9
  3. package/model-function/SuccessfulModelCall.d.ts +10 -7
  4. package/model-function/SuccessfulModelCall.js +2 -9
  5. package/model-function/embed-text/embedText.cjs +4 -2
  6. package/model-function/embed-text/embedText.d.ts +1 -1
  7. package/model-function/embed-text/embedText.js +4 -2
  8. package/model-function/generate-structure/StructureFromTextGenerationModel.cjs +2 -2
  9. package/model-function/generate-structure/StructureFromTextGenerationModel.d.ts +5 -9
  10. package/model-function/generate-structure/StructureFromTextGenerationModel.js +2 -2
  11. package/model-function/generate-structure/StructureGenerationModel.d.ts +3 -2
  12. package/model-function/generate-structure/StructureOrTextGenerationModel.d.ts +4 -5
  13. package/model-function/generate-structure/generateStructure.cjs +5 -2
  14. package/model-function/generate-structure/generateStructure.d.ts +1 -1
  15. package/model-function/generate-structure/generateStructure.js +5 -2
  16. package/model-function/generate-structure/generateStructureOrText.cjs +8 -5
  17. package/model-function/generate-structure/generateStructureOrText.d.ts +2 -2
  18. package/model-function/generate-structure/generateStructureOrText.js +8 -5
  19. package/model-function/index.cjs +2 -3
  20. package/model-function/index.d.ts +2 -3
  21. package/model-function/index.js +2 -3
  22. package/model-provider/openai/OpenAICostCalculator.cjs +6 -5
  23. package/model-provider/openai/OpenAICostCalculator.js +6 -5
  24. package/model-provider/openai/OpenAITextEmbeddingModel.cjs +1 -1
  25. package/model-provider/openai/OpenAITextEmbeddingModel.js +1 -1
  26. package/model-provider/openai/chat/OpenAIChatModel.cjs +47 -9
  27. package/model-provider/openai/chat/OpenAIChatModel.d.ts +15 -5
  28. package/model-provider/openai/chat/OpenAIChatModel.js +47 -9
  29. package/model-provider/openai/chat/OpenAIChatPromptFormat.cjs +78 -0
  30. package/model-provider/openai/chat/OpenAIChatPromptFormat.d.ts +12 -0
  31. package/model-provider/openai/chat/OpenAIChatPromptFormat.js +73 -0
  32. package/model-provider/openai/index.cjs +2 -3
  33. package/model-provider/openai/index.d.ts +1 -1
  34. package/model-provider/openai/index.js +1 -1
  35. package/package.json +1 -1
  36. package/prompt/AlpacaPromptFormat.cjs +23 -21
  37. package/prompt/AlpacaPromptFormat.d.ts +1 -1
  38. package/prompt/AlpacaPromptFormat.js +21 -19
  39. package/prompt/InstructionPrompt.d.ts +9 -0
  40. package/prompt/Llama2PromptFormat.cjs +44 -40
  41. package/prompt/Llama2PromptFormat.d.ts +2 -2
  42. package/prompt/Llama2PromptFormat.js +41 -37
  43. package/prompt/TextPromptFormat.cjs +5 -5
  44. package/prompt/TextPromptFormat.d.ts +2 -2
  45. package/prompt/TextPromptFormat.js +2 -2
  46. package/prompt/VicunaPromptFormat.cjs +39 -37
  47. package/prompt/VicunaPromptFormat.d.ts +1 -1
  48. package/prompt/VicunaPromptFormat.js +37 -35
  49. package/prompt/index.cjs +0 -1
  50. package/prompt/index.d.ts +0 -1
  51. package/prompt/index.js +0 -1
  52. package/tool/useTool.cjs +5 -1
  53. package/tool/useTool.d.ts +1 -1
  54. package/tool/useTool.js +5 -1
  55. package/tool/useToolOrGenerateText.cjs +5 -2
  56. package/tool/useToolOrGenerateText.d.ts +2 -2
  57. package/tool/useToolOrGenerateText.js +5 -2
  58. package/model-function/generate-structure/InstructionWithStructurePrompt.cjs +0 -17
  59. package/model-function/generate-structure/InstructionWithStructurePrompt.d.ts +0 -17
  60. package/model-function/generate-structure/InstructionWithStructurePrompt.js +0 -14
  61. package/model-provider/openai/chat/OpenAIChatPrompt.cjs +0 -135
  62. package/model-provider/openai/chat/OpenAIChatPrompt.d.ts +0 -96
  63. package/model-provider/openai/chat/OpenAIChatPrompt.js +0 -127
  64. package/prompt/OpenAIChatPromptFormat.cjs +0 -74
  65. package/prompt/OpenAIChatPromptFormat.d.ts +0 -12
  66. package/prompt/OpenAIChatPromptFormat.js +0 -69
@@ -11,44 +11,48 @@ const END_SYSTEM = "\n<</SYS>>\n\n";
11
11
  *
12
12
  * @see https://www.philschmid.de/llama-2#how-to-prompt-llama-2-chat
13
13
  */
14
- export const Llama2InstructionPromptFormat = () => ({
15
- stopSequences: [END_SEGMENT],
16
- format: (instruction) => `${BEGIN_SEGMENT}${BEGIN_INSTRUCTION}${instruction.system != null
17
- ? ` ${BEGIN_SYSTEM}${instruction.system}${END_SYSTEM}`
18
- : ""} ${instruction.instruction}${instruction.input != null ? `\n\n${instruction.input}` : ""} ${END_INSTRUCTION}\n`,
19
- });
14
+ export function mapInstructionPromptToLlama2Format() {
15
+ return {
16
+ stopSequences: [END_SEGMENT],
17
+ format: (instruction) => `${BEGIN_SEGMENT}${BEGIN_INSTRUCTION}${instruction.system != null
18
+ ? ` ${BEGIN_SYSTEM}${instruction.system}${END_SYSTEM}`
19
+ : ""} ${instruction.instruction}${instruction.input != null ? `\n\n${instruction.input}` : ""} ${END_INSTRUCTION}\n`,
20
+ };
21
+ }
20
22
  /**
21
23
  * Formats a chat prompt as a Llama 2 prompt.
22
24
  */
23
- export const Llama2ChatPromptFormat = () => ({
24
- format: (chatPrompt) => {
25
- validateChatPrompt(chatPrompt);
26
- let text = "";
27
- for (let i = 0; i < chatPrompt.length; i++) {
28
- const message = chatPrompt[i];
29
- // system message:
30
- if (i === 0 &&
31
- "system" in message &&
32
- typeof message.system === "string") {
33
- // Separate section for system message to simplify implementation
34
- // (this is slightly different from the original instructions):
35
- text += `${BEGIN_SEGMENT}${BEGIN_INSTRUCTION}${BEGIN_SYSTEM}${message.system}${END_SYSTEM}${END_INSTRUCTION}${END_SEGMENT}`;
36
- continue;
25
+ export function mapChatPromptToLlama2Format() {
26
+ return {
27
+ format: (chatPrompt) => {
28
+ validateChatPrompt(chatPrompt);
29
+ let text = "";
30
+ for (let i = 0; i < chatPrompt.length; i++) {
31
+ const message = chatPrompt[i];
32
+ // system message:
33
+ if (i === 0 &&
34
+ "system" in message &&
35
+ typeof message.system === "string") {
36
+ // Separate section for system message to simplify implementation
37
+ // (this is slightly different from the original instructions):
38
+ text += `${BEGIN_SEGMENT}${BEGIN_INSTRUCTION}${BEGIN_SYSTEM}${message.system}${END_SYSTEM}${END_INSTRUCTION}${END_SEGMENT}`;
39
+ continue;
40
+ }
41
+ // user message
42
+ if ("user" in message) {
43
+ text += `${BEGIN_SEGMENT}${BEGIN_INSTRUCTION}${message.user}${END_INSTRUCTION}`;
44
+ continue;
45
+ }
46
+ // ai message:
47
+ if ("ai" in message) {
48
+ text += `${message.ai}${END_SEGMENT}`;
49
+ continue;
50
+ }
51
+ // unsupported message:
52
+ throw new Error(`Unsupported message: ${JSON.stringify(message)}`);
37
53
  }
38
- // user message
39
- if ("user" in message) {
40
- text += `${BEGIN_SEGMENT}${BEGIN_INSTRUCTION}${message.user}${END_INSTRUCTION}`;
41
- continue;
42
- }
43
- // ai message:
44
- if ("ai" in message) {
45
- text += `${message.ai}${END_SEGMENT}`;
46
- continue;
47
- }
48
- // unsupported message:
49
- throw new Error(`Unsupported message: ${JSON.stringify(message)}`);
50
- }
51
- return text;
52
- },
53
- stopSequences: [END_SEGMENT],
54
- });
54
+ return text;
55
+ },
56
+ stopSequences: [END_SEGMENT],
57
+ };
58
+ }
@@ -1,11 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TextChatPromptFormat = exports.TextInstructionPromptFormat = void 0;
3
+ exports.mapChatPromptToTextFormat = exports.mapInstructionPromptToTextFormat = void 0;
4
4
  const validateChatPrompt_js_1 = require("./chat/validateChatPrompt.cjs");
5
5
  /**
6
6
  * Formats an instruction prompt as a basic text prompt.
7
7
  */
8
- const TextInstructionPromptFormat = () => ({
8
+ const mapInstructionPromptToTextFormat = () => ({
9
9
  stopSequences: [],
10
10
  format: (instruction) => {
11
11
  let text = "";
@@ -19,14 +19,14 @@ const TextInstructionPromptFormat = () => ({
19
19
  return text;
20
20
  },
21
21
  });
22
- exports.TextInstructionPromptFormat = TextInstructionPromptFormat;
22
+ exports.mapInstructionPromptToTextFormat = mapInstructionPromptToTextFormat;
23
23
  /**
24
24
  * Formats a chat prompt as a basic text prompt.
25
25
  *
26
26
  * @param user The label of the user in the chat.
27
27
  * @param ai The name of the AI in the chat.
28
28
  */
29
- const TextChatPromptFormat = ({ user, ai }) => ({
29
+ const mapChatPromptToTextFormat = ({ user, ai }) => ({
30
30
  format: (chatPrompt) => {
31
31
  (0, validateChatPrompt_js_1.validateChatPrompt)(chatPrompt);
32
32
  let text = "";
@@ -58,4 +58,4 @@ const TextChatPromptFormat = ({ user, ai }) => ({
58
58
  },
59
59
  stopSequences: [`\n${user}:`],
60
60
  });
61
- exports.TextChatPromptFormat = TextChatPromptFormat;
61
+ exports.mapChatPromptToTextFormat = mapChatPromptToTextFormat;
@@ -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 TextInstructionPromptFormat: () => PromptFormat<InstructionPrompt, string>;
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 TextChatPromptFormat: ({ user, ai, }: {
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 TextInstructionPromptFormat = () => ({
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 TextChatPromptFormat = ({ user, ai }) => ({
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.VicunaChatPromptFormat = void 0;
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
- const VicunaChatPromptFormat = () => ({
20
- format: (chatPrompt) => {
21
- (0, validateChatPrompt_js_1.validateChatPrompt)(chatPrompt);
22
- let text = "";
23
- for (let i = 0; i < chatPrompt.length; i++) {
24
- const message = chatPrompt[i];
25
- // system message:
26
- if (i === 0 &&
27
- "system" in message &&
28
- typeof message.system === "string") {
29
- text += `${message.system}\n\n`;
30
- continue;
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
- // first message was not a system message:
33
- if (i === 0) {
34
- text += `${DEFAULT_SYSTEM_PROMPT}\n\n`;
35
- }
36
- // user message
37
- if ("user" in message) {
38
- text += `USER: ${message.user}\n`;
39
- continue;
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,4 +13,4 @@ import { ChatPrompt } from "./chat/ChatPrompt.js";
13
13
  * ASSISTANT:
14
14
  * ```
15
15
  */
16
- export declare const VicunaChatPromptFormat: () => PromptFormat<ChatPrompt, string>;
16
+ export declare function mapChatPromptToVicunaFormat(): PromptFormat<ChatPrompt, string>;
@@ -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 const VicunaChatPromptFormat = () => ({
17
- format: (chatPrompt) => {
18
- validateChatPrompt(chatPrompt);
19
- let text = "";
20
- for (let i = 0; i < chatPrompt.length; i++) {
21
- const message = chatPrompt[i];
22
- // system message:
23
- if (i === 0 &&
24
- "system" in message &&
25
- typeof message.system === "string") {
26
- text += `${message.system}\n\n`;
27
- continue;
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
- // first message was not a system message:
30
- if (i === 0) {
31
- text += `${DEFAULT_SYSTEM_PROMPT}\n\n`;
32
- }
33
- // user message
34
- if ("user" in message) {
35
- text += `USER: ${message.user}\n`;
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
- }, () => prompt(tool), options).asFullResponse();
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
- }, () => prompt(tool), options).asFullResponse();
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
- const expandedPrompt = prompt(tools);
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
- })), () => expandedPrompt, options);
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, StructureOrTextGenerationPrompt } from "../model-function/generate-structure/StructureOrTextGenerationModel.js";
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 & StructureOrTextGenerationPrompt<RESPONSE>, options?: ModelFunctionOptions<SETTINGS>): Promise<{
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
- const expandedPrompt = prompt(tools);
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
- })), () => expandedPrompt, options);
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;