modelfusion 0.122.0 → 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.
Files changed (33) hide show
  1. package/CHANGELOG.md +37 -0
  2. package/README.md +41 -1
  3. package/index.cjs +0 -1
  4. package/index.d.ts +0 -1
  5. package/index.js +0 -1
  6. package/model-function/ModelCallEvent.d.ts +3 -2
  7. package/model-function/classify/Classifier.cjs +2 -0
  8. package/model-function/classify/Classifier.d.ts +10 -0
  9. package/model-function/classify/Classifier.js +1 -0
  10. package/model-function/classify/ClassifyEvent.cjs +2 -0
  11. package/model-function/classify/ClassifyEvent.d.ts +20 -0
  12. package/model-function/classify/ClassifyEvent.js +1 -0
  13. package/model-function/classify/EmbeddingSimilarityClassifier.cjs +97 -0
  14. package/model-function/classify/EmbeddingSimilarityClassifier.d.ts +40 -0
  15. package/model-function/classify/EmbeddingSimilarityClassifier.js +93 -0
  16. package/model-function/classify/classify.cjs +27 -0
  17. package/model-function/classify/classify.d.ts +17 -0
  18. package/model-function/classify/classify.js +23 -0
  19. package/{classifier → model-function/classify}/index.cjs +4 -1
  20. package/model-function/classify/index.d.ts +4 -0
  21. package/model-function/classify/index.js +4 -0
  22. package/model-function/index.cjs +1 -0
  23. package/model-function/index.d.ts +1 -0
  24. package/model-function/index.js +1 -0
  25. package/model-provider/mistral/MistralTextEmbeddingModel.d.ts +13 -13
  26. package/model-provider/ollama/OllamaChatModel.d.ts +9 -9
  27. package/model-provider/openai/OpenAITextEmbeddingModel.d.ts +12 -12
  28. package/package.json +1 -1
  29. package/classifier/SemanticClassifier.cjs +0 -81
  30. package/classifier/SemanticClassifier.d.ts +0 -25
  31. package/classifier/SemanticClassifier.js +0 -77
  32. package/classifier/index.d.ts +0 -1
  33. package/classifier/index.js +0 -1
package/CHANGELOG.md CHANGED
@@ -1,5 +1,42 @@
1
1
  # Changelog
2
2
 
