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,276 @@
|
|
1
|
+
import SecureJSON from "secure-json-parse";
|
2
|
+
import { z } from "zod";
|
3
|
+
import { AbstractModel } from "../../model-function/AbstractModel.js";
|
4
|
+
import { AsyncQueue } from "../../model-function/generate-text/AsyncQueue.js";
|
5
|
+
import { countTokens } from "../../model-function/tokenize-text/countTokens.js";
|
6
|
+
import { PromptMappingTextGenerationModel } from "../../prompt/PromptMappingTextGenerationModel.js";
|
7
|
+
import { callWithRetryAndThrottle } from "../../util/api/callWithRetryAndThrottle.js";
|
8
|
+
import { createJsonResponseHandler, postJsonToApi, } from "../../util/api/postToApi.js";
|
9
|
+
import { failedCohereCallResponseHandler } from "./CohereError.js";
|
10
|
+
import { CohereTokenizer } from "./CohereTokenizer.js";
|
11
|
+
export const COHERE_TEXT_GENERATION_MODELS = {
|
12
|
+
command: {
|
13
|
+
contextWindowSize: 2048,
|
14
|
+
},
|
15
|
+
"command-nightly": {
|
16
|
+
contextWindowSize: 2048,
|
17
|
+
},
|
18
|
+
"command-light": {
|
19
|
+
contextWindowSize: 2048,
|
20
|
+
},
|
21
|
+
"command-light-nightly": {
|
22
|
+
contextWindowSize: 2048,
|
23
|
+
},
|
24
|
+
};
|
25
|
+
/**
|
26
|
+
* Create a text generation model that calls the Cohere Co.Generate API.
|
27
|
+
*
|
28
|
+
* @see https://docs.cohere.com/reference/generate
|
29
|
+
*
|
30
|
+
* @example
|
31
|
+
* const model = new CohereTextGenerationModel({
|
32
|
+
* model: "command-nightly",
|
33
|
+
* temperature: 0.7,
|
34
|
+
* maxTokens: 500,
|
35
|
+
* });
|
36
|
+
*
|
37
|
+
* const { text } = await generateText(
|
38
|
+
* model,
|
39
|
+
* "Write a short story about a robot learning to love:\n\n"
|
40
|
+
* );
|
41
|
+
*/
|
42
|
+
export class CohereTextGenerationModel extends AbstractModel {
|
43
|
+
constructor(settings) {
|
44
|
+
super({ settings });
|
45
|
+
Object.defineProperty(this, "provider", {
|
46
|
+
enumerable: true,
|
47
|
+
configurable: true,
|
48
|
+
writable: true,
|
49
|
+
value: "cohere"
|
50
|
+
});
|
51
|
+
Object.defineProperty(this, "contextWindowSize", {
|
52
|
+
enumerable: true,
|
53
|
+
configurable: true,
|
54
|
+
writable: true,
|
55
|
+
value: void 0
|
56
|
+
});
|
57
|
+
Object.defineProperty(this, "tokenizer", {
|
58
|
+
enumerable: true,
|
59
|
+
configurable: true,
|
60
|
+
writable: true,
|
61
|
+
value: void 0
|
62
|
+
});
|
63
|
+
this.contextWindowSize =
|
64
|
+
COHERE_TEXT_GENERATION_MODELS[this.settings.model].contextWindowSize;
|
65
|
+
this.tokenizer = new CohereTokenizer({
|
66
|
+
baseUrl: this.settings.baseUrl,
|
67
|
+
apiKey: this.settings.apiKey,
|
68
|
+
model: this.settings.model,
|
69
|
+
retry: this.settings.tokenizerSettings?.retry,
|
70
|
+
throttle: this.settings.tokenizerSettings?.throttle,
|
71
|
+
});
|
72
|
+
}
|
73
|
+
get modelName() {
|
74
|
+
return this.settings.model;
|
75
|
+
}
|
76
|
+
get apiKey() {
|
77
|
+
const apiKey = this.settings.apiKey ?? process.env.COHERE_API_KEY;
|
78
|
+
if (apiKey == null) {
|
79
|
+
throw new Error("No Cohere API key provided. Pass an API key to the constructor or set the COHERE_API_KEY environment variable.");
|
80
|
+
}
|
81
|
+
return apiKey;
|
82
|
+
}
|
83
|
+
async countPromptTokens(input) {
|
84
|
+
return countTokens(this.tokenizer, input);
|
85
|
+
}
|
86
|
+
async callAPI(prompt, options) {
|
87
|
+
const { run, settings, responseFormat } = options;
|
88
|
+
const callSettings = Object.assign({
|
89
|
+
apiKey: this.apiKey,
|
90
|
+
}, this.settings, settings, {
|
91
|
+
abortSignal: run?.abortSignal,
|
92
|
+
prompt,
|
93
|
+
responseFormat,
|
94
|
+
});
|
95
|
+
return callWithRetryAndThrottle({
|
96
|
+
retry: this.settings.retry,
|
97
|
+
throttle: this.settings.throttle,
|
98
|
+
call: async () => callCohereTextGenerationAPI(callSettings),
|
99
|
+
});
|
100
|
+
}
|
101
|
+
generateTextResponse(prompt, options) {
|
102
|
+
return this.callAPI(prompt, {
|
103
|
+
...options,
|
104
|
+
responseFormat: CohereTextGenerationResponseFormat.json,
|
105
|
+
});
|
106
|
+
}
|
107
|
+
extractText(response) {
|
108
|
+
return response.generations[0].text;
|
109
|
+
}
|
110
|
+
generateDeltaStreamResponse(prompt, options) {
|
111
|
+
return this.callAPI(prompt, {
|
112
|
+
...options,
|
113
|
+
responseFormat: CohereTextGenerationResponseFormat.deltaIterable,
|
114
|
+
});
|
115
|
+
}
|
116
|
+
extractTextDelta(fullDelta) {
|
117
|
+
return fullDelta.delta;
|
118
|
+
}
|
119
|
+
mapPrompt(promptMapping) {
|
120
|
+
return new PromptMappingTextGenerationModel({
|
121
|
+
model: this.withStopTokens(promptMapping.stopTokens),
|
122
|
+
promptMapping,
|
123
|
+
});
|
124
|
+
}
|
125
|
+
withSettings(additionalSettings) {
|
126
|
+
return new CohereTextGenerationModel(Object.assign({}, this.settings, additionalSettings));
|
127
|
+
}
|
128
|
+
get maxCompletionTokens() {
|
129
|
+
return this.settings.maxTokens;
|
130
|
+
}
|
131
|
+
withMaxCompletionTokens(maxCompletionTokens) {
|
132
|
+
return this.withSettings({ maxTokens: maxCompletionTokens });
|
133
|
+
}
|
134
|
+
withStopTokens(stopTokens) {
|
135
|
+
// use endSequences instead of stopSequences
|
136
|
+
// to exclude stop tokens from the generated text
|
137
|
+
return this.withSettings({ endSequences: stopTokens });
|
138
|
+
}
|
139
|
+
}
|
140
|
+
const cohereTextGenerationResponseSchema = z.object({
|
141
|
+
id: z.string(),
|
142
|
+
generations: z.array(z.object({
|
143
|
+
id: z.string(),
|
144
|
+
text: z.string(),
|
145
|
+
finish_reason: z.string().optional(),
|
146
|
+
})),
|
147
|
+
prompt: z.string(),
|
148
|
+
meta: z
|
149
|
+
.object({
|
150
|
+
api_version: z.object({
|
151
|
+
version: z.string(),
|
152
|
+
}),
|
153
|
+
})
|
154
|
+
.optional(),
|
155
|
+
});
|
156
|
+
/**
|
157
|
+
* Call the Cohere Co.Generate API to generate a text completion for the given prompt.
|
158
|
+
*
|
159
|
+
* @see https://docs.cohere.com/reference/generate
|
160
|
+
*
|
161
|
+
* @example
|
162
|
+
* const response = await callCohereTextGenerationAPI({
|
163
|
+
* apiKey: COHERE_API_KEY,
|
164
|
+
* model: "command-nightly",
|
165
|
+
* prompt: "Write a short story about a robot learning to love:\n\n",
|
166
|
+
* temperature: 0.7,
|
167
|
+
* maxTokens: 500,
|
168
|
+
* });
|
169
|
+
*
|
170
|
+
* console.log(response.generations[0].text);
|
171
|
+
*/
|
172
|
+
async function callCohereTextGenerationAPI({ baseUrl = "https://api.cohere.ai/v1", abortSignal, responseFormat, apiKey, model, prompt, numGenerations, maxTokens, temperature, k, p, frequencyPenalty, presencePenalty, endSequences, stopSequences, returnLikelihoods, logitBias, truncate, }) {
|
173
|
+
return postJsonToApi({
|
174
|
+
url: `${baseUrl}/generate`,
|
175
|
+
apiKey,
|
176
|
+
body: {
|
177
|
+
stream: responseFormat.stream,
|
178
|
+
model,
|
179
|
+
prompt,
|
180
|
+
num_generations: numGenerations,
|
181
|
+
max_tokens: maxTokens,
|
182
|
+
temperature,
|
183
|
+
k,
|
184
|
+
p,
|
185
|
+
frequency_penalty: frequencyPenalty,
|
186
|
+
presence_penalty: presencePenalty,
|
187
|
+
end_sequences: endSequences,
|
188
|
+
stop_sequences: stopSequences,
|
189
|
+
return_likelihoods: returnLikelihoods,
|
190
|
+
logit_bias: logitBias,
|
191
|
+
truncate,
|
192
|
+
},
|
193
|
+
failedResponseHandler: failedCohereCallResponseHandler,
|
194
|
+
successfulResponseHandler: responseFormat.handler,
|
195
|
+
abortSignal,
|
196
|
+
});
|
197
|
+
}
|
198
|
+
const cohereTextStreamingResponseSchema = z.discriminatedUnion("is_finished", [
|
199
|
+
z.object({
|
200
|
+
text: z.string(),
|
201
|
+
is_finished: z.literal(false),
|
202
|
+
}),
|
203
|
+
z.object({
|
204
|
+
is_finished: z.literal(true),
|
205
|
+
finish_reason: z.string(),
|
206
|
+
response: cohereTextGenerationResponseSchema,
|
207
|
+
}),
|
208
|
+
]);
|
209
|
+
async function createCohereTextGenerationFullDeltaIterableQueue(stream) {
|
210
|
+
const queue = new AsyncQueue();
|
211
|
+
let accumulatedText = "";
|
212
|
+
function processLine(line) {
|
213
|
+
const event = cohereTextStreamingResponseSchema.parse(SecureJSON.parse(line));
|
214
|
+
if (event.is_finished === true) {
|
215
|
+
queue.push({
|
216
|
+
type: "delta",
|
217
|
+
fullDelta: {
|
218
|
+
content: accumulatedText,
|
219
|
+
isComplete: true,
|
220
|
+
delta: "",
|
221
|
+
},
|
222
|
+
});
|
223
|
+
}
|
224
|
+
else {
|
225
|
+
accumulatedText += event.text;
|
226
|
+
queue.push({
|
227
|
+
type: "delta",
|
228
|
+
fullDelta: {
|
229
|
+
content: accumulatedText,
|
230
|
+
isComplete: false,
|
231
|
+
delta: event.text,
|
232
|
+
},
|
233
|
+
});
|
234
|
+
}
|
235
|
+
}
|
236
|
+
// process the stream asynchonously (no 'await' on purpose):
|
237
|
+
(async () => {
|
238
|
+
let unprocessedText = "";
|
239
|
+
const reader = new ReadableStreamDefaultReader(stream);
|
240
|
+
const utf8Decoder = new TextDecoder("utf-8");
|
241
|
+
// eslint-disable-next-line no-constant-condition
|
242
|
+
while (true) {
|
243
|
+
const { value: chunk, done } = await reader.read();
|
244
|
+
if (done) {
|
245
|
+
break;
|
246
|
+
}
|
247
|
+
unprocessedText += utf8Decoder.decode(chunk, { stream: true });
|
248
|
+
const processableLines = unprocessedText.split(/\r\n|\n|\r/g);
|
249
|
+
unprocessedText = processableLines.pop() || "";
|
250
|
+
processableLines.forEach(processLine);
|
251
|
+
}
|
252
|
+
// processing remaining text:
|
253
|
+
if (unprocessedText) {
|
254
|
+
processLine(unprocessedText);
|
255
|
+
}
|
256
|
+
queue.close();
|
257
|
+
})();
|
258
|
+
return queue;
|
259
|
+
}
|
260
|
+
export const CohereTextGenerationResponseFormat = {
|
261
|
+
/**
|
262
|
+
* Returns the response as a JSON object.
|
263
|
+
*/
|
264
|
+
json: {
|
265
|
+
stream: false,
|
266
|
+
handler: createJsonResponseHandler(cohereTextGenerationResponseSchema),
|
267
|
+
},
|
268
|
+
/**
|
269
|
+
* Returns an async iterable over the full deltas (all choices, including full current state at time of event)
|
270
|
+
* of the response stream.
|
271
|
+
*/
|
272
|
+
deltaIterable: {
|
273
|
+
stream: true,
|
274
|
+
handler: async ({ response }) => createCohereTextGenerationFullDeltaIterableQueue(response.body),
|
275
|
+
},
|
276
|
+
};
|
@@ -0,0 +1,136 @@
|
|
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.CohereTokenizer = void 0;
|
7
|
+
const zod_1 = __importDefault(require("zod"));
|
8
|
+
const callWithRetryAndThrottle_js_1 = require("../../util/api/callWithRetryAndThrottle.cjs");
|
9
|
+
const postToApi_js_1 = require("../../util/api/postToApi.cjs");
|
10
|
+
const CohereError_js_1 = require("./CohereError.cjs");
|
11
|
+
/**
|
12
|
+
* Tokenizer for the Cohere models. It uses the Co.Tokenize and Co.Detokenize APIs.
|
13
|
+
*
|
14
|
+
* @see https://docs.cohere.com/reference/tokenize
|
15
|
+
* @see https://docs.cohere.com/reference/detokenize-1
|
16
|
+
*
|
17
|
+
* @example
|
18
|
+
* const tokenizer = new CohereTokenizer({ model: "command-nightly" });
|
19
|
+
*
|
20
|
+
* const text = "At first, Nox didn't know what to do with the pup.";
|
21
|
+
*
|
22
|
+
* const tokenCount = await countTokens(tokenizer, text);
|
23
|
+
* const tokens = await tokenizer.tokenize(text);
|
24
|
+
* const tokensAndTokenTexts = await tokenizer.tokenizeWithTexts(text);
|
25
|
+
* const reconstructedText = await tokenizer.detokenize(tokens);
|
26
|
+
*/
|
27
|
+
class CohereTokenizer {
|
28
|
+
constructor(settings) {
|
29
|
+
Object.defineProperty(this, "settings", {
|
30
|
+
enumerable: true,
|
31
|
+
configurable: true,
|
32
|
+
writable: true,
|
33
|
+
value: void 0
|
34
|
+
});
|
35
|
+
this.settings = settings;
|
36
|
+
}
|
37
|
+
get apiKey() {
|
38
|
+
const apiKey = this.settings.apiKey ?? process.env.COHERE_API_KEY;
|
39
|
+
if (apiKey == null) {
|
40
|
+
throw new Error("No Cohere API key provided. Pass an API key to the constructor or set the COHERE_API_KEY environment variable.");
|
41
|
+
}
|
42
|
+
return apiKey;
|
43
|
+
}
|
44
|
+
async callTokenizeAPI(text, context) {
|
45
|
+
return (0, callWithRetryAndThrottle_js_1.callWithRetryAndThrottle)({
|
46
|
+
retry: this.settings.retry,
|
47
|
+
throttle: this.settings.throttle,
|
48
|
+
call: async () => callCohereTokenizeAPI({
|
49
|
+
abortSignal: context?.abortSignal,
|
50
|
+
apiKey: this.apiKey,
|
51
|
+
text,
|
52
|
+
...this.settings,
|
53
|
+
}),
|
54
|
+
});
|
55
|
+
}
|
56
|
+
async callDeTokenizeAPI(tokens, context) {
|
57
|
+
return (0, callWithRetryAndThrottle_js_1.callWithRetryAndThrottle)({
|
58
|
+
retry: this.settings.retry,
|
59
|
+
throttle: this.settings.throttle,
|
60
|
+
call: async () => callCohereDetokenizeAPI({
|
61
|
+
abortSignal: context?.abortSignal,
|
62
|
+
apiKey: this.apiKey,
|
63
|
+
tokens,
|
64
|
+
...this.settings,
|
65
|
+
}),
|
66
|
+
});
|
67
|
+
}
|
68
|
+
async tokenize(text) {
|
69
|
+
return (await this.tokenizeWithTexts(text)).tokens;
|
70
|
+
}
|
71
|
+
async tokenizeWithTexts(text) {
|
72
|
+
const response = await this.callTokenizeAPI(text);
|
73
|
+
return {
|
74
|
+
tokens: response.tokens,
|
75
|
+
tokenTexts: response.token_strings,
|
76
|
+
};
|
77
|
+
}
|
78
|
+
async detokenize(tokens) {
|
79
|
+
const response = await this.callDeTokenizeAPI(tokens);
|
80
|
+
return response.text;
|
81
|
+
}
|
82
|
+
}
|
83
|
+
exports.CohereTokenizer = CohereTokenizer;
|
84
|
+
const cohereDetokenizationResponseSchema = zod_1.default.object({
|
85
|
+
text: zod_1.default.string(),
|
86
|
+
meta: zod_1.default.object({
|
87
|
+
api_version: zod_1.default.object({
|
88
|
+
version: zod_1.default.string(),
|
89
|
+
}),
|
90
|
+
}),
|
91
|
+
});
|
92
|
+
/**
|
93
|
+
* Call the Cohere Co.Detokenize API to detokenize a text.
|
94
|
+
*
|
95
|
+
* https://docs.cohere.com/reference/detokenize-1
|
96
|
+
*/
|
97
|
+
async function callCohereDetokenizeAPI({ baseUrl = "https://api.cohere.ai/v1", abortSignal, apiKey, model, tokens, }) {
|
98
|
+
return (0, postToApi_js_1.postJsonToApi)({
|
99
|
+
url: `${baseUrl}/detokenize`,
|
100
|
+
apiKey,
|
101
|
+
body: {
|
102
|
+
model,
|
103
|
+
tokens,
|
104
|
+
},
|
105
|
+
failedResponseHandler: CohereError_js_1.failedCohereCallResponseHandler,
|
106
|
+
successfulResponseHandler: (0, postToApi_js_1.createJsonResponseHandler)(cohereDetokenizationResponseSchema),
|
107
|
+
abortSignal,
|
108
|
+
});
|
109
|
+
}
|
110
|
+
const cohereTokenizationResponseSchema = zod_1.default.object({
|
111
|
+
tokens: zod_1.default.array(zod_1.default.number()),
|
112
|
+
token_strings: zod_1.default.array(zod_1.default.string()),
|
113
|
+
meta: zod_1.default.object({
|
114
|
+
api_version: zod_1.default.object({
|
115
|
+
version: zod_1.default.string(),
|
116
|
+
}),
|
117
|
+
}),
|
118
|
+
});
|
119
|
+
/**
|
120
|
+
* Call the Cohere Co.Tokenize API to tokenize a text.
|
121
|
+
*
|
122
|
+
* https://docs.cohere.com/reference/tokenize
|
123
|
+
*/
|
124
|
+
async function callCohereTokenizeAPI({ baseUrl = "https://api.cohere.ai/v1", abortSignal, apiKey, model, text, }) {
|
125
|
+
return (0, postToApi_js_1.postJsonToApi)({
|
126
|
+
url: `${baseUrl}/tokenize`,
|
127
|
+
apiKey,
|
128
|
+
body: {
|
129
|
+
model,
|
130
|
+
text,
|
131
|
+
},
|
132
|
+
failedResponseHandler: CohereError_js_1.failedCohereCallResponseHandler,
|
133
|
+
successfulResponseHandler: (0, postToApi_js_1.createJsonResponseHandler)(cohereTokenizationResponseSchema),
|
134
|
+
abortSignal,
|
135
|
+
});
|
136
|
+
}
|
@@ -0,0 +1,118 @@
|
|
1
|
+
import z from "zod";
|
2
|
+
import { FullTokenizer } 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
|
+
import { CohereTextGenerationModelType } from "./CohereTextGenerationModel.js";
|
7
|
+
import { CohereTextEmbeddingModelType } from "./index.js";
|
8
|
+
export type CohereTokenizerModelType = CohereTextGenerationModelType | CohereTextEmbeddingModelType;
|
9
|
+
export interface CohereTokenizerSettings {
|
10
|
+
model: CohereTokenizerModelType;
|
11
|
+
baseUrl?: string;
|
12
|
+
apiKey?: string;
|
13
|
+
retry?: RetryFunction;
|
14
|
+
throttle?: ThrottleFunction;
|
15
|
+
}
|
16
|
+
/**
|
17
|
+
* Tokenizer for the Cohere models. It uses the Co.Tokenize and Co.Detokenize APIs.
|
18
|
+
*
|
19
|
+
* @see https://docs.cohere.com/reference/tokenize
|
20
|
+
* @see https://docs.cohere.com/reference/detokenize-1
|
21
|
+
*
|
22
|
+
* @example
|
23
|
+
* const tokenizer = new CohereTokenizer({ model: "command-nightly" });
|
24
|
+
*
|
25
|
+
* const text = "At first, Nox didn't know what to do with the pup.";
|
26
|
+
*
|
27
|
+
* const tokenCount = await countTokens(tokenizer, text);
|
28
|
+
* const tokens = await tokenizer.tokenize(text);
|
29
|
+
* const tokensAndTokenTexts = await tokenizer.tokenizeWithTexts(text);
|
30
|
+
* const reconstructedText = await tokenizer.detokenize(tokens);
|
31
|
+
*/
|
32
|
+
export declare class CohereTokenizer implements FullTokenizer {
|
33
|
+
readonly settings: CohereTokenizerSettings;
|
34
|
+
constructor(settings: CohereTokenizerSettings);
|
35
|
+
private get apiKey();
|
36
|
+
callTokenizeAPI(text: string, context?: Run): Promise<CohereTokenizationResponse>;
|
37
|
+
callDeTokenizeAPI(tokens: number[], context?: Run): Promise<CohereDetokenizationResponse>;
|
38
|
+
tokenize(text: string): Promise<number[]>;
|
39
|
+
tokenizeWithTexts(text: string): Promise<{
|
40
|
+
tokens: number[];
|
41
|
+
tokenTexts: string[];
|
42
|
+
}>;
|
43
|
+
detokenize(tokens: number[]): Promise<string>;
|
44
|
+
}
|
45
|
+
declare const cohereDetokenizationResponseSchema: z.ZodObject<{
|
46
|
+
text: z.ZodString;
|
47
|
+
meta: z.ZodObject<{
|
48
|
+
api_version: z.ZodObject<{
|
49
|
+
version: z.ZodString;
|
50
|
+
}, "strip", z.ZodTypeAny, {
|
51
|
+
version: string;
|
52
|
+
}, {
|
53
|
+
version: string;
|
54
|
+
}>;
|
55
|
+
}, "strip", z.ZodTypeAny, {
|
56
|
+
api_version: {
|
57
|
+
version: string;
|
58
|
+
};
|
59
|
+
}, {
|
60
|
+
api_version: {
|
61
|
+
version: string;
|
62
|
+
};
|
63
|
+
}>;
|
64
|
+
}, "strip", z.ZodTypeAny, {
|
65
|
+
text: string;
|
66
|
+
meta: {
|
67
|
+
api_version: {
|
68
|
+
version: string;
|
69
|
+
};
|
70
|
+
};
|
71
|
+
}, {
|
72
|
+
text: string;
|
73
|
+
meta: {
|
74
|
+
api_version: {
|
75
|
+
version: string;
|
76
|
+
};
|
77
|
+
};
|
78
|
+
}>;
|
79
|
+
export type CohereDetokenizationResponse = z.infer<typeof cohereDetokenizationResponseSchema>;
|
80
|
+
declare const cohereTokenizationResponseSchema: z.ZodObject<{
|
81
|
+
tokens: z.ZodArray<z.ZodNumber, "many">;
|
82
|
+
token_strings: z.ZodArray<z.ZodString, "many">;
|
83
|
+
meta: z.ZodObject<{
|
84
|
+
api_version: z.ZodObject<{
|
85
|
+
version: z.ZodString;
|
86
|
+
}, "strip", z.ZodTypeAny, {
|
87
|
+
version: string;
|
88
|
+
}, {
|
89
|
+
version: string;
|
90
|
+
}>;
|
91
|
+
}, "strip", z.ZodTypeAny, {
|
92
|
+
api_version: {
|
93
|
+
version: string;
|
94
|
+
};
|
95
|
+
}, {
|
96
|
+
api_version: {
|
97
|
+
version: string;
|
98
|
+
};
|
99
|
+
}>;
|
100
|
+
}, "strip", z.ZodTypeAny, {
|
101
|
+
meta: {
|
102
|
+
api_version: {
|
103
|
+
version: string;
|
104
|
+
};
|
105
|
+
};
|
106
|
+
tokens: number[];
|
107
|
+
token_strings: string[];
|
108
|
+
}, {
|
109
|
+
meta: {
|
110
|
+
api_version: {
|
111
|
+
version: string;
|
112
|
+
};
|
113
|
+
};
|
114
|
+
tokens: number[];
|
115
|
+
token_strings: string[];
|
116
|
+
}>;
|
117
|
+
export type CohereTokenizationResponse = z.infer<typeof cohereTokenizationResponseSchema>;
|
118
|
+
export {};
|
@@ -0,0 +1,129 @@
|
|
1
|
+
import z from "zod";
|
2
|
+
import { callWithRetryAndThrottle } from "../../util/api/callWithRetryAndThrottle.js";
|
3
|
+
import { createJsonResponseHandler, postJsonToApi, } from "../../util/api/postToApi.js";
|
4
|
+
import { failedCohereCallResponseHandler } from "./CohereError.js";
|
5
|
+
/**
|
6
|
+
* Tokenizer for the Cohere models. It uses the Co.Tokenize and Co.Detokenize APIs.
|
7
|
+
*
|
8
|
+
* @see https://docs.cohere.com/reference/tokenize
|
9
|
+
* @see https://docs.cohere.com/reference/detokenize-1
|
10
|
+
*
|
11
|
+
* @example
|
12
|
+
* const tokenizer = new CohereTokenizer({ model: "command-nightly" });
|
13
|
+
*
|
14
|
+
* const text = "At first, Nox didn't know what to do with the pup.";
|
15
|
+
*
|
16
|
+
* const tokenCount = await countTokens(tokenizer, text);
|
17
|
+
* const tokens = await tokenizer.tokenize(text);
|
18
|
+
* const tokensAndTokenTexts = await tokenizer.tokenizeWithTexts(text);
|
19
|
+
* const reconstructedText = await tokenizer.detokenize(tokens);
|
20
|
+
*/
|
21
|
+
export class CohereTokenizer {
|
22
|
+
constructor(settings) {
|
23
|
+
Object.defineProperty(this, "settings", {
|
24
|
+
enumerable: true,
|
25
|
+
configurable: true,
|
26
|
+
writable: true,
|
27
|
+
value: void 0
|
28
|
+
});
|
29
|
+
this.settings = settings;
|
30
|
+
}
|
31
|
+
get apiKey() {
|
32
|
+
const apiKey = this.settings.apiKey ?? process.env.COHERE_API_KEY;
|
33
|
+
if (apiKey == null) {
|
34
|
+
throw new Error("No Cohere API key provided. Pass an API key to the constructor or set the COHERE_API_KEY environment variable.");
|
35
|
+
}
|
36
|
+
return apiKey;
|
37
|
+
}
|
38
|
+
async callTokenizeAPI(text, context) {
|
39
|
+
return callWithRetryAndThrottle({
|
40
|
+
retry: this.settings.retry,
|
41
|
+
throttle: this.settings.throttle,
|
42
|
+
call: async () => callCohereTokenizeAPI({
|
43
|
+
abortSignal: context?.abortSignal,
|
44
|
+
apiKey: this.apiKey,
|
45
|
+
text,
|
46
|
+
...this.settings,
|
47
|
+
}),
|
48
|
+
});
|
49
|
+
}
|
50
|
+
async callDeTokenizeAPI(tokens, context) {
|
51
|
+
return callWithRetryAndThrottle({
|
52
|
+
retry: this.settings.retry,
|
53
|
+
throttle: this.settings.throttle,
|
54
|
+
call: async () => callCohereDetokenizeAPI({
|
55
|
+
abortSignal: context?.abortSignal,
|
56
|
+
apiKey: this.apiKey,
|
57
|
+
tokens,
|
58
|
+
...this.settings,
|
59
|
+
}),
|
60
|
+
});
|
61
|
+
}
|
62
|
+
async tokenize(text) {
|
63
|
+
return (await this.tokenizeWithTexts(text)).tokens;
|
64
|
+
}
|
65
|
+
async tokenizeWithTexts(text) {
|
66
|
+
const response = await this.callTokenizeAPI(text);
|
67
|
+
return {
|
68
|
+
tokens: response.tokens,
|
69
|
+
tokenTexts: response.token_strings,
|
70
|
+
};
|
71
|
+
}
|
72
|
+
async detokenize(tokens) {
|
73
|
+
const response = await this.callDeTokenizeAPI(tokens);
|
74
|
+
return response.text;
|
75
|
+
}
|
76
|
+
}
|
77
|
+
const cohereDetokenizationResponseSchema = z.object({
|
78
|
+
text: z.string(),
|
79
|
+
meta: z.object({
|
80
|
+
api_version: z.object({
|
81
|
+
version: z.string(),
|
82
|
+
}),
|
83
|
+
}),
|
84
|
+
});
|
85
|
+
/**
|
86
|
+
* Call the Cohere Co.Detokenize API to detokenize a text.
|
87
|
+
*
|
88
|
+
* https://docs.cohere.com/reference/detokenize-1
|
89
|
+
*/
|
90
|
+
async function callCohereDetokenizeAPI({ baseUrl = "https://api.cohere.ai/v1", abortSignal, apiKey, model, tokens, }) {
|
91
|
+
return postJsonToApi({
|
92
|
+
url: `${baseUrl}/detokenize`,
|
93
|
+
apiKey,
|
94
|
+
body: {
|
95
|
+
model,
|
96
|
+
tokens,
|
97
|
+
},
|
98
|
+
failedResponseHandler: failedCohereCallResponseHandler,
|
99
|
+
successfulResponseHandler: createJsonResponseHandler(cohereDetokenizationResponseSchema),
|
100
|
+
abortSignal,
|
101
|
+
});
|
102
|
+
}
|
103
|
+
const cohereTokenizationResponseSchema = z.object({
|
104
|
+
tokens: z.array(z.number()),
|
105
|
+
token_strings: z.array(z.string()),
|
106
|
+
meta: z.object({
|
107
|
+
api_version: z.object({
|
108
|
+
version: z.string(),
|
109
|
+
}),
|
110
|
+
}),
|
111
|
+
});
|
112
|
+
/**
|
113
|
+
* Call the Cohere Co.Tokenize API to tokenize a text.
|
114
|
+
*
|
115
|
+
* https://docs.cohere.com/reference/tokenize
|
116
|
+
*/
|
117
|
+
async function callCohereTokenizeAPI({ baseUrl = "https://api.cohere.ai/v1", abortSignal, apiKey, model, text, }) {
|
118
|
+
return postJsonToApi({
|
119
|
+
url: `${baseUrl}/tokenize`,
|
120
|
+
apiKey,
|
121
|
+
body: {
|
122
|
+
model,
|
123
|
+
text,
|
124
|
+
},
|
125
|
+
failedResponseHandler: failedCohereCallResponseHandler,
|
126
|
+
successfulResponseHandler: createJsonResponseHandler(cohereTokenizationResponseSchema),
|
127
|
+
abortSignal,
|
128
|
+
});
|
129
|
+
}
|
@@ -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.CohereError = void 0;
|
18
|
+
var CohereError_js_1 = require("./CohereError.cjs");
|
19
|
+
Object.defineProperty(exports, "CohereError", { enumerable: true, get: function () { return CohereError_js_1.CohereError; } });
|
20
|
+
__exportStar(require("./CohereTextEmbeddingModel.cjs"), exports);
|
21
|
+
__exportStar(require("./CohereTextGenerationModel.cjs"), exports);
|
22
|
+
__exportStar(require("./CohereTokenizer.cjs"), exports);
|