modelfusion 0.31.0 → 0.33.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 +14 -15
 - package/index.cjs +1 -0
 - package/index.d.ts +1 -0
 - package/index.js +1 -0
 - package/model-function/Model.d.ts +1 -1
 - package/model-provider/openai/OpenAITextGenerationModel.cjs +41 -17
 - package/model-provider/openai/OpenAITextGenerationModel.d.ts +32 -14
 - package/model-provider/openai/OpenAITextGenerationModel.js +41 -17
 - package/model-provider/openai/TikTokenTokenizer.cjs +3 -2
 - package/model-provider/openai/TikTokenTokenizer.js +3 -2
 - package/model-provider/stability/StabilityImageGenerationModel.cjs +6 -0
 - package/model-provider/stability/StabilityImageGenerationModel.d.ts +4 -2
 - package/model-provider/stability/StabilityImageGenerationModel.js +6 -0
 - package/package.json +2 -2
 - package/retriever/Retriever.d.ts +7 -0
 - package/retriever/index.cjs +18 -0
 - package/retriever/index.d.ts +2 -0
 - package/retriever/index.js +2 -0
 - package/retriever/retrieve.cjs +9 -0
 - package/retriever/retrieve.d.ts +3 -0
 - package/retriever/retrieve.js +5 -0
 - package/text-chunk/index.cjs +0 -4
 - package/text-chunk/index.d.ts +0 -4
 - package/text-chunk/index.js +0 -4
 - package/{text-chunk/SimilarTextChunksFromVectorIndexRetriever.cjs → vector-index/VectorIndexRetriever.cjs} +6 -6
 - package/vector-index/VectorIndexRetriever.d.ts +19 -0
 - package/{text-chunk/SimilarTextChunksFromVectorIndexRetriever.js → vector-index/VectorIndexRetriever.js} +4 -4
 - package/vector-index/index.cjs +2 -0
 - package/vector-index/index.d.ts +2 -0
 - package/vector-index/index.js +2 -0
 - package/vector-index/upsertIntoVectorIndex.cjs +15 -0
 - package/vector-index/upsertIntoVectorIndex.d.ts +11 -0
 - package/vector-index/upsertIntoVectorIndex.js +11 -0
 - package/text-chunk/SimilarTextChunksFromVectorIndexRetriever.d.ts +0 -20
 - package/text-chunk/retrieve-text-chunks/TextChunkRetriever.d.ts +0 -8
 - package/text-chunk/retrieve-text-chunks/retrieveTextChunks.cjs +0 -10
 - package/text-chunk/retrieve-text-chunks/retrieveTextChunks.d.ts +0 -6
 - package/text-chunk/retrieve-text-chunks/retrieveTextChunks.js +0 -6
 - package/text-chunk/upsertTextChunks.cjs +0 -15
 - package/text-chunk/upsertTextChunks.d.ts +0 -11
 - package/text-chunk/upsertTextChunks.js +0 -11
 - /package/{text-chunk/retrieve-text-chunks/TextChunkRetriever.cjs → retriever/Retriever.cjs} +0 -0
 - /package/{text-chunk/retrieve-text-chunks/TextChunkRetriever.js → retriever/Retriever.js} +0 -0
 
    
        package/README.md
    CHANGED
    
    | 
         @@ -45,7 +45,9 @@ You can use [prompt formats](https://modelfusion.dev/guide/function/generate-tex 
     | 
|
| 
       45 
45 
     | 
    
         | 
| 
       46 
46 
     | 
    
         
             
            ```ts
         
     | 
| 
       47 
47 
     | 
    
         
             
            const text = await generateText(
         
     | 
| 
       48 
     | 
    
         
            -
              new OpenAITextGenerationModel({ 
     | 
| 
      
 48 
     | 
    
         
            +
              new OpenAITextGenerationModel({
         
     | 
| 
      
 49 
     | 
    
         
            +
                model: "gpt-3.5-turbo-instruct",
         
     | 
| 
      
 50 
     | 
    
         
            +
              }),
         
     | 
| 
       49 
51 
     | 
    
         
             
              "Write a short story about a robot learning to love:\n\n"
         
     | 
| 
       50 
52 
     | 
    
         
             
            );
         
     | 
| 
       51 
53 
     | 
    
         
             
            ```
         
     | 
| 
         @@ -56,14 +58,10 @@ Providers: [OpenAI](https://modelfusion.dev/integration/model-provider/openai), 
     | 
|
| 
       56 
58 
     | 
    
         | 
| 
       57 
59 
     | 
    
         
             
            ```ts
         
     | 
| 
       58 
60 
     | 
    
         
             
            const textStream = await streamText(
         
     | 
| 
       59 
     | 
    
         
            -
              new  
     | 
| 
       60 
     | 
    
         
            -
                model: "gpt-3.5-turbo",
         
     | 
| 
       61 
     | 
    
         
            -
                maxCompletionTokens: 1000,
         
     | 
| 
      
 61 
     | 
    
         
            +
              new OpenAITextGenerationModel({
         
     | 
| 
      
 62 
     | 
    
         
            +
                model: "gpt-3.5-turbo-instruct",
         
     | 
| 
       62 
63 
     | 
    
         
             
              }),
         
     | 
| 
       63 
     | 
    
         
            -
               
     | 
| 
       64 
     | 
    
         
            -
                OpenAIChatMessage.system("You are a story writer."),
         
     | 
| 
       65 
     | 
    
         
            -
                OpenAIChatMessage.user("Write a story about a robot learning to love"),
         
     | 
| 
       66 
     | 
    
         
            -
              ]
         
     | 
| 
      
 64 
     | 
    
         
            +
              "Write a short story about a robot learning to love:\n\n"
         
     | 
| 
       67 
65 
     | 
    
         
             
            );
         
     | 
| 
       68 
66 
     | 
    
         | 
| 
       69 
67 
     | 
    
         
             
            for await (const textFragment of textStream) {
         
     | 
| 
         @@ -121,7 +119,7 @@ ModelFusion model functions return rich results that include the original respon 
     | 
|
| 
       121 
119 
     | 
    
         
             
            // the response type is specific to the model that's being used
         
     | 
| 
       122 
120 
     | 
    
         
             
            const { output, response, metadata } = await generateText(
         
     | 
| 
       123 
121 
     | 
    
         
             
              new OpenAITextGenerationModel({
         
     | 
| 
       124 
     | 
    
         
            -
                model: " 
     | 
| 
      
 122 
     | 
    
         
            +
                model: "gpt-3.5-turbo-instruct",
         
     | 
| 
       125 
123 
     | 
    
         
             
                maxCompletionTokens: 1000,
         
     | 
| 
       126 
124 
     | 
    
         
             
                n: 2, // generate 2 completions
         
     | 
| 
       127 
125 
     | 
    
         
             
              }),
         
     | 
| 
         @@ -367,7 +365,7 @@ const reconstructedText = await tokenizer.detokenize(tokens); 
     | 
|
| 
       367 
365 
     | 
    
         | 
| 
       368 
366 
     | 
    
         
             
            Providers: [OpenAI](https://modelfusion.dev/integration/model-provider/openai), [Cohere](https://modelfusion.dev/integration/model-provider/cohere), [Llama.cpp](https://modelfusion.dev/integration/model-provider/llamacpp)
         
     | 
| 
       369 
367 
     | 
    
         | 
| 
       370 
     | 
    
         
            -
            ### [Upserting and Retrieving  
     | 
| 
      
 368 
     | 
    
         
            +
            ### [Upserting and Retrieving Objects from Vector Indices](https://modelfusion.dev/guide/vector-index)
         
     | 
| 
       371 
369 
     | 
    
         | 
| 
       372 
370 
     | 
    
         
             
            ```ts
         
     | 
| 
       373 
371 
     | 
    
         
             
            const texts = [
         
     | 
| 
         @@ -376,21 +374,22 @@ const texts = [ 
     | 
|
| 
       376 
374 
     | 
    
         
             
              // ...
         
     | 
| 
       377 
375 
     | 
    
         
             
            ];
         
     | 
| 
       378 
376 
     | 
    
         | 
| 
       379 
     | 
    
         
            -
            const vectorIndex = new MemoryVectorIndex< 
     | 
| 
      
 377 
     | 
    
         
            +
            const vectorIndex = new MemoryVectorIndex<string>();
         
     | 
| 
       380 
378 
     | 
    
         
             
            const embeddingModel = new OpenAITextEmbeddingModel({
         
     | 
| 
       381 
379 
     | 
    
         
             
              model: "text-embedding-ada-002",
         
     | 
| 
       382 
380 
     | 
    
         
             
            });
         
     | 
| 
       383 
381 
     | 
    
         | 
| 
       384 
382 
     | 
    
         
             
            // update an index - usually done as part of an ingestion process:
         
     | 
| 
       385 
     | 
    
         
            -
            await  
     | 
| 
      
 383 
     | 
    
         
            +
            await upsertIntoVectorIndex({
         
     | 
| 
       386 
384 
     | 
    
         
             
              vectorIndex,
         
     | 
| 
       387 
385 
     | 
    
         
             
              embeddingModel,
         
     | 
| 
       388 
     | 
    
         
            -
               
     | 
| 
      
 386 
     | 
    
         
            +
              objects: texts,
         
     | 
| 
      
 387 
     | 
    
         
            +
              getValueToEmbed: (text) => text,
         
     | 
| 
       389 
388 
     | 
    
         
             
            });
         
     | 
| 
       390 
389 
     | 
    
         | 
| 
       391 
390 
     | 
    
         
             
            // retrieve text chunks from the vector index - usually done at query time:
         
     | 
| 
       392 
     | 
    
         
            -
            const  
     | 
| 
       393 
     | 
    
         
            -
              new  
     | 
| 
      
 391 
     | 
    
         
            +
            const retrievedTexts = await retrieve(
         
     | 
| 
      
 392 
     | 
    
         
            +
              new VectorIndexRetriever({
         
     | 
| 
       394 
393 
     | 
    
         
             
                vectorIndex,
         
     | 
| 
       395 
394 
     | 
    
         
             
                embeddingModel,
         
     | 
| 
       396 
395 
     | 
    
         
             
                maxResults: 3,
         
     | 
    
        package/index.cjs
    CHANGED
    
    | 
         @@ -21,6 +21,7 @@ __exportStar(require("./model-function/index.cjs"), exports); 
     | 
|
| 
       21 
21 
     | 
    
         
             
            __exportStar(require("./model-provider/index.cjs"), exports);
         
     | 
| 
       22 
22 
     | 
    
         
             
            __exportStar(require("./observability/index.cjs"), exports);
         
     | 
| 
       23 
23 
     | 
    
         
             
            __exportStar(require("./prompt/index.cjs"), exports);
         
     | 
| 
      
 24 
     | 
    
         
            +
            __exportStar(require("./retriever/index.cjs"), exports);
         
     | 
| 
       24 
25 
     | 
    
         
             
            __exportStar(require("./text-chunk/index.cjs"), exports);
         
     | 
| 
       25 
26 
     | 
    
         
             
            __exportStar(require("./tool/index.cjs"), exports);
         
     | 
| 
       26 
27 
     | 
    
         
             
            __exportStar(require("./util/index.cjs"), exports);
         
     | 
    
        package/index.d.ts
    CHANGED
    
    | 
         @@ -5,6 +5,7 @@ export * from "./model-function/index.js"; 
     | 
|
| 
       5 
5 
     | 
    
         
             
            export * from "./model-provider/index.js";
         
     | 
| 
       6 
6 
     | 
    
         
             
            export * from "./observability/index.js";
         
     | 
| 
       7 
7 
     | 
    
         
             
            export * from "./prompt/index.js";
         
     | 
| 
      
 8 
     | 
    
         
            +
            export * from "./retriever/index.js";
         
     | 
| 
       8 
9 
     | 
    
         
             
            export * from "./text-chunk/index.js";
         
     | 
| 
       9 
10 
     | 
    
         
             
            export * from "./tool/index.js";
         
     | 
| 
       10 
11 
     | 
    
         
             
            export * from "./util/index.js";
         
     | 
    
        package/index.js
    CHANGED
    
    | 
         @@ -5,6 +5,7 @@ export * from "./model-function/index.js"; 
     | 
|
| 
       5 
5 
     | 
    
         
             
            export * from "./model-provider/index.js";
         
     | 
| 
       6 
6 
     | 
    
         
             
            export * from "./observability/index.js";
         
     | 
| 
       7 
7 
     | 
    
         
             
            export * from "./prompt/index.js";
         
     | 
| 
      
 8 
     | 
    
         
            +
            export * from "./retriever/index.js";
         
     | 
| 
       8 
9 
     | 
    
         
             
            export * from "./text-chunk/index.js";
         
     | 
| 
       9 
10 
     | 
    
         
             
            export * from "./tool/index.js";
         
     | 
| 
       10 
11 
     | 
    
         
             
            export * from "./util/index.js";
         
     | 
| 
         @@ -21,55 +21,72 @@ const TikTokenTokenizer_js_1 = require("./TikTokenTokenizer.cjs"); 
     | 
|
| 
       21 
21 
     | 
    
         
             
             * @see https://openai.com/pricing
         
     | 
| 
       22 
22 
     | 
    
         
             
             */
         
     | 
| 
       23 
23 
     | 
    
         
             
            exports.OPENAI_TEXT_GENERATION_MODELS = {
         
     | 
| 
      
 24 
     | 
    
         
            +
                "gpt-3.5-turbo-instruct": {
         
     | 
| 
      
 25 
     | 
    
         
            +
                    contextWindowSize: 4097,
         
     | 
| 
      
 26 
     | 
    
         
            +
                    promptTokenCostInMillicents: 0.15,
         
     | 
| 
      
 27 
     | 
    
         
            +
                    completionTokenCostInMillicents: 0.2,
         
     | 
| 
      
 28 
     | 
    
         
            +
                },
         
     | 
| 
       24 
29 
     | 
    
         
             
                "davinci-002": {
         
     | 
| 
       25 
30 
     | 
    
         
             
                    contextWindowSize: 16384,
         
     | 
| 
       26 
     | 
    
         
            -
                     
     | 
| 
      
 31 
     | 
    
         
            +
                    promptTokenCostInMillicents: 0.2,
         
     | 
| 
      
 32 
     | 
    
         
            +
                    completionTokenCostInMillicents: 0.2,
         
     | 
| 
       27 
33 
     | 
    
         
             
                    fineTunedTokenCostInMillicents: 1.2,
         
     | 
| 
       28 
34 
     | 
    
         
             
                },
         
     | 
| 
       29 
35 
     | 
    
         
             
                "babbage-002": {
         
     | 
| 
       30 
36 
     | 
    
         
             
                    contextWindowSize: 16384,
         
     | 
| 
       31 
     | 
    
         
            -
                     
     | 
| 
      
 37 
     | 
    
         
            +
                    promptTokenCostInMillicents: 0.04,
         
     | 
| 
      
 38 
     | 
    
         
            +
                    completionTokenCostInMillicents: 0.04,
         
     | 
| 
       32 
39 
     | 
    
         
             
                    fineTunedTokenCostInMillicents: 0.16,
         
     | 
| 
       33 
40 
     | 
    
         
             
                },
         
     | 
| 
       34 
41 
     | 
    
         
             
                "text-davinci-003": {
         
     | 
| 
       35 
42 
     | 
    
         
             
                    contextWindowSize: 4096,
         
     | 
| 
       36 
     | 
    
         
            -
                     
     | 
| 
      
 43 
     | 
    
         
            +
                    promptTokenCostInMillicents: 2,
         
     | 
| 
      
 44 
     | 
    
         
            +
                    completionTokenCostInMillicents: 2,
         
     | 
| 
       37 
45 
     | 
    
         
             
                },
         
     | 
| 
       38 
46 
     | 
    
         
             
                "text-davinci-002": {
         
     | 
| 
       39 
47 
     | 
    
         
             
                    contextWindowSize: 4096,
         
     | 
| 
       40 
     | 
    
         
            -
                     
     | 
| 
      
 48 
     | 
    
         
            +
                    promptTokenCostInMillicents: 2,
         
     | 
| 
      
 49 
     | 
    
         
            +
                    completionTokenCostInMillicents: 2,
         
     | 
| 
       41 
50 
     | 
    
         
             
                },
         
     | 
| 
       42 
51 
     | 
    
         
             
                "code-davinci-002": {
         
     | 
| 
       43 
52 
     | 
    
         
             
                    contextWindowSize: 8000,
         
     | 
| 
       44 
     | 
    
         
            -
                     
     | 
| 
      
 53 
     | 
    
         
            +
                    promptTokenCostInMillicents: 2,
         
     | 
| 
      
 54 
     | 
    
         
            +
                    completionTokenCostInMillicents: 2,
         
     | 
| 
       45 
55 
     | 
    
         
             
                },
         
     | 
| 
       46 
56 
     | 
    
         
             
                davinci: {
         
     | 
| 
       47 
57 
     | 
    
         
             
                    contextWindowSize: 2048,
         
     | 
| 
       48 
     | 
    
         
            -
                     
     | 
| 
      
 58 
     | 
    
         
            +
                    promptTokenCostInMillicents: 2,
         
     | 
| 
      
 59 
     | 
    
         
            +
                    completionTokenCostInMillicents: 2,
         
     | 
| 
       49 
60 
     | 
    
         
             
                },
         
     | 
| 
       50 
61 
     | 
    
         
             
                "text-curie-001": {
         
     | 
| 
       51 
62 
     | 
    
         
             
                    contextWindowSize: 2048,
         
     | 
| 
       52 
     | 
    
         
            -
                     
     | 
| 
      
 63 
     | 
    
         
            +
                    promptTokenCostInMillicents: 0.2,
         
     | 
| 
      
 64 
     | 
    
         
            +
                    completionTokenCostInMillicents: 0.2,
         
     | 
| 
       53 
65 
     | 
    
         
             
                },
         
     | 
| 
       54 
66 
     | 
    
         
             
                curie: {
         
     | 
| 
       55 
67 
     | 
    
         
             
                    contextWindowSize: 2048,
         
     | 
| 
       56 
     | 
    
         
            -
                     
     | 
| 
      
 68 
     | 
    
         
            +
                    promptTokenCostInMillicents: 0.2,
         
     | 
| 
      
 69 
     | 
    
         
            +
                    completionTokenCostInMillicents: 0.2,
         
     | 
| 
       57 
70 
     | 
    
         
             
                },
         
     | 
| 
       58 
71 
     | 
    
         
             
                "text-babbage-001": {
         
     | 
| 
       59 
72 
     | 
    
         
             
                    contextWindowSize: 2048,
         
     | 
| 
       60 
     | 
    
         
            -
                     
     | 
| 
      
 73 
     | 
    
         
            +
                    promptTokenCostInMillicents: 0.05,
         
     | 
| 
      
 74 
     | 
    
         
            +
                    completionTokenCostInMillicents: 0.05,
         
     | 
| 
       61 
75 
     | 
    
         
             
                },
         
     | 
| 
       62 
76 
     | 
    
         
             
                babbage: {
         
     | 
| 
       63 
77 
     | 
    
         
             
                    contextWindowSize: 2048,
         
     | 
| 
       64 
     | 
    
         
            -
                     
     | 
| 
      
 78 
     | 
    
         
            +
                    promptTokenCostInMillicents: 0.05,
         
     | 
| 
      
 79 
     | 
    
         
            +
                    completionTokenCostInMillicents: 0.05,
         
     | 
| 
       65 
80 
     | 
    
         
             
                },
         
     | 
| 
       66 
81 
     | 
    
         
             
                "text-ada-001": {
         
     | 
| 
       67 
82 
     | 
    
         
             
                    contextWindowSize: 2048,
         
     | 
| 
       68 
     | 
    
         
            -
                     
     | 
| 
      
 83 
     | 
    
         
            +
                    promptTokenCostInMillicents: 0.04,
         
     | 
| 
      
 84 
     | 
    
         
            +
                    completionTokenCostInMillicents: 0.04,
         
     | 
| 
       69 
85 
     | 
    
         
             
                },
         
     | 
| 
       70 
86 
     | 
    
         
             
                ada: {
         
     | 
| 
       71 
87 
     | 
    
         
             
                    contextWindowSize: 2048,
         
     | 
| 
       72 
     | 
    
         
            -
                     
     | 
| 
      
 88 
     | 
    
         
            +
                    promptTokenCostInMillicents: 0.04,
         
     | 
| 
      
 89 
     | 
    
         
            +
                    completionTokenCostInMillicents: 0.04,
         
     | 
| 
       73 
90 
     | 
    
         
             
                },
         
     | 
| 
       74 
91 
     | 
    
         
             
            };
         
     | 
| 
       75 
92 
     | 
    
         
             
            function getOpenAITextGenerationModelInformation(model) {
         
     | 
| 
         @@ -80,7 +97,8 @@ function getOpenAITextGenerationModelInformation(model) { 
     | 
|
| 
       80 
97 
     | 
    
         
             
                        baseModel: model,
         
     | 
| 
       81 
98 
     | 
    
         
             
                        isFineTuned: false,
         
     | 
| 
       82 
99 
     | 
    
         
             
                        contextWindowSize: baseModelInformation.contextWindowSize,
         
     | 
| 
       83 
     | 
    
         
            -
                         
     | 
| 
      
 100 
     | 
    
         
            +
                        promptTokenCostInMillicents: baseModelInformation.promptTokenCostInMillicents,
         
     | 
| 
      
 101 
     | 
    
         
            +
                        completionTokenCostInMillicents: baseModelInformation.completionTokenCostInMillicents,
         
     | 
| 
       84 
102 
     | 
    
         
             
                    };
         
     | 
| 
       85 
103 
     | 
    
         
             
                }
         
     | 
| 
       86 
104 
     | 
    
         
             
                // Extract the base model from the fine-tuned model:
         
     | 
| 
         @@ -92,7 +110,8 @@ function getOpenAITextGenerationModelInformation(model) { 
     | 
|
| 
       92 
110 
     | 
    
         
             
                        baseModel: baseModel,
         
     | 
| 
       93 
111 
     | 
    
         
             
                        isFineTuned: true,
         
     | 
| 
       94 
112 
     | 
    
         
             
                        contextWindowSize: baseModelInformation.contextWindowSize,
         
     | 
| 
       95 
     | 
    
         
            -
                         
     | 
| 
      
 113 
     | 
    
         
            +
                        promptTokenCostInMillicents: baseModelInformation.fineTunedTokenCostInMillicents,
         
     | 
| 
      
 114 
     | 
    
         
            +
                        completionTokenCostInMillicents: baseModelInformation.fineTunedTokenCostInMillicents,
         
     | 
| 
       96 
115 
     | 
    
         
             
                    };
         
     | 
| 
       97 
116 
     | 
    
         
             
                }
         
     | 
| 
       98 
117 
     | 
    
         
             
                throw new Error(`Unknown OpenAI chat base model ${baseModel}.`);
         
     | 
| 
         @@ -102,8 +121,13 @@ const isOpenAITextGenerationModel = (model) => model in exports.OPENAI_TEXT_GENE 
     | 
|
| 
       102 
121 
     | 
    
         
             
                model.startsWith("ft:davinci-002:") ||
         
     | 
| 
       103 
122 
     | 
    
         
             
                model.startsWith("ft:babbage-002:");
         
     | 
| 
       104 
123 
     | 
    
         
             
            exports.isOpenAITextGenerationModel = isOpenAITextGenerationModel;
         
     | 
| 
       105 
     | 
    
         
            -
            const calculateOpenAITextGenerationCostInMillicents = ({ model, response, }) =>  
     | 
| 
       106 
     | 
    
         
            -
                getOpenAITextGenerationModelInformation(model) 
     | 
| 
      
 124 
     | 
    
         
            +
            const calculateOpenAITextGenerationCostInMillicents = ({ model, response, }) => {
         
     | 
| 
      
 125 
     | 
    
         
            +
                const modelInformation = getOpenAITextGenerationModelInformation(model);
         
     | 
| 
      
 126 
     | 
    
         
            +
                return (response.usage.prompt_tokens *
         
     | 
| 
      
 127 
     | 
    
         
            +
                    modelInformation.promptTokenCostInMillicents +
         
     | 
| 
      
 128 
     | 
    
         
            +
                    response.usage.completion_tokens *
         
     | 
| 
      
 129 
     | 
    
         
            +
                        modelInformation.completionTokenCostInMillicents);
         
     | 
| 
      
 130 
     | 
    
         
            +
            };
         
     | 
| 
       107 
131 
     | 
    
         
             
            exports.calculateOpenAITextGenerationCostInMillicents = calculateOpenAITextGenerationCostInMillicents;
         
     | 
| 
       108 
132 
     | 
    
         
             
            /**
         
     | 
| 
       109 
133 
     | 
    
         
             
             * Create a text generation model that calls the OpenAI text completion API.
         
     | 
| 
         @@ -112,7 +136,7 @@ exports.calculateOpenAITextGenerationCostInMillicents = calculateOpenAITextGener 
     | 
|
| 
       112 
136 
     | 
    
         
             
             *
         
     | 
| 
       113 
137 
     | 
    
         
             
             * @example
         
     | 
| 
       114 
138 
     | 
    
         
             
             * const model = new OpenAITextGenerationModel({
         
     | 
| 
       115 
     | 
    
         
            -
             *   model: " 
     | 
| 
      
 139 
     | 
    
         
            +
             *   model: "gpt-3.5-turbo-instruct",
         
     | 
| 
       116 
140 
     | 
    
         
             
             *   temperature: 0.7,
         
     | 
| 
       117 
141 
     | 
    
         
             
             *   maxCompletionTokens: 500,
         
     | 
| 
       118 
142 
     | 
    
         
             
             *   retry: retryWithExponentialBackoff({ maxTries: 5 }),
         
     | 
| 
         @@ -14,62 +14,80 @@ import { TikTokenTokenizer } from "./TikTokenTokenizer.js"; 
     | 
|
| 
       14 
14 
     | 
    
         
             
             * @see https://openai.com/pricing
         
     | 
| 
       15 
15 
     | 
    
         
             
             */
         
     | 
| 
       16 
16 
     | 
    
         
             
            export declare const OPENAI_TEXT_GENERATION_MODELS: {
         
     | 
| 
      
 17 
     | 
    
         
            +
                "gpt-3.5-turbo-instruct": {
         
     | 
| 
      
 18 
     | 
    
         
            +
                    contextWindowSize: number;
         
     | 
| 
      
 19 
     | 
    
         
            +
                    promptTokenCostInMillicents: number;
         
     | 
| 
      
 20 
     | 
    
         
            +
                    completionTokenCostInMillicents: number;
         
     | 
| 
      
 21 
     | 
    
         
            +
                };
         
     | 
| 
       17 
22 
     | 
    
         
             
                "davinci-002": {
         
     | 
| 
       18 
23 
     | 
    
         
             
                    contextWindowSize: number;
         
     | 
| 
       19 
     | 
    
         
            -
                     
     | 
| 
      
 24 
     | 
    
         
            +
                    promptTokenCostInMillicents: number;
         
     | 
| 
      
 25 
     | 
    
         
            +
                    completionTokenCostInMillicents: number;
         
     | 
| 
       20 
26 
     | 
    
         
             
                    fineTunedTokenCostInMillicents: number;
         
     | 
| 
       21 
27 
     | 
    
         
             
                };
         
     | 
| 
       22 
28 
     | 
    
         
             
                "babbage-002": {
         
     | 
| 
       23 
29 
     | 
    
         
             
                    contextWindowSize: number;
         
     | 
| 
       24 
     | 
    
         
            -
                     
     | 
| 
      
 30 
     | 
    
         
            +
                    promptTokenCostInMillicents: number;
         
     | 
| 
      
 31 
     | 
    
         
            +
                    completionTokenCostInMillicents: number;
         
     | 
| 
       25 
32 
     | 
    
         
             
                    fineTunedTokenCostInMillicents: number;
         
     | 
| 
       26 
33 
     | 
    
         
             
                };
         
     | 
| 
       27 
34 
     | 
    
         
             
                "text-davinci-003": {
         
     | 
| 
       28 
35 
     | 
    
         
             
                    contextWindowSize: number;
         
     | 
| 
       29 
     | 
    
         
            -
                     
     | 
| 
      
 36 
     | 
    
         
            +
                    promptTokenCostInMillicents: number;
         
     | 
| 
      
 37 
     | 
    
         
            +
                    completionTokenCostInMillicents: number;
         
     | 
| 
       30 
38 
     | 
    
         
             
                };
         
     | 
| 
       31 
39 
     | 
    
         
             
                "text-davinci-002": {
         
     | 
| 
       32 
40 
     | 
    
         
             
                    contextWindowSize: number;
         
     | 
| 
       33 
     | 
    
         
            -
                     
     | 
| 
      
 41 
     | 
    
         
            +
                    promptTokenCostInMillicents: number;
         
     | 
| 
      
 42 
     | 
    
         
            +
                    completionTokenCostInMillicents: number;
         
     | 
| 
       34 
43 
     | 
    
         
             
                };
         
     | 
| 
       35 
44 
     | 
    
         
             
                "code-davinci-002": {
         
     | 
| 
       36 
45 
     | 
    
         
             
                    contextWindowSize: number;
         
     | 
| 
       37 
     | 
    
         
            -
                     
     | 
| 
      
 46 
     | 
    
         
            +
                    promptTokenCostInMillicents: number;
         
     | 
| 
      
 47 
     | 
    
         
            +
                    completionTokenCostInMillicents: number;
         
     | 
| 
       38 
48 
     | 
    
         
             
                };
         
     | 
| 
       39 
49 
     | 
    
         
             
                davinci: {
         
     | 
| 
       40 
50 
     | 
    
         
             
                    contextWindowSize: number;
         
     | 
| 
       41 
     | 
    
         
            -
                     
     | 
| 
      
 51 
     | 
    
         
            +
                    promptTokenCostInMillicents: number;
         
     | 
| 
      
 52 
     | 
    
         
            +
                    completionTokenCostInMillicents: number;
         
     | 
| 
       42 
53 
     | 
    
         
             
                };
         
     | 
| 
       43 
54 
     | 
    
         
             
                "text-curie-001": {
         
     | 
| 
       44 
55 
     | 
    
         
             
                    contextWindowSize: number;
         
     | 
| 
       45 
     | 
    
         
            -
                     
     | 
| 
      
 56 
     | 
    
         
            +
                    promptTokenCostInMillicents: number;
         
     | 
| 
      
 57 
     | 
    
         
            +
                    completionTokenCostInMillicents: number;
         
     | 
| 
       46 
58 
     | 
    
         
             
                };
         
     | 
| 
       47 
59 
     | 
    
         
             
                curie: {
         
     | 
| 
       48 
60 
     | 
    
         
             
                    contextWindowSize: number;
         
     | 
| 
       49 
     | 
    
         
            -
                     
     | 
| 
      
 61 
     | 
    
         
            +
                    promptTokenCostInMillicents: number;
         
     | 
| 
      
 62 
     | 
    
         
            +
                    completionTokenCostInMillicents: number;
         
     | 
| 
       50 
63 
     | 
    
         
             
                };
         
     | 
| 
       51 
64 
     | 
    
         
             
                "text-babbage-001": {
         
     | 
| 
       52 
65 
     | 
    
         
             
                    contextWindowSize: number;
         
     | 
| 
       53 
     | 
    
         
            -
                     
     | 
| 
      
 66 
     | 
    
         
            +
                    promptTokenCostInMillicents: number;
         
     | 
| 
      
 67 
     | 
    
         
            +
                    completionTokenCostInMillicents: number;
         
     | 
| 
       54 
68 
     | 
    
         
             
                };
         
     | 
| 
       55 
69 
     | 
    
         
             
                babbage: {
         
     | 
| 
       56 
70 
     | 
    
         
             
                    contextWindowSize: number;
         
     | 
| 
       57 
     | 
    
         
            -
                     
     | 
| 
      
 71 
     | 
    
         
            +
                    promptTokenCostInMillicents: number;
         
     | 
| 
      
 72 
     | 
    
         
            +
                    completionTokenCostInMillicents: number;
         
     | 
| 
       58 
73 
     | 
    
         
             
                };
         
     | 
| 
       59 
74 
     | 
    
         
             
                "text-ada-001": {
         
     | 
| 
       60 
75 
     | 
    
         
             
                    contextWindowSize: number;
         
     | 
| 
       61 
     | 
    
         
            -
                     
     | 
| 
      
 76 
     | 
    
         
            +
                    promptTokenCostInMillicents: number;
         
     | 
| 
      
 77 
     | 
    
         
            +
                    completionTokenCostInMillicents: number;
         
     | 
| 
       62 
78 
     | 
    
         
             
                };
         
     | 
| 
       63 
79 
     | 
    
         
             
                ada: {
         
     | 
| 
       64 
80 
     | 
    
         
             
                    contextWindowSize: number;
         
     | 
| 
       65 
     | 
    
         
            -
                     
     | 
| 
      
 81 
     | 
    
         
            +
                    promptTokenCostInMillicents: number;
         
     | 
| 
      
 82 
     | 
    
         
            +
                    completionTokenCostInMillicents: number;
         
     | 
| 
       66 
83 
     | 
    
         
             
                };
         
     | 
| 
       67 
84 
     | 
    
         
             
            };
         
     | 
| 
       68 
85 
     | 
    
         
             
            export declare function getOpenAITextGenerationModelInformation(model: OpenAITextGenerationModelType): {
         
     | 
| 
       69 
86 
     | 
    
         
             
                baseModel: OpenAITextGenerationBaseModelType;
         
     | 
| 
       70 
87 
     | 
    
         
             
                isFineTuned: boolean;
         
     | 
| 
       71 
88 
     | 
    
         
             
                contextWindowSize: number;
         
     | 
| 
       72 
     | 
    
         
            -
                 
     | 
| 
      
 89 
     | 
    
         
            +
                promptTokenCostInMillicents: number;
         
     | 
| 
      
 90 
     | 
    
         
            +
                completionTokenCostInMillicents: number;
         
     | 
| 
       73 
91 
     | 
    
         
             
            };
         
     | 
| 
       74 
92 
     | 
    
         
             
            type FineTuneableOpenAITextGenerationModelType = "davinci-002" | "babbage-002";
         
     | 
| 
       75 
93 
     | 
    
         
             
            type FineTunedOpenAITextGenerationModelType = `ft:${FineTuneableOpenAITextGenerationModelType}:${string}:${string}:${string}`;
         
     | 
| 
         @@ -106,7 +124,7 @@ export interface OpenAITextGenerationModelSettings extends TextGenerationModelSe 
     | 
|
| 
       106 
124 
     | 
    
         
             
             *
         
     | 
| 
       107 
125 
     | 
    
         
             
             * @example
         
     | 
| 
       108 
126 
     | 
    
         
             
             * const model = new OpenAITextGenerationModel({
         
     | 
| 
       109 
     | 
    
         
            -
             *   model: " 
     | 
| 
      
 127 
     | 
    
         
            +
             *   model: "gpt-3.5-turbo-instruct",
         
     | 
| 
       110 
128 
     | 
    
         
             
             *   temperature: 0.7,
         
     | 
| 
       111 
129 
     | 
    
         
             
             *   maxCompletionTokens: 500,
         
     | 
| 
       112 
130 
     | 
    
         
             
             *   retry: retryWithExponentialBackoff({ maxTries: 5 }),
         
     | 
| 
         @@ -15,55 +15,72 @@ import { TikTokenTokenizer } from "./TikTokenTokenizer.js"; 
     | 
|
| 
       15 
15 
     | 
    
         
             
             * @see https://openai.com/pricing
         
     | 
| 
       16 
16 
     | 
    
         
             
             */
         
     | 
| 
       17 
17 
     | 
    
         
             
            export const OPENAI_TEXT_GENERATION_MODELS = {
         
     | 
| 
      
 18 
     | 
    
         
            +
                "gpt-3.5-turbo-instruct": {
         
     | 
| 
      
 19 
     | 
    
         
            +
                    contextWindowSize: 4097,
         
     | 
| 
      
 20 
     | 
    
         
            +
                    promptTokenCostInMillicents: 0.15,
         
     | 
| 
      
 21 
     | 
    
         
            +
                    completionTokenCostInMillicents: 0.2,
         
     | 
| 
      
 22 
     | 
    
         
            +
                },
         
     | 
| 
       18 
23 
     | 
    
         
             
                "davinci-002": {
         
     | 
| 
       19 
24 
     | 
    
         
             
                    contextWindowSize: 16384,
         
     | 
| 
       20 
     | 
    
         
            -
                     
     | 
| 
      
 25 
     | 
    
         
            +
                    promptTokenCostInMillicents: 0.2,
         
     | 
| 
      
 26 
     | 
    
         
            +
                    completionTokenCostInMillicents: 0.2,
         
     | 
| 
       21 
27 
     | 
    
         
             
                    fineTunedTokenCostInMillicents: 1.2,
         
     | 
| 
       22 
28 
     | 
    
         
             
                },
         
     | 
| 
       23 
29 
     | 
    
         
             
                "babbage-002": {
         
     | 
| 
       24 
30 
     | 
    
         
             
                    contextWindowSize: 16384,
         
     | 
| 
       25 
     | 
    
         
            -
                     
     | 
| 
      
 31 
     | 
    
         
            +
                    promptTokenCostInMillicents: 0.04,
         
     | 
| 
      
 32 
     | 
    
         
            +
                    completionTokenCostInMillicents: 0.04,
         
     | 
| 
       26 
33 
     | 
    
         
             
                    fineTunedTokenCostInMillicents: 0.16,
         
     | 
| 
       27 
34 
     | 
    
         
             
                },
         
     | 
| 
       28 
35 
     | 
    
         
             
                "text-davinci-003": {
         
     | 
| 
       29 
36 
     | 
    
         
             
                    contextWindowSize: 4096,
         
     | 
| 
       30 
     | 
    
         
            -
                     
     | 
| 
      
 37 
     | 
    
         
            +
                    promptTokenCostInMillicents: 2,
         
     | 
| 
      
 38 
     | 
    
         
            +
                    completionTokenCostInMillicents: 2,
         
     | 
| 
       31 
39 
     | 
    
         
             
                },
         
     | 
| 
       32 
40 
     | 
    
         
             
                "text-davinci-002": {
         
     | 
| 
       33 
41 
     | 
    
         
             
                    contextWindowSize: 4096,
         
     | 
| 
       34 
     | 
    
         
            -
                     
     | 
| 
      
 42 
     | 
    
         
            +
                    promptTokenCostInMillicents: 2,
         
     | 
| 
      
 43 
     | 
    
         
            +
                    completionTokenCostInMillicents: 2,
         
     | 
| 
       35 
44 
     | 
    
         
             
                },
         
     | 
| 
       36 
45 
     | 
    
         
             
                "code-davinci-002": {
         
     | 
| 
       37 
46 
     | 
    
         
             
                    contextWindowSize: 8000,
         
     | 
| 
       38 
     | 
    
         
            -
                     
     | 
| 
      
 47 
     | 
    
         
            +
                    promptTokenCostInMillicents: 2,
         
     | 
| 
      
 48 
     | 
    
         
            +
                    completionTokenCostInMillicents: 2,
         
     | 
| 
       39 
49 
     | 
    
         
             
                },
         
     | 
| 
       40 
50 
     | 
    
         
             
                davinci: {
         
     | 
| 
       41 
51 
     | 
    
         
             
                    contextWindowSize: 2048,
         
     | 
| 
       42 
     | 
    
         
            -
                     
     | 
| 
      
 52 
     | 
    
         
            +
                    promptTokenCostInMillicents: 2,
         
     | 
| 
      
 53 
     | 
    
         
            +
                    completionTokenCostInMillicents: 2,
         
     | 
| 
       43 
54 
     | 
    
         
             
                },
         
     | 
| 
       44 
55 
     | 
    
         
             
                "text-curie-001": {
         
     | 
| 
       45 
56 
     | 
    
         
             
                    contextWindowSize: 2048,
         
     | 
| 
       46 
     | 
    
         
            -
                     
     | 
| 
      
 57 
     | 
    
         
            +
                    promptTokenCostInMillicents: 0.2,
         
     | 
| 
      
 58 
     | 
    
         
            +
                    completionTokenCostInMillicents: 0.2,
         
     | 
| 
       47 
59 
     | 
    
         
             
                },
         
     | 
| 
       48 
60 
     | 
    
         
             
                curie: {
         
     | 
| 
       49 
61 
     | 
    
         
             
                    contextWindowSize: 2048,
         
     | 
| 
       50 
     | 
    
         
            -
                     
     | 
| 
      
 62 
     | 
    
         
            +
                    promptTokenCostInMillicents: 0.2,
         
     | 
| 
      
 63 
     | 
    
         
            +
                    completionTokenCostInMillicents: 0.2,
         
     | 
| 
       51 
64 
     | 
    
         
             
                },
         
     | 
| 
       52 
65 
     | 
    
         
             
                "text-babbage-001": {
         
     | 
| 
       53 
66 
     | 
    
         
             
                    contextWindowSize: 2048,
         
     | 
| 
       54 
     | 
    
         
            -
                     
     | 
| 
      
 67 
     | 
    
         
            +
                    promptTokenCostInMillicents: 0.05,
         
     | 
| 
      
 68 
     | 
    
         
            +
                    completionTokenCostInMillicents: 0.05,
         
     | 
| 
       55 
69 
     | 
    
         
             
                },
         
     | 
| 
       56 
70 
     | 
    
         
             
                babbage: {
         
     | 
| 
       57 
71 
     | 
    
         
             
                    contextWindowSize: 2048,
         
     | 
| 
       58 
     | 
    
         
            -
                     
     | 
| 
      
 72 
     | 
    
         
            +
                    promptTokenCostInMillicents: 0.05,
         
     | 
| 
      
 73 
     | 
    
         
            +
                    completionTokenCostInMillicents: 0.05,
         
     | 
| 
       59 
74 
     | 
    
         
             
                },
         
     | 
| 
       60 
75 
     | 
    
         
             
                "text-ada-001": {
         
     | 
| 
       61 
76 
     | 
    
         
             
                    contextWindowSize: 2048,
         
     | 
| 
       62 
     | 
    
         
            -
                     
     | 
| 
      
 77 
     | 
    
         
            +
                    promptTokenCostInMillicents: 0.04,
         
     | 
| 
      
 78 
     | 
    
         
            +
                    completionTokenCostInMillicents: 0.04,
         
     | 
| 
       63 
79 
     | 
    
         
             
                },
         
     | 
| 
       64 
80 
     | 
    
         
             
                ada: {
         
     | 
| 
       65 
81 
     | 
    
         
             
                    contextWindowSize: 2048,
         
     | 
| 
       66 
     | 
    
         
            -
                     
     | 
| 
      
 82 
     | 
    
         
            +
                    promptTokenCostInMillicents: 0.04,
         
     | 
| 
      
 83 
     | 
    
         
            +
                    completionTokenCostInMillicents: 0.04,
         
     | 
| 
       67 
84 
     | 
    
         
             
                },
         
     | 
| 
       68 
85 
     | 
    
         
             
            };
         
     | 
| 
       69 
86 
     | 
    
         
             
            export function getOpenAITextGenerationModelInformation(model) {
         
     | 
| 
         @@ -74,7 +91,8 @@ export function getOpenAITextGenerationModelInformation(model) { 
     | 
|
| 
       74 
91 
     | 
    
         
             
                        baseModel: model,
         
     | 
| 
       75 
92 
     | 
    
         
             
                        isFineTuned: false,
         
     | 
| 
       76 
93 
     | 
    
         
             
                        contextWindowSize: baseModelInformation.contextWindowSize,
         
     | 
| 
       77 
     | 
    
         
            -
                         
     | 
| 
      
 94 
     | 
    
         
            +
                        promptTokenCostInMillicents: baseModelInformation.promptTokenCostInMillicents,
         
     | 
| 
      
 95 
     | 
    
         
            +
                        completionTokenCostInMillicents: baseModelInformation.completionTokenCostInMillicents,
         
     | 
| 
       78 
96 
     | 
    
         
             
                    };
         
     | 
| 
       79 
97 
     | 
    
         
             
                }
         
     | 
| 
       80 
98 
     | 
    
         
             
                // Extract the base model from the fine-tuned model:
         
     | 
| 
         @@ -86,7 +104,8 @@ export function getOpenAITextGenerationModelInformation(model) { 
     | 
|
| 
       86 
104 
     | 
    
         
             
                        baseModel: baseModel,
         
     | 
| 
       87 
105 
     | 
    
         
             
                        isFineTuned: true,
         
     | 
| 
       88 
106 
     | 
    
         
             
                        contextWindowSize: baseModelInformation.contextWindowSize,
         
     | 
| 
       89 
     | 
    
         
            -
                         
     | 
| 
      
 107 
     | 
    
         
            +
                        promptTokenCostInMillicents: baseModelInformation.fineTunedTokenCostInMillicents,
         
     | 
| 
      
 108 
     | 
    
         
            +
                        completionTokenCostInMillicents: baseModelInformation.fineTunedTokenCostInMillicents,
         
     | 
| 
       90 
109 
     | 
    
         
             
                    };
         
     | 
| 
       91 
110 
     | 
    
         
             
                }
         
     | 
| 
       92 
111 
     | 
    
         
             
                throw new Error(`Unknown OpenAI chat base model ${baseModel}.`);
         
     | 
| 
         @@ -94,8 +113,13 @@ export function getOpenAITextGenerationModelInformation(model) { 
     | 
|
| 
       94 
113 
     | 
    
         
             
            export const isOpenAITextGenerationModel = (model) => model in OPENAI_TEXT_GENERATION_MODELS ||
         
     | 
| 
       95 
114 
     | 
    
         
             
                model.startsWith("ft:davinci-002:") ||
         
     | 
| 
       96 
115 
     | 
    
         
             
                model.startsWith("ft:babbage-002:");
         
     | 
| 
       97 
     | 
    
         
            -
            export const calculateOpenAITextGenerationCostInMillicents = ({ model, response, }) =>  
     | 
| 
       98 
     | 
    
         
            -
                getOpenAITextGenerationModelInformation(model) 
     | 
| 
      
 116 
     | 
    
         
            +
            export const calculateOpenAITextGenerationCostInMillicents = ({ model, response, }) => {
         
     | 
| 
      
 117 
     | 
    
         
            +
                const modelInformation = getOpenAITextGenerationModelInformation(model);
         
     | 
| 
      
 118 
     | 
    
         
            +
                return (response.usage.prompt_tokens *
         
     | 
| 
      
 119 
     | 
    
         
            +
                    modelInformation.promptTokenCostInMillicents +
         
     | 
| 
      
 120 
     | 
    
         
            +
                    response.usage.completion_tokens *
         
     | 
| 
      
 121 
     | 
    
         
            +
                        modelInformation.completionTokenCostInMillicents);
         
     | 
| 
      
 122 
     | 
    
         
            +
            };
         
     | 
| 
       99 
123 
     | 
    
         
             
            /**
         
     | 
| 
       100 
124 
     | 
    
         
             
             * Create a text generation model that calls the OpenAI text completion API.
         
     | 
| 
       101 
125 
     | 
    
         
             
             *
         
     | 
| 
         @@ -103,7 +127,7 @@ export const calculateOpenAITextGenerationCostInMillicents = ({ model, response, 
     | 
|
| 
       103 
127 
     | 
    
         
             
             *
         
     | 
| 
       104 
128 
     | 
    
         
             
             * @example
         
     | 
| 
       105 
129 
     | 
    
         
             
             * const model = new OpenAITextGenerationModel({
         
     | 
| 
       106 
     | 
    
         
            -
             *   model: " 
     | 
| 
      
 130 
     | 
    
         
            +
             *   model: "gpt-3.5-turbo-instruct",
         
     | 
| 
       107 
131 
     | 
    
         
             
             *   temperature: 0.7,
         
     | 
| 
       108 
132 
     | 
    
         
             
             *   maxCompletionTokens: 500,
         
     | 
| 
       109 
133 
     | 
    
         
             
             *   retry: retryWithExponentialBackoff({ maxTries: 5 }),
         
     | 
| 
         @@ -57,8 +57,6 @@ function getEncodingNameForModel(model) { 
     | 
|
| 
       57 
57 
     | 
    
         
             
                    case "text-davinci-003": {
         
     | 
| 
       58 
58 
     | 
    
         
             
                        return "p50k_base";
         
     | 
| 
       59 
59 
     | 
    
         
             
                    }
         
     | 
| 
       60 
     | 
    
         
            -
                    case "babbage-002":
         
     | 
| 
       61 
     | 
    
         
            -
                    case "davinci-002":
         
     | 
| 
       62 
60 
     | 
    
         
             
                    case "ada":
         
     | 
| 
       63 
61 
     | 
    
         
             
                    case "babbage":
         
     | 
| 
       64 
62 
     | 
    
         
             
                    case "curie":
         
     | 
| 
         @@ -68,11 +66,14 @@ function getEncodingNameForModel(model) { 
     | 
|
| 
       68 
66 
     | 
    
         
             
                    case "text-curie-001": {
         
     | 
| 
       69 
67 
     | 
    
         
             
                        return "r50k_base";
         
     | 
| 
       70 
68 
     | 
    
         
             
                    }
         
     | 
| 
      
 69 
     | 
    
         
            +
                    case "babbage-002":
         
     | 
| 
      
 70 
     | 
    
         
            +
                    case "davinci-002":
         
     | 
| 
       71 
71 
     | 
    
         
             
                    case "gpt-3.5-turbo":
         
     | 
| 
       72 
72 
     | 
    
         
             
                    case "gpt-3.5-turbo-0301":
         
     | 
| 
       73 
73 
     | 
    
         
             
                    case "gpt-3.5-turbo-0613":
         
     | 
| 
       74 
74 
     | 
    
         
             
                    case "gpt-3.5-turbo-16k":
         
     | 
| 
       75 
75 
     | 
    
         
             
                    case "gpt-3.5-turbo-16k-0613":
         
     | 
| 
      
 76 
     | 
    
         
            +
                    case "gpt-3.5-turbo-instruct":
         
     | 
| 
       76 
77 
     | 
    
         
             
                    case "gpt-4":
         
     | 
| 
       77 
78 
     | 
    
         
             
                    case "gpt-4-0314":
         
     | 
| 
       78 
79 
     | 
    
         
             
                    case "gpt-4-0613":
         
     | 
| 
         @@ -53,8 +53,6 @@ function getEncodingNameForModel(model) { 
     | 
|
| 
       53 
53 
     | 
    
         
             
                    case "text-davinci-003": {
         
     | 
| 
       54 
54 
     | 
    
         
             
                        return "p50k_base";
         
     | 
| 
       55 
55 
     | 
    
         
             
                    }
         
     | 
| 
       56 
     | 
    
         
            -
                    case "babbage-002":
         
     | 
| 
       57 
     | 
    
         
            -
                    case "davinci-002":
         
     | 
| 
       58 
56 
     | 
    
         
             
                    case "ada":
         
     | 
| 
       59 
57 
     | 
    
         
             
                    case "babbage":
         
     | 
| 
       60 
58 
     | 
    
         
             
                    case "curie":
         
     | 
| 
         @@ -64,11 +62,14 @@ function getEncodingNameForModel(model) { 
     | 
|
| 
       64 
62 
     | 
    
         
             
                    case "text-curie-001": {
         
     | 
| 
       65 
63 
     | 
    
         
             
                        return "r50k_base";
         
     | 
| 
       66 
64 
     | 
    
         
             
                    }
         
     | 
| 
      
 65 
     | 
    
         
            +
                    case "babbage-002":
         
     | 
| 
      
 66 
     | 
    
         
            +
                    case "davinci-002":
         
     | 
| 
       67 
67 
     | 
    
         
             
                    case "gpt-3.5-turbo":
         
     | 
| 
       68 
68 
     | 
    
         
             
                    case "gpt-3.5-turbo-0301":
         
     | 
| 
       69 
69 
     | 
    
         
             
                    case "gpt-3.5-turbo-0613":
         
     | 
| 
       70 
70 
     | 
    
         
             
                    case "gpt-3.5-turbo-16k":
         
     | 
| 
       71 
71 
     | 
    
         
             
                    case "gpt-3.5-turbo-16k-0613":
         
     | 
| 
      
 72 
     | 
    
         
            +
                    case "gpt-3.5-turbo-instruct":
         
     | 
| 
       72 
73 
     | 
    
         
             
                    case "gpt-4":
         
     | 
| 
       73 
74 
     | 
    
         
             
                    case "gpt-4-0314":
         
     | 
| 
       74 
75 
     | 
    
         
             
                    case "gpt-4-0613":
         
     | 
| 
         @@ -86,6 +86,12 @@ class StabilityImageGenerationModel extends AbstractModel_js_1.AbstractModel { 
     | 
|
| 
       86 
86 
     | 
    
         
             
                }
         
     | 
| 
       87 
87 
     | 
    
         
             
            }
         
     | 
| 
       88 
88 
     | 
    
         
             
            exports.StabilityImageGenerationModel = StabilityImageGenerationModel;
         
     | 
| 
      
 89 
     | 
    
         
            +
            const stabilityImageGenerationModels = [
         
     | 
| 
      
 90 
     | 
    
         
            +
                "stable-diffusion-v1-5",
         
     | 
| 
      
 91 
     | 
    
         
            +
                "stable-diffusion-512-v2-1",
         
     | 
| 
      
 92 
     | 
    
         
            +
                "stable-diffusion-xl-1024-v0-9",
         
     | 
| 
      
 93 
     | 
    
         
            +
                "stable-diffusion-xl-1024-v1-0",
         
     | 
| 
      
 94 
     | 
    
         
            +
            ];
         
     | 
| 
       89 
95 
     | 
    
         
             
            const stabilityImageGenerationResponseSchema = zod_1.z.object({
         
     | 
| 
       90 
96 
     | 
    
         
             
                artifacts: zod_1.z.array(zod_1.z.object({
         
     | 
| 
       91 
97 
     | 
    
         
             
                    base64: zod_1.z.string(),
         
     | 
| 
         @@ -28,7 +28,7 @@ import { ImageGenerationModel, ImageGenerationModelSettings } from "../../model- 
     | 
|
| 
       28 
28 
     | 
    
         
             
            export declare class StabilityImageGenerationModel extends AbstractModel<StabilityImageGenerationModelSettings> implements ImageGenerationModel<StabilityImageGenerationPrompt, StabilityImageGenerationResponse, StabilityImageGenerationModelSettings> {
         
     | 
| 
       29 
29 
     | 
    
         
             
                constructor(settings: StabilityImageGenerationModelSettings);
         
     | 
| 
       30 
30 
     | 
    
         
             
                readonly provider: "stability";
         
     | 
| 
       31 
     | 
    
         
            -
                get modelName():  
     | 
| 
      
 31 
     | 
    
         
            +
                get modelName(): StabilityImageGenerationModelType;
         
     | 
| 
       32 
32 
     | 
    
         
             
                callAPI(input: StabilityImageGenerationPrompt, options?: ModelFunctionOptions<StabilityImageGenerationModelSettings>): Promise<StabilityImageGenerationResponse>;
         
     | 
| 
       33 
33 
     | 
    
         
             
                get settingsForEvent(): Partial<StabilityImageGenerationModelSettings>;
         
     | 
| 
       34 
34 
     | 
    
         
             
                generateImageResponse(prompt: StabilityImageGenerationPrompt, options?: ModelFunctionOptions<StabilityImageGenerationModelSettings>): Promise<{
         
     | 
| 
         @@ -41,9 +41,11 @@ export declare class StabilityImageGenerationModel extends AbstractModel<Stabili 
     | 
|
| 
       41 
41 
     | 
    
         
             
                extractBase64Image(response: StabilityImageGenerationResponse): string;
         
     | 
| 
       42 
42 
     | 
    
         
             
                withSettings(additionalSettings: StabilityImageGenerationModelSettings): this;
         
     | 
| 
       43 
43 
     | 
    
         
             
            }
         
     | 
| 
      
 44 
     | 
    
         
            +
            declare const stabilityImageGenerationModels: readonly ["stable-diffusion-v1-5", "stable-diffusion-512-v2-1", "stable-diffusion-xl-1024-v0-9", "stable-diffusion-xl-1024-v1-0"];
         
     | 
| 
      
 45 
     | 
    
         
            +
            export type StabilityImageGenerationModelType = (typeof stabilityImageGenerationModels)[number] | (string & {});
         
     | 
| 
       44 
46 
     | 
    
         
             
            export interface StabilityImageGenerationModelSettings extends ImageGenerationModelSettings {
         
     | 
| 
       45 
47 
     | 
    
         
             
                api?: ApiConfiguration;
         
     | 
| 
       46 
     | 
    
         
            -
                model:  
     | 
| 
      
 48 
     | 
    
         
            +
                model: StabilityImageGenerationModelType;
         
     | 
| 
       47 
49 
     | 
    
         
             
                height?: number;
         
     | 
| 
       48 
50 
     | 
    
         
             
                width?: number;
         
     | 
| 
       49 
51 
     | 
    
         
             
                cfgScale?: number;
         
     | 
| 
         @@ -82,6 +82,12 @@ export class StabilityImageGenerationModel extends AbstractModel { 
     | 
|
| 
       82 
82 
     | 
    
         
             
                    return new StabilityImageGenerationModel(Object.assign({}, this.settings, additionalSettings));
         
     | 
| 
       83 
83 
     | 
    
         
             
                }
         
     | 
| 
       84 
84 
     | 
    
         
             
            }
         
     | 
| 
      
 85 
     | 
    
         
            +
            const stabilityImageGenerationModels = [
         
     | 
| 
      
 86 
     | 
    
         
            +
                "stable-diffusion-v1-5",
         
     | 
| 
      
 87 
     | 
    
         
            +
                "stable-diffusion-512-v2-1",
         
     | 
| 
      
 88 
     | 
    
         
            +
                "stable-diffusion-xl-1024-v0-9",
         
     | 
| 
      
 89 
     | 
    
         
            +
                "stable-diffusion-xl-1024-v1-0",
         
     | 
| 
      
 90 
     | 
    
         
            +
            ];
         
     | 
| 
       85 
91 
     | 
    
         
             
            const stabilityImageGenerationResponseSchema = z.object({
         
     | 
| 
       86 
92 
     | 
    
         
             
                artifacts: z.array(z.object({
         
     | 
| 
       87 
93 
     | 
    
         
             
                    base64: z.string(),
         
     | 
    
        package/package.json
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            {
         
     | 
| 
       2 
2 
     | 
    
         
             
              "name": "modelfusion",
         
     | 
| 
       3 
3 
     | 
    
         
             
              "description": "Build multimodal applications, chatbots, and agents with JavaScript and TypeScript.",
         
     | 
| 
       4 
     | 
    
         
            -
              "version": "0. 
     | 
| 
      
 4 
     | 
    
         
            +
              "version": "0.33.0",
         
     | 
| 
       5 
5 
     | 
    
         
             
              "author": "Lars Grammel",
         
     | 
| 
       6 
6 
     | 
    
         
             
              "license": "MIT",
         
     | 
| 
       7 
7 
     | 
    
         
             
              "keywords": [
         
     | 
| 
         @@ -55,7 +55,7 @@ 
     | 
|
| 
       55 
55 
     | 
    
         
             
                "js-tiktoken": "1.0.7",
         
     | 
| 
       56 
56 
     | 
    
         
             
                "nanoid": "3.3.6",
         
     | 
| 
       57 
57 
     | 
    
         
             
                "secure-json-parse": "2.7.0",
         
     | 
| 
       58 
     | 
    
         
            -
                "zod": "3. 
     | 
| 
      
 58 
     | 
    
         
            +
                "zod": "3.21.4",
         
     | 
| 
       59 
59 
     | 
    
         
             
                "zod-to-json-schema": "3.21.4"
         
     | 
| 
       60 
60 
     | 
    
         
             
              },
         
     | 
| 
       61 
61 
     | 
    
         
             
              "devDependencies": {
         
     | 
| 
         @@ -0,0 +1,7 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            import { ModelFunctionOptions } from "../model-function/ModelFunctionOptions.js";
         
     | 
| 
      
 2 
     | 
    
         
            +
            export interface RetrieverSettings {
         
     | 
| 
      
 3 
     | 
    
         
            +
            }
         
     | 
| 
      
 4 
     | 
    
         
            +
            export interface Retriever<OBJECT, QUERY, SETTINGS extends RetrieverSettings> {
         
     | 
| 
      
 5 
     | 
    
         
            +
                retrieve(query: QUERY, options?: ModelFunctionOptions<RetrieverSettings>): Promise<OBJECT[]>;
         
     | 
| 
      
 6 
     | 
    
         
            +
                withSettings(additionalSettings: Partial<SETTINGS>): this;
         
     | 
| 
      
 7 
     | 
    
         
            +
            }
         
     | 
| 
         @@ -0,0 +1,18 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            "use strict";
         
     | 
| 
      
 2 
     | 
    
         
            +
            var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
         
     | 
| 
      
 3 
     | 
    
         
            +
                if (k2 === undefined) k2 = k;
         
     | 
| 
      
 4 
     | 
    
         
            +
                var desc = Object.getOwnPropertyDescriptor(m, k);
         
     | 
| 
      
 5 
     | 
    
         
            +
                if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
         
     | 
| 
      
 6 
     | 
    
         
            +
                  desc = { enumerable: true, get: function() { return m[k]; } };
         
     | 
| 
      
 7 
     | 
    
         
            +
                }
         
     | 
| 
      
 8 
     | 
    
         
            +
                Object.defineProperty(o, k2, desc);
         
     | 
| 
      
 9 
     | 
    
         
            +
            }) : (function(o, m, k, k2) {
         
     | 
| 
      
 10 
     | 
    
         
            +
                if (k2 === undefined) k2 = k;
         
     | 
| 
      
 11 
     | 
    
         
            +
                o[k2] = m[k];
         
     | 
| 
      
 12 
     | 
    
         
            +
            }));
         
     | 
| 
      
 13 
     | 
    
         
            +
            var __exportStar = (this && this.__exportStar) || function(m, exports) {
         
     | 
| 
      
 14 
     | 
    
         
            +
                for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
         
     | 
| 
      
 15 
     | 
    
         
            +
            };
         
     | 
| 
      
 16 
     | 
    
         
            +
            Object.defineProperty(exports, "__esModule", { value: true });
         
     | 
| 
      
 17 
     | 
    
         
            +
            __exportStar(require("./Retriever.cjs"), exports);
         
     | 
| 
      
 18 
     | 
    
         
            +
            __exportStar(require("./retrieve.cjs"), exports);
         
     | 
| 
         @@ -0,0 +1,9 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            "use strict";
         
     | 
| 
      
 2 
     | 
    
         
            +
            Object.defineProperty(exports, "__esModule", { value: true });
         
     | 
| 
      
 3 
     | 
    
         
            +
            exports.retrieve = void 0;
         
     | 
| 
      
 4 
     | 
    
         
            +
            async function retrieve(retriever, query, options) {
         
     | 
| 
      
 5 
     | 
    
         
            +
                // TODO add error handling, events, duration tracking, etc.
         
     | 
| 
      
 6 
     | 
    
         
            +
                // TODO metadata handling
         
     | 
| 
      
 7 
     | 
    
         
            +
                return retriever.retrieve(query, options);
         
     | 
| 
      
 8 
     | 
    
         
            +
            }
         
     | 
| 
      
 9 
     | 
    
         
            +
            exports.retrieve = retrieve;
         
     | 
| 
         @@ -0,0 +1,3 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            import { ModelFunctionOptions } from "../model-function/ModelFunctionOptions.js";
         
     | 
| 
      
 2 
     | 
    
         
            +
            import { Retriever, RetrieverSettings } from "./Retriever.js";
         
     | 
| 
      
 3 
     | 
    
         
            +
            export declare function retrieve<OBJECT, QUERY, SETTINGS extends RetrieverSettings>(retriever: Retriever<OBJECT, QUERY, SETTINGS>, query: QUERY, options?: ModelFunctionOptions<SETTINGS>): Promise<OBJECT[]>;
         
     | 
    
        package/text-chunk/index.cjs
    CHANGED
    
    | 
         @@ -14,12 +14,8 @@ 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("./SimilarTextChunksFromVectorIndexRetriever.cjs"), exports);
         
     | 
| 
       18 
17 
     | 
    
         
             
            __exportStar(require("./TextChunk.cjs"), exports);
         
     | 
| 
       19 
     | 
    
         
            -
            __exportStar(require("./retrieve-text-chunks/TextChunkRetriever.cjs"), exports);
         
     | 
| 
       20 
     | 
    
         
            -
            __exportStar(require("./retrieve-text-chunks/retrieveTextChunks.cjs"), exports);
         
     | 
| 
       21 
18 
     | 
    
         
             
            __exportStar(require("./split/SplitFunction.cjs"), exports);
         
     | 
| 
       22 
19 
     | 
    
         
             
            __exportStar(require("./split/splitOnSeparator.cjs"), exports);
         
     | 
| 
       23 
20 
     | 
    
         
             
            __exportStar(require("./split/splitRecursively.cjs"), exports);
         
     | 
| 
       24 
21 
     | 
    
         
             
            __exportStar(require("./split/splitTextChunks.cjs"), exports);
         
     | 
| 
       25 
     | 
    
         
            -
            __exportStar(require("./upsertTextChunks.cjs"), exports);
         
     | 
    
        package/text-chunk/index.d.ts
    CHANGED
    
    | 
         @@ -1,9 +1,5 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
            export * from "./SimilarTextChunksFromVectorIndexRetriever.js";
         
     | 
| 
       2 
1 
     | 
    
         
             
            export * from "./TextChunk.js";
         
     | 
| 
       3 
     | 
    
         
            -
            export * from "./retrieve-text-chunks/TextChunkRetriever.js";
         
     | 
| 
       4 
     | 
    
         
            -
            export * from "./retrieve-text-chunks/retrieveTextChunks.js";
         
     | 
| 
       5 
2 
     | 
    
         
             
            export * from "./split/SplitFunction.js";
         
     | 
| 
       6 
3 
     | 
    
         
             
            export * from "./split/splitOnSeparator.js";
         
     | 
| 
       7 
4 
     | 
    
         
             
            export * from "./split/splitRecursively.js";
         
     | 
| 
       8 
5 
     | 
    
         
             
            export * from "./split/splitTextChunks.js";
         
     | 
| 
       9 
     | 
    
         
            -
            export * from "./upsertTextChunks.js";
         
     | 
    
        package/text-chunk/index.js
    CHANGED
    
    | 
         @@ -1,9 +1,5 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
            export * from "./SimilarTextChunksFromVectorIndexRetriever.js";
         
     | 
| 
       2 
1 
     | 
    
         
             
            export * from "./TextChunk.js";
         
     | 
| 
       3 
     | 
    
         
            -
            export * from "./retrieve-text-chunks/TextChunkRetriever.js";
         
     | 
| 
       4 
     | 
    
         
            -
            export * from "./retrieve-text-chunks/retrieveTextChunks.js";
         
     | 
| 
       5 
2 
     | 
    
         
             
            export * from "./split/SplitFunction.js";
         
     | 
| 
       6 
3 
     | 
    
         
             
            export * from "./split/splitOnSeparator.js";
         
     | 
| 
       7 
4 
     | 
    
         
             
            export * from "./split/splitRecursively.js";
         
     | 
| 
       8 
5 
     | 
    
         
             
            export * from "./split/splitTextChunks.js";
         
     | 
| 
       9 
     | 
    
         
            -
            export * from "./upsertTextChunks.js";
         
     | 
| 
         @@ -1,8 +1,8 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            "use strict";
         
     | 
| 
       2 
2 
     | 
    
         
             
            Object.defineProperty(exports, "__esModule", { value: true });
         
     | 
| 
       3 
     | 
    
         
            -
            exports. 
     | 
| 
      
 3 
     | 
    
         
            +
            exports.VectorIndexRetriever = void 0;
         
     | 
| 
       4 
4 
     | 
    
         
             
            const embedText_js_1 = require("../model-function/embed-text/embedText.cjs");
         
     | 
| 
       5 
     | 
    
         
            -
            class  
     | 
| 
      
 5 
     | 
    
         
            +
            class VectorIndexRetriever {
         
     | 
| 
       6 
6 
     | 
    
         
             
                constructor({ vectorIndex, embeddingModel, maxResults, similarityThreshold, }) {
         
     | 
| 
       7 
7 
     | 
    
         
             
                    Object.defineProperty(this, "vectorIndex", {
         
     | 
| 
       8 
8 
     | 
    
         
             
                        enumerable: true,
         
     | 
| 
         @@ -29,9 +29,9 @@ class SimilarTextChunksFromVectorIndexRetriever { 
     | 
|
| 
       29 
29 
     | 
    
         
             
                        similarityThreshold,
         
     | 
| 
       30 
30 
     | 
    
         
             
                    };
         
     | 
| 
       31 
31 
     | 
    
         
             
                }
         
     | 
| 
       32 
     | 
    
         
            -
                async  
     | 
| 
      
 32 
     | 
    
         
            +
                async retrieve(query, options) {
         
     | 
| 
       33 
33 
     | 
    
         
             
                    if (options?.settings != null) {
         
     | 
| 
       34 
     | 
    
         
            -
                        return this.withSettings(options.settings). 
     | 
| 
      
 34 
     | 
    
         
            +
                        return this.withSettings(options.settings).retrieve(query, {
         
     | 
| 
       35 
35 
     | 
    
         
             
                            functionId: options.functionId,
         
     | 
| 
       36 
36 
     | 
    
         
             
                            observers: options.observers,
         
     | 
| 
       37 
37 
     | 
    
         
             
                            run: options.run,
         
     | 
| 
         @@ -49,10 +49,10 @@ class SimilarTextChunksFromVectorIndexRetriever { 
     | 
|
| 
       49 
49 
     | 
    
         
             
                    return queryResult.map((item) => item.data);
         
     | 
| 
       50 
50 
     | 
    
         
             
                }
         
     | 
| 
       51 
51 
     | 
    
         
             
                withSettings(additionalSettings) {
         
     | 
| 
       52 
     | 
    
         
            -
                    return new  
     | 
| 
      
 52 
     | 
    
         
            +
                    return new VectorIndexRetriever(Object.assign({}, this.settings, additionalSettings, {
         
     | 
| 
       53 
53 
     | 
    
         
             
                        vectorIndex: this.vectorIndex,
         
     | 
| 
       54 
54 
     | 
    
         
             
                        embeddingModel: this.embeddingModel,
         
     | 
| 
       55 
55 
     | 
    
         
             
                    }));
         
     | 
| 
       56 
56 
     | 
    
         
             
                }
         
     | 
| 
       57 
57 
     | 
    
         
             
            }
         
     | 
| 
       58 
     | 
    
         
            -
            exports. 
     | 
| 
      
 58 
     | 
    
         
            +
            exports.VectorIndexRetriever = VectorIndexRetriever;
         
     | 
| 
         @@ -0,0 +1,19 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            import { ModelFunctionOptions } from "../model-function/ModelFunctionOptions.js";
         
     | 
| 
      
 2 
     | 
    
         
            +
            import { TextEmbeddingModel, TextEmbeddingModelSettings } from "../model-function/embed-text/TextEmbeddingModel.js";
         
     | 
| 
      
 3 
     | 
    
         
            +
            import { Retriever, RetrieverSettings } from "../retriever/Retriever.js";
         
     | 
| 
      
 4 
     | 
    
         
            +
            import { VectorIndex } from "./VectorIndex.js";
         
     | 
| 
      
 5 
     | 
    
         
            +
            export interface VectorIndexRetrieverSettings {
         
     | 
| 
      
 6 
     | 
    
         
            +
                maxResults?: number;
         
     | 
| 
      
 7 
     | 
    
         
            +
                similarityThreshold?: number;
         
     | 
| 
      
 8 
     | 
    
         
            +
            }
         
     | 
| 
      
 9 
     | 
    
         
            +
            export declare class VectorIndexRetriever<OBJECT, INDEX> implements Retriever<OBJECT, string, VectorIndexRetrieverSettings> {
         
     | 
| 
      
 10 
     | 
    
         
            +
                private readonly vectorIndex;
         
     | 
| 
      
 11 
     | 
    
         
            +
                private readonly embeddingModel;
         
     | 
| 
      
 12 
     | 
    
         
            +
                private readonly settings;
         
     | 
| 
      
 13 
     | 
    
         
            +
                constructor({ vectorIndex, embeddingModel, maxResults, similarityThreshold, }: {
         
     | 
| 
      
 14 
     | 
    
         
            +
                    vectorIndex: VectorIndex<OBJECT, INDEX>;
         
     | 
| 
      
 15 
     | 
    
         
            +
                    embeddingModel: TextEmbeddingModel<unknown, TextEmbeddingModelSettings>;
         
     | 
| 
      
 16 
     | 
    
         
            +
                } & VectorIndexRetrieverSettings);
         
     | 
| 
      
 17 
     | 
    
         
            +
                retrieve(query: string, options?: ModelFunctionOptions<RetrieverSettings>): Promise<OBJECT[]>;
         
     | 
| 
      
 18 
     | 
    
         
            +
                withSettings(additionalSettings: Partial<VectorIndexRetrieverSettings>): this;
         
     | 
| 
      
 19 
     | 
    
         
            +
            }
         
     | 
| 
         @@ -1,5 +1,5 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            import { embedText } from "../model-function/embed-text/embedText.js";
         
     | 
| 
       2 
     | 
    
         
            -
            export class  
     | 
| 
      
 2 
     | 
    
         
            +
            export class VectorIndexRetriever {
         
     | 
| 
       3 
3 
     | 
    
         
             
                constructor({ vectorIndex, embeddingModel, maxResults, similarityThreshold, }) {
         
     | 
| 
       4 
4 
     | 
    
         
             
                    Object.defineProperty(this, "vectorIndex", {
         
     | 
| 
       5 
5 
     | 
    
         
             
                        enumerable: true,
         
     | 
| 
         @@ -26,9 +26,9 @@ export class SimilarTextChunksFromVectorIndexRetriever { 
     | 
|
| 
       26 
26 
     | 
    
         
             
                        similarityThreshold,
         
     | 
| 
       27 
27 
     | 
    
         
             
                    };
         
     | 
| 
       28 
28 
     | 
    
         
             
                }
         
     | 
| 
       29 
     | 
    
         
            -
                async  
     | 
| 
      
 29 
     | 
    
         
            +
                async retrieve(query, options) {
         
     | 
| 
       30 
30 
     | 
    
         
             
                    if (options?.settings != null) {
         
     | 
| 
       31 
     | 
    
         
            -
                        return this.withSettings(options.settings). 
     | 
| 
      
 31 
     | 
    
         
            +
                        return this.withSettings(options.settings).retrieve(query, {
         
     | 
| 
       32 
32 
     | 
    
         
             
                            functionId: options.functionId,
         
     | 
| 
       33 
33 
     | 
    
         
             
                            observers: options.observers,
         
     | 
| 
       34 
34 
     | 
    
         
             
                            run: options.run,
         
     | 
| 
         @@ -46,7 +46,7 @@ export class SimilarTextChunksFromVectorIndexRetriever { 
     | 
|
| 
       46 
46 
     | 
    
         
             
                    return queryResult.map((item) => item.data);
         
     | 
| 
       47 
47 
     | 
    
         
             
                }
         
     | 
| 
       48 
48 
     | 
    
         
             
                withSettings(additionalSettings) {
         
     | 
| 
       49 
     | 
    
         
            -
                    return new  
     | 
| 
      
 49 
     | 
    
         
            +
                    return new VectorIndexRetriever(Object.assign({}, this.settings, additionalSettings, {
         
     | 
| 
       50 
50 
     | 
    
         
             
                        vectorIndex: this.vectorIndex,
         
     | 
| 
       51 
51 
     | 
    
         
             
                        embeddingModel: this.embeddingModel,
         
     | 
| 
       52 
52 
     | 
    
         
             
                    }));
         
     | 
    
        package/vector-index/index.cjs
    CHANGED
    
    | 
         @@ -15,5 +15,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) { 
     | 
|
| 
       15 
15 
     | 
    
         
             
            };
         
     | 
| 
       16 
16 
     | 
    
         
             
            Object.defineProperty(exports, "__esModule", { value: true });
         
     | 
| 
       17 
17 
     | 
    
         
             
            __exportStar(require("./VectorIndex.cjs"), exports);
         
     | 
| 
      
 18 
     | 
    
         
            +
            __exportStar(require("./VectorIndexRetriever.cjs"), exports);
         
     | 
| 
       18 
19 
     | 
    
         
             
            __exportStar(require("./memory/MemoryVectorIndex.cjs"), exports);
         
     | 
| 
       19 
20 
     | 
    
         
             
            __exportStar(require("./pinecone/PineconeVectorIndex.cjs"), exports);
         
     | 
| 
      
 21 
     | 
    
         
            +
            __exportStar(require("./upsertIntoVectorIndex.cjs"), exports);
         
     | 
    
        package/vector-index/index.d.ts
    CHANGED
    
    
    
        package/vector-index/index.js
    CHANGED
    
    
| 
         @@ -0,0 +1,15 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            "use strict";
         
     | 
| 
      
 2 
     | 
    
         
            +
            Object.defineProperty(exports, "__esModule", { value: true });
         
     | 
| 
      
 3 
     | 
    
         
            +
            exports.upsertIntoVectorIndex = void 0;
         
     | 
| 
      
 4 
     | 
    
         
            +
            const nanoid_1 = require("nanoid");
         
     | 
| 
      
 5 
     | 
    
         
            +
            const embedText_js_1 = require("../model-function/embed-text/embedText.cjs");
         
     | 
| 
      
 6 
     | 
    
         
            +
            async function upsertIntoVectorIndex({ vectorIndex, embeddingModel, generateId = nanoid_1.nanoid, objects, getValueToEmbed, getId, }, options) {
         
     | 
| 
      
 7 
     | 
    
         
            +
                // many embedding models support bulk embedding, so we first embed all texts:
         
     | 
| 
      
 8 
     | 
    
         
            +
                const embeddings = await (0, embedText_js_1.embedTexts)(embeddingModel, objects.map(getValueToEmbed), options);
         
     | 
| 
      
 9 
     | 
    
         
            +
                await vectorIndex.upsertMany(objects.map((object, i) => ({
         
     | 
| 
      
 10 
     | 
    
         
            +
                    id: getId?.(object, i) ?? generateId(),
         
     | 
| 
      
 11 
     | 
    
         
            +
                    vector: embeddings[i],
         
     | 
| 
      
 12 
     | 
    
         
            +
                    data: object,
         
     | 
| 
      
 13 
     | 
    
         
            +
                })));
         
     | 
| 
      
 14 
     | 
    
         
            +
            }
         
     | 
| 
      
 15 
     | 
    
         
            +
            exports.upsertIntoVectorIndex = upsertIntoVectorIndex;
         
     | 
| 
         @@ -0,0 +1,11 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            import { ModelFunctionOptions } from "../model-function/ModelFunctionOptions.js";
         
     | 
| 
      
 2 
     | 
    
         
            +
            import { TextEmbeddingModel, TextEmbeddingModelSettings } from "../model-function/embed-text/TextEmbeddingModel.js";
         
     | 
| 
      
 3 
     | 
    
         
            +
            import { VectorIndex } from "./VectorIndex.js";
         
     | 
| 
      
 4 
     | 
    
         
            +
            export declare function upsertIntoVectorIndex<OBJECT, SETTINGS extends TextEmbeddingModelSettings>({ vectorIndex, embeddingModel, generateId, objects, getValueToEmbed, getId, }: {
         
     | 
| 
      
 5 
     | 
    
         
            +
                vectorIndex: VectorIndex<OBJECT, unknown>;
         
     | 
| 
      
 6 
     | 
    
         
            +
                embeddingModel: TextEmbeddingModel<unknown, SETTINGS>;
         
     | 
| 
      
 7 
     | 
    
         
            +
                generateId?: () => string;
         
     | 
| 
      
 8 
     | 
    
         
            +
                objects: OBJECT[];
         
     | 
| 
      
 9 
     | 
    
         
            +
                getValueToEmbed: (object: OBJECT, index: number) => string;
         
     | 
| 
      
 10 
     | 
    
         
            +
                getId?: (object: OBJECT, index: number) => string | undefined;
         
     | 
| 
      
 11 
     | 
    
         
            +
            }, options?: ModelFunctionOptions<SETTINGS>): Promise<void>;
         
     | 
| 
         @@ -0,0 +1,11 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            import { nanoid as createId } from "nanoid";
         
     | 
| 
      
 2 
     | 
    
         
            +
            import { embedTexts } from "../model-function/embed-text/embedText.js";
         
     | 
| 
      
 3 
     | 
    
         
            +
            export async function upsertIntoVectorIndex({ vectorIndex, embeddingModel, generateId = createId, objects, getValueToEmbed, getId, }, options) {
         
     | 
| 
      
 4 
     | 
    
         
            +
                // many embedding models support bulk embedding, so we first embed all texts:
         
     | 
| 
      
 5 
     | 
    
         
            +
                const embeddings = await embedTexts(embeddingModel, objects.map(getValueToEmbed), options);
         
     | 
| 
      
 6 
     | 
    
         
            +
                await vectorIndex.upsertMany(objects.map((object, i) => ({
         
     | 
| 
      
 7 
     | 
    
         
            +
                    id: getId?.(object, i) ?? generateId(),
         
     | 
| 
      
 8 
     | 
    
         
            +
                    vector: embeddings[i],
         
     | 
| 
      
 9 
     | 
    
         
            +
                    data: object,
         
     | 
| 
      
 10 
     | 
    
         
            +
                })));
         
     | 
| 
      
 11 
     | 
    
         
            +
            }
         
     | 
| 
         @@ -1,20 +0,0 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
            import { ModelFunctionOptions } from "../model-function/ModelFunctionOptions.js";
         
     | 
| 
       2 
     | 
    
         
            -
            import { TextEmbeddingModel, TextEmbeddingModelSettings } from "../model-function/embed-text/TextEmbeddingModel.js";
         
     | 
| 
       3 
     | 
    
         
            -
            import { TextChunk } from "./TextChunk.js";
         
     | 
| 
       4 
     | 
    
         
            -
            import { TextChunkRetriever, TextChunkRetrieverSettings } from "./retrieve-text-chunks/TextChunkRetriever.js";
         
     | 
| 
       5 
     | 
    
         
            -
            import { VectorIndex } from "../vector-index/VectorIndex.js";
         
     | 
| 
       6 
     | 
    
         
            -
            export interface SimilarTextChunksFromVectorIndexRetrieverSettings {
         
     | 
| 
       7 
     | 
    
         
            -
                maxResults?: number;
         
     | 
| 
       8 
     | 
    
         
            -
                similarityThreshold?: number;
         
     | 
| 
       9 
     | 
    
         
            -
            }
         
     | 
| 
       10 
     | 
    
         
            -
            export declare class SimilarTextChunksFromVectorIndexRetriever<CHUNK extends TextChunk, INDEX, SETTINGS extends TextEmbeddingModelSettings> implements TextChunkRetriever<CHUNK, string, SimilarTextChunksFromVectorIndexRetrieverSettings> {
         
     | 
| 
       11 
     | 
    
         
            -
                private readonly vectorIndex;
         
     | 
| 
       12 
     | 
    
         
            -
                private readonly embeddingModel;
         
     | 
| 
       13 
     | 
    
         
            -
                private readonly settings;
         
     | 
| 
       14 
     | 
    
         
            -
                constructor({ vectorIndex, embeddingModel, maxResults, similarityThreshold, }: {
         
     | 
| 
       15 
     | 
    
         
            -
                    vectorIndex: VectorIndex<CHUNK, INDEX>;
         
     | 
| 
       16 
     | 
    
         
            -
                    embeddingModel: TextEmbeddingModel<unknown, SETTINGS>;
         
     | 
| 
       17 
     | 
    
         
            -
                } & SimilarTextChunksFromVectorIndexRetrieverSettings);
         
     | 
| 
       18 
     | 
    
         
            -
                retrieveTextChunks(query: string, options?: ModelFunctionOptions<TextChunkRetrieverSettings>): Promise<CHUNK[]>;
         
     | 
| 
       19 
     | 
    
         
            -
                withSettings(additionalSettings: Partial<SimilarTextChunksFromVectorIndexRetrieverSettings>): this;
         
     | 
| 
       20 
     | 
    
         
            -
            }
         
     | 
| 
         @@ -1,8 +0,0 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
            import { ModelFunctionOptions } from "../../model-function/ModelFunctionOptions.js";
         
     | 
| 
       2 
     | 
    
         
            -
            import { TextChunk } from "../TextChunk.js";
         
     | 
| 
       3 
     | 
    
         
            -
            export interface TextChunkRetrieverSettings {
         
     | 
| 
       4 
     | 
    
         
            -
            }
         
     | 
| 
       5 
     | 
    
         
            -
            export interface TextChunkRetriever<CHUNK extends TextChunk, QUERY, SETTINGS extends TextChunkRetrieverSettings> {
         
     | 
| 
       6 
     | 
    
         
            -
                retrieveTextChunks(query: QUERY, options?: ModelFunctionOptions<TextChunkRetrieverSettings>): Promise<CHUNK[]>;
         
     | 
| 
       7 
     | 
    
         
            -
                withSettings(additionalSettings: Partial<SETTINGS>): this;
         
     | 
| 
       8 
     | 
    
         
            -
            }
         
     | 
| 
         @@ -1,10 +0,0 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
            "use strict";
         
     | 
| 
       2 
     | 
    
         
            -
            Object.defineProperty(exports, "__esModule", { value: true });
         
     | 
| 
       3 
     | 
    
         
            -
            exports.retrieveTextChunks = void 0;
         
     | 
| 
       4 
     | 
    
         
            -
            async function retrieveTextChunks(retriever, query, options) {
         
     | 
| 
       5 
     | 
    
         
            -
                // TODO add error handling, events, duration tracking, etc.
         
     | 
| 
       6 
     | 
    
         
            -
                return {
         
     | 
| 
       7 
     | 
    
         
            -
                    chunks: await retriever.retrieveTextChunks(query, options),
         
     | 
| 
       8 
     | 
    
         
            -
                };
         
     | 
| 
       9 
     | 
    
         
            -
            }
         
     | 
| 
       10 
     | 
    
         
            -
            exports.retrieveTextChunks = retrieveTextChunks;
         
     | 
| 
         @@ -1,6 +0,0 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
            import { ModelFunctionOptions } from "../../model-function/ModelFunctionOptions.js";
         
     | 
| 
       2 
     | 
    
         
            -
            import { TextChunk } from "../TextChunk.js";
         
     | 
| 
       3 
     | 
    
         
            -
            import { TextChunkRetriever, TextChunkRetrieverSettings } from "./TextChunkRetriever.js";
         
     | 
| 
       4 
     | 
    
         
            -
            export declare function retrieveTextChunks<CHUNK extends TextChunk, QUERY, SETTINGS extends TextChunkRetrieverSettings>(retriever: TextChunkRetriever<CHUNK, QUERY, SETTINGS>, query: QUERY, options?: ModelFunctionOptions<SETTINGS>): Promise<{
         
     | 
| 
       5 
     | 
    
         
            -
                chunks: CHUNK[];
         
     | 
| 
       6 
     | 
    
         
            -
            }>;
         
     | 
| 
         @@ -1,15 +0,0 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
            "use strict";
         
     | 
| 
       2 
     | 
    
         
            -
            Object.defineProperty(exports, "__esModule", { value: true });
         
     | 
| 
       3 
     | 
    
         
            -
            exports.upsertTextChunks = void 0;
         
     | 
| 
       4 
     | 
    
         
            -
            const nanoid_1 = require("nanoid");
         
     | 
| 
       5 
     | 
    
         
            -
            const embedText_js_1 = require("../model-function/embed-text/embedText.cjs");
         
     | 
| 
       6 
     | 
    
         
            -
            async function upsertTextChunks({ vectorIndex, embeddingModel, generateId = nanoid_1.nanoid, chunks, ids, }, options) {
         
     | 
| 
       7 
     | 
    
         
            -
                // many embedding models support bulk embedding, so we first embed all texts:
         
     | 
| 
       8 
     | 
    
         
            -
                const embeddings = await (0, embedText_js_1.embedTexts)(embeddingModel, chunks.map((chunk) => chunk.text), options);
         
     | 
| 
       9 
     | 
    
         
            -
                await vectorIndex.upsertMany(chunks.map((chunk, i) => ({
         
     | 
| 
       10 
     | 
    
         
            -
                    id: ids?.[i] ?? generateId(),
         
     | 
| 
       11 
     | 
    
         
            -
                    vector: embeddings[i],
         
     | 
| 
       12 
     | 
    
         
            -
                    data: chunk,
         
     | 
| 
       13 
     | 
    
         
            -
                })));
         
     | 
| 
       14 
     | 
    
         
            -
            }
         
     | 
| 
       15 
     | 
    
         
            -
            exports.upsertTextChunks = upsertTextChunks;
         
     | 
| 
         @@ -1,11 +0,0 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
            import { ModelFunctionOptions } from "../model-function/ModelFunctionOptions.js";
         
     | 
| 
       2 
     | 
    
         
            -
            import { TextEmbeddingModel, TextEmbeddingModelSettings } from "../model-function/embed-text/TextEmbeddingModel.js";
         
     | 
| 
       3 
     | 
    
         
            -
            import { TextChunk } from "./TextChunk.js";
         
     | 
| 
       4 
     | 
    
         
            -
            import { VectorIndex } from "../vector-index/VectorIndex.js";
         
     | 
| 
       5 
     | 
    
         
            -
            export declare function upsertTextChunks<CHUNK extends TextChunk, SETTINGS extends TextEmbeddingModelSettings>({ vectorIndex, embeddingModel, generateId, chunks, ids, }: {
         
     | 
| 
       6 
     | 
    
         
            -
                vectorIndex: VectorIndex<CHUNK, unknown>;
         
     | 
| 
       7 
     | 
    
         
            -
                embeddingModel: TextEmbeddingModel<unknown, SETTINGS>;
         
     | 
| 
       8 
     | 
    
         
            -
                generateId?: () => string;
         
     | 
| 
       9 
     | 
    
         
            -
                chunks: CHUNK[];
         
     | 
| 
       10 
     | 
    
         
            -
                ids?: Array<string | undefined>;
         
     | 
| 
       11 
     | 
    
         
            -
            }, options?: ModelFunctionOptions<SETTINGS>): Promise<void>;
         
     | 
| 
         @@ -1,11 +0,0 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
            import { nanoid as createId } from "nanoid";
         
     | 
| 
       2 
     | 
    
         
            -
            import { embedTexts } from "../model-function/embed-text/embedText.js";
         
     | 
| 
       3 
     | 
    
         
            -
            export async function upsertTextChunks({ vectorIndex, embeddingModel, generateId = createId, chunks, ids, }, options) {
         
     | 
| 
       4 
     | 
    
         
            -
                // many embedding models support bulk embedding, so we first embed all texts:
         
     | 
| 
       5 
     | 
    
         
            -
                const embeddings = await embedTexts(embeddingModel, chunks.map((chunk) => chunk.text), options);
         
     | 
| 
       6 
     | 
    
         
            -
                await vectorIndex.upsertMany(chunks.map((chunk, i) => ({
         
     | 
| 
       7 
     | 
    
         
            -
                    id: ids?.[i] ?? generateId(),
         
     | 
| 
       8 
     | 
    
         
            -
                    vector: embeddings[i],
         
     | 
| 
       9 
     | 
    
         
            -
                    data: chunk,
         
     | 
| 
       10 
     | 
    
         
            -
                })));
         
     | 
| 
       11 
     | 
    
         
            -
            }
         
     | 
| 
         
            File without changes
         
     | 
| 
         
            File without changes
         
     |