3
+ ## v0.123.0 - 2024-01-13
4
+
5
+ ### Added
6
+
7
+ - `classify` model function ([docs](https://modelfusion.dev/guide/function/classify)) for classifying values. The `SemanticClassifier` has been renamed to `EmbeddingSimilarityClassifier` and can be used in conjunction with `classify`:
8
+
9
+ ```ts
10
+ import { classify, EmbeddingSimilarityClassifier, openai } from "modelfusion";
11
+
12
+ const classifier = new EmbeddingSimilarityClassifier({
13
+ embeddingModel: openai.TextEmbedder({ model: "text-embedding-ada-002" }),
14
+ similarityThreshold: 0.82,
15
+ clusters: [
16
+ {
17
+ name: "politics" as const,
18
+ values: [
19
+ "they will save the country!",
20
+ // ...
21
+ ],
22
+ },
23
+ {
24
+ name: "chitchat" as const,
25
+ values: [
26
+ "how's the weather today?",
27
+ // ...
28
+ ],
29
+ },
30
+ ],
31
+ });
32
+
33
+ // strongly typed result:
34
+ const result = await classify({
35
+ model: classifier,
36
+ value: "don't you love politics?",
37
+ });
38
+ ```
39
+
3
40
  ## v0.122.0 - 2024-01-13
4
41
 
5
42
  ### Changed
package/README.md CHANGED
@@ -22,7 +22,7 @@
22
22
  - **Built for production**: ModelFusion is fully tree-shakeable, can be used in serverless environments, and only uses a minimal set of dependencies.
23
23
 
24
24
  > [!NOTE]
25
- > ModelFusion is getting closer to a stable v1, which is expected in Q1/2024. The main API is now mostly stable, but until version 1.0 there may be breaking changes. Feedback and suggestions are welcome.
25
+ > ModelFusion is getting closer to a stable v1, which is expected in Q2/2024. The main API is now mostly stable, but until version 1.0 there may be breaking changes. Feedback and suggestions are welcome.
26
26
 
27
27
  ## Quick Install
28
28
 
@@ -291,6 +291,8 @@ Providers: [OpenAI (Whisper)](https://modelfusion.dev/integration/model-provider
291
291
  Create embeddings for text and other values. Embeddings are vectors that represent the essence of the values in the context of the model.
292
292
 
293
293
  ```ts
294
+ import { embed, embedMany, openai } from "modelfusion";
295
+
294
296
  // embed single value:
295
297
  const embedding = await embed({
296
298
  model: openai.TextEmbedder({ model: "text-embedding-ada-002" }),
@@ -309,6 +311,43 @@ const embeddings = await embedMany({
309
311
 
310
312
  Providers: [OpenAI](https://modelfusion.dev/integration/model-provider/openai), [Llama.cpp](https://modelfusion.dev/integration/model-provider/llamacpp), [Ollama](https://modelfusion.dev/integration/model-provider/ollama), [Mistral](https://modelfusion.dev/integration/model-provider/mistral), [Hugging Face](https://modelfusion.dev/integration/model-provider/huggingface), [Cohere](https://modelfusion.dev/integration/model-provider/cohere)
311
313
 
314
+ ### [Classify Value](https://modelfusion.dev/guide/function/classify)
315
+
316
+ Classifies a value into a category.
317
+
318
+ ```ts
319
+ import { classify, EmbeddingSimilarityClassifier, openai } from "modelfusion";
320
+
321
+ const classifier = new EmbeddingSimilarityClassifier({
322
+ embeddingModel: openai.TextEmbedder({ model: "text-embedding-ada-002" }),
323
+ similarityThreshold: 0.82,
324
+ clusters: [
325
+ {
326
+ name: "politics" as const,
327
+ values: [
328
+ "they will save the country!",
329
+ // ...
330
+ ],
331
+ },
332
+ {
333
+ name: "chitchat" as const,
334
+ values: [
335
+ "how's the weather today?",
336
+ // ...
337
+ ],
338
+ },
339
+ ],
340
+ });
341
+
342
+ // strongly typed result:
343
+ const result = await classify({
344
+ model: classifier,
345
+ value: "don't you love politics?",
346
+ });
347
+ ```
348
+
349
+ Classifiers: [EmbeddingSimilarityClassifier](https://modelfusion.dev/guide/function/classify#embeddingsimilarityclassifier)
350
+
312
351
  ### [Tokenize Text](https://modelfusion.dev/guide/function/tokenize-text)
313
352
 
314
353
  Split text into tokens and reconstruct the text from tokens.
@@ -552,6 +591,7 @@ modelfusion.setLogFormat("detailed-object"); // log full events
552
591
  - [Generate transcription](https://modelfusion.dev/guide/function/generation-transcription)
553
592
  - [Tokenize Text](https://modelfusion.dev/guide/function/tokenize-text)
554
593
  - [Embed Value](https://modelfusion.dev/guide/function/embed)
594
+ - [Classify Value](https://modelfusion.dev/guide/function/classify)
555
595
  - [Tools](https://modelfusion.dev/guide/tools)
556
596
  - [Use Tool](https://modelfusion.dev/guide/tools/use-tool)
557
597
  - [Use Tools](https://modelfusion.dev/guide/tools/use-tools)
package/index.cjs CHANGED
@@ -14,7 +14,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./classifier/index.cjs"), exports);
18
17
  __exportStar(require("./core/index.cjs"), exports);
19
18
  __exportStar(require("./model-function/index.cjs"), exports);
20
19
  __exportStar(require("./model-provider/index.cjs"), exports);
package/index.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- export * from "./classifier/index.js";
2
1
  export * from "./core/index.js";
3
2
  export * from "./model-function/index.js";
4
3
  export * from "./model-provider/index.js";
package/index.js CHANGED
@@ -1,4 +1,3 @@
1
- export * from "./classifier/index.js";
2
1
  export * from "./core/index.js";
3
2
  export * from "./model-function/index.js";
4
3
  export * from "./model-provider/index.js";
@@ -2,6 +2,7 @@ import { BaseFunctionFinishedEvent, BaseFunctionStartedEvent } from "../core/Fun
2
2
  import { ToolCallGenerationFinishedEvent, ToolCallGenerationStartedEvent } from "../tool/generate-tool-call/ToolCallGenerationEvent.js";
3
3
  import { ToolCallsGenerationFinishedEvent, ToolCallsGenerationStartedEvent } from "../tool/generate-tool-calls/ToolCallsGenerationEvent.js";
4
4
  import { ModelInformation } from "./ModelInformation.js";
5
+ import { ClassifyFinishedEvent, ClassifyStartedEvent } from "./classify/ClassifyEvent.js";
5
6
  import { EmbeddingFinishedEvent, EmbeddingStartedEvent } from "./embed/EmbeddingEvent.js";
6
7
  import { ImageGenerationFinishedEvent, ImageGenerationStartedEvent } from "./generate-image/ImageGenerationEvent.js";
7
8
  import { SpeechGenerationFinishedEvent, SpeechGenerationStartedEvent, SpeechStreamingFinishedEvent, SpeechStreamingStartedEvent } from "./generate-speech/SpeechGenerationEvent.js";
@@ -52,5 +53,5 @@ export interface BaseModelCallFinishedEvent extends BaseFunctionFinishedEvent {
52
53
  */
53
54
  result: BaseModelCallFinishedEventResult;
54
55
  }
55
- export type ModelCallStartedEvent = EmbeddingStartedEvent | ImageGenerationStartedEvent | SpeechGenerationStartedEvent | SpeechStreamingStartedEvent | StructureGenerationStartedEvent | StructureStreamingStartedEvent | TextGenerationStartedEvent | TextStreamingStartedEvent | ToolCallGenerationStartedEvent | ToolCallsGenerationStartedEvent | TranscriptionStartedEvent;
56
- export type ModelCallFinishedEvent = EmbeddingFinishedEvent | ImageGenerationFinishedEvent | SpeechGenerationFinishedEvent | SpeechStreamingFinishedEvent | StructureGenerationFinishedEvent | StructureStreamingFinishedEvent | TextGenerationFinishedEvent | TextStreamingFinishedEvent | ToolCallGenerationFinishedEvent | ToolCallsGenerationFinishedEvent | TranscriptionFinishedEvent;
56
+ export type ModelCallStartedEvent = ClassifyStartedEvent | EmbeddingStartedEvent | ImageGenerationStartedEvent | SpeechGenerationStartedEvent | SpeechStreamingStartedEvent | StructureGenerationStartedEvent | StructureStreamingStartedEvent | TextGenerationStartedEvent | TextStreamingStartedEvent | ToolCallGenerationStartedEvent | ToolCallsGenerationStartedEvent | TranscriptionStartedEvent;
57
+ export type ModelCallFinishedEvent = ClassifyFinishedEvent | EmbeddingFinishedEvent | ImageGenerationFinishedEvent | SpeechGenerationFinishedEvent | SpeechStreamingFinishedEvent | StructureGenerationFinishedEvent | StructureStreamingFinishedEvent | TextGenerationFinishedEvent | TextStreamingFinishedEvent | ToolCallGenerationFinishedEvent | ToolCallsGenerationFinishedEvent | TranscriptionFinishedEvent;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,10 @@
1
+ import { FunctionCallOptions } from "../../core/FunctionOptions.js";
2
+ import { Model, ModelSettings } from "../Model.js";
3
+ export interface ClassifierSettings extends ModelSettings {
4
+ }
5
+ export interface Classifier<VALUE, CLASS extends string | null, SETTINGS extends ClassifierSettings = ClassifierSettings> extends Model<SETTINGS> {
6
+ doClassify(value: VALUE, options: FunctionCallOptions): PromiseLike<{
7
+ rawResponse: unknown | undefined;
8
+ class: CLASS;
9
+ }>;
10
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,20 @@
1
+ import { BaseModelCallFinishedEvent, BaseModelCallStartedEvent } from "../ModelCallEvent.js";
2
+ export interface ClassifyStartedEvent extends BaseModelCallStartedEvent {
3
+ functionType: "classify";
4
+ input: unknown | Array<unknown>;
5
+ }
6
+ export type ClassifyFinishedEventResult = {
7
+ status: "success";
8
+ rawResponse: unknown;
9
+ value: unknown;
10
+ } | {
11
+ status: "error";
12
+ error: unknown;
13
+ } | {
14
+ status: "abort";
15
+ };
16
+ export interface ClassifyFinishedEvent extends BaseModelCallFinishedEvent {
17
+ functionType: "classify";
18
+ input: unknown;
19
+ result: ClassifyFinishedEventResult;
20
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,97 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EmbeddingSimilarityClassifier = void 0;
4
+ const cosineSimilarity_js_1 = require("../../util/cosineSimilarity.cjs");
5
+ const embed_js_1 = require("../embed/embed.cjs");
6
+ /**
7
+ * Classifies values based on their distance to the values from a set of clusters.
8
+ * When the distance is below a certain threshold, the value is classified as belonging to the cluster,
9
+ * and the cluster name is returned. Otherwise, the value is classified as null.
10
+ */
11
+ class EmbeddingSimilarityClassifier {
12
+ constructor(settings) {
13
+ Object.defineProperty(this, "settings", {
14
+ enumerable: true,
15
+ configurable: true,
16
+ writable: true,
17
+ value: void 0
18
+ });
19
+ Object.defineProperty(this, "modelInformation", {
20
+ enumerable: true,
21
+ configurable: true,
22
+ writable: true,
23
+ value: {
24
+ provider: "modelfusion",
25
+ modelName: "EmbeddingSimilarityClassifier",
26
+ }
27
+ });
28
+ Object.defineProperty(this, "embeddings", {
29
+ enumerable: true,
30
+ configurable: true,
31
+ writable: true,
32
+ value: void 0
33
+ });
34
+ this.settings = settings;
35
+ }
36
+ async getEmbeddings(options) {
37
+ if (this.embeddings != null) {
38
+ return this.embeddings;
39
+ }
40
+ const embeddings = [];
41
+ for (const cluster of this.settings.clusters) {
42
+ const clusterEmbeddings = await (0, embed_js_1.embedMany)({
43
+ model: this.settings.embeddingModel,
44
+ values: cluster.values,
45
+ ...options,
46
+ });
47
+ for (let i = 0; i < clusterEmbeddings.length; i++) {
48
+ embeddings.push({
49
+ embedding: clusterEmbeddings[i],
50
+ clusterValue: cluster.values[i],
51
+ clusterName: cluster.name,
52
+ });
53
+ }
54
+ }
55
+ this.embeddings = embeddings; // lazy caching
56
+ return embeddings;
57
+ }
58
+ async doClassify(value, options) {
59
+ const valueEmbedding = await (0, embed_js_1.embed)({
60
+ model: this.settings.embeddingModel,
61
+ value,
62
+ ...options,
63
+ });
64
+ const clusterEmbeddings = await this.getEmbeddings(options);
65
+ const allMatches = [];
66
+ for (const embedding of clusterEmbeddings) {
67
+ const similarity = (0, cosineSimilarity_js_1.cosineSimilarity)(valueEmbedding, embedding.embedding);
68
+ if (similarity >= this.settings.similarityThreshold) {
69
+ allMatches.push({
70
+ similarity,
71
+ clusterValue: embedding.clusterValue,
72
+ clusterName: embedding.clusterName,
73
+ });
74
+ }
75
+ }
76
+ // sort (highest similarity first)
77
+ allMatches.sort((a, b) => b.similarity - a.similarity);
78
+ return {
79
+ class: allMatches.length > 0
80
+ ? allMatches[0].clusterName
81
+ : null,
82
+ rawResponse: undefined,
83
+ };
84
+ }
85
+ get settingsForEvent() {
86
+ const eventSettingProperties = [
87
+ "clusters",
88
+ "embeddingModel",
89
+ "similarityThreshold",
90
+ ];
91
+ return Object.fromEntries(Object.entries(this.settings).filter(([key]) => eventSettingProperties.includes(key)));
92
+ }
93
+ withSettings(additionalSettings) {
94
+ return new EmbeddingSimilarityClassifier(Object.assign({}, this.settings, additionalSettings));
95
+ }
96
+ }
97
+ exports.EmbeddingSimilarityClassifier = EmbeddingSimilarityClassifier;
@@ -0,0 +1,40 @@
1
+ import { FunctionCallOptions } from "../../core/FunctionOptions.js";
2
+ import { Vector } from "../../core/Vector.js";
3
+ import { EmbeddingModel } from "../embed/EmbeddingModel.js";
4
+ import { Classifier, ClassifierSettings } from "./Classifier.js";
5
+ export interface ValueCluster<VALUE, NAME extends string> {
6
+ name: NAME;
7
+ values: VALUE[];
8
+ }
9
+ export interface EmbeddingSimilarityClassifierSettings<VALUE, CLUSTERS extends Array<ValueCluster<VALUE, string>>> extends ClassifierSettings {
10
+ clusters: CLUSTERS;
11
+ embeddingModel: EmbeddingModel<VALUE>;
12
+ similarityThreshold: number;
13
+ }
14
+ /**
15
+ * Classifies values based on their distance to the values from a set of clusters.
16
+ * When the distance is below a certain threshold, the value is classified as belonging to the cluster,
17
+ * and the cluster name is returned. Otherwise, the value is classified as null.
18
+ */
19
+ export declare class EmbeddingSimilarityClassifier<VALUE, CLUSTERS extends Array<ValueCluster<VALUE, string>>> implements Classifier<VALUE, ClusterNames<CLUSTERS> | null, EmbeddingSimilarityClassifierSettings<VALUE, CLUSTERS>> {
20
+ readonly settings: EmbeddingSimilarityClassifierSettings<VALUE, CLUSTERS>;
21
+ readonly modelInformation: {
22
+ provider: string;
23
+ modelName: string;
24
+ };
25
+ private embeddings;
26
+ constructor(settings: EmbeddingSimilarityClassifierSettings<VALUE, CLUSTERS>);
27
+ getEmbeddings(options: FunctionCallOptions): Promise<{
28
+ embedding: Vector;
29
+ clusterValue: VALUE;
30
+ clusterName: string;
31
+ }[]>;
32
+ doClassify(value: VALUE, options: FunctionCallOptions): Promise<{
33
+ class: ClusterNames<CLUSTERS> | null;
34
+ rawResponse: undefined;
35
+ }>;
36
+ get settingsForEvent(): Partial<EmbeddingSimilarityClassifierSettings<VALUE, CLUSTERS>>;
37
+ withSettings(additionalSettings: Partial<EmbeddingSimilarityClassifierSettings<VALUE, CLUSTERS>>): this;
38
+ }
39
+ type ClusterNames<CLUSTERS> = CLUSTERS extends Array<ValueCluster<unknown, infer NAME>> ? NAME : never;
40
+ export {};
@@ -0,0 +1,93 @@
1
+ import { cosineSimilarity } from "../../util/cosineSimilarity.js";
2
+ import { embed, embedMany } from "../embed/embed.js";
3
+ /**
4
+ * Classifies values based on their distance to the values from a set of clusters.
5
+ * When the distance is below a certain threshold, the value is classified as belonging to the cluster,
6
+ * and the cluster name is returned. Otherwise, the value is classified as null.
7
+ */
8
+ export class EmbeddingSimilarityClassifier {
9
+ constructor(settings) {
10
+ Object.defineProperty(this, "settings", {
11
+ enumerable: true,
12
+ configurable: true,
13
+ writable: true,
14
+ value: void 0
15
+ });
16
+ Object.defineProperty(this, "modelInformation", {
17
+ enumerable: true,
18
+ configurable: true,
19
+ writable: true,
20
+ value: {
21
+ provider: "modelfusion",
22
+ modelName: "EmbeddingSimilarityClassifier",
23
+ }
24
+ });
25
+ Object.defineProperty(this, "embeddings", {
26
+ enumerable: true,
27
+ configurable: true,
28
+ writable: true,
29
+ value: void 0
30
+ });
31
+ this.settings = settings;
32
+ }
33
+ async getEmbeddings(options) {
34
+ if (this.embeddings != null) {
35
+ return this.embeddings;
36
+ }
37
+ const embeddings = [];
38
+ for (const cluster of this.settings.clusters) {
39
+ const clusterEmbeddings = await embedMany({
40
+ model: this.settings.embeddingModel,
41
+ values: cluster.values,
42
+ ...options,
43
+ });
44
+ for (let i = 0; i < clusterEmbeddings.length; i++) {
45
+ embeddings.push({
46
+ embedding: clusterEmbeddings[i],
47
+ clusterValue: cluster.values[i],
48
+ clusterName: cluster.name,
49
+ });
50
+ }
51
+ }
52
+ this.embeddings = embeddings; // lazy caching
53
+ return embeddings;
54
+ }
55
+ async doClassify(value, options) {
56
+ const valueEmbedding = await embed({
57
+ model: this.settings.embeddingModel,
58
+ value,
59
+ ...options,
60
+ });
61
+ const clusterEmbeddings = await this.getEmbeddings(options);
62
+ const allMatches = [];
63
+ for (const embedding of clusterEmbeddings) {
64
+ const similarity = cosineSimilarity(valueEmbedding, embedding.embedding);
65
+ if (similarity >= this.settings.similarityThreshold) {
66
+ allMatches.push({
67
+ similarity,
68
+ clusterValue: embedding.clusterValue,
69
+ clusterName: embedding.clusterName,
70
+ });
71
+ }
72
+ }
73
+ // sort (highest similarity first)
74
+ allMatches.sort((a, b) => b.similarity - a.similarity);
75
+ return {
76
+ class: allMatches.length > 0
77
+ ? allMatches[0].clusterName
78
+ : null,
79
+ rawResponse: undefined,
80
+ };
81
+ }
82
+ get settingsForEvent() {
83
+ const eventSettingProperties = [
84
+ "clusters",
85
+ "embeddingModel",
86
+ "similarityThreshold",
87
+ ];
88
+ return Object.fromEntries(Object.entries(this.settings).filter(([key]) => eventSettingProperties.includes(key)));
89
+ }
90
+ withSettings(additionalSettings) {
91
+ return new EmbeddingSimilarityClassifier(Object.assign({}, this.settings, additionalSettings));
92
+ }
93
+ }
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.classify = void 0;
4
+ const executeStandardCall_js_1 = require("../executeStandardCall.cjs");
5
+ async function classify({ model, value, fullResponse, ...options }) {
6
+ const callResponse = await (0, executeStandardCall_js_1.executeStandardCall)({
7
+ functionType: "classify",
8
+ input: value,
9
+ model,
10
+ options,
11
+ generateResponse: async (options) => {
12
+ const result = await model.doClassify(value, options);
13
+ return {
14
+ rawResponse: result.rawResponse,
15
+ extractedValue: result.class,
16
+ };
17
+ },
18
+ });
19
+ return fullResponse
20
+ ? {
21
+ class: callResponse.value,
22
+ rawResponse: callResponse.rawResponse,
23
+ metadata: callResponse.metadata,
24
+ }
25
+ : callResponse.value;
26
+ }
27
+ exports.classify = classify;
@@ -0,0 +1,17 @@
1
+ import { FunctionOptions } from "../../core/FunctionOptions.js";
2
+ import { ModelCallMetadata } from "../ModelCallMetadata.js";
3
+ import { Classifier, ClassifierSettings } from "./Classifier.js";
4
+ export declare function classify<VALUE, CLASS extends string | null>(args: {
5
+ model: Classifier<VALUE, CLASS, ClassifierSettings>;
6
+ value: VALUE;
7
+ fullResponse?: false;
8
+ } & FunctionOptions): Promise<CLASS>;
9
+ export declare function classify<VALUE, CLASS extends string | null>(args: {
10
+ model: Classifier<VALUE, CLASS, ClassifierSettings>;
11
+ value: VALUE;
12
+ fullResponse: true;
13
+ } & FunctionOptions): Promise<{
14
+ class: CLASS;
15
+ rawResponse: unknown;
16
+ metadata: ModelCallMetadata;
17
+ }>;
@@ -0,0 +1,23 @@
1
+ import { executeStandardCall } from "../executeStandardCall.js";
2
+ export async function classify({ model, value, fullResponse, ...options }) {
3
+ const callResponse = await executeStandardCall({
4
+ functionType: "classify",
5
+ input: value,
6
+ model,
7
+ options,
8
+ generateResponse: async (options) => {
9
+ const result = await model.doClassify(value, options);
10
+ return {
11
+ rawResponse: result.rawResponse,
12
+ extractedValue: result.class,
13
+ };
14
+ },
15
+ });
16
+ return fullResponse
17
+ ? {
18
+ class: callResponse.value,
19
+ rawResponse: callResponse.rawResponse,
20
+ metadata: callResponse.metadata,
21
+ }
22
+ : callResponse.value;
23
+ }
@@ -14,4 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./SemanticClassifier.cjs"), exports);
17
+ __exportStar(require("./Classifier.cjs"), exports);
18
+ __exportStar(require("./ClassifyEvent.cjs"), exports);
19
+ __exportStar(require("./EmbeddingSimilarityClassifier.cjs"), exports);
20
+ __exportStar(require("./classify.cjs"), exports);
@@ -0,0 +1,4 @@
1
+ export * from "./Classifier.js";
2
+ export * from "./ClassifyEvent.js";
3
+ export * from "./EmbeddingSimilarityClassifier.js";
4
+ export * from "./classify.js";
@@ -0,0 +1,4 @@
1
+ export * from "./Classifier.js";
2
+ export * from "./ClassifyEvent.js";
3
+ export * from "./EmbeddingSimilarityClassifier.js";
4
+ export * from "./classify.js";
@@ -20,6 +20,7 @@ __exportStar(require("./ModelCallEvent.cjs"), exports);
20
20
  __exportStar(require("./ModelCallMetadata.cjs"), exports);
21
21
  __exportStar(require("./ModelInformation.cjs"), exports);
22
22
  __exportStar(require("./PromptTemplate.cjs"), exports);
23
+ __exportStar(require("./classify/index.cjs"), exports);
23
24
  __exportStar(require("./embed/EmbeddingEvent.cjs"), exports);
24
25
  __exportStar(require("./embed/EmbeddingModel.cjs"), exports);
25
26
  __exportStar(require("./embed/embed.cjs"), exports);
@@ -4,6 +4,7 @@ export * from "./ModelCallEvent.js";
4
4
  export * from "./ModelCallMetadata.js";
5
5
  export * from "./ModelInformation.js";
6
6
  export * from "./PromptTemplate.js";
7
+ export * from "./classify/index.js";
7
8
  export * from "./embed/EmbeddingEvent.js";
8
9
  export * from "./embed/EmbeddingModel.js";
9
10
  export * from "./embed/embed.js";
@@ -4,6 +4,7 @@ export * from "./ModelCallEvent.js";
4
4
  export * from "./ModelCallMetadata.js";
5
5
  export * from "./ModelInformation.js";
6
6
  export * from "./PromptTemplate.js";
7
+ export * from "./classify/index.js";
7
8
  export * from "./embed/EmbeddingEvent.js";
8
9
  export * from "./embed/EmbeddingModel.js";
9
10
  export * from "./embed/embed.js";
@@ -32,16 +32,16 @@ export declare class MistralTextEmbeddingModel extends AbstractModel<MistralText
32
32
  doEmbedValues(texts: string[], options: FunctionCallOptions): Promise<{
33
33
  rawResponse: {
34
34
  object: string;
35
- model: string;
36
- usage: {
37
- prompt_tokens: number;
38
- total_tokens: number;
39
- };
40
35
  data: {
41
36
  object: string;
42
37
  embedding: number[];
43
38
  index: number;
44
39
  }[];
40
+ model: string;
41
+ usage: {
42
+ prompt_tokens: number;
43
+ total_tokens: number;
44
+ };
45
45
  id: string;
46
46
  };
47
47
  embeddings: number[][];
@@ -77,29 +77,29 @@ declare const MistralTextEmbeddingResponseSchema: z.ZodObject<{
77
77
  }>;
78
78
  }, "strip", z.ZodTypeAny, {
79
79
  object: string;
80
- model: string;
81
- usage: {
82
- prompt_tokens: number;
83
- total_tokens: number;
84
- };
85
80
  data: {
86
81
  object: string;
87
82
  embedding: number[];
88
83
  index: number;
89
84
  }[];
90
- id: string;
91
- }, {
92
- object: string;
93
85
  model: string;
94
86
  usage: {
95
87
  prompt_tokens: number;
96
88
  total_tokens: number;
97
89
  };
90
+ id: string;
91
+ }, {
92
+ object: string;
98
93
  data: {
99
94
  object: string;
100
95
  embedding: number[];
101
96
  index: number;
102
97
  }[];
98
+ model: string;
99
+ usage: {
100
+ prompt_tokens: number;
101
+ total_tokens: number;
102
+ };
103
103
  id: string;
104
104
  }>;
105
105
  export type MistralTextEmbeddingResponse = z.infer<typeof MistralTextEmbeddingResponseSchema>;
@@ -40,11 +40,11 @@ export declare class OllamaChatModel extends AbstractModel<OllamaChatModelSettin
40
40
  get settingsForEvent(): Partial<OllamaChatModelSettings>;
41
41
  doGenerateTexts(prompt: OllamaChatPrompt, options: FunctionCallOptions): Promise<{
42
42
  rawResponse: {
43
- model: string;
44
43
  message: {
45
44
  role: string;
46
45
  content: string;
47
46
  };
47
+ model: string;
48
48
  done: true;
49
49
  created_at: string;
50
50
  total_duration: number;
@@ -61,11 +61,11 @@ export declare class OllamaChatModel extends AbstractModel<OllamaChatModelSettin
61
61
  }>;
62
62
  restoreGeneratedTexts(rawResponse: unknown): {
63
63
  rawResponse: {
64
- model: string;
65
64
  message: {
66
65
  role: string;
67
66
  content: string;
68
67
  };
68
+ model: string;
69
69
  done: true;
70
70
  created_at: string;
71
71
  total_duration: number;
@@ -82,11 +82,11 @@ export declare class OllamaChatModel extends AbstractModel<OllamaChatModelSettin
82
82
  };
83
83
  private processTextGenerationResponse;
84
84
  doStreamText(prompt: OllamaChatPrompt, options: FunctionCallOptions): Promise<AsyncIterable<import("../../index.js").Delta<{
85
- model: string;
86
85
  message: {
87
86
  role: string;
88
87
  content: string;
89
88
  };
89
+ model: string;
90
90
  done: false;
91
91
  created_at: string;
92
92
  } | {
@@ -141,11 +141,11 @@ declare const ollamaChatResponseSchema: z.ZodObject<{
141
141
  eval_count: z.ZodNumber;
142
142
  eval_duration: z.ZodNumber;
143
143
  }, "strip", z.ZodTypeAny, {
144
- model: string;
145
144
  message: {
146
145
  role: string;
147
146
  content: string;
148
147
  };
148
+ model: string;
149
149
  done: true;
150
150
  created_at: string;
151
151
  total_duration: number;
@@ -155,11 +155,11 @@ declare const ollamaChatResponseSchema: z.ZodObject<{
155
155
  prompt_eval_count?: number | undefined;
156
156
  prompt_eval_duration?: number | undefined;
157
157
  }, {
158
- model: string;
159
158
  message: {
160
159
  role: string;
161
160
  content: string;
162
161
  };
162
+ model: string;
163
163
  done: true;
164
164
  created_at: string;
165
165
  total_duration: number;
@@ -185,19 +185,19 @@ declare const ollamaChatStreamChunkSchema: z.ZodDiscriminatedUnion<"done", [z.Zo
185
185
  content: string;
186
186
  }>;
187
187
  }, "strip", z.ZodTypeAny, {
188
- model: string;
189
188
  message: {
190
189
  role: string;
191
190
  content: string;
192
191
  };
192
+ model: string;
193
193
  done: false;
194
194
  created_at: string;
195
195
  }, {
196
- model: string;
197
196
  message: {
198
197
  role: string;
199
198
  content: string;
200
199
  };
200
+ model: string;
201
201
  done: false;
202
202
  created_at: string;
203
203
  }>, z.ZodObject<{
@@ -247,11 +247,11 @@ export declare const OllamaChatResponseFormat: {
247
247
  requestBodyValues: unknown;
248
248
  response: Response;
249
249
  }) => Promise<{
250
- model: string;
251
250
  message: {
252
251
  role: string;
253
252
  content: string;
254
253
  };
254
+ model: string;
255
255
  done: true;
256
256
  created_at: string;
257
257
  total_duration: number;
@@ -271,11 +271,11 @@ export declare const OllamaChatResponseFormat: {
271
271
  handler: ({ response }: {
272
272
  response: Response;
273
273
  }) => Promise<AsyncIterable<import("../../index.js").Delta<{
274
- model: string;
275
274
  message: {
276
275
  role: string;
277
276
  content: string;
278
277
  };
278
+ model: string;
279
279
  done: false;
280
280
  created_at: string;
281
281
  } | {
@@ -52,16 +52,16 @@ export declare class OpenAITextEmbeddingModel extends AbstractModel<OpenAITextEm
52
52
  doEmbedValues(texts: string[], callOptions: FunctionCallOptions): Promise<{
53
53
  rawResponse: {
54
54
  object: "list";
55
- model: string;
56
- usage: {
57
- prompt_tokens: number;
58
- total_tokens: number;
59
- };
60
55
  data: {
61
56
  object: "embedding";
62
57
  embedding: number[];
63
58
  index: number;
64
59
  }[];
60
+ model: string;
61
+ usage: {
62
+ prompt_tokens: number;
63
+ total_tokens: number;
64
+ };
65
65
  };
66
66
  embeddings: number[][];
67
67
  }>;
@@ -95,28 +95,28 @@ declare const openAITextEmbeddingResponseSchema: z.ZodObject<{
95
95
  }>;
96
96
  }, "strip", z.ZodTypeAny, {
97
97
  object: "list";
98
- model: string;
99
- usage: {
100
- prompt_tokens: number;
101
- total_tokens: number;
102
- };
103
98
  data: {
104
99
  object: "embedding";
105
100
  embedding: number[];
106
101
  index: number;
107
102
  }[];
108
- }, {
109
- object: "list";
110
103
  model: string;
111
104
  usage: {
112
105
  prompt_tokens: number;
113
106
  total_tokens: number;
114
107
  };
108
+ }, {
109
+ object: "list";
115
110
  data: {
116
111
  object: "embedding";
117
112
  embedding: number[];
118
113
  index: number;
119
114
  }[];
115
+ model: string;
116
+ usage: {
117
+ prompt_tokens: number;
118
+ total_tokens: number;
119
+ };
120
120
  }>;
121
121
  export type OpenAITextEmbeddingResponse = z.infer<typeof openAITextEmbeddingResponseSchema>;
122
122
  export {};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "modelfusion",
3
3
  "description": "The TypeScript library for building AI applications.",
4
- "version": "0.122.0",
4
+ "version": "0.123.0",
5
5
  "author": "Lars Grammel",
6
6
  "license": "MIT",
7
7
  "keywords": [
@@ -1,81 +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)({
43
- model: this.embeddingModel,
44
- values: cluster.values,
45
- });
46
- for (let i = 0; i < clusterEmbeddings.length; i++) {
47
- embeddings.push({
48
- embedding: clusterEmbeddings[i],
49
- clusterValue: cluster.values[i],
50
- clusterName: cluster.name,
51
- });
52
- }
53
- }
54
- this.embeddings = embeddings; // lazy caching
55
- return embeddings;
56
- }
57
- async classify(value) {
58
- const valueEmbedding = await (0, embed_js_1.embed)({
59
- model: this.embeddingModel,
60
- value,
61
- });
62
- const clusterEmbeddings = await this.getEmbeddings();
63
- const allMatches = [];
64
- for (const embedding of clusterEmbeddings) {
65
- const similarity = (0, cosineSimilarity_js_1.cosineSimilarity)(valueEmbedding, embedding.embedding);
66
- if (similarity >= this.similarityThreshold) {
67
- allMatches.push({
68
- similarity,
69
- clusterValue: embedding.clusterValue,
70
- clusterName: embedding.clusterName,
71
- });
72
- }
73
- }
74
- // sort (highest similarity first)
75
- allMatches.sort((a, b) => b.similarity - a.similarity);
76
- return allMatches.length > 0
77
- ? allMatches[0].clusterName
78
- : null;
79
- }
80
- }
81
- 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,77 +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({
40
- model: this.embeddingModel,
41
- values: cluster.values,
42
- });
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 embed({
56
- model: this.embeddingModel,
57
- value,
58
- });
59
- const clusterEmbeddings = await this.getEmbeddings();
60
- const allMatches = [];
61
- for (const embedding of clusterEmbeddings) {
62
- const similarity = cosineSimilarity(valueEmbedding, embedding.embedding);
63
- if (similarity >= this.similarityThreshold) {
64
- allMatches.push({
65
- similarity,
66
- clusterValue: embedding.clusterValue,
67
- clusterName: embedding.clusterName,
68
- });
69
- }
70
- }
71
- // sort (highest similarity first)
72
- allMatches.sort((a, b) => b.similarity - a.similarity);
73
- return allMatches.length > 0
74
- ? allMatches[0].clusterName
75
- : null;
76
- }
77
- }
@@ -1 +0,0 @@
1
- export * from "./SemanticClassifier.js";
@@ -1 +0,0 @@
1
- export * from "./SemanticClassifier.js";