modelfusion 0.122.0 → 0.124.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 (55) hide show
  1. package/CHANGELOG.md +56 -0
  2. package/README.md +42 -2
  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/AbstractOpenAITextEmbeddingModel.cjs +82 -0
  28. package/model-provider/openai/AbstractOpenAITextEmbeddingModel.d.ts +91 -0
  29. package/model-provider/openai/AbstractOpenAITextEmbeddingModel.js +78 -0
  30. package/model-provider/openai/OpenAIFacade.cjs +18 -18
  31. package/model-provider/openai/OpenAIFacade.d.ts +18 -18
  32. package/model-provider/openai/OpenAIFacade.js +18 -18
  33. package/model-provider/openai/OpenAITextEmbeddingModel.cjs +3 -68
  34. package/model-provider/openai/OpenAITextEmbeddingModel.d.ts +4 -82
  35. package/model-provider/openai/OpenAITextEmbeddingModel.js +3 -68
  36. package/model-provider/openai/index.cjs +1 -0
  37. package/model-provider/openai/index.d.ts +1 -0
  38. package/model-provider/openai/index.js +1 -0
  39. package/model-provider/openai-compatible/OpenAICompatibleChatModel.d.ts +2 -0
  40. package/model-provider/openai-compatible/OpenAICompatibleCompletionModel.d.ts +2 -0
  41. package/model-provider/openai-compatible/OpenAICompatibleFacade.cjs +28 -7
  42. package/model-provider/openai-compatible/OpenAICompatibleFacade.d.ts +24 -6
  43. package/model-provider/openai-compatible/OpenAICompatibleFacade.js +26 -6
  44. package/model-provider/openai-compatible/OpenAICompatibleTextEmbeddingModel.cjs +27 -0
  45. package/model-provider/openai-compatible/OpenAICompatibleTextEmbeddingModel.d.ts +18 -0
  46. package/model-provider/openai-compatible/OpenAICompatibleTextEmbeddingModel.js +23 -0
  47. package/model-provider/openai-compatible/index.cjs +1 -0
  48. package/model-provider/openai-compatible/index.d.ts +1 -0
  49. package/model-provider/openai-compatible/index.js +1 -0
  50. package/package.json +1 -1
  51. package/classifier/SemanticClassifier.cjs +0 -81
  52. package/classifier/SemanticClassifier.d.ts +0 -25
  53. package/classifier/SemanticClassifier.js +0 -77
  54. package/classifier/index.d.ts +0 -1
  55. package/classifier/index.js +0 -1
package/CHANGELOG.md CHANGED
@@ -1,5 +1,61 @@
1
1
  # Changelog
2
2
 
3
+ ## v0.124.0 - 2024-01-13
4
+
5
+ ### Added
6
+
7
+ - [Embedding-support for OpenAI-compatible providers](https://modelfusion.dev/integration/model-provider/openaicompatible/#embed-text). You can for example use the Together AI embedding endpoint:
8
+
9
+ ```ts
10
+ import { embed, openaicompatible } from "modelfusion";
11
+
12
+ const embedding = await embed({
13
+ model: openaicompatible.TextEmbedder({
14
+ api: openaicompatible.TogetherAIApi(),
15
+ provider: "openaicompatible-togetherai",
16
+ model: "togethercomputer/m2-bert-80M-8k-retrieval",
17
+ }),
18
+ value: "At first, Nox didn't know what to do with the pup.",
19
+ });
20
+ ```
21
+
22
+ ## v0.123.0 - 2024-01-13
23
+
24
+ ### Added
25
+
26
+ - `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`:
27
+
28
+ ```ts
29
+ import { classify, EmbeddingSimilarityClassifier, openai } from "modelfusion";
30
+
31
+ const classifier = new EmbeddingSimilarityClassifier({
32
+ embeddingModel: openai.TextEmbedder({ model: "text-embedding-ada-002" }),
33
+ similarityThreshold: 0.82,
34
+ clusters: [
35
+ {
36
+ name: "politics" as const,
37
+ values: [
38
+ "they will save the country!",
39
+ // ...
40
+ ],
41
+ },
42
+ {
43
+ name: "chitchat" as const,
44
+ values: [
45
+ "how's the weather today?",
46
+ // ...
47
+ ],
48
+ },
49
+ ],
50
+ });
51
+
52
+ // strongly typed result:
53
+ const result = await classify({
54
+ model: classifier,
55
+ value: "don't you love politics?",
56
+ });
57
+ ```
58
+
3
59
  ## v0.122.0 - 2024-01-13
4
60
 
5
61
  ### 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" }),
@@ -307,7 +309,44 @@ const embeddings = await embedMany({
307
309
  });
308
310
  ```
309
311
 
310
- 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)
312
+ Providers: [OpenAI](https://modelfusion.dev/integration/model-provider/openai), [OpenAI compatible](https://modelfusion.dev/integration/model-provider/openaicompatible), [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)
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)
311
350
 
312
351
  ### [Tokenize Text](https://modelfusion.dev/guide/function/tokenize-text)
313
352
 
@@ -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>;