@llumiverse/drivers 0.20.0 → 0.22.0-dev.1
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/lib/cjs/adobe/firefly.js +6 -5
- package/lib/cjs/adobe/firefly.js.map +1 -1
- package/lib/cjs/azure/azure_foundry.js +388 -0
- package/lib/cjs/azure/azure_foundry.js.map +1 -0
- package/lib/cjs/bedrock/index.js +62 -39
- package/lib/cjs/bedrock/index.js.map +1 -1
- package/lib/cjs/groq/index.js +178 -22
- package/lib/cjs/groq/index.js.map +1 -1
- package/lib/cjs/huggingface_ie.js +5 -4
- package/lib/cjs/huggingface_ie.js.map +1 -1
- package/lib/cjs/index.js +3 -2
- package/lib/cjs/index.js.map +1 -1
- package/lib/cjs/mistral/index.js +5 -3
- package/lib/cjs/mistral/index.js.map +1 -1
- package/lib/cjs/openai/azure_openai.js +72 -0
- package/lib/cjs/openai/azure_openai.js.map +1 -0
- package/lib/cjs/openai/index.js +13 -16
- package/lib/cjs/openai/index.js.map +1 -1
- package/lib/cjs/openai/openai.js +2 -2
- package/lib/cjs/openai/openai.js.map +1 -1
- package/lib/cjs/openai/openai_format.js +138 -0
- package/lib/cjs/openai/openai_format.js.map +1 -0
- package/lib/cjs/replicate.js +3 -3
- package/lib/cjs/replicate.js.map +1 -1
- package/lib/cjs/test/utils.js +1 -1
- package/lib/cjs/test/utils.js.map +1 -1
- package/lib/cjs/test-driver/TestErrorCompletionStream.js +20 -0
- package/lib/cjs/test-driver/TestErrorCompletionStream.js.map +1 -0
- package/lib/cjs/test-driver/TestValidationErrorCompletionStream.js +24 -0
- package/lib/cjs/test-driver/TestValidationErrorCompletionStream.js.map +1 -0
- package/lib/cjs/test-driver/index.js +109 -0
- package/lib/cjs/test-driver/index.js.map +1 -0
- package/lib/cjs/test-driver/utils.js +30 -0
- package/lib/cjs/test-driver/utils.js.map +1 -0
- package/lib/cjs/togetherai/index.js +2 -2
- package/lib/cjs/togetherai/index.js.map +1 -1
- package/lib/cjs/vertexai/embeddings/embeddings-text.js +1 -1
- package/lib/cjs/vertexai/embeddings/embeddings-text.js.map +1 -1
- package/lib/cjs/vertexai/index.js +90 -28
- package/lib/cjs/vertexai/index.js.map +1 -1
- package/lib/cjs/vertexai/models/claude.js +14 -12
- package/lib/cjs/vertexai/models/claude.js.map +1 -1
- package/lib/cjs/vertexai/models/gemini.js +158 -52
- package/lib/cjs/vertexai/models/gemini.js.map +1 -1
- package/lib/cjs/vertexai/models/imagen.js +11 -22
- package/lib/cjs/vertexai/models/imagen.js.map +1 -1
- package/lib/cjs/vertexai/models/llama.js +4 -3
- package/lib/cjs/vertexai/models/llama.js.map +1 -1
- package/lib/cjs/watsonx/index.js +3 -3
- package/lib/cjs/watsonx/index.js.map +1 -1
- package/lib/cjs/xai/index.js +4 -4
- package/lib/cjs/xai/index.js.map +1 -1
- package/lib/esm/adobe/firefly.js +6 -5
- package/lib/esm/adobe/firefly.js.map +1 -1
- package/lib/esm/azure/azure_foundry.js +382 -0
- package/lib/esm/azure/azure_foundry.js.map +1 -0
- package/lib/esm/bedrock/index.js +62 -39
- package/lib/esm/bedrock/index.js.map +1 -1
- package/lib/esm/groq/index.js +178 -22
- package/lib/esm/groq/index.js.map +1 -1
- package/lib/esm/huggingface_ie.js +6 -5
- package/lib/esm/huggingface_ie.js.map +1 -1
- package/lib/esm/index.js +3 -2
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/mistral/index.js +5 -3
- package/lib/esm/mistral/index.js.map +1 -1
- package/lib/esm/openai/azure_openai.js +68 -0
- package/lib/esm/openai/azure_openai.js.map +1 -0
- package/lib/esm/openai/index.js +12 -15
- package/lib/esm/openai/index.js.map +1 -1
- package/lib/esm/openai/openai.js +2 -2
- package/lib/esm/openai/openai.js.map +1 -1
- package/lib/esm/openai/openai_format.js +134 -0
- package/lib/esm/openai/openai_format.js.map +1 -0
- package/lib/esm/replicate.js +3 -3
- package/lib/esm/replicate.js.map +1 -1
- package/lib/esm/src/adobe/firefly.js +116 -0
- package/lib/esm/src/adobe/firefly.js.map +1 -0
- package/lib/esm/src/azure/azure_foundry.js +382 -0
- package/lib/esm/src/azure/azure_foundry.js.map +1 -0
- package/lib/esm/src/bedrock/converse.js +278 -0
- package/lib/esm/src/bedrock/converse.js.map +1 -0
- package/lib/esm/src/bedrock/index.js +819 -0
- package/lib/esm/src/bedrock/index.js.map +1 -0
- package/lib/esm/src/bedrock/nova-image-payload.js +203 -0
- package/lib/esm/src/bedrock/nova-image-payload.js.map +1 -0
- package/lib/esm/src/bedrock/payloads.js +2 -0
- package/lib/esm/src/bedrock/payloads.js.map +1 -0
- package/lib/esm/src/bedrock/s3.js +99 -0
- package/lib/esm/src/bedrock/s3.js.map +1 -0
- package/lib/esm/src/groq/index.js +286 -0
- package/lib/esm/src/groq/index.js.map +1 -0
- package/lib/esm/src/huggingface_ie.js +196 -0
- package/lib/esm/src/huggingface_ie.js.map +1 -0
- package/lib/esm/src/index.js +14 -0
- package/lib/esm/src/index.js.map +1 -0
- package/lib/esm/src/mistral/index.js +169 -0
- package/lib/esm/src/mistral/index.js.map +1 -0
- package/lib/esm/src/mistral/types.js +80 -0
- package/lib/esm/src/mistral/types.js.map +1 -0
- package/lib/esm/src/openai/azure_openai.js +68 -0
- package/lib/esm/src/openai/azure_openai.js.map +1 -0
- package/lib/esm/src/openai/index.js +460 -0
- package/lib/esm/src/openai/index.js.map +1 -0
- package/lib/esm/src/openai/openai.js +14 -0
- package/lib/esm/src/openai/openai.js.map +1 -0
- package/lib/esm/src/openai/openai_format.js +134 -0
- package/lib/esm/src/openai/openai_format.js.map +1 -0
- package/lib/esm/src/replicate.js +268 -0
- package/lib/esm/src/replicate.js.map +1 -0
- package/lib/esm/src/test/TestErrorCompletionStream.js +16 -0
- package/lib/esm/src/test/TestErrorCompletionStream.js.map +1 -0
- package/lib/esm/src/test/TestValidationErrorCompletionStream.js +20 -0
- package/lib/esm/src/test/TestValidationErrorCompletionStream.js.map +1 -0
- package/lib/esm/src/test/index.js +91 -0
- package/lib/esm/src/test/index.js.map +1 -0
- package/lib/esm/src/test/utils.js +25 -0
- package/lib/esm/src/test/utils.js.map +1 -0
- package/lib/esm/src/togetherai/index.js +122 -0
- package/lib/esm/src/togetherai/index.js.map +1 -0
- package/lib/esm/src/togetherai/interfaces.js +2 -0
- package/lib/esm/src/togetherai/interfaces.js.map +1 -0
- package/lib/esm/src/vertexai/debug.js +6 -0
- package/lib/esm/src/vertexai/debug.js.map +1 -0
- package/lib/esm/src/vertexai/embeddings/embeddings-image.js +24 -0
- package/lib/esm/src/vertexai/embeddings/embeddings-image.js.map +1 -0
- package/lib/esm/src/vertexai/embeddings/embeddings-text.js +20 -0
- package/lib/esm/src/vertexai/embeddings/embeddings-text.js.map +1 -0
- package/lib/esm/src/vertexai/index.js +332 -0
- package/lib/esm/src/vertexai/index.js.map +1 -0
- package/lib/esm/src/vertexai/models/claude.js +372 -0
- package/lib/esm/src/vertexai/models/claude.js.map +1 -0
- package/lib/esm/src/vertexai/models/gemini.js +806 -0
- package/lib/esm/src/vertexai/models/gemini.js.map +1 -0
- package/lib/esm/src/vertexai/models/imagen.js +302 -0
- package/lib/esm/src/vertexai/models/imagen.js.map +1 -0
- package/lib/esm/src/vertexai/models/llama.js +179 -0
- package/lib/esm/src/vertexai/models/llama.js.map +1 -0
- package/lib/esm/src/vertexai/models.js +21 -0
- package/lib/esm/src/vertexai/models.js.map +1 -0
- package/lib/esm/src/watsonx/index.js +157 -0
- package/lib/esm/src/watsonx/index.js.map +1 -0
- package/lib/esm/src/watsonx/interfaces.js +2 -0
- package/lib/esm/src/watsonx/interfaces.js.map +1 -0
- package/lib/esm/src/xai/index.js +64 -0
- package/lib/esm/src/xai/index.js.map +1 -0
- package/lib/esm/test/utils.js +1 -1
- package/lib/esm/test/utils.js.map +1 -1
- package/lib/esm/test-driver/TestErrorCompletionStream.js +16 -0
- package/lib/esm/test-driver/TestErrorCompletionStream.js.map +1 -0
- package/lib/esm/test-driver/TestValidationErrorCompletionStream.js +20 -0
- package/lib/esm/test-driver/TestValidationErrorCompletionStream.js.map +1 -0
- package/lib/esm/test-driver/index.js +91 -0
- package/lib/esm/test-driver/index.js.map +1 -0
- package/lib/esm/test-driver/utils.js +25 -0
- package/lib/esm/test-driver/utils.js.map +1 -0
- package/lib/esm/togetherai/index.js +2 -2
- package/lib/esm/togetherai/index.js.map +1 -1
- package/lib/esm/tsconfig.tsbuildinfo +1 -0
- package/lib/esm/vertexai/embeddings/embeddings-text.js +1 -1
- package/lib/esm/vertexai/embeddings/embeddings-text.js.map +1 -1
- package/lib/esm/vertexai/index.js +91 -29
- package/lib/esm/vertexai/index.js.map +1 -1
- package/lib/esm/vertexai/models/claude.js +14 -12
- package/lib/esm/vertexai/models/claude.js.map +1 -1
- package/lib/esm/vertexai/models/gemini.js +159 -53
- package/lib/esm/vertexai/models/gemini.js.map +1 -1
- package/lib/esm/vertexai/models/imagen.js +8 -16
- package/lib/esm/vertexai/models/imagen.js.map +1 -1
- package/lib/esm/vertexai/models/llama.js +4 -3
- package/lib/esm/vertexai/models/llama.js.map +1 -1
- package/lib/esm/vertexai/models.js.map +1 -1
- package/lib/esm/watsonx/index.js +3 -3
- package/lib/esm/watsonx/index.js.map +1 -1
- package/lib/esm/xai/index.js +3 -3
- package/lib/esm/xai/index.js.map +1 -1
- package/lib/types/adobe/firefly.d.ts +3 -3
- package/lib/types/adobe/firefly.d.ts.map +1 -1
- package/lib/types/azure/azure_foundry.d.ts +50 -0
- package/lib/types/azure/azure_foundry.d.ts.map +1 -0
- package/lib/types/bedrock/index.d.ts +4 -4
- package/lib/types/bedrock/index.d.ts.map +1 -1
- package/lib/types/groq/index.d.ts +8 -5
- package/lib/types/groq/index.d.ts.map +1 -1
- package/lib/types/huggingface_ie.d.ts +8 -5
- package/lib/types/huggingface_ie.d.ts.map +1 -1
- package/lib/types/index.d.ts +3 -2
- package/lib/types/index.d.ts.map +1 -1
- package/lib/types/mistral/index.d.ts +4 -4
- package/lib/types/mistral/index.d.ts.map +1 -1
- package/lib/types/openai/azure_openai.d.ts +25 -0
- package/lib/types/openai/azure_openai.d.ts.map +1 -0
- package/lib/types/openai/index.d.ts +6 -7
- package/lib/types/openai/index.d.ts.map +1 -1
- package/lib/types/openai/openai.d.ts +2 -2
- package/lib/types/openai/openai.d.ts.map +1 -1
- package/lib/types/openai/openai_format.d.ts +19 -0
- package/lib/types/openai/openai_format.d.ts.map +1 -0
- package/lib/types/replicate.d.ts +6 -3
- package/lib/types/replicate.d.ts.map +1 -1
- package/lib/types/src/adobe/firefly.d.ts +29 -0
- package/lib/types/src/azure/azure_foundry.d.ts +49 -0
- package/lib/types/src/bedrock/converse.d.ts +8 -0
- package/lib/types/src/bedrock/index.d.ts +57 -0
- package/lib/types/src/bedrock/nova-image-payload.d.ts +73 -0
- package/lib/types/src/bedrock/payloads.d.ts +11 -0
- package/lib/types/src/bedrock/s3.d.ts +22 -0
- package/lib/types/src/groq/index.d.ts +26 -0
- package/lib/types/src/huggingface_ie.d.ts +34 -0
- package/lib/types/src/index.d.ts +13 -0
- package/lib/types/src/mistral/index.d.ts +24 -0
- package/lib/types/src/mistral/types.d.ts +131 -0
- package/lib/types/src/openai/azure_openai.d.ts +24 -0
- package/lib/types/src/openai/index.d.ts +24 -0
- package/lib/types/src/openai/openai.d.ts +14 -0
- package/lib/types/src/openai/openai_format.d.ts +18 -0
- package/lib/types/src/replicate.d.ts +47 -0
- package/lib/types/src/test/TestErrorCompletionStream.d.ts +8 -0
- package/lib/types/src/test/TestValidationErrorCompletionStream.d.ts +8 -0
- package/lib/types/src/test/index.d.ts +23 -0
- package/lib/types/src/test/utils.d.ts +4 -0
- package/lib/types/src/togetherai/index.d.ts +22 -0
- package/lib/types/src/togetherai/interfaces.d.ts +95 -0
- package/lib/types/src/vertexai/debug.d.ts +1 -0
- package/lib/types/src/vertexai/embeddings/embeddings-image.d.ts +10 -0
- package/lib/types/src/vertexai/embeddings/embeddings-text.d.ts +9 -0
- package/lib/types/src/vertexai/index.d.ts +52 -0
- package/lib/types/src/vertexai/models/claude.d.ts +17 -0
- package/lib/types/src/vertexai/models/gemini.d.ts +17 -0
- package/lib/types/src/vertexai/models/imagen.d.ts +74 -0
- package/lib/types/src/vertexai/models/llama.d.ts +19 -0
- package/lib/types/src/vertexai/models.d.ts +14 -0
- package/lib/types/src/watsonx/index.d.ts +26 -0
- package/lib/types/src/watsonx/interfaces.d.ts +64 -0
- package/lib/types/src/xai/index.d.ts +18 -0
- package/lib/types/test-driver/TestErrorCompletionStream.d.ts +9 -0
- package/lib/types/test-driver/TestErrorCompletionStream.d.ts.map +1 -0
- package/lib/types/test-driver/TestValidationErrorCompletionStream.d.ts +9 -0
- package/lib/types/test-driver/TestValidationErrorCompletionStream.d.ts.map +1 -0
- package/lib/types/test-driver/index.d.ts +24 -0
- package/lib/types/test-driver/index.d.ts.map +1 -0
- package/lib/types/test-driver/utils.d.ts +5 -0
- package/lib/types/test-driver/utils.d.ts.map +1 -0
- package/lib/types/togetherai/index.d.ts +3 -3
- package/lib/types/togetherai/index.d.ts.map +1 -1
- package/lib/types/vertexai/index.d.ts +10 -7
- package/lib/types/vertexai/index.d.ts.map +1 -1
- package/lib/types/vertexai/models/claude.d.ts.map +1 -1
- package/lib/types/vertexai/models/gemini.d.ts +3 -2
- package/lib/types/vertexai/models/gemini.d.ts.map +1 -1
- package/lib/types/vertexai/models/imagen.d.ts +2 -2
- package/lib/types/vertexai/models/imagen.d.ts.map +1 -1
- package/lib/types/vertexai/models/llama.d.ts +2 -2
- package/lib/types/vertexai/models/llama.d.ts.map +1 -1
- package/lib/types/vertexai/models.d.ts +2 -2
- package/lib/types/vertexai/models.d.ts.map +1 -1
- package/lib/types/watsonx/index.d.ts +3 -3
- package/lib/types/watsonx/index.d.ts.map +1 -1
- package/lib/types/xai/index.d.ts.map +1 -1
- package/package.json +29 -25
- package/src/adobe/firefly.ts +12 -20
- package/src/azure/azure_foundry.ts +458 -0
- package/src/bedrock/index.ts +65 -43
- package/src/groq/index.ts +219 -32
- package/src/huggingface_ie.ts +13 -14
- package/src/index.ts +3 -2
- package/src/mistral/index.ts +11 -9
- package/src/openai/azure_openai.ts +92 -0
- package/src/openai/index.ts +27 -28
- package/src/openai/openai.ts +2 -5
- package/src/openai/openai_format.ts +165 -0
- package/src/replicate.ts +12 -12
- package/src/{test → test-driver}/utils.ts +1 -1
- package/src/togetherai/index.ts +7 -7
- package/src/vertexai/embeddings/embeddings-text.ts +2 -2
- package/src/vertexai/index.ts +104 -38
- package/src/vertexai/models/claude.ts +18 -16
- package/src/vertexai/models/gemini.ts +195 -67
- package/src/vertexai/models/imagen.ts +15 -26
- package/src/vertexai/models/llama.ts +6 -5
- package/src/vertexai/models.ts +4 -4
- package/src/watsonx/index.ts +10 -10
- package/src/xai/index.ts +13 -15
- package/lib/cjs/openai/azure.js +0 -31
- package/lib/cjs/openai/azure.js.map +0 -1
- package/lib/esm/openai/azure.js +0 -27
- package/lib/esm/openai/azure.js.map +0 -1
- package/lib/types/openai/azure.d.ts +0 -20
- package/lib/types/openai/azure.d.ts.map +0 -1
- package/src/openai/azure.ts +0 -54
- /package/src/{test → test-driver}/TestErrorCompletionStream.ts +0 -0
- /package/src/{test → test-driver}/TestValidationErrorCompletionStream.ts +0 -0
- /package/src/{test → test-driver}/index.ts +0 -0
package/src/bedrock/index.ts
CHANGED
|
@@ -7,9 +7,9 @@ import { S3Client } from "@aws-sdk/client-s3";
|
|
|
7
7
|
import { AwsCredentialIdentity, Provider } from "@aws-sdk/types";
|
|
8
8
|
import {
|
|
9
9
|
AbstractDriver, AIModel, Completion, CompletionChunkObject, DataSource, DriverOptions, EmbeddingsOptions, EmbeddingsResult,
|
|
10
|
-
ExecutionOptions, ExecutionTokenUsage,
|
|
10
|
+
ExecutionOptions, ExecutionTokenUsage, Modalities, PromptSegment,
|
|
11
11
|
TextFallbackOptions, ToolDefinition, ToolUse, TrainingJob, TrainingJobStatus, TrainingOptions,
|
|
12
|
-
BedrockClaudeOptions, BedrockPalmyraOptions, getMaxTokensLimitBedrock, NovaCanvasOptions,
|
|
12
|
+
BedrockClaudeOptions, BedrockPalmyraOptions, BedrockGptOssOptions, getMaxTokensLimitBedrock, NovaCanvasOptions,
|
|
13
13
|
modelModalitiesToArray, getModelCapabilities,
|
|
14
14
|
StatelessExecutionOptions,
|
|
15
15
|
ModelOptions
|
|
@@ -74,11 +74,13 @@ function maxTokenFallbackClaude(option: StatelessExecutionOptions): number {
|
|
|
74
74
|
if (modelOptions && typeof modelOptions.max_tokens === "number") {
|
|
75
75
|
return modelOptions.max_tokens;
|
|
76
76
|
} else {
|
|
77
|
+
const thinking_budget = modelOptions?.thinking_budget_tokens ?? 0;
|
|
78
|
+
let maxSupportedTokens = getMaxTokensLimitBedrock(option.model) ?? 8192; // Should always return a number for claude, 8192 is to satisfy the TypeScript type checker;
|
|
77
79
|
// Fallback to the default max tokens limit for the model
|
|
78
|
-
if (option.model.includes('claude-3-7-sonnet') && (modelOptions?.thinking_budget_tokens ?? 0) <
|
|
79
|
-
|
|
80
|
+
if (option.model.includes('claude-3-7-sonnet') && (modelOptions?.thinking_budget_tokens ?? 0) < 48000) {
|
|
81
|
+
maxSupportedTokens = 64000; // Claude 3.7 can go up to 128k with a beta header, but when no max tokens is specified, we default to 64k.
|
|
80
82
|
}
|
|
81
|
-
return
|
|
83
|
+
return Math.min(16000 + thinking_budget, maxSupportedTokens); // Cap to 16k, to avoid taking up too much context window and quota.
|
|
82
84
|
}
|
|
83
85
|
}
|
|
84
86
|
|
|
@@ -130,7 +132,7 @@ export class BedrockDriver extends AbstractDriver<BedrockDriverOptions, BedrockP
|
|
|
130
132
|
return await formatConversePrompt(segments, opts);
|
|
131
133
|
}
|
|
132
134
|
|
|
133
|
-
|
|
135
|
+
getExtractedExecution(result: ConverseResponse, _prompt?: BedrockPrompt, options?: ExecutionOptions): CompletionChunkObject {
|
|
134
136
|
let resultText = "";
|
|
135
137
|
let reasoning = "";
|
|
136
138
|
|
|
@@ -139,10 +141,9 @@ export class BedrockDriver extends AbstractDriver<BedrockDriverOptions, BedrockP
|
|
|
139
141
|
// Get text output
|
|
140
142
|
if (content.text) {
|
|
141
143
|
resultText += content.text;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
const claudeOptions = options.model_options as BedrockClaudeOptions;
|
|
144
|
+
} else if (content.reasoningContent) {
|
|
145
|
+
// Get reasoning content only if include_thoughts is true
|
|
146
|
+
const claudeOptions = options?.model_options as BedrockClaudeOptions;
|
|
146
147
|
if (claudeOptions?.include_thoughts) {
|
|
147
148
|
if (content.reasoningContent.reasoningText) {
|
|
148
149
|
reasoning += content.reasoningContent.reasoningText.text;
|
|
@@ -151,7 +152,15 @@ export class BedrockDriver extends AbstractDriver<BedrockDriverOptions, BedrockP
|
|
|
151
152
|
const redactedData = new TextDecoder().decode(content.reasoningContent.redactedContent);
|
|
152
153
|
reasoning += `[Redacted thinking: ${redactedData}]`;
|
|
153
154
|
}
|
|
155
|
+
} else {
|
|
156
|
+
this.logger.info("[Bedrock] Not outputting reasoning content as include_thoughts is false");
|
|
154
157
|
}
|
|
158
|
+
} else {
|
|
159
|
+
// Get content block type
|
|
160
|
+
const type = Object.keys(content).find(
|
|
161
|
+
key => key !== '$unknown' && content[key as keyof typeof content] !== undefined
|
|
162
|
+
);
|
|
163
|
+
this.logger.info("[Bedrock] Unsupported content response type:", type);
|
|
155
164
|
}
|
|
156
165
|
}
|
|
157
166
|
|
|
@@ -162,7 +171,7 @@ export class BedrockDriver extends AbstractDriver<BedrockDriverOptions, BedrockP
|
|
|
162
171
|
}
|
|
163
172
|
|
|
164
173
|
const completionResult: CompletionChunkObject = {
|
|
165
|
-
result: reasoning + resultText,
|
|
174
|
+
result: reasoning + resultText ? [{ type: "text", value: reasoning + resultText }] : [],
|
|
166
175
|
token_usage: {
|
|
167
176
|
prompt: result.usage?.inputTokens,
|
|
168
177
|
result: result.usage?.outputTokens,
|
|
@@ -174,7 +183,7 @@ export class BedrockDriver extends AbstractDriver<BedrockDriverOptions, BedrockP
|
|
|
174
183
|
return completionResult;
|
|
175
184
|
};
|
|
176
185
|
|
|
177
|
-
|
|
186
|
+
getExtractedStream(result: ConverseStreamOutput, _prompt?: BedrockPrompt, options?: ExecutionOptions): CompletionChunkObject {
|
|
178
187
|
let output: string = "";
|
|
179
188
|
let reasoning: string = "";
|
|
180
189
|
let stop_reason = "";
|
|
@@ -209,7 +218,15 @@ export class BedrockDriver extends AbstractDriver<BedrockDriverOptions, BedrockP
|
|
|
209
218
|
} else if (delta.reasoningContent.signature) {
|
|
210
219
|
// Handle signature updates for reasoning content - end of thinking
|
|
211
220
|
reasoning = "\n\n";
|
|
221
|
+
// Putting logging here so it only triggers once.
|
|
222
|
+
this.logger.info("[Bedrock] Not outputting reasoning content as include_thoughts is false");
|
|
212
223
|
}
|
|
224
|
+
} else if (delta) {
|
|
225
|
+
// Get content block type
|
|
226
|
+
const type = Object.keys(delta).find(
|
|
227
|
+
key => key !== '$unknown' && (delta as any)[key] !== undefined
|
|
228
|
+
);
|
|
229
|
+
this.logger.info("[Bedrock] Unsupported content response type:", type);
|
|
213
230
|
}
|
|
214
231
|
}
|
|
215
232
|
|
|
@@ -235,7 +252,7 @@ export class BedrockDriver extends AbstractDriver<BedrockDriverOptions, BedrockP
|
|
|
235
252
|
}
|
|
236
253
|
|
|
237
254
|
const completionResult: CompletionChunkObject = {
|
|
238
|
-
result: reasoning + output,
|
|
255
|
+
result: reasoning + output ? [{ type: "text", value: reasoning + output }] : [],
|
|
239
256
|
token_usage: token_usage,
|
|
240
257
|
finish_reason: converseFinishReason(stop_reason),
|
|
241
258
|
};
|
|
@@ -335,26 +352,24 @@ export class BedrockDriver extends AbstractDriver<BedrockDriverOptions, BedrockP
|
|
|
335
352
|
});
|
|
336
353
|
|
|
337
354
|
let tool_use: ToolUse[] | undefined = undefined;
|
|
338
|
-
//Get tool requests
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
} satisfies ToolUse);
|
|
347
|
-
}
|
|
348
|
-
return tools;
|
|
349
|
-
}, []);
|
|
350
|
-
//If no tools were used, set to undefined
|
|
351
|
-
if (tool_use && tool_use.length == 0) {
|
|
352
|
-
tool_use = undefined;
|
|
355
|
+
//Get tool requests, we check tool use regardless of finish reason, as you can hit length and still get a valid response.
|
|
356
|
+
tool_use = res.output?.message?.content?.reduce((tools: ToolUse[], c) => {
|
|
357
|
+
if (c.toolUse) {
|
|
358
|
+
tools.push({
|
|
359
|
+
tool_name: c.toolUse.name ?? "",
|
|
360
|
+
tool_input: c.toolUse.input as any,
|
|
361
|
+
id: c.toolUse.toolUseId ?? "",
|
|
362
|
+
} satisfies ToolUse);
|
|
353
363
|
}
|
|
364
|
+
return tools;
|
|
365
|
+
}, []);
|
|
366
|
+
//If no tools were used, set to undefined
|
|
367
|
+
if (tool_use && tool_use.length == 0) {
|
|
368
|
+
tool_use = undefined;
|
|
354
369
|
}
|
|
355
370
|
|
|
356
371
|
const completion = {
|
|
357
|
-
...
|
|
372
|
+
...this.getExtractedExecution(res, prompt, options),
|
|
358
373
|
original_response: options.include_original_response ? res : undefined,
|
|
359
374
|
conversation: conversation,
|
|
360
375
|
tool_use: tool_use,
|
|
@@ -376,11 +391,11 @@ export class BedrockDriver extends AbstractDriver<BedrockDriverOptions, BedrockP
|
|
|
376
391
|
}
|
|
377
392
|
|
|
378
393
|
return transformAsyncIterator(stream, (streamSegment: ConverseStreamOutput) => {
|
|
379
|
-
return
|
|
394
|
+
return this.getExtractedStream(streamSegment, prompt, options);
|
|
380
395
|
});
|
|
381
396
|
|
|
382
397
|
}).catch((err) => {
|
|
383
|
-
this.logger.error("[Bedrock] Failed to stream", err);
|
|
398
|
+
this.logger.error("[Bedrock] Failed to stream", { error: err });
|
|
384
399
|
throw err;
|
|
385
400
|
});
|
|
386
401
|
}
|
|
@@ -428,7 +443,6 @@ export class BedrockDriver extends AbstractDriver<BedrockDriverOptions, BedrockP
|
|
|
428
443
|
} else if (options.model.includes("mistral")) {
|
|
429
444
|
//7B instruct and 8x7B instruct
|
|
430
445
|
if (options.model.includes("7b")) {
|
|
431
|
-
supportsJSONPrefill = true;
|
|
432
446
|
additionalField = { top_k: model_options.top_k };
|
|
433
447
|
//Does not support system messages
|
|
434
448
|
if (prompt.system && prompt.system?.length != 0) {
|
|
@@ -484,12 +498,17 @@ export class BedrockDriver extends AbstractDriver<BedrockDriverOptions, BedrockP
|
|
|
484
498
|
}
|
|
485
499
|
} else if (options.model.includes("deepseek")) {
|
|
486
500
|
//DeepSeek models support no additional options
|
|
501
|
+
} else if (options.model.includes("gpt-oss")) {
|
|
502
|
+
const gptOssOptions = model_options as ModelOptions as BedrockGptOssOptions;
|
|
503
|
+
additionalField = {
|
|
504
|
+
reasoning_effort: gptOssOptions?.reasoning_effort,
|
|
505
|
+
};
|
|
487
506
|
}
|
|
488
507
|
|
|
489
508
|
//If last message is "```json", add corresponding ``` as a stop sequence.
|
|
490
509
|
if (prompt.messages && prompt.messages.length > 0) {
|
|
491
510
|
if (prompt.messages[prompt.messages.length - 1].content?.[0].text === "```json") {
|
|
492
|
-
|
|
511
|
+
const stopSeq = model_options.stop_sequence;
|
|
493
512
|
if (!stopSeq) {
|
|
494
513
|
model_options.stop_sequence = ["```"];
|
|
495
514
|
} else if (!stopSeq.includes("```")) {
|
|
@@ -521,8 +540,8 @@ export class BedrockDriver extends AbstractDriver<BedrockDriverOptions, BedrockP
|
|
|
521
540
|
}
|
|
522
541
|
};
|
|
523
542
|
|
|
524
|
-
//Only add tools if they are defined
|
|
525
|
-
if (tool_defs) {
|
|
543
|
+
//Only add tools if they are defined and not empty
|
|
544
|
+
if (tool_defs?.length) {
|
|
526
545
|
request.toolConfig = {
|
|
527
546
|
tools: tool_defs,
|
|
528
547
|
}
|
|
@@ -532,7 +551,7 @@ export class BedrockDriver extends AbstractDriver<BedrockDriverOptions, BedrockP
|
|
|
532
551
|
}
|
|
533
552
|
|
|
534
553
|
|
|
535
|
-
async requestImageGeneration(prompt: NovaMessagesPrompt, options: ExecutionOptions): Promise<Completion
|
|
554
|
+
async requestImageGeneration(prompt: NovaMessagesPrompt, options: ExecutionOptions): Promise<Completion> {
|
|
536
555
|
if (options.output_modality !== Modalities.image) {
|
|
537
556
|
throw new Error(`Image generation requires image output_modality`);
|
|
538
557
|
}
|
|
@@ -564,13 +583,14 @@ export class BedrockDriver extends AbstractDriver<BedrockDriverOptions, BedrockP
|
|
|
564
583
|
|
|
565
584
|
const decoder = new TextDecoder();
|
|
566
585
|
const body = decoder.decode(res.body);
|
|
567
|
-
const
|
|
586
|
+
const bedrockResult = JSON.parse(body);
|
|
568
587
|
|
|
569
588
|
return {
|
|
570
|
-
error:
|
|
571
|
-
result: {
|
|
572
|
-
|
|
573
|
-
|
|
589
|
+
error: bedrockResult.error,
|
|
590
|
+
result: bedrockResult.images.map((image: any) => ({
|
|
591
|
+
type: "image" as const,
|
|
592
|
+
value: image
|
|
593
|
+
}))
|
|
574
594
|
}
|
|
575
595
|
}
|
|
576
596
|
|
|
@@ -683,7 +703,7 @@ export class BedrockDriver extends AbstractDriver<BedrockDriverOptions, BedrockP
|
|
|
683
703
|
foundationModels = foundationModels.filter(foundationFilter);
|
|
684
704
|
}
|
|
685
705
|
|
|
686
|
-
const supportedPublishers = ["amazon", "anthropic", "cohere", "ai21", "mistral", "meta", "deepseek", "writer"];
|
|
706
|
+
const supportedPublishers = ["amazon", "anthropic", "cohere", "ai21", "mistral", "meta", "deepseek", "writer", "openai"];
|
|
687
707
|
const unsupportedModelsByPublisher = {
|
|
688
708
|
amazon: ["titan-image-generator", "nova-reel", "nova-sonic", "rerank"],
|
|
689
709
|
anthropic: [],
|
|
@@ -693,6 +713,7 @@ export class BedrockDriver extends AbstractDriver<BedrockDriverOptions, BedrockP
|
|
|
693
713
|
meta: [],
|
|
694
714
|
deepseek: [],
|
|
695
715
|
writer: [],
|
|
716
|
+
openai: [],
|
|
696
717
|
};
|
|
697
718
|
|
|
698
719
|
// Helper function to check if model should be filtered out
|
|
@@ -737,7 +758,6 @@ export class BedrockDriver extends AbstractDriver<BedrockDriverOptions, BedrockP
|
|
|
737
758
|
id: m.modelArn ?? m.modelId,
|
|
738
759
|
name: `${m.providerName} ${m.modelName}`,
|
|
739
760
|
provider: this.provider,
|
|
740
|
-
//description: ``,
|
|
741
761
|
owner: m.providerName,
|
|
742
762
|
can_stream: m.responseStreamingSupported ?? false,
|
|
743
763
|
input_modalities: m.inputModalities ? formatAmazonModalities(m.inputModalities) : modelModalitiesToArray(modelCapability.input),
|
|
@@ -762,6 +782,7 @@ export class BedrockDriver extends AbstractDriver<BedrockDriverOptions, BedrockP
|
|
|
762
782
|
id: m.modelArn,
|
|
763
783
|
name: m.modelName ?? m.modelArn,
|
|
764
784
|
provider: this.provider,
|
|
785
|
+
owner: "custom",
|
|
765
786
|
description: `Custom model from ${m.baseModelName}`,
|
|
766
787
|
is_custom: true,
|
|
767
788
|
input_modalities: modelModalitiesToArray(modelCapability.input),
|
|
@@ -801,6 +822,7 @@ export class BedrockDriver extends AbstractDriver<BedrockDriverOptions, BedrockP
|
|
|
801
822
|
id: p.inferenceProfileArn ?? p.inferenceProfileId,
|
|
802
823
|
name: p.inferenceProfileName ?? p.inferenceProfileArn,
|
|
803
824
|
provider: this.provider,
|
|
825
|
+
owner: providerName,
|
|
804
826
|
input_modalities: modelModalitiesToArray(modelCapability.input),
|
|
805
827
|
output_modalities: modelModalitiesToArray(modelCapability.output),
|
|
806
828
|
tool_support: modelCapability.tool_support,
|
package/src/groq/index.ts
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import { AIModel, AbstractDriver, Completion, CompletionChunkObject, DriverOptions, EmbeddingsOptions, EmbeddingsResult, ExecutionOptions, PromptSegment, TextFallbackOptions } from "@llumiverse/core";
|
|
1
|
+
import { AIModel, AbstractDriver, Completion, CompletionChunkObject, DriverOptions, EmbeddingsOptions, EmbeddingsResult, ExecutionOptions, PromptSegment, TextFallbackOptions, ToolDefinition, ToolUse } from "@llumiverse/core";
|
|
2
2
|
import { transformAsyncIterator } from "@llumiverse/core/async";
|
|
3
|
-
import {
|
|
4
|
-
import Groq from "groq-sdk";
|
|
3
|
+
import { formatOpenAILikeMultimodalPrompt } from "../openai/openai_format.js";
|
|
5
4
|
|
|
5
|
+
import Groq from "groq-sdk";
|
|
6
|
+
import type { ChatCompletionMessageParam, ChatCompletionTool } from "groq-sdk/resources/chat/completions";
|
|
6
7
|
|
|
7
8
|
interface GroqDriverOptions extends DriverOptions {
|
|
8
9
|
apiKey: string;
|
|
9
10
|
endpoint_url?: string;
|
|
10
11
|
}
|
|
11
12
|
|
|
12
|
-
|
|
13
|
-
export class GroqDriver extends AbstractDriver<GroqDriverOptions, OpenAITextMessage[]> {
|
|
13
|
+
export class GroqDriver extends AbstractDriver<GroqDriverOptions, ChatCompletionMessageParam[]> {
|
|
14
14
|
static PROVIDER = "groq";
|
|
15
15
|
provider = GroqDriver.PROVIDER;
|
|
16
16
|
apiKey: string;
|
|
@@ -46,27 +46,169 @@ export class GroqDriver extends AbstractDriver<GroqDriverOptions, OpenAITextMess
|
|
|
46
46
|
return undefined;
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
-
protected async formatPrompt(segments: PromptSegment[], opts: ExecutionOptions): Promise<
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
49
|
+
protected async formatPrompt(segments: PromptSegment[], opts: ExecutionOptions): Promise<ChatCompletionMessageParam[]> {
|
|
50
|
+
// Use OpenAI's multimodal formatter as base then convert to Groq types
|
|
51
|
+
const openaiMessages = await formatOpenAILikeMultimodalPrompt(segments, {
|
|
52
|
+
...opts,
|
|
53
|
+
multimodal: true,
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
// Convert OpenAI ChatCompletionMessageParam[] to Groq ChatCompletionMessageParam[]
|
|
57
|
+
// Handle differences between OpenAI and Groq SDK types
|
|
58
|
+
const groqMessages: ChatCompletionMessageParam[] = openaiMessages.map(msg => {
|
|
59
|
+
// Handle OpenAI developer messages - convert to system messages for Groq
|
|
60
|
+
if (msg.role === 'developer' || msg.role === 'system') {
|
|
61
|
+
const systemMsg: ChatCompletionMessageParam = {
|
|
62
|
+
role: 'system',
|
|
63
|
+
content: Array.isArray(msg.content)
|
|
64
|
+
? msg.content.map(part => part.text).join('\n')
|
|
65
|
+
: msg.content,
|
|
66
|
+
// Preserve name if present
|
|
67
|
+
...(msg.name && { name: msg.name })
|
|
68
|
+
};
|
|
69
|
+
return systemMsg;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Handle user messages - filter content parts to only supported types
|
|
73
|
+
if (msg.role === 'user') {
|
|
74
|
+
let content: string | Array<{ type: 'text', text: string } | { type: 'image_url', image_url: { url: string, detail?: 'auto' | 'low' | 'high' } }> | undefined = undefined;
|
|
75
|
+
|
|
76
|
+
if (typeof msg.content === 'string') {
|
|
77
|
+
content = msg.content;
|
|
78
|
+
} else if (Array.isArray(msg.content)) {
|
|
79
|
+
// Filter to only text and image_url parts that Groq supports
|
|
80
|
+
const supportedParts = msg.content.filter(part =>
|
|
81
|
+
part.type === 'text' || part.type === 'image_url'
|
|
82
|
+
).map(part => {
|
|
83
|
+
if (part.type === 'text') {
|
|
84
|
+
return { type: 'text' as const, text: part.text };
|
|
85
|
+
} else if (part.type === 'image_url') {
|
|
86
|
+
return {
|
|
87
|
+
type: 'image_url' as const,
|
|
88
|
+
image_url: {
|
|
89
|
+
url: part.image_url.url,
|
|
90
|
+
...(part.image_url.detail && { detail: part.image_url.detail })
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
return null;
|
|
95
|
+
}).filter(Boolean) as Array<{ type: 'text', text: string } | { type: 'image_url', image_url: { url: string, detail?: 'auto' | 'low' | 'high' } }>;
|
|
96
|
+
|
|
97
|
+
content = supportedParts.length > 0 ? supportedParts : 'Content not supported';
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const userMsg: ChatCompletionMessageParam = {
|
|
101
|
+
role: 'user',
|
|
102
|
+
content: content ?? "",
|
|
103
|
+
// Preserve name if present
|
|
104
|
+
...(msg.name && { name: msg.name })
|
|
105
|
+
};
|
|
106
|
+
return userMsg;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// Handle assistant messages - handle content arrays if needed
|
|
110
|
+
if (msg.role === 'assistant') {
|
|
111
|
+
const assistantMsg: ChatCompletionMessageParam = {
|
|
112
|
+
role: 'assistant',
|
|
113
|
+
content: Array.isArray(msg.content)
|
|
114
|
+
? msg.content.map(part => 'text' in part ? part.text : '').filter(Boolean).join('\n') || null
|
|
115
|
+
: msg.content,
|
|
116
|
+
// Preserve other assistant message properties
|
|
117
|
+
...(msg.tool_calls && { tool_calls: msg.tool_calls }),
|
|
118
|
+
...(msg.name && { name: msg.name })
|
|
119
|
+
};
|
|
120
|
+
return assistantMsg;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// For tool and function messages, they should be compatible
|
|
124
|
+
if (msg.role === 'tool') {
|
|
125
|
+
const toolMsg: ChatCompletionMessageParam = {
|
|
126
|
+
role: 'tool',
|
|
127
|
+
tool_call_id: msg.tool_call_id,
|
|
128
|
+
content: Array.isArray(msg.content)
|
|
129
|
+
? msg.content.map(part => part.text).join('\n')
|
|
130
|
+
: msg.content
|
|
131
|
+
};
|
|
132
|
+
return toolMsg;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
if (msg.role === 'function') {
|
|
136
|
+
const functionMsg: ChatCompletionMessageParam = {
|
|
137
|
+
role: 'function',
|
|
138
|
+
name: msg.name,
|
|
139
|
+
content: msg.content
|
|
140
|
+
};
|
|
141
|
+
return functionMsg;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// Fallback - should not reach here but provides type safety
|
|
145
|
+
throw new Error(`Unsupported message role: ${(msg as any).role}`);
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
return groqMessages;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
private getToolDefinitions(tools: ToolDefinition[] | undefined): ChatCompletionTool[] | undefined {
|
|
152
|
+
if (!tools || tools.length === 0) {
|
|
153
|
+
return undefined;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
return tools.map(tool => ({
|
|
157
|
+
type: 'function' as const,
|
|
158
|
+
function: {
|
|
159
|
+
name: tool.name,
|
|
160
|
+
description: tool.description,
|
|
161
|
+
parameters: tool.input_schema as any,
|
|
162
|
+
}
|
|
163
|
+
}));
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
private extractToolUse(message: any): ToolUse[] | undefined {
|
|
167
|
+
if (!message.tool_calls || message.tool_calls.length === 0) {
|
|
168
|
+
return undefined;
|
|
57
169
|
}
|
|
58
|
-
|
|
170
|
+
|
|
171
|
+
return message.tool_calls.map((toolCall: any) => ({
|
|
172
|
+
id: toolCall.id,
|
|
173
|
+
tool_name: toolCall.function.name,
|
|
174
|
+
tool_input: JSON.parse(toolCall.function.arguments || '{}'),
|
|
175
|
+
}));
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
private sanitizeMessagesForGroq(messages: ChatCompletionMessageParam[]): ChatCompletionMessageParam[] {
|
|
179
|
+
return messages.map(message => {
|
|
180
|
+
// Remove any reasoning field from message objects
|
|
181
|
+
const { reasoning, ...sanitizedMessage } = message as any;
|
|
182
|
+
|
|
183
|
+
// If message has content array, filter out reasoning content types
|
|
184
|
+
if (Array.isArray(sanitizedMessage.content)) {
|
|
185
|
+
sanitizedMessage.content = sanitizedMessage.content.filter((part: any) => {
|
|
186
|
+
// Filter out any reasoning-related content parts
|
|
187
|
+
return part.type !== 'reasoning' && !('reasoning' in part);
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
return sanitizedMessage as ChatCompletionMessageParam;
|
|
192
|
+
});
|
|
59
193
|
}
|
|
60
194
|
|
|
61
|
-
async requestTextCompletion(messages:
|
|
195
|
+
async requestTextCompletion(messages: ChatCompletionMessageParam[], options: ExecutionOptions): Promise<Completion> {
|
|
62
196
|
if (options.model_options?._option_id !== "text-fallback" && options.model_options?._option_id !== "groq-deepseek-thinking") {
|
|
63
|
-
this.logger.warn("Invalid model options", {options: options.model_options });
|
|
197
|
+
this.logger.warn("Invalid model options", { options: options.model_options });
|
|
64
198
|
}
|
|
65
199
|
options.model_options = options.model_options as TextFallbackOptions;
|
|
66
200
|
|
|
201
|
+
// Update conversation with current messages
|
|
202
|
+
let conversation = updateConversation(options.conversation as ChatCompletionMessageParam[], messages);
|
|
203
|
+
|
|
204
|
+
// Filter out any reasoning content that Groq doesn't support
|
|
205
|
+
conversation = this.sanitizeMessagesForGroq(conversation);
|
|
206
|
+
|
|
207
|
+
const tools = this.getToolDefinitions(options.tools);
|
|
208
|
+
|
|
67
209
|
const res = await this.client.chat.completions.create({
|
|
68
210
|
model: options.model,
|
|
69
|
-
messages:
|
|
211
|
+
messages: conversation,
|
|
70
212
|
max_completion_tokens: options.model_options?.max_tokens,
|
|
71
213
|
temperature: options.model_options?.temperature,
|
|
72
214
|
top_p: options.model_options?.top_p,
|
|
@@ -75,33 +217,54 @@ export class GroqDriver extends AbstractDriver<GroqDriverOptions, OpenAITextMess
|
|
|
75
217
|
presence_penalty: options.model_options?.presence_penalty,
|
|
76
218
|
frequency_penalty: options.model_options?.frequency_penalty,
|
|
77
219
|
response_format: this.getResponseFormat(options),
|
|
220
|
+
tools: tools,
|
|
78
221
|
});
|
|
79
222
|
|
|
80
|
-
|
|
81
223
|
const choice = res.choices[0];
|
|
82
224
|
const result = choice.message.content;
|
|
83
225
|
|
|
226
|
+
// Extract tool use from the response
|
|
227
|
+
const tool_use = this.extractToolUse(choice.message);
|
|
228
|
+
|
|
229
|
+
// Update conversation with the response
|
|
230
|
+
conversation = updateConversation(conversation, [choice.message]);
|
|
231
|
+
|
|
232
|
+
let finish_reason = choice.finish_reason;
|
|
233
|
+
if (tool_use && tool_use.length > 0) {
|
|
234
|
+
finish_reason = "tool_calls";
|
|
235
|
+
}
|
|
236
|
+
|
|
84
237
|
return {
|
|
85
|
-
result: result,
|
|
238
|
+
result: result ? [{ type: "text", value: result }] : [],
|
|
86
239
|
token_usage: {
|
|
87
240
|
prompt: res.usage?.prompt_tokens,
|
|
88
241
|
result: res.usage?.completion_tokens,
|
|
89
242
|
total: res.usage?.total_tokens,
|
|
90
243
|
},
|
|
91
|
-
finish_reason:
|
|
244
|
+
finish_reason: finish_reason,
|
|
92
245
|
original_response: options.include_original_response ? res : undefined,
|
|
246
|
+
conversation,
|
|
247
|
+
tool_use,
|
|
93
248
|
};
|
|
94
249
|
}
|
|
95
250
|
|
|
96
|
-
async requestTextCompletionStream(messages:
|
|
251
|
+
async requestTextCompletionStream(messages: ChatCompletionMessageParam[], options: ExecutionOptions): Promise<AsyncIterable<CompletionChunkObject>> {
|
|
97
252
|
if (options.model_options?._option_id !== "text-fallback") {
|
|
98
|
-
this.logger.warn("Invalid model options", {options: options.model_options });
|
|
253
|
+
this.logger.warn("Invalid model options", { options: options.model_options });
|
|
99
254
|
}
|
|
100
255
|
options.model_options = options.model_options as TextFallbackOptions;
|
|
101
256
|
|
|
257
|
+
// Update conversation with current messages
|
|
258
|
+
let conversation = updateConversation(options.conversation as ChatCompletionMessageParam[], messages);
|
|
259
|
+
|
|
260
|
+
// Filter out any reasoning content that Groq doesn't support
|
|
261
|
+
conversation = this.sanitizeMessagesForGroq(conversation);
|
|
262
|
+
|
|
263
|
+
const tools = this.getToolDefinitions(options.tools);
|
|
264
|
+
|
|
102
265
|
const res = await this.client.chat.completions.create({
|
|
103
266
|
model: options.model,
|
|
104
|
-
messages:
|
|
267
|
+
messages: conversation,
|
|
105
268
|
max_completion_tokens: options.model_options?.max_tokens,
|
|
106
269
|
temperature: options.model_options?.temperature,
|
|
107
270
|
top_p: options.model_options?.top_p,
|
|
@@ -110,17 +273,28 @@ export class GroqDriver extends AbstractDriver<GroqDriverOptions, OpenAITextMess
|
|
|
110
273
|
presence_penalty: options.model_options?.presence_penalty,
|
|
111
274
|
frequency_penalty: options.model_options?.frequency_penalty,
|
|
112
275
|
stream: true,
|
|
276
|
+
tools: tools,
|
|
113
277
|
});
|
|
114
278
|
|
|
115
|
-
return transformAsyncIterator(res, (
|
|
116
|
-
|
|
117
|
-
finish_reason
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
}
|
|
123
|
-
|
|
279
|
+
return transformAsyncIterator(res, (chunk) => {
|
|
280
|
+
const choice = chunk.choices[0];
|
|
281
|
+
let finish_reason = choice.finish_reason;
|
|
282
|
+
|
|
283
|
+
// Check for tool calls in the delta
|
|
284
|
+
if (choice.delta.tool_calls && choice.delta.tool_calls.length > 0) {
|
|
285
|
+
finish_reason = "tool_calls";
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
return {
|
|
289
|
+
result: choice.delta.content ? [{ type: "text", value: choice.delta.content }] : [],
|
|
290
|
+
finish_reason: finish_reason,
|
|
291
|
+
token_usage: {
|
|
292
|
+
prompt: chunk.x_groq?.usage?.prompt_tokens,
|
|
293
|
+
result: chunk.x_groq?.usage?.completion_tokens,
|
|
294
|
+
total: chunk.x_groq?.usage?.total_tokens,
|
|
295
|
+
},
|
|
296
|
+
} as CompletionChunkObject;
|
|
297
|
+
});
|
|
124
298
|
}
|
|
125
299
|
|
|
126
300
|
async listModels(): Promise<AIModel<string>[]> {
|
|
@@ -154,4 +328,17 @@ export class GroqDriver extends AbstractDriver<GroqDriverOptions, OpenAITextMess
|
|
|
154
328
|
throw new Error("Method not implemented.");
|
|
155
329
|
}
|
|
156
330
|
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* Update the conversation messages by combining existing conversation with new messages
|
|
335
|
+
* @param conversation Existing conversation messages
|
|
336
|
+
* @param messages New messages to add
|
|
337
|
+
* @returns Combined conversation
|
|
338
|
+
*/
|
|
339
|
+
function updateConversation(
|
|
340
|
+
conversation: ChatCompletionMessageParam[] | undefined,
|
|
341
|
+
messages: ChatCompletionMessageParam[]
|
|
342
|
+
): ChatCompletionMessageParam[] {
|
|
343
|
+
return (conversation || []).concat(messages);
|
|
157
344
|
}
|