modelfusion 0.0.44
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/LICENSE +21 -0
- package/README.md +429 -0
- package/composed-function/index.cjs +22 -0
- package/composed-function/index.d.ts +6 -0
- package/composed-function/index.js +6 -0
- package/composed-function/summarize/SummarizationFunction.cjs +2 -0
- package/composed-function/summarize/SummarizationFunction.d.ts +4 -0
- package/composed-function/summarize/SummarizationFunction.js +1 -0
- package/composed-function/summarize/summarizeRecursively.cjs +19 -0
- package/composed-function/summarize/summarizeRecursively.d.ts +11 -0
- package/composed-function/summarize/summarizeRecursively.js +15 -0
- package/composed-function/summarize/summarizeRecursivelyWithTextGenerationAndTokenSplitting.cjs +29 -0
- package/composed-function/summarize/summarizeRecursivelyWithTextGenerationAndTokenSplitting.d.ts +24 -0
- package/composed-function/summarize/summarizeRecursivelyWithTextGenerationAndTokenSplitting.js +25 -0
- package/composed-function/use-tool/NoSuchToolError.cjs +17 -0
- package/composed-function/use-tool/NoSuchToolError.d.ts +4 -0
- package/composed-function/use-tool/NoSuchToolError.js +13 -0
- package/composed-function/use-tool/Tool.cjs +43 -0
- package/composed-function/use-tool/Tool.d.ts +15 -0
- package/composed-function/use-tool/Tool.js +39 -0
- package/composed-function/use-tool/useTool.cjs +59 -0
- package/composed-function/use-tool/useTool.d.ts +36 -0
- package/composed-function/use-tool/useTool.js +54 -0
- package/cost/Cost.cjs +38 -0
- package/cost/Cost.d.ts +16 -0
- package/cost/Cost.js +34 -0
- package/cost/CostCalculator.cjs +2 -0
- package/cost/CostCalculator.d.ts +8 -0
- package/cost/CostCalculator.js +1 -0
- package/cost/calculateCost.cjs +28 -0
- package/cost/calculateCost.d.ts +7 -0
- package/cost/calculateCost.js +24 -0
- package/cost/index.cjs +19 -0
- package/cost/index.d.ts +3 -0
- package/cost/index.js +3 -0
- package/index.cjs +25 -0
- package/index.d.ts +9 -0
- package/index.js +9 -0
- package/model-function/AbstractModel.cjs +22 -0
- package/model-function/AbstractModel.d.ts +12 -0
- package/model-function/AbstractModel.js +18 -0
- package/model-function/FunctionOptions.cjs +2 -0
- package/model-function/FunctionOptions.d.ts +6 -0
- package/model-function/FunctionOptions.js +1 -0
- package/model-function/Model.cjs +2 -0
- package/model-function/Model.d.ts +23 -0
- package/model-function/Model.js +1 -0
- package/model-function/ModelCallEvent.cjs +2 -0
- package/model-function/ModelCallEvent.d.ts +18 -0
- package/model-function/ModelCallEvent.js +1 -0
- package/model-function/ModelCallEventSource.cjs +42 -0
- package/model-function/ModelCallEventSource.d.ts +13 -0
- package/model-function/ModelCallEventSource.js +38 -0
- package/model-function/ModelCallObserver.cjs +2 -0
- package/model-function/ModelCallObserver.d.ts +5 -0
- package/model-function/ModelCallObserver.js +1 -0
- package/model-function/ModelInformation.cjs +2 -0
- package/model-function/ModelInformation.d.ts +4 -0
- package/model-function/ModelInformation.js +1 -0
- package/model-function/SuccessfulModelCall.cjs +22 -0
- package/model-function/SuccessfulModelCall.d.ts +9 -0
- package/model-function/SuccessfulModelCall.js +18 -0
- package/model-function/embed-text/TextEmbeddingEvent.cjs +2 -0
- package/model-function/embed-text/TextEmbeddingEvent.d.ts +23 -0
- package/model-function/embed-text/TextEmbeddingEvent.js +1 -0
- package/model-function/embed-text/TextEmbeddingModel.cjs +2 -0
- package/model-function/embed-text/TextEmbeddingModel.d.ts +18 -0
- package/model-function/embed-text/TextEmbeddingModel.js +1 -0
- package/model-function/embed-text/embedText.cjs +90 -0
- package/model-function/embed-text/embedText.d.ts +33 -0
- package/model-function/embed-text/embedText.js +85 -0
- package/model-function/executeCall.cjs +60 -0
- package/model-function/executeCall.d.ts +27 -0
- package/model-function/executeCall.js +56 -0
- package/model-function/generate-image/ImageGenerationEvent.cjs +2 -0
- package/model-function/generate-image/ImageGenerationEvent.d.ts +22 -0
- package/model-function/generate-image/ImageGenerationEvent.js +1 -0
- package/model-function/generate-image/ImageGenerationModel.cjs +2 -0
- package/model-function/generate-image/ImageGenerationModel.d.ts +8 -0
- package/model-function/generate-image/ImageGenerationModel.js +1 -0
- package/model-function/generate-image/generateImage.cjs +63 -0
- package/model-function/generate-image/generateImage.d.ts +23 -0
- package/model-function/generate-image/generateImage.js +59 -0
- package/model-function/generate-json/GenerateJsonModel.cjs +2 -0
- package/model-function/generate-json/GenerateJsonModel.d.ts +10 -0
- package/model-function/generate-json/GenerateJsonModel.js +1 -0
- package/model-function/generate-json/GenerateJsonOrTextModel.cjs +2 -0
- package/model-function/generate-json/GenerateJsonOrTextModel.d.ts +18 -0
- package/model-function/generate-json/GenerateJsonOrTextModel.js +1 -0
- package/model-function/generate-json/JsonGenerationEvent.cjs +2 -0
- package/model-function/generate-json/JsonGenerationEvent.d.ts +22 -0
- package/model-function/generate-json/JsonGenerationEvent.js +1 -0
- package/model-function/generate-json/NoSuchSchemaError.cjs +17 -0
- package/model-function/generate-json/NoSuchSchemaError.d.ts +4 -0
- package/model-function/generate-json/NoSuchSchemaError.js +13 -0
- package/model-function/generate-json/SchemaDefinition.cjs +2 -0
- package/model-function/generate-json/SchemaDefinition.d.ts +6 -0
- package/model-function/generate-json/SchemaDefinition.js +1 -0
- package/model-function/generate-json/SchemaValidationError.cjs +36 -0
- package/model-function/generate-json/SchemaValidationError.d.ts +11 -0
- package/model-function/generate-json/SchemaValidationError.js +32 -0
- package/model-function/generate-json/generateJson.cjs +61 -0
- package/model-function/generate-json/generateJson.d.ts +9 -0
- package/model-function/generate-json/generateJson.js +57 -0
- package/model-function/generate-json/generateJsonOrText.cjs +74 -0
- package/model-function/generate-json/generateJsonOrText.d.ts +25 -0
- package/model-function/generate-json/generateJsonOrText.js +70 -0
- package/model-function/generate-text/AsyncQueue.cjs +66 -0
- package/model-function/generate-text/AsyncQueue.d.ts +17 -0
- package/model-function/generate-text/AsyncQueue.js +62 -0
- package/model-function/generate-text/DeltaEvent.cjs +2 -0
- package/model-function/generate-text/DeltaEvent.d.ts +7 -0
- package/model-function/generate-text/DeltaEvent.js +1 -0
- package/model-function/generate-text/TextDeltaEventSource.cjs +54 -0
- package/model-function/generate-text/TextDeltaEventSource.d.ts +5 -0
- package/model-function/generate-text/TextDeltaEventSource.js +46 -0
- package/model-function/generate-text/TextGenerationEvent.cjs +2 -0
- package/model-function/generate-text/TextGenerationEvent.d.ts +22 -0
- package/model-function/generate-text/TextGenerationEvent.js +1 -0
- package/model-function/generate-text/TextGenerationModel.cjs +2 -0
- package/model-function/generate-text/TextGenerationModel.d.ts +42 -0
- package/model-function/generate-text/TextGenerationModel.js +1 -0
- package/model-function/generate-text/TextStreamingEvent.cjs +2 -0
- package/model-function/generate-text/TextStreamingEvent.d.ts +22 -0
- package/model-function/generate-text/TextStreamingEvent.js +1 -0
- package/model-function/generate-text/extractTextDeltas.cjs +23 -0
- package/model-function/generate-text/extractTextDeltas.d.ts +7 -0
- package/model-function/generate-text/extractTextDeltas.js +19 -0
- package/model-function/generate-text/generateText.cjs +67 -0
- package/model-function/generate-text/generateText.d.ts +20 -0
- package/model-function/generate-text/generateText.js +63 -0
- package/model-function/generate-text/parseEventSourceReadableStream.cjs +30 -0
- package/model-function/generate-text/parseEventSourceReadableStream.d.ts +8 -0
- package/model-function/generate-text/parseEventSourceReadableStream.js +26 -0
- package/model-function/generate-text/streamText.cjs +115 -0
- package/model-function/generate-text/streamText.d.ts +11 -0
- package/model-function/generate-text/streamText.js +111 -0
- package/model-function/index.cjs +47 -0
- package/model-function/index.d.ts +31 -0
- package/model-function/index.js +31 -0
- package/model-function/tokenize-text/Tokenizer.cjs +2 -0
- package/model-function/tokenize-text/Tokenizer.d.ts +19 -0
- package/model-function/tokenize-text/Tokenizer.js +1 -0
- package/model-function/tokenize-text/countTokens.cjs +10 -0
- package/model-function/tokenize-text/countTokens.d.ts +5 -0
- package/model-function/tokenize-text/countTokens.js +6 -0
- package/model-function/transcribe-audio/TranscriptionEvent.cjs +2 -0
- package/model-function/transcribe-audio/TranscriptionEvent.d.ts +22 -0
- package/model-function/transcribe-audio/TranscriptionEvent.js +1 -0
- package/model-function/transcribe-audio/TranscriptionModel.cjs +2 -0
- package/model-function/transcribe-audio/TranscriptionModel.d.ts +8 -0
- package/model-function/transcribe-audio/TranscriptionModel.js +1 -0
- package/model-function/transcribe-audio/transcribe.cjs +62 -0
- package/model-function/transcribe-audio/transcribe.d.ts +22 -0
- package/model-function/transcribe-audio/transcribe.js +58 -0
- package/model-provider/automatic1111/Automatic1111Error.cjs +39 -0
- package/model-provider/automatic1111/Automatic1111Error.d.ts +31 -0
- package/model-provider/automatic1111/Automatic1111Error.js +31 -0
- package/model-provider/automatic1111/Automatic1111ImageGenerationModel.cjs +76 -0
- package/model-provider/automatic1111/Automatic1111ImageGenerationModel.d.ts +54 -0
- package/model-provider/automatic1111/Automatic1111ImageGenerationModel.js +72 -0
- package/model-provider/automatic1111/index.cjs +20 -0
- package/model-provider/automatic1111/index.d.ts +2 -0
- package/model-provider/automatic1111/index.js +2 -0
- package/model-provider/cohere/CohereError.cjs +36 -0
- package/model-provider/cohere/CohereError.d.ts +22 -0
- package/model-provider/cohere/CohereError.js +28 -0
- package/model-provider/cohere/CohereTextEmbeddingModel.cjs +172 -0
- package/model-provider/cohere/CohereTextEmbeddingModel.d.ts +119 -0
- package/model-provider/cohere/CohereTextEmbeddingModel.js +165 -0
- package/model-provider/cohere/CohereTextGenerationModel.cjs +283 -0
- package/model-provider/cohere/CohereTextGenerationModel.d.ts +203 -0
- package/model-provider/cohere/CohereTextGenerationModel.js +276 -0
- package/model-provider/cohere/CohereTokenizer.cjs +136 -0
- package/model-provider/cohere/CohereTokenizer.d.ts +118 -0
- package/model-provider/cohere/CohereTokenizer.js +129 -0
- package/model-provider/cohere/index.cjs +22 -0
- package/model-provider/cohere/index.d.ts +4 -0
- package/model-provider/cohere/index.js +4 -0
- package/model-provider/huggingface/HuggingFaceError.cjs +52 -0
- package/model-provider/huggingface/HuggingFaceError.d.ts +22 -0
- package/model-provider/huggingface/HuggingFaceError.js +44 -0
- package/model-provider/huggingface/HuggingFaceTextGenerationModel.cjs +174 -0
- package/model-provider/huggingface/HuggingFaceTextGenerationModel.d.ts +75 -0
- package/model-provider/huggingface/HuggingFaceTextGenerationModel.js +167 -0
- package/model-provider/huggingface/index.cjs +20 -0
- package/model-provider/huggingface/index.d.ts +2 -0
- package/model-provider/huggingface/index.js +2 -0
- package/model-provider/index.cjs +22 -0
- package/model-provider/index.d.ts +6 -0
- package/model-provider/index.js +6 -0
- package/model-provider/llamacpp/LlamaCppError.cjs +52 -0
- package/model-provider/llamacpp/LlamaCppError.d.ts +22 -0
- package/model-provider/llamacpp/LlamaCppError.js +44 -0
- package/model-provider/llamacpp/LlamaCppTextEmbeddingModel.cjs +96 -0
- package/model-provider/llamacpp/LlamaCppTextEmbeddingModel.d.ts +40 -0
- package/model-provider/llamacpp/LlamaCppTextEmbeddingModel.js +89 -0
- package/model-provider/llamacpp/LlamaCppTextGenerationModel.cjs +245 -0
- package/model-provider/llamacpp/LlamaCppTextGenerationModel.d.ts +399 -0
- package/model-provider/llamacpp/LlamaCppTextGenerationModel.js +238 -0
- package/model-provider/llamacpp/LlamaCppTokenizer.cjs +64 -0
- package/model-provider/llamacpp/LlamaCppTokenizer.d.ts +38 -0
- package/model-provider/llamacpp/LlamaCppTokenizer.js +57 -0
- package/model-provider/llamacpp/index.cjs +22 -0
- package/model-provider/llamacpp/index.d.ts +4 -0
- package/model-provider/llamacpp/index.js +4 -0
- package/model-provider/openai/OpenAICostCalculator.cjs +71 -0
- package/model-provider/openai/OpenAICostCalculator.d.ts +6 -0
- package/model-provider/openai/OpenAICostCalculator.js +67 -0
- package/model-provider/openai/OpenAIError.cjs +50 -0
- package/model-provider/openai/OpenAIError.d.ts +47 -0
- package/model-provider/openai/OpenAIError.js +42 -0
- package/model-provider/openai/OpenAIImageGenerationModel.cjs +124 -0
- package/model-provider/openai/OpenAIImageGenerationModel.d.ts +113 -0
- package/model-provider/openai/OpenAIImageGenerationModel.js +119 -0
- package/model-provider/openai/OpenAIModelSettings.cjs +2 -0
- package/model-provider/openai/OpenAIModelSettings.d.ts +8 -0
- package/model-provider/openai/OpenAIModelSettings.js +1 -0
- package/model-provider/openai/OpenAITextEmbeddingModel.cjs +171 -0
- package/model-provider/openai/OpenAITextEmbeddingModel.d.ts +122 -0
- package/model-provider/openai/OpenAITextEmbeddingModel.js +162 -0
- package/model-provider/openai/OpenAITextGenerationModel.cjs +326 -0
- package/model-provider/openai/OpenAITextGenerationModel.d.ts +254 -0
- package/model-provider/openai/OpenAITextGenerationModel.js +317 -0
- package/model-provider/openai/OpenAITranscriptionModel.cjs +195 -0
- package/model-provider/openai/OpenAITranscriptionModel.d.ts +196 -0
- package/model-provider/openai/OpenAITranscriptionModel.js +187 -0
- package/model-provider/openai/TikTokenTokenizer.cjs +86 -0
- package/model-provider/openai/TikTokenTokenizer.d.ts +35 -0
- package/model-provider/openai/TikTokenTokenizer.js +82 -0
- package/model-provider/openai/chat/OpenAIChatMessage.cjs +24 -0
- package/model-provider/openai/chat/OpenAIChatMessage.d.ts +26 -0
- package/model-provider/openai/chat/OpenAIChatMessage.js +21 -0
- package/model-provider/openai/chat/OpenAIChatModel.cjs +288 -0
- package/model-provider/openai/chat/OpenAIChatModel.d.ts +344 -0
- package/model-provider/openai/chat/OpenAIChatModel.js +279 -0
- package/model-provider/openai/chat/OpenAIChatPrompt.cjs +143 -0
- package/model-provider/openai/chat/OpenAIChatPrompt.d.ts +108 -0
- package/model-provider/openai/chat/OpenAIChatPrompt.js +135 -0
- package/model-provider/openai/chat/OpenAIChatStreamIterable.cjs +112 -0
- package/model-provider/openai/chat/OpenAIChatStreamIterable.d.ts +19 -0
- package/model-provider/openai/chat/OpenAIChatStreamIterable.js +105 -0
- package/model-provider/openai/chat/countOpenAIChatMessageTokens.cjs +28 -0
- package/model-provider/openai/chat/countOpenAIChatMessageTokens.d.ts +20 -0
- package/model-provider/openai/chat/countOpenAIChatMessageTokens.js +23 -0
- package/model-provider/openai/index.cjs +31 -0
- package/model-provider/openai/index.d.ts +13 -0
- package/model-provider/openai/index.js +12 -0
- package/model-provider/stability/StabilityError.cjs +36 -0
- package/model-provider/stability/StabilityError.d.ts +22 -0
- package/model-provider/stability/StabilityError.js +28 -0
- package/model-provider/stability/StabilityImageGenerationModel.cjs +133 -0
- package/model-provider/stability/StabilityImageGenerationModel.d.ts +95 -0
- package/model-provider/stability/StabilityImageGenerationModel.js +129 -0
- package/model-provider/stability/index.cjs +20 -0
- package/model-provider/stability/index.d.ts +2 -0
- package/model-provider/stability/index.js +2 -0
- package/package.json +87 -0
- package/prompt/InstructionPrompt.cjs +2 -0
- package/prompt/InstructionPrompt.d.ts +7 -0
- package/prompt/InstructionPrompt.js +1 -0
- package/prompt/Llama2PromptMapping.cjs +56 -0
- package/prompt/Llama2PromptMapping.d.ts +10 -0
- package/prompt/Llama2PromptMapping.js +51 -0
- package/prompt/OpenAIChatPromptMapping.cjs +62 -0
- package/prompt/OpenAIChatPromptMapping.d.ts +6 -0
- package/prompt/OpenAIChatPromptMapping.js +57 -0
- package/prompt/PromptMapping.cjs +2 -0
- package/prompt/PromptMapping.d.ts +7 -0
- package/prompt/PromptMapping.js +1 -0
- package/prompt/PromptMappingTextGenerationModel.cjs +88 -0
- package/prompt/PromptMappingTextGenerationModel.d.ts +26 -0
- package/prompt/PromptMappingTextGenerationModel.js +84 -0
- package/prompt/TextPromptMapping.cjs +50 -0
- package/prompt/TextPromptMapping.d.ts +14 -0
- package/prompt/TextPromptMapping.js +45 -0
- package/prompt/chat/ChatPrompt.cjs +2 -0
- package/prompt/chat/ChatPrompt.d.ts +33 -0
- package/prompt/chat/ChatPrompt.js +1 -0
- package/prompt/chat/trimChatPrompt.cjs +50 -0
- package/prompt/chat/trimChatPrompt.d.ts +19 -0
- package/prompt/chat/trimChatPrompt.js +46 -0
- package/prompt/chat/validateChatPrompt.cjs +36 -0
- package/prompt/chat/validateChatPrompt.d.ts +8 -0
- package/prompt/chat/validateChatPrompt.js +31 -0
- package/prompt/index.cjs +25 -0
- package/prompt/index.d.ts +9 -0
- package/prompt/index.js +9 -0
- package/run/ConsoleLogger.cjs +12 -0
- package/run/ConsoleLogger.d.ts +6 -0
- package/run/ConsoleLogger.js +8 -0
- package/run/DefaultRun.cjs +78 -0
- package/run/DefaultRun.d.ts +24 -0
- package/run/DefaultRun.js +74 -0
- package/run/IdMetadata.cjs +2 -0
- package/run/IdMetadata.d.ts +7 -0
- package/run/IdMetadata.js +1 -0
- package/run/Run.cjs +2 -0
- package/run/Run.d.ts +27 -0
- package/run/Run.js +1 -0
- package/run/RunFunction.cjs +2 -0
- package/run/RunFunction.d.ts +13 -0
- package/run/RunFunction.js +1 -0
- package/run/Vector.cjs +2 -0
- package/run/Vector.d.ts +5 -0
- package/run/Vector.js +1 -0
- package/run/index.cjs +22 -0
- package/run/index.d.ts +6 -0
- package/run/index.js +6 -0
- package/text-chunk/TextChunk.cjs +2 -0
- package/text-chunk/TextChunk.d.ts +3 -0
- package/text-chunk/TextChunk.js +1 -0
- package/text-chunk/index.cjs +22 -0
- package/text-chunk/index.d.ts +6 -0
- package/text-chunk/index.js +6 -0
- package/text-chunk/retrieve-text-chunks/TextChunkRetriever.cjs +2 -0
- package/text-chunk/retrieve-text-chunks/TextChunkRetriever.d.ts +8 -0
- package/text-chunk/retrieve-text-chunks/TextChunkRetriever.js +1 -0
- package/text-chunk/retrieve-text-chunks/retrieveTextChunks.cjs +10 -0
- package/text-chunk/retrieve-text-chunks/retrieveTextChunks.d.ts +6 -0
- package/text-chunk/retrieve-text-chunks/retrieveTextChunks.js +6 -0
- package/text-chunk/split/SplitFunction.cjs +2 -0
- package/text-chunk/split/SplitFunction.d.ts +4 -0
- package/text-chunk/split/SplitFunction.js +1 -0
- package/text-chunk/split/splitOnSeparator.cjs +12 -0
- package/text-chunk/split/splitOnSeparator.d.ts +8 -0
- package/text-chunk/split/splitOnSeparator.js +7 -0
- package/text-chunk/split/splitRecursively.cjs +41 -0
- package/text-chunk/split/splitRecursively.d.ts +22 -0
- package/text-chunk/split/splitRecursively.js +33 -0
- package/util/DurationMeasurement.cjs +42 -0
- package/util/DurationMeasurement.d.ts +5 -0
- package/util/DurationMeasurement.js +38 -0
- package/util/ErrorHandler.cjs +2 -0
- package/util/ErrorHandler.d.ts +1 -0
- package/util/ErrorHandler.js +1 -0
- package/util/SafeResult.cjs +2 -0
- package/util/SafeResult.d.ts +8 -0
- package/util/SafeResult.js +1 -0
- package/util/api/AbortError.cjs +9 -0
- package/util/api/AbortError.d.ts +3 -0
- package/util/api/AbortError.js +5 -0
- package/util/api/ApiCallError.cjs +45 -0
- package/util/api/ApiCallError.d.ts +15 -0
- package/util/api/ApiCallError.js +41 -0
- package/util/api/RetryError.cjs +24 -0
- package/util/api/RetryError.d.ts +10 -0
- package/util/api/RetryError.js +20 -0
- package/util/api/RetryFunction.cjs +2 -0
- package/util/api/RetryFunction.d.ts +1 -0
- package/util/api/RetryFunction.js +1 -0
- package/util/api/ThrottleFunction.cjs +2 -0
- package/util/api/ThrottleFunction.d.ts +1 -0
- package/util/api/ThrottleFunction.js +1 -0
- package/util/api/callWithRetryAndThrottle.cjs +7 -0
- package/util/api/callWithRetryAndThrottle.d.ts +7 -0
- package/util/api/callWithRetryAndThrottle.js +3 -0
- package/util/api/postToApi.cjs +103 -0
- package/util/api/postToApi.d.ts +29 -0
- package/util/api/postToApi.js +96 -0
- package/util/api/retryNever.cjs +8 -0
- package/util/api/retryNever.d.ts +4 -0
- package/util/api/retryNever.js +4 -0
- package/util/api/retryWithExponentialBackoff.cjs +48 -0
- package/util/api/retryWithExponentialBackoff.d.ts +10 -0
- package/util/api/retryWithExponentialBackoff.js +44 -0
- package/util/api/throttleMaxConcurrency.cjs +65 -0
- package/util/api/throttleMaxConcurrency.d.ts +7 -0
- package/util/api/throttleMaxConcurrency.js +61 -0
- package/util/api/throttleUnlimitedConcurrency.cjs +8 -0
- package/util/api/throttleUnlimitedConcurrency.d.ts +5 -0
- package/util/api/throttleUnlimitedConcurrency.js +4 -0
- package/util/cosineSimilarity.cjs +26 -0
- package/util/cosineSimilarity.d.ts +11 -0
- package/util/cosineSimilarity.js +22 -0
- package/util/index.cjs +26 -0
- package/util/index.d.ts +10 -0
- package/util/index.js +10 -0
- package/util/never.cjs +6 -0
- package/util/never.d.ts +1 -0
- package/util/never.js +2 -0
- package/util/runSafe.cjs +15 -0
- package/util/runSafe.d.ts +2 -0
- package/util/runSafe.js +11 -0
- package/vector-index/VectorIndex.cjs +2 -0
- package/vector-index/VectorIndex.d.ts +18 -0
- package/vector-index/VectorIndex.js +1 -0
- package/vector-index/VectorIndexSimilarTextChunkRetriever.cjs +57 -0
- package/vector-index/VectorIndexSimilarTextChunkRetriever.d.ts +20 -0
- package/vector-index/VectorIndexSimilarTextChunkRetriever.js +53 -0
- package/vector-index/VectorIndexTextChunkStore.cjs +77 -0
- package/vector-index/VectorIndexTextChunkStore.d.ts +35 -0
- package/vector-index/VectorIndexTextChunkStore.js +73 -0
- package/vector-index/index.cjs +22 -0
- package/vector-index/index.d.ts +6 -0
- package/vector-index/index.js +6 -0
- package/vector-index/memory/MemoryVectorIndex.cjs +63 -0
- package/vector-index/memory/MemoryVectorIndex.d.ts +31 -0
- package/vector-index/memory/MemoryVectorIndex.js +56 -0
- package/vector-index/pinecone/PineconeVectorIndex.cjs +66 -0
- package/vector-index/pinecone/PineconeVectorIndex.d.ts +29 -0
- package/vector-index/pinecone/PineconeVectorIndex.js +62 -0
- package/vector-index/upsertTextChunks.cjs +15 -0
- package/vector-index/upsertTextChunks.d.ts +11 -0
- package/vector-index/upsertTextChunks.js +11 -0
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import z from "zod";
|
|
2
|
+
import { BasicTokenizer } from "../../model-function/tokenize-text/Tokenizer.js";
|
|
3
|
+
import { Run } from "../../run/Run.js";
|
|
4
|
+
import { RetryFunction } from "../../util/api/RetryFunction.js";
|
|
5
|
+
import { ThrottleFunction } from "../../util/api/ThrottleFunction.js";
|
|
6
|
+
export interface LlamaCppTokenizerSettings {
|
|
7
|
+
baseUrl?: string;
|
|
8
|
+
retry?: RetryFunction;
|
|
9
|
+
throttle?: ThrottleFunction;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Tokenizer for LlamaCpp.
|
|
13
|
+
|
|
14
|
+
* @example
|
|
15
|
+
* const tokenizer = new LlamaCppTokenizer();
|
|
16
|
+
*
|
|
17
|
+
* const text = "At first, Nox didn't know what to do with the pup.";
|
|
18
|
+
*
|
|
19
|
+
* const tokenCount = await countTokens(tokenizer, text);
|
|
20
|
+
* const tokens = await tokenizer.tokenize(text);
|
|
21
|
+
* const tokensAndTokenTexts = await tokenizer.tokenizeWithTexts(text);
|
|
22
|
+
* const reconstructedText = await tokenizer.detokenize(tokens);
|
|
23
|
+
*/
|
|
24
|
+
export declare class LlamaCppTokenizer implements BasicTokenizer {
|
|
25
|
+
readonly settings: LlamaCppTokenizerSettings;
|
|
26
|
+
constructor(settings?: LlamaCppTokenizerSettings);
|
|
27
|
+
callTokenizeAPI(text: string, context?: Run): Promise<LlamaCppTokenizationResponse>;
|
|
28
|
+
tokenize(text: string): Promise<number[]>;
|
|
29
|
+
}
|
|
30
|
+
declare const llamaCppTokenizationResponseSchema: z.ZodObject<{
|
|
31
|
+
tokens: z.ZodArray<z.ZodNumber, "many">;
|
|
32
|
+
}, "strip", z.ZodTypeAny, {
|
|
33
|
+
tokens: number[];
|
|
34
|
+
}, {
|
|
35
|
+
tokens: number[];
|
|
36
|
+
}>;
|
|
37
|
+
export type LlamaCppTokenizationResponse = z.infer<typeof llamaCppTokenizationResponseSchema>;
|
|
38
|
+
export {};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import z from "zod";
|
|
2
|
+
import { callWithRetryAndThrottle } from "../../util/api/callWithRetryAndThrottle.js";
|
|
3
|
+
import { createJsonResponseHandler, postJsonToApi, } from "../../util/api/postToApi.js";
|
|
4
|
+
import { failedLlamaCppCallResponseHandler } from "./LlamaCppError.js";
|
|
5
|
+
/**
|
|
6
|
+
* Tokenizer for LlamaCpp.
|
|
7
|
+
|
|
8
|
+
* @example
|
|
9
|
+
* const tokenizer = new LlamaCppTokenizer();
|
|
10
|
+
*
|
|
11
|
+
* const text = "At first, Nox didn't know what to do with the pup.";
|
|
12
|
+
*
|
|
13
|
+
* const tokenCount = await countTokens(tokenizer, text);
|
|
14
|
+
* const tokens = await tokenizer.tokenize(text);
|
|
15
|
+
* const tokensAndTokenTexts = await tokenizer.tokenizeWithTexts(text);
|
|
16
|
+
* const reconstructedText = await tokenizer.detokenize(tokens);
|
|
17
|
+
*/
|
|
18
|
+
export class LlamaCppTokenizer {
|
|
19
|
+
constructor(settings = {}) {
|
|
20
|
+
Object.defineProperty(this, "settings", {
|
|
21
|
+
enumerable: true,
|
|
22
|
+
configurable: true,
|
|
23
|
+
writable: true,
|
|
24
|
+
value: void 0
|
|
25
|
+
});
|
|
26
|
+
this.settings = settings;
|
|
27
|
+
}
|
|
28
|
+
async callTokenizeAPI(text, context) {
|
|
29
|
+
return callWithRetryAndThrottle({
|
|
30
|
+
retry: this.settings.retry,
|
|
31
|
+
throttle: this.settings.throttle,
|
|
32
|
+
call: async () => callLlamaCppTokenizeAPI({
|
|
33
|
+
abortSignal: context?.abortSignal,
|
|
34
|
+
text,
|
|
35
|
+
...this.settings,
|
|
36
|
+
}),
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
async tokenize(text) {
|
|
40
|
+
const response = await this.callTokenizeAPI(text);
|
|
41
|
+
return response.tokens;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
const llamaCppTokenizationResponseSchema = z.object({
|
|
45
|
+
tokens: z.array(z.number()),
|
|
46
|
+
});
|
|
47
|
+
async function callLlamaCppTokenizeAPI({ baseUrl = "http://127.0.0.1:8080", abortSignal, text, }) {
|
|
48
|
+
return postJsonToApi({
|
|
49
|
+
url: `${baseUrl}/tokenize`,
|
|
50
|
+
body: {
|
|
51
|
+
content: text,
|
|
52
|
+
},
|
|
53
|
+
failedResponseHandler: failedLlamaCppCallResponseHandler,
|
|
54
|
+
successfulResponseHandler: createJsonResponseHandler(llamaCppTokenizationResponseSchema),
|
|
55
|
+
abortSignal,
|
|
56
|
+
});
|
|
57
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
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
|
+
exports.LlamaCppError = void 0;
|
|
18
|
+
var LlamaCppError_js_1 = require("./LlamaCppError.cjs");
|
|
19
|
+
Object.defineProperty(exports, "LlamaCppError", { enumerable: true, get: function () { return LlamaCppError_js_1.LlamaCppError; } });
|
|
20
|
+
__exportStar(require("./LlamaCppTextEmbeddingModel.cjs"), exports);
|
|
21
|
+
__exportStar(require("./LlamaCppTextGenerationModel.cjs"), exports);
|
|
22
|
+
__exportStar(require("./LlamaCppTokenizer.cjs"), exports);
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OpenAICostCalculator = void 0;
|
|
4
|
+
const OpenAIImageGenerationModel_js_1 = require("./OpenAIImageGenerationModel.cjs");
|
|
5
|
+
const OpenAITextEmbeddingModel_js_1 = require("./OpenAITextEmbeddingModel.cjs");
|
|
6
|
+
const OpenAITextGenerationModel_js_1 = require("./OpenAITextGenerationModel.cjs");
|
|
7
|
+
const OpenAITranscriptionModel_js_1 = require("./OpenAITranscriptionModel.cjs");
|
|
8
|
+
const OpenAIChatModel_js_1 = require("./chat/OpenAIChatModel.cjs");
|
|
9
|
+
class OpenAICostCalculator {
|
|
10
|
+
constructor() {
|
|
11
|
+
Object.defineProperty(this, "provider", {
|
|
12
|
+
enumerable: true,
|
|
13
|
+
configurable: true,
|
|
14
|
+
writable: true,
|
|
15
|
+
value: "openai"
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
async calculateCostInMillicents(call) {
|
|
19
|
+
const type = call.type;
|
|
20
|
+
const model = call.model.modelName;
|
|
21
|
+
switch (type) {
|
|
22
|
+
case "image-generation": {
|
|
23
|
+
return (0, OpenAIImageGenerationModel_js_1.calculateOpenAIImageGenerationCostInMillicents)({
|
|
24
|
+
settings: call.settings,
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
case "text-embedding": {
|
|
28
|
+
if (model == null) {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
if ((0, OpenAITextEmbeddingModel_js_1.isOpenAIEmbeddingModel)(model)) {
|
|
32
|
+
return (0, OpenAITextEmbeddingModel_js_1.calculateOpenAIEmbeddingCostInMillicents)({
|
|
33
|
+
model,
|
|
34
|
+
responses: call.response,
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
break;
|
|
38
|
+
}
|
|
39
|
+
case "json-generation":
|
|
40
|
+
case "text-generation": {
|
|
41
|
+
if (model == null) {
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
if ((0, OpenAIChatModel_js_1.isOpenAIChatModel)(model)) {
|
|
45
|
+
return (0, OpenAIChatModel_js_1.calculateOpenAIChatCostInMillicents)({
|
|
46
|
+
model,
|
|
47
|
+
response: call.response,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
if ((0, OpenAITextGenerationModel_js_1.isOpenAITextGenerationModel)(model)) {
|
|
51
|
+
return (0, OpenAITextGenerationModel_js_1.calculateOpenAITextGenerationCostInMillicents)({
|
|
52
|
+
model,
|
|
53
|
+
response: call.response,
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
break;
|
|
57
|
+
}
|
|
58
|
+
case "transcription": {
|
|
59
|
+
if (model == null) {
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
return (0, OpenAITranscriptionModel_js_1.calculateOpenAITranscriptionCostInMillicents)({
|
|
63
|
+
model: model,
|
|
64
|
+
response: call.response,
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
exports.OpenAICostCalculator = OpenAICostCalculator;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { CostCalculator } from "../../cost/CostCalculator.js";
|
|
2
|
+
import { SuccessfulModelCall } from "../../model-function/SuccessfulModelCall.js";
|
|
3
|
+
export declare class OpenAICostCalculator implements CostCalculator {
|
|
4
|
+
readonly provider = "openai";
|
|
5
|
+
calculateCostInMillicents(call: SuccessfulModelCall): Promise<number | null>;
|
|
6
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { calculateOpenAIImageGenerationCostInMillicents, } from "./OpenAIImageGenerationModel.js";
|
|
2
|
+
import { calculateOpenAIEmbeddingCostInMillicents, isOpenAIEmbeddingModel, } from "./OpenAITextEmbeddingModel.js";
|
|
3
|
+
import { calculateOpenAITextGenerationCostInMillicents, isOpenAITextGenerationModel, } from "./OpenAITextGenerationModel.js";
|
|
4
|
+
import { calculateOpenAITranscriptionCostInMillicents, } from "./OpenAITranscriptionModel.js";
|
|
5
|
+
import { calculateOpenAIChatCostInMillicents, isOpenAIChatModel, } from "./chat/OpenAIChatModel.js";
|
|
6
|
+
export class OpenAICostCalculator {
|
|
7
|
+
constructor() {
|
|
8
|
+
Object.defineProperty(this, "provider", {
|
|
9
|
+
enumerable: true,
|
|
10
|
+
configurable: true,
|
|
11
|
+
writable: true,
|
|
12
|
+
value: "openai"
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
async calculateCostInMillicents(call) {
|
|
16
|
+
const type = call.type;
|
|
17
|
+
const model = call.model.modelName;
|
|
18
|
+
switch (type) {
|
|
19
|
+
case "image-generation": {
|
|
20
|
+
return calculateOpenAIImageGenerationCostInMillicents({
|
|
21
|
+
settings: call.settings,
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
case "text-embedding": {
|
|
25
|
+
if (model == null) {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
if (isOpenAIEmbeddingModel(model)) {
|
|
29
|
+
return calculateOpenAIEmbeddingCostInMillicents({
|
|
30
|
+
model,
|
|
31
|
+
responses: call.response,
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
break;
|
|
35
|
+
}
|
|
36
|
+
case "json-generation":
|
|
37
|
+
case "text-generation": {
|
|
38
|
+
if (model == null) {
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
if (isOpenAIChatModel(model)) {
|
|
42
|
+
return calculateOpenAIChatCostInMillicents({
|
|
43
|
+
model,
|
|
44
|
+
response: call.response,
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
if (isOpenAITextGenerationModel(model)) {
|
|
48
|
+
return calculateOpenAITextGenerationCostInMillicents({
|
|
49
|
+
model,
|
|
50
|
+
response: call.response,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
break;
|
|
54
|
+
}
|
|
55
|
+
case "transcription": {
|
|
56
|
+
if (model == null) {
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
return calculateOpenAITranscriptionCostInMillicents({
|
|
60
|
+
model: model,
|
|
61
|
+
response: call.response,
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.failedOpenAICallResponseHandler = exports.OpenAIError = exports.openAIErrorDataSchema = void 0;
|
|
7
|
+
const secure_json_parse_1 = __importDefault(require("secure-json-parse"));
|
|
8
|
+
const zod_1 = require("zod");
|
|
9
|
+
const ApiCallError_js_1 = require("../../util/api/ApiCallError.cjs");
|
|
10
|
+
exports.openAIErrorDataSchema = zod_1.z.object({
|
|
11
|
+
error: zod_1.z.object({
|
|
12
|
+
message: zod_1.z.string(),
|
|
13
|
+
type: zod_1.z.string(),
|
|
14
|
+
param: zod_1.z.any().nullable(),
|
|
15
|
+
code: zod_1.z.string().nullable(),
|
|
16
|
+
}),
|
|
17
|
+
});
|
|
18
|
+
class OpenAIError extends ApiCallError_js_1.ApiCallError {
|
|
19
|
+
constructor({ data, statusCode, url, requestBodyValues, message = data.error.message, }) {
|
|
20
|
+
super({
|
|
21
|
+
message,
|
|
22
|
+
statusCode,
|
|
23
|
+
requestBodyValues,
|
|
24
|
+
url,
|
|
25
|
+
isRetryable: (statusCode === 429 &&
|
|
26
|
+
// insufficient_quota is also reported as a 429, but it's not retryable:
|
|
27
|
+
data.error.type !== "insufficient_quota") ||
|
|
28
|
+
statusCode >= 500,
|
|
29
|
+
});
|
|
30
|
+
Object.defineProperty(this, "data", {
|
|
31
|
+
enumerable: true,
|
|
32
|
+
configurable: true,
|
|
33
|
+
writable: true,
|
|
34
|
+
value: void 0
|
|
35
|
+
});
|
|
36
|
+
this.data = data;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
exports.OpenAIError = OpenAIError;
|
|
40
|
+
const failedOpenAICallResponseHandler = async ({ response, url, requestBodyValues }) => {
|
|
41
|
+
const responseBody = await response.text();
|
|
42
|
+
const parsedError = exports.openAIErrorDataSchema.parse(secure_json_parse_1.default.parse(responseBody));
|
|
43
|
+
return new OpenAIError({
|
|
44
|
+
url,
|
|
45
|
+
requestBodyValues,
|
|
46
|
+
statusCode: response.status,
|
|
47
|
+
data: parsedError,
|
|
48
|
+
});
|
|
49
|
+
};
|
|
50
|
+
exports.failedOpenAICallResponseHandler = failedOpenAICallResponseHandler;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { ApiCallError } from "../../util/api/ApiCallError.js";
|
|
3
|
+
import { ResponseHandler } from "../../util/api/postToApi.js";
|
|
4
|
+
export declare const openAIErrorDataSchema: z.ZodObject<{
|
|
5
|
+
error: z.ZodObject<{
|
|
6
|
+
message: z.ZodString;
|
|
7
|
+
type: z.ZodString;
|
|
8
|
+
param: z.ZodNullable<z.ZodAny>;
|
|
9
|
+
code: z.ZodNullable<z.ZodString>;
|
|
10
|
+
}, "strip", z.ZodTypeAny, {
|
|
11
|
+
code: string | null;
|
|
12
|
+
message: string;
|
|
13
|
+
type: string;
|
|
14
|
+
param?: any;
|
|
15
|
+
}, {
|
|
16
|
+
code: string | null;
|
|
17
|
+
message: string;
|
|
18
|
+
type: string;
|
|
19
|
+
param?: any;
|
|
20
|
+
}>;
|
|
21
|
+
}, "strip", z.ZodTypeAny, {
|
|
22
|
+
error: {
|
|
23
|
+
code: string | null;
|
|
24
|
+
message: string;
|
|
25
|
+
type: string;
|
|
26
|
+
param?: any;
|
|
27
|
+
};
|
|
28
|
+
}, {
|
|
29
|
+
error: {
|
|
30
|
+
code: string | null;
|
|
31
|
+
message: string;
|
|
32
|
+
type: string;
|
|
33
|
+
param?: any;
|
|
34
|
+
};
|
|
35
|
+
}>;
|
|
36
|
+
export type OpenAIErrorData = z.infer<typeof openAIErrorDataSchema>;
|
|
37
|
+
export declare class OpenAIError extends ApiCallError {
|
|
38
|
+
readonly data: OpenAIErrorData;
|
|
39
|
+
constructor({ data, statusCode, url, requestBodyValues, message, }: {
|
|
40
|
+
message?: string;
|
|
41
|
+
statusCode: number;
|
|
42
|
+
url: string;
|
|
43
|
+
requestBodyValues: unknown;
|
|
44
|
+
data: OpenAIErrorData;
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
export declare const failedOpenAICallResponseHandler: ResponseHandler<ApiCallError>;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import SecureJSON from "secure-json-parse";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { ApiCallError } from "../../util/api/ApiCallError.js";
|
|
4
|
+
export const openAIErrorDataSchema = z.object({
|
|
5
|
+
error: z.object({
|
|
6
|
+
message: z.string(),
|
|
7
|
+
type: z.string(),
|
|
8
|
+
param: z.any().nullable(),
|
|
9
|
+
code: z.string().nullable(),
|
|
10
|
+
}),
|
|
11
|
+
});
|
|
12
|
+
export class OpenAIError extends ApiCallError {
|
|
13
|
+
constructor({ data, statusCode, url, requestBodyValues, message = data.error.message, }) {
|
|
14
|
+
super({
|
|
15
|
+
message,
|
|
16
|
+
statusCode,
|
|
17
|
+
requestBodyValues,
|
|
18
|
+
url,
|
|
19
|
+
isRetryable: (statusCode === 429 &&
|
|
20
|
+
// insufficient_quota is also reported as a 429, but it's not retryable:
|
|
21
|
+
data.error.type !== "insufficient_quota") ||
|
|
22
|
+
statusCode >= 500,
|
|
23
|
+
});
|
|
24
|
+
Object.defineProperty(this, "data", {
|
|
25
|
+
enumerable: true,
|
|
26
|
+
configurable: true,
|
|
27
|
+
writable: true,
|
|
28
|
+
value: void 0
|
|
29
|
+
});
|
|
30
|
+
this.data = data;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
export const failedOpenAICallResponseHandler = async ({ response, url, requestBodyValues }) => {
|
|
34
|
+
const responseBody = await response.text();
|
|
35
|
+
const parsedError = openAIErrorDataSchema.parse(SecureJSON.parse(responseBody));
|
|
36
|
+
return new OpenAIError({
|
|
37
|
+
url,
|
|
38
|
+
requestBodyValues,
|
|
39
|
+
statusCode: response.status,
|
|
40
|
+
data: parsedError,
|
|
41
|
+
});
|
|
42
|
+
};
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OpenAIImageGenerationResponseFormat = exports.OpenAIImageGenerationModel = exports.calculateOpenAIImageGenerationCostInMillicents = void 0;
|
|
4
|
+
const zod_1 = require("zod");
|
|
5
|
+
const AbstractModel_js_1 = require("../../model-function/AbstractModel.cjs");
|
|
6
|
+
const callWithRetryAndThrottle_js_1 = require("../../util/api/callWithRetryAndThrottle.cjs");
|
|
7
|
+
const postToApi_js_1 = require("../../util/api/postToApi.cjs");
|
|
8
|
+
const OpenAIError_js_1 = require("./OpenAIError.cjs");
|
|
9
|
+
/**
|
|
10
|
+
* @see https://openai.com/pricing
|
|
11
|
+
*/
|
|
12
|
+
const sizeToCostInMillicents = {
|
|
13
|
+
"1024x1024": 2000,
|
|
14
|
+
"512x512": 1800,
|
|
15
|
+
"256x256": 1600,
|
|
16
|
+
};
|
|
17
|
+
const calculateOpenAIImageGenerationCostInMillicents = ({ settings, }) => (settings.n ?? 1) * sizeToCostInMillicents[settings.size ?? "1024x1024"];
|
|
18
|
+
exports.calculateOpenAIImageGenerationCostInMillicents = calculateOpenAIImageGenerationCostInMillicents;
|
|
19
|
+
/**
|
|
20
|
+
* Create an image generation model that calls the OpenAI AI image creation API.
|
|
21
|
+
*
|
|
22
|
+
* @see https://platform.openai.com/docs/api-reference/images/create
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* const { image } = await generateImage(
|
|
26
|
+
* new OpenAIImageGenerationModel({ size: "512x512" }),
|
|
27
|
+
* "the wicked witch of the west in the style of early 19th century painting"
|
|
28
|
+
* );
|
|
29
|
+
*/
|
|
30
|
+
class OpenAIImageGenerationModel extends AbstractModel_js_1.AbstractModel {
|
|
31
|
+
constructor(settings) {
|
|
32
|
+
super({ settings });
|
|
33
|
+
Object.defineProperty(this, "provider", {
|
|
34
|
+
enumerable: true,
|
|
35
|
+
configurable: true,
|
|
36
|
+
writable: true,
|
|
37
|
+
value: "openai"
|
|
38
|
+
});
|
|
39
|
+
Object.defineProperty(this, "modelName", {
|
|
40
|
+
enumerable: true,
|
|
41
|
+
configurable: true,
|
|
42
|
+
writable: true,
|
|
43
|
+
value: null
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
get apiKey() {
|
|
47
|
+
const apiKey = this.settings.apiKey ?? process.env.OPENAI_API_KEY;
|
|
48
|
+
if (apiKey == null) {
|
|
49
|
+
throw new Error(`OpenAI API key is missing. Pass it as an argument to the constructor or set it as an environment variable named OPENAI_API_KEY.`);
|
|
50
|
+
}
|
|
51
|
+
return apiKey;
|
|
52
|
+
}
|
|
53
|
+
async callAPI(prompt, options) {
|
|
54
|
+
const run = options?.run;
|
|
55
|
+
const settings = options?.settings;
|
|
56
|
+
const responseFormat = options?.responseFormat;
|
|
57
|
+
const callSettings = Object.assign({
|
|
58
|
+
apiKey: this.apiKey,
|
|
59
|
+
user: this.settings.isUserIdForwardingEnabled ? run?.userId : undefined,
|
|
60
|
+
}, this.settings, settings, {
|
|
61
|
+
abortSignal: run?.abortSignal,
|
|
62
|
+
prompt,
|
|
63
|
+
responseFormat,
|
|
64
|
+
});
|
|
65
|
+
return (0, callWithRetryAndThrottle_js_1.callWithRetryAndThrottle)({
|
|
66
|
+
retry: callSettings.retry,
|
|
67
|
+
throttle: callSettings.throttle,
|
|
68
|
+
call: async () => callOpenAIImageGenerationAPI(callSettings),
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
generateImageResponse(prompt, options) {
|
|
72
|
+
return this.callAPI(prompt, {
|
|
73
|
+
responseFormat: exports.OpenAIImageGenerationResponseFormat.base64Json,
|
|
74
|
+
functionId: options?.functionId,
|
|
75
|
+
settings: options?.settings,
|
|
76
|
+
run: options?.run,
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
extractBase64Image(response) {
|
|
80
|
+
return response.data[0].b64_json;
|
|
81
|
+
}
|
|
82
|
+
withSettings(additionalSettings) {
|
|
83
|
+
return new OpenAIImageGenerationModel(Object.assign({}, this.settings, additionalSettings));
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
exports.OpenAIImageGenerationModel = OpenAIImageGenerationModel;
|
|
87
|
+
const openAIImageGenerationUrlSchema = zod_1.z.object({
|
|
88
|
+
created: zod_1.z.number(),
|
|
89
|
+
data: zod_1.z.array(zod_1.z.object({
|
|
90
|
+
url: zod_1.z.string(),
|
|
91
|
+
})),
|
|
92
|
+
});
|
|
93
|
+
const openAIImageGenerationBase64JsonSchema = zod_1.z.object({
|
|
94
|
+
created: zod_1.z.number(),
|
|
95
|
+
data: zod_1.z.array(zod_1.z.object({
|
|
96
|
+
b64_json: zod_1.z.string(),
|
|
97
|
+
})),
|
|
98
|
+
});
|
|
99
|
+
exports.OpenAIImageGenerationResponseFormat = {
|
|
100
|
+
url: {
|
|
101
|
+
type: "url",
|
|
102
|
+
handler: (0, postToApi_js_1.createJsonResponseHandler)(openAIImageGenerationUrlSchema),
|
|
103
|
+
},
|
|
104
|
+
base64Json: {
|
|
105
|
+
type: "b64_json",
|
|
106
|
+
handler: (0, postToApi_js_1.createJsonResponseHandler)(openAIImageGenerationBase64JsonSchema),
|
|
107
|
+
},
|
|
108
|
+
};
|
|
109
|
+
async function callOpenAIImageGenerationAPI({ baseUrl = "https://api.openai.com/v1", abortSignal, apiKey, prompt, n, size, responseFormat, user, }) {
|
|
110
|
+
return (0, postToApi_js_1.postJsonToApi)({
|
|
111
|
+
url: `${baseUrl}/images/generations`,
|
|
112
|
+
apiKey,
|
|
113
|
+
body: {
|
|
114
|
+
prompt,
|
|
115
|
+
n,
|
|
116
|
+
size,
|
|
117
|
+
response_format: responseFormat.type,
|
|
118
|
+
user,
|
|
119
|
+
},
|
|
120
|
+
failedResponseHandler: OpenAIError_js_1.failedOpenAICallResponseHandler,
|
|
121
|
+
successfulResponseHandler: responseFormat?.handler,
|
|
122
|
+
abortSignal,
|
|
123
|
+
});
|
|
124
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { AbstractModel } from "../../model-function/AbstractModel.js";
|
|
3
|
+
import { FunctionOptions } from "../../model-function/FunctionOptions.js";
|
|
4
|
+
import { ImageGenerationModel, ImageGenerationModelSettings } from "../../model-function/generate-image/ImageGenerationModel.js";
|
|
5
|
+
import { ResponseHandler } from "../../util/api/postToApi.js";
|
|
6
|
+
import { OpenAIModelSettings } from "./OpenAIModelSettings.js";
|
|
7
|
+
export interface OpenAIImageGenerationCallSettings {
|
|
8
|
+
n?: number;
|
|
9
|
+
size?: "256x256" | "512x512" | "1024x1024";
|
|
10
|
+
}
|
|
11
|
+
export declare const calculateOpenAIImageGenerationCostInMillicents: ({ settings, }: {
|
|
12
|
+
settings: OpenAIImageGenerationSettings;
|
|
13
|
+
}) => number;
|
|
14
|
+
export interface OpenAIImageGenerationSettings extends ImageGenerationModelSettings, OpenAIImageGenerationCallSettings, OpenAIModelSettings {
|
|
15
|
+
isUserIdForwardingEnabled?: boolean;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Create an image generation model that calls the OpenAI AI image creation API.
|
|
19
|
+
*
|
|
20
|
+
* @see https://platform.openai.com/docs/api-reference/images/create
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* const { image } = await generateImage(
|
|
24
|
+
* new OpenAIImageGenerationModel({ size: "512x512" }),
|
|
25
|
+
* "the wicked witch of the west in the style of early 19th century painting"
|
|
26
|
+
* );
|
|
27
|
+
*/
|
|
28
|
+
export declare class OpenAIImageGenerationModel extends AbstractModel<OpenAIImageGenerationSettings> implements ImageGenerationModel<string, OpenAIImageGenerationBase64JsonResponse, OpenAIImageGenerationSettings> {
|
|
29
|
+
constructor(settings: OpenAIImageGenerationSettings);
|
|
30
|
+
readonly provider: "openai";
|
|
31
|
+
readonly modelName: null;
|
|
32
|
+
private get apiKey();
|
|
33
|
+
callAPI<RESULT>(prompt: string, options: {
|
|
34
|
+
responseFormat: OpenAIImageGenerationResponseFormatType<RESULT>;
|
|
35
|
+
} & FunctionOptions<Partial<OpenAIImageGenerationCallSettings & OpenAIModelSettings & {
|
|
36
|
+
user?: string;
|
|
37
|
+
}>>): Promise<RESULT>;
|
|
38
|
+
generateImageResponse(prompt: string, options?: FunctionOptions<OpenAIImageGenerationSettings>): Promise<{
|
|
39
|
+
data: {
|
|
40
|
+
b64_json: string;
|
|
41
|
+
}[];
|
|
42
|
+
created: number;
|
|
43
|
+
}>;
|
|
44
|
+
extractBase64Image(response: OpenAIImageGenerationBase64JsonResponse): string;
|
|
45
|
+
withSettings(additionalSettings: Partial<OpenAIImageGenerationSettings>): this;
|
|
46
|
+
}
|
|
47
|
+
export type OpenAIImageGenerationResponseFormatType<T> = {
|
|
48
|
+
type: "b64_json" | "url";
|
|
49
|
+
handler: ResponseHandler<T>;
|
|
50
|
+
};
|
|
51
|
+
declare const openAIImageGenerationUrlSchema: z.ZodObject<{
|
|
52
|
+
created: z.ZodNumber;
|
|
53
|
+
data: z.ZodArray<z.ZodObject<{
|
|
54
|
+
url: z.ZodString;
|
|
55
|
+
}, "strip", z.ZodTypeAny, {
|
|
56
|
+
url: string;
|
|
57
|
+
}, {
|
|
58
|
+
url: string;
|
|
59
|
+
}>, "many">;
|
|
60
|
+
}, "strip", z.ZodTypeAny, {
|
|
61
|
+
data: {
|
|
62
|
+
url: string;
|
|
63
|
+
}[];
|
|
64
|
+
created: number;
|
|
65
|
+
}, {
|
|
66
|
+
data: {
|
|
67
|
+
url: string;
|
|
68
|
+
}[];
|
|
69
|
+
created: number;
|
|
70
|
+
}>;
|
|
71
|
+
export type OpenAIImageGenerationUrlResponse = z.infer<typeof openAIImageGenerationUrlSchema>;
|
|
72
|
+
declare const openAIImageGenerationBase64JsonSchema: z.ZodObject<{
|
|
73
|
+
created: z.ZodNumber;
|
|
74
|
+
data: z.ZodArray<z.ZodObject<{
|
|
75
|
+
b64_json: z.ZodString;
|
|
76
|
+
}, "strip", z.ZodTypeAny, {
|
|
77
|
+
b64_json: string;
|
|
78
|
+
}, {
|
|
79
|
+
b64_json: string;
|
|
80
|
+
}>, "many">;
|
|
81
|
+
}, "strip", z.ZodTypeAny, {
|
|
82
|
+
data: {
|
|
83
|
+
b64_json: string;
|
|
84
|
+
}[];
|
|
85
|
+
created: number;
|
|
86
|
+
}, {
|
|
87
|
+
data: {
|
|
88
|
+
b64_json: string;
|
|
89
|
+
}[];
|
|
90
|
+
created: number;
|
|
91
|
+
}>;
|
|
92
|
+
export type OpenAIImageGenerationBase64JsonResponse = z.infer<typeof openAIImageGenerationBase64JsonSchema>;
|
|
93
|
+
export declare const OpenAIImageGenerationResponseFormat: {
|
|
94
|
+
url: {
|
|
95
|
+
type: "url";
|
|
96
|
+
handler: ResponseHandler<{
|
|
97
|
+
data: {
|
|
98
|
+
url: string;
|
|
99
|
+
}[];
|
|
100
|
+
created: number;
|
|
101
|
+
}>;
|
|
102
|
+
};
|
|
103
|
+
base64Json: {
|
|
104
|
+
type: "b64_json";
|
|
105
|
+
handler: ResponseHandler<{
|
|
106
|
+
data: {
|
|
107
|
+
b64_json: string;
|
|
108
|
+
}[];
|
|
109
|
+
created: number;
|
|
110
|
+
}>;
|
|
111
|
+
};
|
|
112
|
+
};
|
|
113
|
+
export {};
|