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,112 @@
|
|
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.createOpenAIChatFullDeltaIterableQueue = void 0;
|
7
|
+
const secure_json_parse_1 = __importDefault(require("secure-json-parse"));
|
8
|
+
const zod_1 = require("zod");
|
9
|
+
const AsyncQueue_js_1 = require("../../../model-function/generate-text/AsyncQueue.cjs");
|
10
|
+
const parseEventSourceReadableStream_js_1 = require("../../../model-function/generate-text/parseEventSourceReadableStream.cjs");
|
11
|
+
const chatResponseStreamEventSchema = zod_1.z.object({
|
12
|
+
choices: zod_1.z.array(zod_1.z.object({
|
13
|
+
delta: zod_1.z.object({
|
14
|
+
role: zod_1.z.enum(["assistant", "user"]).optional(),
|
15
|
+
content: zod_1.z.string().nullable().optional(),
|
16
|
+
function_call: zod_1.z
|
17
|
+
.object({
|
18
|
+
name: zod_1.z.string().optional(),
|
19
|
+
arguments: zod_1.z.string().optional(),
|
20
|
+
})
|
21
|
+
.optional(),
|
22
|
+
}),
|
23
|
+
finish_reason: zod_1.z.enum(["stop", "length"]).nullable(),
|
24
|
+
index: zod_1.z.number(),
|
25
|
+
})),
|
26
|
+
created: zod_1.z.number(),
|
27
|
+
id: zod_1.z.string(),
|
28
|
+
model: zod_1.z.string(),
|
29
|
+
object: zod_1.z.string(),
|
30
|
+
});
|
31
|
+
async function createOpenAIChatFullDeltaIterableQueue(stream) {
|
32
|
+
const queue = new AsyncQueue_js_1.AsyncQueue();
|
33
|
+
const streamDelta = [];
|
34
|
+
// process the stream asynchonously (no 'await' on purpose):
|
35
|
+
(0, parseEventSourceReadableStream_js_1.parseEventSourceReadableStream)({
|
36
|
+
stream,
|
37
|
+
callback: (event) => {
|
38
|
+
if (event.type !== "event") {
|
39
|
+
return;
|
40
|
+
}
|
41
|
+
const data = event.data;
|
42
|
+
if (data === "[DONE]") {
|
43
|
+
queue.close();
|
44
|
+
return;
|
45
|
+
}
|
46
|
+
try {
|
47
|
+
const json = secure_json_parse_1.default.parse(data);
|
48
|
+
const parseResult = chatResponseStreamEventSchema.safeParse(json);
|
49
|
+
if (!parseResult.success) {
|
50
|
+
queue.push({
|
51
|
+
type: "error",
|
52
|
+
error: parseResult.error,
|
53
|
+
});
|
54
|
+
queue.close();
|
55
|
+
return;
|
56
|
+
}
|
57
|
+
const event = parseResult.data;
|
58
|
+
for (let i = 0; i < event.choices.length; i++) {
|
59
|
+
const eventChoice = event.choices[i];
|
60
|
+
const delta = eventChoice.delta;
|
61
|
+
if (streamDelta[i] == null) {
|
62
|
+
streamDelta[i] = {
|
63
|
+
role: undefined,
|
64
|
+
content: "",
|
65
|
+
isComplete: false,
|
66
|
+
delta,
|
67
|
+
};
|
68
|
+
}
|
69
|
+
const choice = streamDelta[i];
|
70
|
+
choice.delta = delta;
|
71
|
+
if (eventChoice.finish_reason != null) {
|
72
|
+
choice.isComplete = true;
|
73
|
+
}
|
74
|
+
if (delta.content != undefined) {
|
75
|
+
choice.content += delta.content;
|
76
|
+
}
|
77
|
+
if (delta.function_call != undefined) {
|
78
|
+
if (choice.function_call == undefined) {
|
79
|
+
choice.function_call = {
|
80
|
+
name: "",
|
81
|
+
arguments: "",
|
82
|
+
};
|
83
|
+
}
|
84
|
+
if (delta.function_call.name != undefined) {
|
85
|
+
choice.function_call.name += delta.function_call.name;
|
86
|
+
}
|
87
|
+
if (delta.function_call.arguments != undefined) {
|
88
|
+
choice.function_call.arguments += delta.function_call.arguments;
|
89
|
+
}
|
90
|
+
}
|
91
|
+
if (delta.role != undefined) {
|
92
|
+
choice.role = delta.role;
|
93
|
+
}
|
94
|
+
}
|
95
|
+
// Since we're mutating the choices array in an async scenario,
|
96
|
+
// we need to make a deep copy:
|
97
|
+
const streamDeltaDeepCopy = JSON.parse(JSON.stringify(streamDelta));
|
98
|
+
queue.push({
|
99
|
+
type: "delta",
|
100
|
+
fullDelta: streamDeltaDeepCopy,
|
101
|
+
});
|
102
|
+
}
|
103
|
+
catch (error) {
|
104
|
+
queue.push({ type: "error", error });
|
105
|
+
queue.close();
|
106
|
+
return;
|
107
|
+
}
|
108
|
+
},
|
109
|
+
});
|
110
|
+
return queue;
|
111
|
+
}
|
112
|
+
exports.createOpenAIChatFullDeltaIterableQueue = createOpenAIChatFullDeltaIterableQueue;
|
@@ -0,0 +1,19 @@
|
|
1
|
+
import { DeltaEvent } from "../../../model-function/generate-text/DeltaEvent.js";
|
2
|
+
export type OpenAIChatDelta = Array<{
|
3
|
+
role: "assistant" | "user" | undefined;
|
4
|
+
content: string;
|
5
|
+
function_call?: {
|
6
|
+
name: string;
|
7
|
+
arguments: string;
|
8
|
+
};
|
9
|
+
isComplete: boolean;
|
10
|
+
delta: {
|
11
|
+
role?: "assistant" | "user";
|
12
|
+
content?: string | null;
|
13
|
+
function_call?: {
|
14
|
+
name?: string;
|
15
|
+
arguments?: string;
|
16
|
+
};
|
17
|
+
};
|
18
|
+
}>;
|
19
|
+
export declare function createOpenAIChatFullDeltaIterableQueue(stream: ReadableStream<Uint8Array>): Promise<AsyncIterable<DeltaEvent<OpenAIChatDelta>>>;
|
@@ -0,0 +1,105 @@
|
|
1
|
+
import SecureJSON from "secure-json-parse";
|
2
|
+
import { z } from "zod";
|
3
|
+
import { AsyncQueue } from "../../../model-function/generate-text/AsyncQueue.js";
|
4
|
+
import { parseEventSourceReadableStream } from "../../../model-function/generate-text/parseEventSourceReadableStream.js";
|
5
|
+
const chatResponseStreamEventSchema = z.object({
|
6
|
+
choices: z.array(z.object({
|
7
|
+
delta: z.object({
|
8
|
+
role: z.enum(["assistant", "user"]).optional(),
|
9
|
+
content: z.string().nullable().optional(),
|
10
|
+
function_call: z
|
11
|
+
.object({
|
12
|
+
name: z.string().optional(),
|
13
|
+
arguments: z.string().optional(),
|
14
|
+
})
|
15
|
+
.optional(),
|
16
|
+
}),
|
17
|
+
finish_reason: z.enum(["stop", "length"]).nullable(),
|
18
|
+
index: z.number(),
|
19
|
+
})),
|
20
|
+
created: z.number(),
|
21
|
+
id: z.string(),
|
22
|
+
model: z.string(),
|
23
|
+
object: z.string(),
|
24
|
+
});
|
25
|
+
export async function createOpenAIChatFullDeltaIterableQueue(stream) {
|
26
|
+
const queue = new AsyncQueue();
|
27
|
+
const streamDelta = [];
|
28
|
+
// process the stream asynchonously (no 'await' on purpose):
|
29
|
+
parseEventSourceReadableStream({
|
30
|
+
stream,
|
31
|
+
callback: (event) => {
|
32
|
+
if (event.type !== "event") {
|
33
|
+
return;
|
34
|
+
}
|
35
|
+
const data = event.data;
|
36
|
+
if (data === "[DONE]") {
|
37
|
+
queue.close();
|
38
|
+
return;
|
39
|
+
}
|
40
|
+
try {
|
41
|
+
const json = SecureJSON.parse(data);
|
42
|
+
const parseResult = chatResponseStreamEventSchema.safeParse(json);
|
43
|
+
if (!parseResult.success) {
|
44
|
+
queue.push({
|
45
|
+
type: "error",
|
46
|
+
error: parseResult.error,
|
47
|
+
});
|
48
|
+
queue.close();
|
49
|
+
return;
|
50
|
+
}
|
51
|
+
const event = parseResult.data;
|
52
|
+
for (let i = 0; i < event.choices.length; i++) {
|
53
|
+
const eventChoice = event.choices[i];
|
54
|
+
const delta = eventChoice.delta;
|
55
|
+
if (streamDelta[i] == null) {
|
56
|
+
streamDelta[i] = {
|
57
|
+
role: undefined,
|
58
|
+
content: "",
|
59
|
+
isComplete: false,
|
60
|
+
delta,
|
61
|
+
};
|
62
|
+
}
|
63
|
+
const choice = streamDelta[i];
|
64
|
+
choice.delta = delta;
|
65
|
+
if (eventChoice.finish_reason != null) {
|
66
|
+
choice.isComplete = true;
|
67
|
+
}
|
68
|
+
if (delta.content != undefined) {
|
69
|
+
choice.content += delta.content;
|
70
|
+
}
|
71
|
+
if (delta.function_call != undefined) {
|
72
|
+
if (choice.function_call == undefined) {
|
73
|
+
choice.function_call = {
|
74
|
+
name: "",
|
75
|
+
arguments: "",
|
76
|
+
};
|
77
|
+
}
|
78
|
+
if (delta.function_call.name != undefined) {
|
79
|
+
choice.function_call.name += delta.function_call.name;
|
80
|
+
}
|
81
|
+
if (delta.function_call.arguments != undefined) {
|
82
|
+
choice.function_call.arguments += delta.function_call.arguments;
|
83
|
+
}
|
84
|
+
}
|
85
|
+
if (delta.role != undefined) {
|
86
|
+
choice.role = delta.role;
|
87
|
+
}
|
88
|
+
}
|
89
|
+
// Since we're mutating the choices array in an async scenario,
|
90
|
+
// we need to make a deep copy:
|
91
|
+
const streamDeltaDeepCopy = JSON.parse(JSON.stringify(streamDelta));
|
92
|
+
queue.push({
|
93
|
+
type: "delta",
|
94
|
+
fullDelta: streamDeltaDeepCopy,
|
95
|
+
});
|
96
|
+
}
|
97
|
+
catch (error) {
|
98
|
+
queue.push({ type: "error", error });
|
99
|
+
queue.close();
|
100
|
+
return;
|
101
|
+
}
|
102
|
+
},
|
103
|
+
});
|
104
|
+
return queue;
|
105
|
+
}
|
@@ -0,0 +1,28 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.countOpenAIChatPromptTokens = exports.countOpenAIChatMessageTokens = exports.OPENAI_CHAT_MESSAGE_BASE_TOKEN_COUNT = exports.OPENAI_CHAT_PROMPT_BASE_TOKEN_COUNT = void 0;
|
4
|
+
const countTokens_js_1 = require("../../../model-function/tokenize-text/countTokens.cjs");
|
5
|
+
const TikTokenTokenizer_js_1 = require("../TikTokenTokenizer.cjs");
|
6
|
+
/**
|
7
|
+
* Prompt tokens that are included automatically for every full
|
8
|
+
* chat prompt (several messages) that is sent to OpenAI.
|
9
|
+
*/
|
10
|
+
exports.OPENAI_CHAT_PROMPT_BASE_TOKEN_COUNT = 2;
|
11
|
+
/**
|
12
|
+
* Prompt tokens that are included automatically for every
|
13
|
+
* message that is sent to OpenAI.
|
14
|
+
*/
|
15
|
+
exports.OPENAI_CHAT_MESSAGE_BASE_TOKEN_COUNT = 5;
|
16
|
+
async function countOpenAIChatMessageTokens({ message, model, }) {
|
17
|
+
return (exports.OPENAI_CHAT_MESSAGE_BASE_TOKEN_COUNT +
|
18
|
+
(await (0, countTokens_js_1.countTokens)(new TikTokenTokenizer_js_1.TikTokenTokenizer({ model }), message.content ?? "")));
|
19
|
+
}
|
20
|
+
exports.countOpenAIChatMessageTokens = countOpenAIChatMessageTokens;
|
21
|
+
async function countOpenAIChatPromptTokens({ messages, model, }) {
|
22
|
+
let tokens = exports.OPENAI_CHAT_PROMPT_BASE_TOKEN_COUNT;
|
23
|
+
for (const message of messages) {
|
24
|
+
tokens += await countOpenAIChatMessageTokens({ message, model });
|
25
|
+
}
|
26
|
+
return tokens;
|
27
|
+
}
|
28
|
+
exports.countOpenAIChatPromptTokens = countOpenAIChatPromptTokens;
|
@@ -0,0 +1,20 @@
|
|
1
|
+
import { OpenAIChatMessage } from "./OpenAIChatMessage.js";
|
2
|
+
import { OpenAIChatModelType } from "./OpenAIChatModel.js";
|
3
|
+
/**
|
4
|
+
* Prompt tokens that are included automatically for every full
|
5
|
+
* chat prompt (several messages) that is sent to OpenAI.
|
6
|
+
*/
|
7
|
+
export declare const OPENAI_CHAT_PROMPT_BASE_TOKEN_COUNT = 2;
|
8
|
+
/**
|
9
|
+
* Prompt tokens that are included automatically for every
|
10
|
+
* message that is sent to OpenAI.
|
11
|
+
*/
|
12
|
+
export declare const OPENAI_CHAT_MESSAGE_BASE_TOKEN_COUNT = 5;
|
13
|
+
export declare function countOpenAIChatMessageTokens({ message, model, }: {
|
14
|
+
message: OpenAIChatMessage;
|
15
|
+
model: OpenAIChatModelType;
|
16
|
+
}): Promise<number>;
|
17
|
+
export declare function countOpenAIChatPromptTokens({ messages, model, }: {
|
18
|
+
messages: OpenAIChatMessage[];
|
19
|
+
model: OpenAIChatModelType;
|
20
|
+
}): Promise<number>;
|
@@ -0,0 +1,23 @@
|
|
1
|
+
import { countTokens } from "../../../model-function/tokenize-text/countTokens.js";
|
2
|
+
import { TikTokenTokenizer } from "../TikTokenTokenizer.js";
|
3
|
+
/**
|
4
|
+
* Prompt tokens that are included automatically for every full
|
5
|
+
* chat prompt (several messages) that is sent to OpenAI.
|
6
|
+
*/
|
7
|
+
export const OPENAI_CHAT_PROMPT_BASE_TOKEN_COUNT = 2;
|
8
|
+
/**
|
9
|
+
* Prompt tokens that are included automatically for every
|
10
|
+
* message that is sent to OpenAI.
|
11
|
+
*/
|
12
|
+
export const OPENAI_CHAT_MESSAGE_BASE_TOKEN_COUNT = 5;
|
13
|
+
export async function countOpenAIChatMessageTokens({ message, model, }) {
|
14
|
+
return (OPENAI_CHAT_MESSAGE_BASE_TOKEN_COUNT +
|
15
|
+
(await countTokens(new TikTokenTokenizer({ model }), message.content ?? "")));
|
16
|
+
}
|
17
|
+
export async function countOpenAIChatPromptTokens({ messages, model, }) {
|
18
|
+
let tokens = OPENAI_CHAT_PROMPT_BASE_TOKEN_COUNT;
|
19
|
+
for (const message of messages) {
|
20
|
+
tokens += await countOpenAIChatMessageTokens({ message, model });
|
21
|
+
}
|
22
|
+
return tokens;
|
23
|
+
}
|
@@ -0,0 +1,31 @@
|
|
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.OpenAIChatFunctionPrompt = exports.OpenAIError = void 0;
|
18
|
+
__exportStar(require("./OpenAICostCalculator.cjs"), exports);
|
19
|
+
var OpenAIError_js_1 = require("./OpenAIError.cjs");
|
20
|
+
Object.defineProperty(exports, "OpenAIError", { enumerable: true, get: function () { return OpenAIError_js_1.OpenAIError; } });
|
21
|
+
__exportStar(require("./OpenAIImageGenerationModel.cjs"), exports);
|
22
|
+
__exportStar(require("./OpenAIModelSettings.cjs"), exports);
|
23
|
+
__exportStar(require("./OpenAITextEmbeddingModel.cjs"), exports);
|
24
|
+
__exportStar(require("./OpenAITextGenerationModel.cjs"), exports);
|
25
|
+
__exportStar(require("./OpenAITranscriptionModel.cjs"), exports);
|
26
|
+
__exportStar(require("./TikTokenTokenizer.cjs"), exports);
|
27
|
+
__exportStar(require("./chat/OpenAIChatMessage.cjs"), exports);
|
28
|
+
__exportStar(require("./chat/OpenAIChatModel.cjs"), exports);
|
29
|
+
var OpenAIChatPrompt_js_1 = require("./chat/OpenAIChatPrompt.cjs");
|
30
|
+
Object.defineProperty(exports, "OpenAIChatFunctionPrompt", { enumerable: true, get: function () { return OpenAIChatPrompt_js_1.OpenAIChatFunctionPrompt; } });
|
31
|
+
__exportStar(require("./chat/countOpenAIChatMessageTokens.cjs"), exports);
|
@@ -0,0 +1,13 @@
|
|
1
|
+
export * from "./OpenAICostCalculator.js";
|
2
|
+
export { OpenAIError, OpenAIErrorData } from "./OpenAIError.js";
|
3
|
+
export * from "./OpenAIImageGenerationModel.js";
|
4
|
+
export * from "./OpenAIModelSettings.js";
|
5
|
+
export * from "./OpenAITextEmbeddingModel.js";
|
6
|
+
export * from "./OpenAITextGenerationModel.js";
|
7
|
+
export * from "./OpenAITranscriptionModel.js";
|
8
|
+
export * from "./TikTokenTokenizer.js";
|
9
|
+
export * from "./chat/OpenAIChatMessage.js";
|
10
|
+
export * from "./chat/OpenAIChatModel.js";
|
11
|
+
export { OpenAIChatFunctionPrompt, OpenAIFunctionDescription, } from "./chat/OpenAIChatPrompt.js";
|
12
|
+
export { OpenAIChatDelta } from "./chat/OpenAIChatStreamIterable.js";
|
13
|
+
export * from "./chat/countOpenAIChatMessageTokens.js";
|
@@ -0,0 +1,12 @@
|
|
1
|
+
export * from "./OpenAICostCalculator.js";
|
2
|
+
export { OpenAIError } from "./OpenAIError.js";
|
3
|
+
export * from "./OpenAIImageGenerationModel.js";
|
4
|
+
export * from "./OpenAIModelSettings.js";
|
5
|
+
export * from "./OpenAITextEmbeddingModel.js";
|
6
|
+
export * from "./OpenAITextGenerationModel.js";
|
7
|
+
export * from "./OpenAITranscriptionModel.js";
|
8
|
+
export * from "./TikTokenTokenizer.js";
|
9
|
+
export * from "./chat/OpenAIChatMessage.js";
|
10
|
+
export * from "./chat/OpenAIChatModel.js";
|
11
|
+
export { OpenAIChatFunctionPrompt, } from "./chat/OpenAIChatPrompt.js";
|
12
|
+
export * from "./chat/countOpenAIChatMessageTokens.js";
|
@@ -0,0 +1,36 @@
|
|
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.failedStabilityCallResponseHandler = exports.StabilityError = exports.stabilityErrorDataSchema = 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.stabilityErrorDataSchema = zod_1.z.object({
|
11
|
+
message: zod_1.z.string(),
|
12
|
+
});
|
13
|
+
class StabilityError extends ApiCallError_js_1.ApiCallError {
|
14
|
+
constructor({ data, statusCode, url, requestBodyValues, message = data.message, }) {
|
15
|
+
super({ message, statusCode, requestBodyValues, url });
|
16
|
+
Object.defineProperty(this, "data", {
|
17
|
+
enumerable: true,
|
18
|
+
configurable: true,
|
19
|
+
writable: true,
|
20
|
+
value: void 0
|
21
|
+
});
|
22
|
+
this.data = data;
|
23
|
+
}
|
24
|
+
}
|
25
|
+
exports.StabilityError = StabilityError;
|
26
|
+
const failedStabilityCallResponseHandler = async ({ response, url, requestBodyValues }) => {
|
27
|
+
const responseBody = await response.text();
|
28
|
+
const parsedError = exports.stabilityErrorDataSchema.parse(secure_json_parse_1.default.parse(responseBody));
|
29
|
+
return new StabilityError({
|
30
|
+
url,
|
31
|
+
requestBodyValues,
|
32
|
+
statusCode: response.status,
|
33
|
+
data: parsedError,
|
34
|
+
});
|
35
|
+
};
|
36
|
+
exports.failedStabilityCallResponseHandler = failedStabilityCallResponseHandler;
|
@@ -0,0 +1,22 @@
|
|
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 stabilityErrorDataSchema: z.ZodObject<{
|
5
|
+
message: z.ZodString;
|
6
|
+
}, "strip", z.ZodTypeAny, {
|
7
|
+
message: string;
|
8
|
+
}, {
|
9
|
+
message: string;
|
10
|
+
}>;
|
11
|
+
export type StabilityErrorData = z.infer<typeof stabilityErrorDataSchema>;
|
12
|
+
export declare class StabilityError extends ApiCallError {
|
13
|
+
readonly data: StabilityErrorData;
|
14
|
+
constructor({ data, statusCode, url, requestBodyValues, message, }: {
|
15
|
+
message?: string;
|
16
|
+
statusCode: number;
|
17
|
+
url: string;
|
18
|
+
requestBodyValues: unknown;
|
19
|
+
data: StabilityErrorData;
|
20
|
+
});
|
21
|
+
}
|
22
|
+
export declare const failedStabilityCallResponseHandler: ResponseHandler<ApiCallError>;
|
@@ -0,0 +1,28 @@
|
|
1
|
+
import SecureJSON from "secure-json-parse";
|
2
|
+
import { z } from "zod";
|
3
|
+
import { ApiCallError } from "../../util/api/ApiCallError.js";
|
4
|
+
export const stabilityErrorDataSchema = z.object({
|
5
|
+
message: z.string(),
|
6
|
+
});
|
7
|
+
export class StabilityError extends ApiCallError {
|
8
|
+
constructor({ data, statusCode, url, requestBodyValues, message = data.message, }) {
|
9
|
+
super({ message, statusCode, requestBodyValues, url });
|
10
|
+
Object.defineProperty(this, "data", {
|
11
|
+
enumerable: true,
|
12
|
+
configurable: true,
|
13
|
+
writable: true,
|
14
|
+
value: void 0
|
15
|
+
});
|
16
|
+
this.data = data;
|
17
|
+
}
|
18
|
+
}
|
19
|
+
export const failedStabilityCallResponseHandler = async ({ response, url, requestBodyValues }) => {
|
20
|
+
const responseBody = await response.text();
|
21
|
+
const parsedError = stabilityErrorDataSchema.parse(SecureJSON.parse(responseBody));
|
22
|
+
return new StabilityError({
|
23
|
+
url,
|
24
|
+
requestBodyValues,
|
25
|
+
statusCode: response.status,
|
26
|
+
data: parsedError,
|
27
|
+
});
|
28
|
+
};
|
@@ -0,0 +1,133 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.StabilityImageGenerationModel = 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 StabilityError_js_1 = require("./StabilityError.cjs");
|
9
|
+
/**
|
10
|
+
* Create an image generation model that calls the Stability AI image generation API.
|
11
|
+
*
|
12
|
+
* @see https://api.stability.ai/docs#tag/v1generation/operation/textToImage
|
13
|
+
*
|
14
|
+
* @example
|
15
|
+
* const { image } = await generateImage(
|
16
|
+
* new StabilityImageGenerationModel({
|
17
|
+
* model: "stable-diffusion-512-v2-1",
|
18
|
+
* cfgScale: 7,
|
19
|
+
* clipGuidancePreset: "FAST_BLUE",
|
20
|
+
* height: 512,
|
21
|
+
* width: 512,
|
22
|
+
* samples: 1,
|
23
|
+
* steps: 30,
|
24
|
+
* })
|
25
|
+
* [
|
26
|
+
* { text: "the wicked witch of the west" },
|
27
|
+
* { text: "style of early 19th century painting", weight: 0.5 },
|
28
|
+
* ]
|
29
|
+
* );
|
30
|
+
*/
|
31
|
+
class StabilityImageGenerationModel extends AbstractModel_js_1.AbstractModel {
|
32
|
+
constructor(settings) {
|
33
|
+
super({ settings });
|
34
|
+
Object.defineProperty(this, "provider", {
|
35
|
+
enumerable: true,
|
36
|
+
configurable: true,
|
37
|
+
writable: true,
|
38
|
+
value: "stability"
|
39
|
+
});
|
40
|
+
}
|
41
|
+
get modelName() {
|
42
|
+
return this.settings.model;
|
43
|
+
}
|
44
|
+
get apiKey() {
|
45
|
+
const apiKey = this.settings.apiKey ?? process.env.STABILITY_API_KEY;
|
46
|
+
if (apiKey == null) {
|
47
|
+
throw new Error("No API key provided. Either pass an API key to the constructor or set the STABILITY_API_KEY environment variable.");
|
48
|
+
}
|
49
|
+
return apiKey;
|
50
|
+
}
|
51
|
+
async callAPI(input, options) {
|
52
|
+
const run = options?.run;
|
53
|
+
const settings = options?.settings;
|
54
|
+
const callSettings = Object.assign({
|
55
|
+
apiKey: this.apiKey,
|
56
|
+
}, this.settings, settings, {
|
57
|
+
abortSignal: run?.abortSignal,
|
58
|
+
engineId: this.settings.model,
|
59
|
+
textPrompts: input,
|
60
|
+
});
|
61
|
+
return (0, callWithRetryAndThrottle_js_1.callWithRetryAndThrottle)({
|
62
|
+
retry: this.settings.retry,
|
63
|
+
throttle: this.settings.throttle,
|
64
|
+
call: async () => callStabilityImageGenerationAPI(callSettings),
|
65
|
+
});
|
66
|
+
}
|
67
|
+
generateImageResponse(prompt, options) {
|
68
|
+
return this.callAPI(prompt, options);
|
69
|
+
}
|
70
|
+
extractBase64Image(response) {
|
71
|
+
return response.artifacts[0].base64;
|
72
|
+
}
|
73
|
+
withSettings(additionalSettings) {
|
74
|
+
return new StabilityImageGenerationModel(Object.assign({}, this.settings, additionalSettings));
|
75
|
+
}
|
76
|
+
}
|
77
|
+
exports.StabilityImageGenerationModel = StabilityImageGenerationModel;
|
78
|
+
const stabilityImageGenerationResponseSchema = zod_1.z.object({
|
79
|
+
artifacts: zod_1.z.array(zod_1.z.object({
|
80
|
+
base64: zod_1.z.string(),
|
81
|
+
seed: zod_1.z.number(),
|
82
|
+
finishReason: zod_1.z.enum(["SUCCESS", "ERROR", "CONTENT_FILTERED"]),
|
83
|
+
})),
|
84
|
+
});
|
85
|
+
/**
|
86
|
+
* Call the Stability AI API for image generation.
|
87
|
+
*
|
88
|
+
* @see https://api.stability.ai/docs#tag/v1generation/operation/textToImage
|
89
|
+
*
|
90
|
+
* @example
|
91
|
+
* const imageResponse = await callStabilityImageGenerationAPI({
|
92
|
+
* apiKey: STABILITY_API_KEY,
|
93
|
+
* engineId: "stable-diffusion-512-v2-1",
|
94
|
+
* textPrompts: [
|
95
|
+
* { text: "the wicked witch of the west" },
|
96
|
+
* { text: "style of early 19th century painting", weight: 0.5 },
|
97
|
+
* ],
|
98
|
+
* cfgScale: 7,
|
99
|
+
* clipGuidancePreset: "FAST_BLUE",
|
100
|
+
* height: 512,
|
101
|
+
* width: 512,
|
102
|
+
* samples: 1,
|
103
|
+
* steps: 30,
|
104
|
+
* });
|
105
|
+
*
|
106
|
+
* imageResponse.artifacts.forEach((image, index) => {
|
107
|
+
* fs.writeFileSync(
|
108
|
+
* `./stability-image-example-${index}.png`,
|
109
|
+
* Buffer.from(image.base64, "base64")
|
110
|
+
* );
|
111
|
+
* });
|
112
|
+
*/
|
113
|
+
async function callStabilityImageGenerationAPI({ baseUrl = "https://api.stability.ai/v1", abortSignal, apiKey, engineId, height, width, textPrompts, cfgScale, clipGuidancePreset, sampler, samples, seed, steps, stylePreset, }) {
|
114
|
+
return (0, postToApi_js_1.postJsonToApi)({
|
115
|
+
url: `${baseUrl}/generation/${engineId}/text-to-image`,
|
116
|
+
apiKey,
|
117
|
+
body: {
|
118
|
+
height,
|
119
|
+
width,
|
120
|
+
text_prompts: textPrompts,
|
121
|
+
cfg_scale: cfgScale,
|
122
|
+
clip_guidance_preset: clipGuidancePreset,
|
123
|
+
sampler,
|
124
|
+
samples,
|
125
|
+
seed,
|
126
|
+
steps,
|
127
|
+
style_preset: stylePreset,
|
128
|
+
},
|
129
|
+
failedResponseHandler: StabilityError_js_1.failedStabilityCallResponseHandler,
|
130
|
+
successfulResponseHandler: (0, postToApi_js_1.createJsonResponseHandler)(stabilityImageGenerationResponseSchema),
|
131
|
+
abortSignal,
|
132
|
+
});
|
133
|
+
}
|