modelfusion 0.10.0 → 0.12.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 CHANGED
@@ -8,7 +8,7 @@
8
8
  [![Discord](https://discordapp.com/api/guilds/1136309340740006029/widget.png?style=shield)](https://discord.gg/GqCwYZATem)
9
9
  [![Created by Lars Grammel](https://img.shields.io/badge/created%20by-@lgrammel-4BBAAB.svg)](https://twitter.com/lgrammel)
10
10
 
11
- [Introduction](#introduction) | [Quick Install](#quick-install) | [Usage](#usage-examples) | [Features](#features) | [Integrations](#integrations) | [Documentation](#documentation) | [Examples](#more-examples) | [modelfusion.dev](https://modelfusion.dev)
11
+ [Introduction](#introduction) | [Quick Install](#quick-install) | [Usage](#usage-examples) | [Features](#features) | [Integrations](#integrations) | [Documentation](#documentation) | [Examples](#more-examples) | [Contributing](#contributing) | [modelfusion.dev](https://modelfusion.dev)
12
12
 
13
13
  > [!NOTE]
14
14
  > ModelFusion is in its initial development phase. Until version 1.0 there may be breaking changes, because I am still exploring the API design. Feedback and suggestions are welcome.
@@ -451,6 +451,12 @@ Record audio with push-to-talk and transcribe it using Whisper, implemented as a
451
451
 
452
452
  TypeScript implementation of the BabyAGI classic and BabyBeeAGI.
453
453
 
454
+ ### [Wikipedia Agent](https://github.com/lgrammel/modelfusion/tree/main/examples/wikipedia-agent)
455
+
456
+ > _terminal app_, _ReAct agent_, _GPT-4_, _OpenAI functions_, _tools_
457
+
458
+ Get answers to questions from Wikipedia, e.g. "Who was born first, Einstein or Picasso?"
459
+
454
460
  ### [Middle school math agent](https://github.com/lgrammel/modelfusion/tree/main/examples/middle-school-math-agent)
455
461
 
456
462
  > _terminal app_, _agent_, _tools_, _GPT-4_
@@ -462,3 +468,9 @@ Small agent that solves middle school math problems. It uses a calculator tool t
462
468
  > _terminal app_, _PDF parsing_, _recursive information extraction_, _in memory vector index, \_style example retrieval_, _OpenAI GPT-4_, _cost calculation_
463
469
 
464
470
  Extracts information about a topic from a PDF and writes a tweet in your own style about it.
471
+
472
+ ## Contributing
473
+
474
+ ### [Contributing Guide](https://github.com/lgrammel/modelfusion/blob/main/CONTRIBUTING.md)
475
+
476
+ Read the [ModelFusion contributing guide](https://github.com/lgrammel/modelfusion/blob/main/CONTRIBUTING.md) to learn about the development process, how to propose bugfixes and improvements, and how to build and test your changes.
@@ -2,9 +2,7 @@ import { FunctionOptions } from "../FunctionOptions.js";
2
2
  import { Model, ModelSettings } from "../Model.js";
3
3
  export interface GenerateJsonModelSettings extends ModelSettings {
4
4
  }
5
- export interface GenerateJsonPrompt<RESPONSE> {
6
- extractJson(response: RESPONSE): unknown;
7
- }
8
5
  export interface GenerateJsonModel<PROMPT, RESPONSE, SETTINGS extends GenerateJsonModelSettings> extends Model<SETTINGS> {
9
- generateJsonResponse(prompt: PROMPT & GenerateJsonPrompt<RESPONSE>, options?: FunctionOptions<SETTINGS>): PromiseLike<RESPONSE>;
6
+ generateJsonResponse(prompt: PROMPT, options?: FunctionOptions<SETTINGS>): PromiseLike<RESPONSE>;
7
+ extractJson(response: RESPONSE): unknown;
10
8
  }
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.InstructionWithSchemaPrompt = void 0;
4
+ exports.InstructionWithSchemaPrompt = {
5
+ forSchema({ instruction, schemaDefinition, }) {
6
+ return { schemaDefinition, instruction };
7
+ },
8
+ forTool({ instruction, tool, }) {
9
+ return exports.InstructionWithSchemaPrompt.forSchema({
10
+ instruction,
11
+ schemaDefinition: tool.inputSchemaDefinition,
12
+ });
13
+ },
14
+ forToolCurried(instruction) {
15
+ return (tool) => this.forTool({ instruction, tool });
16
+ },
17
+ };
@@ -0,0 +1,26 @@
1
+ import { Tool } from "../../tool/Tool.js";
2
+ import { SchemaDefinition } from "./SchemaDefinition.js";
3
+ export type InstructionWithSchema<NAME extends string, STRUCTURE> = {
4
+ instruction: string;
5
+ schemaDefinition: SchemaDefinition<NAME, STRUCTURE>;
6
+ };
7
+ export declare const InstructionWithSchemaPrompt: {
8
+ forSchema<STRUCTURE>({ instruction, schemaDefinition, }: {
9
+ instruction: string;
10
+ schemaDefinition: SchemaDefinition<string, STRUCTURE>;
11
+ }): {
12
+ schemaDefinition: SchemaDefinition<string, STRUCTURE>;
13
+ instruction: string;
14
+ };
15
+ forTool<INPUT, OUTPUT>({ instruction, tool, }: {
16
+ instruction: string;
17
+ tool: Tool<string, INPUT, OUTPUT>;
18
+ }): {
19
+ schemaDefinition: SchemaDefinition<string, INPUT>;
20
+ instruction: string;
21
+ };
22
+ forToolCurried<INPUT_1, OUTPUT_1>(instruction: string): (tool: Tool<string, INPUT_1, OUTPUT_1>) => {
23
+ schemaDefinition: SchemaDefinition<string, INPUT_1>;
24
+ instruction: string;
25
+ };
26
+ };
@@ -0,0 +1,14 @@
1
+ export const InstructionWithSchemaPrompt = {
2
+ forSchema({ instruction, schemaDefinition, }) {
3
+ return { schemaDefinition, instruction };
4
+ },
5
+ forTool({ instruction, tool, }) {
6
+ return InstructionWithSchemaPrompt.forSchema({
7
+ instruction,
8
+ schemaDefinition: tool.inputSchemaDefinition,
9
+ });
10
+ },
11
+ forToolCurried(instruction) {
12
+ return (tool) => this.forTool({ instruction, tool });
13
+ },
14
+ };
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.JsonTextGenerationModel = void 0;
4
+ const generateText_js_1 = require("../generate-text/generateText.cjs");
5
+ class JsonTextGenerationModel {
6
+ constructor({ model, format, }) {
7
+ Object.defineProperty(this, "model", {
8
+ enumerable: true,
9
+ configurable: true,
10
+ writable: true,
11
+ value: void 0
12
+ });
13
+ Object.defineProperty(this, "format", {
14
+ enumerable: true,
15
+ configurable: true,
16
+ writable: true,
17
+ value: void 0
18
+ });
19
+ this.model = model;
20
+ this.format = format;
21
+ }
22
+ get modelInformation() {
23
+ return this.model.modelInformation;
24
+ }
25
+ get settings() {
26
+ return this.model.settings;
27
+ }
28
+ async generateJsonResponse(prompt, options) {
29
+ return await (0, generateText_js_1.generateText)(this.model, this.format.createPrompt(prompt), options);
30
+ }
31
+ extractJson(response) {
32
+ return this.format.extractJson(response);
33
+ }
34
+ withSettings(additionalSettings) {
35
+ return new JsonTextGenerationModel({
36
+ model: this.model.withSettings(additionalSettings),
37
+ format: this.format,
38
+ });
39
+ }
40
+ }
41
+ exports.JsonTextGenerationModel = JsonTextGenerationModel;
@@ -0,0 +1,25 @@
1
+ import { TextGenerationModel, TextGenerationModelSettings } from "../generate-text/TextGenerationModel.js";
2
+ import { SchemaDefinition } from "./SchemaDefinition.js";
3
+ import { InstructionWithSchema } from "./InstructionWithSchemaPrompt.js";
4
+ import { GenerateJsonModel } from "./GenerateJsonModel.js";
5
+ import { FunctionOptions } from "../FunctionOptions.js";
6
+ export type JsonTextPromptFormat = {
7
+ createPrompt: (prompt: {
8
+ instruction: string;
9
+ schemaDefinition: SchemaDefinition<string, unknown>;
10
+ }) => string;
11
+ extractJson: (response: string) => unknown;
12
+ };
13
+ export declare class JsonTextGenerationModel<SETTINGS extends TextGenerationModelSettings, MODEL extends TextGenerationModel<string, any, any, SETTINGS>> implements GenerateJsonModel<InstructionWithSchema<string, unknown>, string, SETTINGS> {
14
+ private readonly model;
15
+ private readonly format;
16
+ constructor({ model, format, }: {
17
+ model: MODEL;
18
+ format: JsonTextPromptFormat;
19
+ });
20
+ get modelInformation(): import("../ModelInformation.js").ModelInformation;
21
+ get settings(): SETTINGS;
22
+ generateJsonResponse(prompt: InstructionWithSchema<string, unknown>, options?: FunctionOptions<SETTINGS> | undefined): Promise<string>;
23
+ extractJson(response: string): unknown;
24
+ withSettings(additionalSettings: Partial<SETTINGS>): this;
25
+ }
@@ -0,0 +1,37 @@
1
+ import { generateText } from "../generate-text/generateText.js";
2
+ export class JsonTextGenerationModel {
3
+ constructor({ model, format, }) {
4
+ Object.defineProperty(this, "model", {
5
+ enumerable: true,
6
+ configurable: true,
7
+ writable: true,
8
+ value: void 0
9
+ });
10
+ Object.defineProperty(this, "format", {
11
+ enumerable: true,
12
+ configurable: true,
13
+ writable: true,
14
+ value: void 0
15
+ });
16
+ this.model = model;
17
+ this.format = format;
18
+ }
19
+ get modelInformation() {
20
+ return this.model.modelInformation;
21
+ }
22
+ get settings() {
23
+ return this.model.settings;
24
+ }
25
+ async generateJsonResponse(prompt, options) {
26
+ return await generateText(this.model, this.format.createPrompt(prompt), options);
27
+ }
28
+ extractJson(response) {
29
+ return this.format.extractJson(response);
30
+ }
31
+ withSettings(additionalSettings) {
32
+ return new JsonTextGenerationModel({
33
+ model: this.model.withSettings(additionalSettings),
34
+ format: this.format,
35
+ });
36
+ }
37
+ }
@@ -10,7 +10,7 @@ async function generateJson(model, schemaDefinition, prompt, options) {
10
10
  options,
11
11
  generateResponse: (options) => model.generateJsonResponse(expandedPrompt, options),
12
12
  extractOutputValue: (response) => {
13
- const json = expandedPrompt.extractJson(response);
13
+ const json = model.extractJson(response);
14
14
  const parseResult = schemaDefinition.schema.safeParse(json);
15
15
  if (!parseResult.success) {
16
16
  throw new SchemaValidationError_js_1.SchemaValidationError({
@@ -1,14 +1,14 @@
1
1
  import { FunctionOptions } from "../FunctionOptions.js";
2
2
  import { CallMetadata } from "../executeCall.js";
3
- import { GenerateJsonModel, GenerateJsonModelSettings, GenerateJsonPrompt } from "./GenerateJsonModel.js";
3
+ import { GenerateJsonModel, GenerateJsonModelSettings } from "./GenerateJsonModel.js";
4
4
  import { SchemaDefinition } from "./SchemaDefinition.js";
5
- export declare function generateJson<STRUCTURE, PROMPT, RESPONSE, NAME extends string, SETTINGS extends GenerateJsonModelSettings>(model: GenerateJsonModel<PROMPT, RESPONSE, SETTINGS>, schemaDefinition: SchemaDefinition<NAME, STRUCTURE>, prompt: (schemaDefinition: SchemaDefinition<NAME, STRUCTURE>) => PROMPT & GenerateJsonPrompt<RESPONSE>, options: FunctionOptions<SETTINGS> & {
5
+ export declare function generateJson<STRUCTURE, PROMPT, RESPONSE, NAME extends string, SETTINGS extends GenerateJsonModelSettings>(model: GenerateJsonModel<PROMPT, RESPONSE, SETTINGS>, schemaDefinition: SchemaDefinition<NAME, STRUCTURE>, prompt: (schemaDefinition: SchemaDefinition<NAME, STRUCTURE>) => PROMPT, options: FunctionOptions<SETTINGS> & {
6
6
  fullResponse: true;
7
7
  }): Promise<{
8
8
  value: STRUCTURE;
9
9
  response: RESPONSE;
10
10
  metadata: CallMetadata<GenerateJsonModel<PROMPT, RESPONSE, SETTINGS>>;
11
11
  }>;
12
- export declare function generateJson<STRUCTURE, PROMPT, RESPONSE, NAME extends string, SETTINGS extends GenerateJsonModelSettings>(model: GenerateJsonModel<PROMPT, RESPONSE, SETTINGS>, schemaDefinition: SchemaDefinition<NAME, STRUCTURE>, prompt: (schemaDefinition: SchemaDefinition<NAME, STRUCTURE>) => PROMPT & GenerateJsonPrompt<RESPONSE>, options?: FunctionOptions<SETTINGS> & {
12
+ export declare function generateJson<STRUCTURE, PROMPT, RESPONSE, NAME extends string, SETTINGS extends GenerateJsonModelSettings>(model: GenerateJsonModel<PROMPT, RESPONSE, SETTINGS>, schemaDefinition: SchemaDefinition<NAME, STRUCTURE>, prompt: (schemaDefinition: SchemaDefinition<NAME, STRUCTURE>) => PROMPT, options?: FunctionOptions<SETTINGS> & {
13
13
  fullResponse?: false;
14
14
  }): Promise<STRUCTURE>;
@@ -7,7 +7,7 @@ export async function generateJson(model, schemaDefinition, prompt, options) {
7
7
  options,
8
8
  generateResponse: (options) => model.generateJsonResponse(expandedPrompt, options),
9
9
  extractOutputValue: (response) => {
10
- const json = expandedPrompt.extractJson(response);
10
+ const json = model.extractJson(response);
11
11
  const parseResult = schemaDefinition.schema.safeParse(json);
12
12
  if (!parseResult.success) {
13
13
  throw new SchemaValidationError({
@@ -17,7 +17,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./FunctionOptions.cjs"), exports);
18
18
  __exportStar(require("./Model.cjs"), exports);
19
19
  __exportStar(require("./ModelCallEvent.cjs"), exports);
20
- __exportStar(require("../run/RunFunctionObserver.cjs"), exports);
21
20
  __exportStar(require("./ModelInformation.cjs"), exports);
22
21
  __exportStar(require("./SuccessfulModelCall.cjs"), exports);
23
22
  __exportStar(require("./embed-text/TextEmbeddingEvent.cjs"), exports);
@@ -28,17 +27,19 @@ __exportStar(require("./generate-image/ImageGenerationModel.cjs"), exports);
28
27
  __exportStar(require("./generate-image/generateImage.cjs"), exports);
29
28
  __exportStar(require("./generate-json/GenerateJsonModel.cjs"), exports);
30
29
  __exportStar(require("./generate-json/GenerateJsonOrTextModel.cjs"), exports);
30
+ __exportStar(require("./generate-json/InstructionWithSchemaPrompt.cjs"), exports);
31
31
  __exportStar(require("./generate-json/JsonGenerationEvent.cjs"), exports);
32
+ __exportStar(require("./generate-json/JsonTextGenerationModel.cjs"), exports);
32
33
  __exportStar(require("./generate-json/NoSuchSchemaError.cjs"), exports);
33
34
  __exportStar(require("./generate-json/SchemaDefinition.cjs"), exports);
34
35
  __exportStar(require("./generate-json/SchemaValidationError.cjs"), exports);
35
36
  __exportStar(require("./generate-json/generateJson.cjs"), exports);
36
37
  __exportStar(require("./generate-json/generateJsonOrText.cjs"), exports);
38
+ __exportStar(require("./generate-text/DeltaEvent.cjs"), exports);
39
+ __exportStar(require("./generate-text/TextDeltaEventSource.cjs"), exports);
37
40
  __exportStar(require("./generate-text/TextGenerationEvent.cjs"), exports);
38
41
  __exportStar(require("./generate-text/TextGenerationModel.cjs"), exports);
39
42
  __exportStar(require("./generate-text/generateText.cjs"), exports);
40
- __exportStar(require("./generate-text/DeltaEvent.cjs"), exports);
41
- __exportStar(require("./generate-text/TextDeltaEventSource.cjs"), exports);
42
43
  __exportStar(require("./generate-text/streamText.cjs"), exports);
43
44
  __exportStar(require("./tokenize-text/Tokenizer.cjs"), exports);
44
45
  __exportStar(require("./tokenize-text/countTokens.cjs"), exports);
@@ -1,7 +1,6 @@
1
1
  export * from "./FunctionOptions.js";
2
2
  export * from "./Model.js";
3
3
  export * from "./ModelCallEvent.js";
4
- export * from "../run/RunFunctionObserver.js";
5
4
  export * from "./ModelInformation.js";
6
5
  export * from "./SuccessfulModelCall.js";
7
6
  export * from "./embed-text/TextEmbeddingEvent.js";
@@ -12,17 +11,19 @@ export * from "./generate-image/ImageGenerationModel.js";
12
11
  export * from "./generate-image/generateImage.js";
13
12
  export * from "./generate-json/GenerateJsonModel.js";
14
13
  export * from "./generate-json/GenerateJsonOrTextModel.js";
14
+ export * from "./generate-json/InstructionWithSchemaPrompt.js";
15
15
  export * from "./generate-json/JsonGenerationEvent.js";
16
+ export * from "./generate-json/JsonTextGenerationModel.js";
16
17
  export * from "./generate-json/NoSuchSchemaError.js";
17
18
  export * from "./generate-json/SchemaDefinition.js";
18
19
  export * from "./generate-json/SchemaValidationError.js";
19
20
  export * from "./generate-json/generateJson.js";
20
21
  export * from "./generate-json/generateJsonOrText.js";
22
+ export * from "./generate-text/DeltaEvent.js";
23
+ export * from "./generate-text/TextDeltaEventSource.js";
21
24
  export * from "./generate-text/TextGenerationEvent.js";
22
25
  export * from "./generate-text/TextGenerationModel.js";
23
26
  export * from "./generate-text/generateText.js";
24
- export * from "./generate-text/DeltaEvent.js";
25
- export * from "./generate-text/TextDeltaEventSource.js";
26
27
  export * from "./generate-text/streamText.js";
27
28
  export * from "./tokenize-text/Tokenizer.js";
28
29
  export * from "./tokenize-text/countTokens.js";
@@ -1,7 +1,6 @@
1
1
  export * from "./FunctionOptions.js";
2
2
  export * from "./Model.js";
3
3
  export * from "./ModelCallEvent.js";
4
- export * from "../run/RunFunctionObserver.js";
5
4
  export * from "./ModelInformation.js";
6
5
  export * from "./SuccessfulModelCall.js";
7
6
  export * from "./embed-text/TextEmbeddingEvent.js";
@@ -12,17 +11,19 @@ export * from "./generate-image/ImageGenerationModel.js";
12
11
  export * from "./generate-image/generateImage.js";
13
12
  export * from "./generate-json/GenerateJsonModel.js";
14
13
  export * from "./generate-json/GenerateJsonOrTextModel.js";
14
+ export * from "./generate-json/InstructionWithSchemaPrompt.js";
15
15
  export * from "./generate-json/JsonGenerationEvent.js";
16
+ export * from "./generate-json/JsonTextGenerationModel.js";
16
17
  export * from "./generate-json/NoSuchSchemaError.js";
17
18
  export * from "./generate-json/SchemaDefinition.js";
18
19
  export * from "./generate-json/SchemaValidationError.js";
19
20
  export * from "./generate-json/generateJson.js";
20
21
  export * from "./generate-json/generateJsonOrText.js";
22
+ export * from "./generate-text/DeltaEvent.js";
23
+ export * from "./generate-text/TextDeltaEventSource.js";
21
24
  export * from "./generate-text/TextGenerationEvent.js";
22
25
  export * from "./generate-text/TextGenerationModel.js";
23
26
  export * from "./generate-text/generateText.js";
24
- export * from "./generate-text/DeltaEvent.js";
25
- export * from "./generate-text/TextDeltaEventSource.js";
26
27
  export * from "./generate-text/streamText.js";
27
28
  export * from "./tokenize-text/Tokenizer.js";
28
29
  export * from "./tokenize-text/countTokens.js";
@@ -4,6 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.OpenAIChatResponseFormat = exports.OpenAIChatModel = exports.calculateOpenAIChatCostInMillicents = exports.isOpenAIChatModel = exports.OPENAI_CHAT_MODELS = void 0;
7
+ const secure_json_parse_1 = __importDefault(require("secure-json-parse"));
7
8
  const zod_1 = __importDefault(require("zod"));
8
9
  const AbstractModel_js_1 = require("../../../model-function/AbstractModel.cjs");
9
10
  const PromptMappingTextGenerationModel_js_1 = require("../../../prompt/PromptMappingTextGenerationModel.cjs");
@@ -200,6 +201,10 @@ class OpenAIChatModel extends AbstractModel_js_1.AbstractModel {
200
201
  run: options?.run,
201
202
  });
202
203
  }
204
+ extractJson(response) {
205
+ const jsonText = response.choices[0].message.function_call.arguments;
206
+ return secure_json_parse_1.default.parse(jsonText);
207
+ }
203
208
  mapPrompt(promptMapping) {
204
209
  return new PromptMappingTextGenerationModel_js_1.PromptMappingTextGenerationModel({
205
210
  model: this.withStopTokens(promptMapping.stopTokens),
@@ -1,6 +1,7 @@
1
1
  import z from "zod";
2
2
  import { AbstractModel } from "../../../model-function/AbstractModel.js";
3
3
  import { FunctionOptions } from "../../../model-function/FunctionOptions.js";
4
+ import { GenerateJsonModel } from "../../../model-function/generate-json/GenerateJsonModel.js";
4
5
  import { GenerateJsonOrTextModel } from "../../../model-function/generate-json/GenerateJsonOrTextModel.js";
5
6
  import { DeltaEvent } from "../../../model-function/generate-text/DeltaEvent.js";
6
7
  import { TextGenerationModel, TextGenerationModelSettings } from "../../../model-function/generate-text/TextGenerationModel.js";
@@ -116,7 +117,7 @@ export interface OpenAIChatSettings extends TextGenerationModelSettings, OpenAIM
116
117
  * ),
117
118
  * ]);
118
119
  */
119
- export declare class OpenAIChatModel extends AbstractModel<OpenAIChatSettings> implements TextGenerationModel<OpenAIChatMessage[], OpenAIChatResponse, OpenAIChatDelta, OpenAIChatSettings>, GenerateJsonOrTextModel<OpenAIChatSingleFunctionPrompt<unknown> | OpenAIChatAutoFunctionPrompt<Array<OpenAIFunctionDescription<unknown>>>, OpenAIChatResponse, OpenAIChatSettings> {
120
+ export declare class OpenAIChatModel extends AbstractModel<OpenAIChatSettings> implements TextGenerationModel<OpenAIChatMessage[], OpenAIChatResponse, OpenAIChatDelta, OpenAIChatSettings>, GenerateJsonModel<OpenAIChatSingleFunctionPrompt<unknown>, OpenAIChatResponse, OpenAIChatSettings>, GenerateJsonOrTextModel<OpenAIChatAutoFunctionPrompt<Array<OpenAIFunctionDescription<unknown>>>, OpenAIChatResponse, OpenAIChatSettings> {
120
121
  constructor(settings: OpenAIChatSettings);
121
122
  readonly provider: "openai";
122
123
  get modelName(): "gpt-4" | "gpt-4-0314" | "gpt-4-0613" | "gpt-4-32k" | "gpt-4-32k-0314" | "gpt-4-32k-0613" | "gpt-3.5-turbo" | "gpt-3.5-turbo-0301" | "gpt-3.5-turbo-0613" | "gpt-3.5-turbo-16k" | "gpt-3.5-turbo-16k-0613";
@@ -168,6 +169,7 @@ export declare class OpenAIChatModel extends AbstractModel<OpenAIChatSettings> i
168
169
  * @see https://platform.openai.com/docs/guides/gpt/function-calling
169
170
  */
170
171
  generateJsonResponse(prompt: OpenAIChatSingleFunctionPrompt<unknown> | OpenAIChatAutoFunctionPrompt<Array<OpenAIFunctionDescription<unknown>>>, options?: FunctionOptions<OpenAIChatSettings> | undefined): PromiseLike<OpenAIChatResponse>;
172
+ extractJson(response: OpenAIChatResponse): unknown;
171
173
  mapPrompt<INPUT_PROMPT>(promptMapping: PromptMapping<INPUT_PROMPT, OpenAIChatMessage[]>): PromptMappingTextGenerationModel<INPUT_PROMPT, OpenAIChatMessage[], OpenAIChatResponse, OpenAIChatDelta, OpenAIChatSettings, this>;
172
174
  withSettings(additionalSettings: Partial<OpenAIChatSettings>): this;
173
175
  get maxCompletionTokens(): number | undefined;
@@ -1,3 +1,4 @@
1
+ import SecureJSON from "secure-json-parse";
1
2
  import z from "zod";
2
3
  import { AbstractModel } from "../../../model-function/AbstractModel.js";
3
4
  import { PromptMappingTextGenerationModel } from "../../../prompt/PromptMappingTextGenerationModel.js";
@@ -192,6 +193,10 @@ export class OpenAIChatModel extends AbstractModel {
192
193
  run: options?.run,
193
194
  });
194
195
  }
196
+ extractJson(response) {
197
+ const jsonText = response.choices[0].message.function_call.arguments;
198
+ return SecureJSON.parse(jsonText);
199
+ }
195
200
  mapPrompt(promptMapping) {
196
201
  return new PromptMappingTextGenerationModel({
197
202
  model: this.withStopTokens(promptMapping.stopTokens),
@@ -78,10 +78,6 @@ class OpenAIChatSingleFunctionPrompt {
78
78
  this.messages = messages;
79
79
  this.fn = fn;
80
80
  }
81
- extractJson(response) {
82
- const jsonText = response.choices[0].message.function_call.arguments;
83
- return secure_json_parse_1.default.parse(jsonText);
84
- }
85
81
  get functionCall() {
86
82
  return { name: this.fn.name };
87
83
  }
@@ -1,5 +1,4 @@
1
1
  import z from "zod";
2
- import { GenerateJsonPrompt } from "../../../model-function/generate-json/GenerateJsonModel.js";
3
2
  import { GenerateJsonOrTextPrompt } from "../../../model-function/generate-json/GenerateJsonOrTextModel.js";
4
3
  import { SchemaDefinition } from "../../../model-function/generate-json/SchemaDefinition.js";
5
4
  import { Tool } from "../../../tool/Tool.js";
@@ -56,14 +55,13 @@ export declare const OpenAIChatFunctionPrompt: {
56
55
  parameters: z.ZodType<any, z.ZodTypeDef, any>;
57
56
  }[]>;
58
57
  };
59
- export declare class OpenAIChatSingleFunctionPrompt<T> implements GenerateJsonPrompt<OpenAIChatResponse> {
58
+ export declare class OpenAIChatSingleFunctionPrompt<FUNCTION> {
60
59
  readonly messages: OpenAIChatMessage[];
61
- readonly fn: OpenAIFunctionDescription<T>;
60
+ readonly fn: OpenAIFunctionDescription<FUNCTION>;
62
61
  constructor({ messages, fn, }: {
63
62
  messages: OpenAIChatMessage[];
64
- fn: OpenAIFunctionDescription<T>;
63
+ fn: OpenAIFunctionDescription<FUNCTION>;
65
64
  });
66
- extractJson(response: OpenAIChatResponse): any;
67
65
  get functionCall(): {
68
66
  name: string;
69
67
  };
@@ -72,10 +72,6 @@ export class OpenAIChatSingleFunctionPrompt {
72
72
  this.messages = messages;
73
73
  this.fn = fn;
74
74
  }
75
- extractJson(response) {
76
- const jsonText = response.choices[0].message.function_call.arguments;
77
- return SecureJSON.parse(jsonText);
78
- }
79
75
  get functionCall() {
80
76
  return { name: this.fn.name };
81
77
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "modelfusion",
3
3
  "description": "Build AI applications, chatbots, and agents with JavaScript and TypeScript.",
4
- "version": "0.10.0",
4
+ "version": "0.12.0",
5
5
  "author": "Lars Grammel",
6
6
  "license": "MIT",
7
7
  "keywords": [
@@ -67,11 +67,11 @@
67
67
  "eslint": "^8.45.0",
68
68
  "eslint-config-prettier": "9.0.0",
69
69
  "husky": "^8.0.3",
70
- "lint-staged": "13.2.3",
70
+ "lint-staged": "14.0.0",
71
71
  "prettier": "3.0.1",
72
72
  "rimraf": "5.0.1",
73
73
  "typescript": "5.1.6",
74
- "zod": "3.21.4",
74
+ "zod": "3.22.0",
75
75
  "zod-to-json-schema": "3.21.4"
76
76
  },
77
77
  "peerDependencies": {
@@ -13,31 +13,39 @@ const OUTPUT_SCHEMA = zod_1.z.object({
13
13
  snippet: zod_1.z.string(),
14
14
  })),
15
15
  });
16
- /**
17
- * @see https://serpapi.com/search-api
18
- */
19
16
  class WebSearchTool extends Tool_js_1.Tool {
20
- constructor(options) {
17
+ constructor({ name, description, queryDescription = "Search query", execute, }) {
21
18
  super({
22
- name: options.name,
23
- description: options.description,
24
- inputSchema: INPUT_SCHEMA,
19
+ name,
20
+ description,
21
+ inputSchema: WebSearchTool.createInputSchema(queryDescription),
25
22
  outputSchema: OUTPUT_SCHEMA,
26
- execute: options.execute,
23
+ execute,
27
24
  });
25
+ Object.defineProperty(this, "outputSchema", {
26
+ enumerable: true,
27
+ configurable: true,
28
+ writable: true,
29
+ value: void 0
30
+ });
31
+ this.outputSchema = OUTPUT_SCHEMA;
28
32
  }
29
33
  }
30
34
  exports.WebSearchTool = WebSearchTool;
31
35
  // expose the schemas to library consumers:
32
- Object.defineProperty(WebSearchTool, "INPUT_SCHEMA", {
36
+ Object.defineProperty(WebSearchTool, "createInputSchema", {
33
37
  enumerable: true,
34
38
  configurable: true,
35
39
  writable: true,
36
- value: INPUT_SCHEMA
40
+ value: (description) =>
41
+ // same structure, but with description:
42
+ zod_1.z.object({
43
+ query: zod_1.z.string().describe(description),
44
+ })
37
45
  });
38
- Object.defineProperty(WebSearchTool, "OUTPUT_SCHEMA", {
46
+ Object.defineProperty(WebSearchTool, "createOutputSchema", {
39
47
  enumerable: true,
40
48
  configurable: true,
41
49
  writable: true,
42
- value: OUTPUT_SCHEMA
50
+ value: () => OUTPUT_SCHEMA
43
51
  });
@@ -34,18 +34,15 @@ declare const OUTPUT_SCHEMA: z.ZodObject<{
34
34
  snippet: string;
35
35
  }[];
36
36
  }>;
37
- /**
38
- * @see https://serpapi.com/search-api
39
- */
40
37
  export declare class WebSearchTool<NAME extends string> extends Tool<NAME, z.infer<typeof INPUT_SCHEMA>, z.infer<typeof OUTPUT_SCHEMA>> {
41
- static readonly INPUT_SCHEMA: z.ZodObject<{
38
+ static readonly createInputSchema: (description: string) => z.ZodObject<{
42
39
  query: z.ZodString;
43
40
  }, "strip", z.ZodTypeAny, {
44
41
  query: string;
45
42
  }, {
46
43
  query: string;
47
44
  }>;
48
- static readonly OUTPUT_SCHEMA: z.ZodObject<{
45
+ static readonly createOutputSchema: () => z.ZodObject<{
49
46
  results: z.ZodArray<z.ZodObject<{
50
47
  title: z.ZodString;
51
48
  link: z.ZodString;
@@ -72,9 +69,11 @@ export declare class WebSearchTool<NAME extends string> extends Tool<NAME, z.inf
72
69
  snippet: string;
73
70
  }[];
74
71
  }>;
75
- constructor(options: {
72
+ readonly outputSchema: typeof OUTPUT_SCHEMA;
73
+ constructor({ name, description, queryDescription, execute, }: {
76
74
  name: NAME;
77
75
  description: string;
76
+ queryDescription?: string;
78
77
  execute(input: z.infer<typeof INPUT_SCHEMA>): Promise<z.infer<typeof OUTPUT_SCHEMA>>;
79
78
  });
80
79
  }
@@ -10,30 +10,38 @@ const OUTPUT_SCHEMA = z.object({
10
10
  snippet: z.string(),
11
11
  })),
12
12
  });
13
- /**
14
- * @see https://serpapi.com/search-api
15
- */
16
13
  export class WebSearchTool extends Tool {
17
- constructor(options) {
14
+ constructor({ name, description, queryDescription = "Search query", execute, }) {
18
15
  super({
19
- name: options.name,
20
- description: options.description,
21
- inputSchema: INPUT_SCHEMA,
16
+ name,
17
+ description,
18
+ inputSchema: WebSearchTool.createInputSchema(queryDescription),
22
19
  outputSchema: OUTPUT_SCHEMA,
23
- execute: options.execute,
20
+ execute,
24
21
  });
22
+ Object.defineProperty(this, "outputSchema", {
23
+ enumerable: true,
24
+ configurable: true,
25
+ writable: true,
26
+ value: void 0
27
+ });
28
+ this.outputSchema = OUTPUT_SCHEMA;
25
29
  }
26
30
  }
27
31
  // expose the schemas to library consumers:
28
- Object.defineProperty(WebSearchTool, "INPUT_SCHEMA", {
32
+ Object.defineProperty(WebSearchTool, "createInputSchema", {
29
33
  enumerable: true,
30
34
  configurable: true,
31
35
  writable: true,
32
- value: INPUT_SCHEMA
36
+ value: (description) =>
37
+ // same structure, but with description:
38
+ z.object({
39
+ query: z.string().describe(description),
40
+ })
33
41
  });
34
- Object.defineProperty(WebSearchTool, "OUTPUT_SCHEMA", {
42
+ Object.defineProperty(WebSearchTool, "createOutputSchema", {
35
43
  enumerable: true,
36
44
  configurable: true,
37
45
  writable: true,
38
- value: OUTPUT_SCHEMA
46
+ value: () => OUTPUT_SCHEMA
39
47
  });
package/tool/useTool.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { FunctionOptions } from "../model-function/FunctionOptions.js";
2
- import { GenerateJsonModel, GenerateJsonModelSettings, GenerateJsonPrompt } from "../model-function/generate-json/GenerateJsonModel.js";
2
+ import { GenerateJsonModel, GenerateJsonModelSettings } from "../model-function/generate-json/GenerateJsonModel.js";
3
3
  import { Tool } from "./Tool.js";
4
4
  /**
5
5
  * `useTool` uses `generateJson` to generate parameters for a tool and then executes the tool with the parameters.
@@ -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 GenerateJsonModelSettings, TOOL extends Tool<any, any, any>>(model: GenerateJsonModel<PROMPT, RESPONSE, SETTINGS>, tool: TOOL, prompt: (tool: TOOL) => PROMPT & GenerateJsonPrompt<RESPONSE>, options?: FunctionOptions<SETTINGS>): Promise<{
11
+ export declare function useTool<PROMPT, RESPONSE, SETTINGS extends GenerateJsonModelSettings, TOOL extends Tool<any, any, any>>(model: GenerateJsonModel<PROMPT, RESPONSE, SETTINGS>, tool: TOOL, prompt: (tool: TOOL) => PROMPT, options?: FunctionOptions<SETTINGS>): Promise<{
12
12
  tool: TOOL["name"];
13
13
  parameters: TOOL["inputSchema"];
14
14
  result: Awaited<ReturnType<TOOL["execute"]>>;