|
1 | 1 | import { BEDROCK } from '../../globals';
|
2 |
| -import { EmbedResponse } from '../../types/embedRequestBody'; |
| 2 | +import { EmbedParams, EmbedResponse } from '../../types/embedRequestBody'; |
3 | 3 | import { Params } from '../../types/requestBody';
|
4 | 4 | import { ErrorResponse, ProviderConfig } from '../types';
|
5 | 5 | import { generateInvalidProviderResponseError } from '../utils';
|
6 | 6 | import { BedrockErrorResponseTransform } from './chatComplete';
|
7 | 7 |
|
8 | 8 | export const BedrockCohereEmbedConfig: ProviderConfig = {
|
9 |
| - input: { |
10 |
| - param: 'texts', |
11 |
| - required: true, |
12 |
| - transform: (params: any): string[] => { |
13 |
| - if (Array.isArray(params.input)) { |
14 |
| - return params.input; |
15 |
| - } else { |
16 |
| - return [params.input]; |
17 |
| - } |
| 9 | + input: [ |
| 10 | + { |
| 11 | + param: 'texts', |
| 12 | + required: false, |
| 13 | + transform: (params: EmbedParams): string[] | undefined => { |
| 14 | + if (typeof params.input === 'string') return [params.input]; |
| 15 | + else if (Array.isArray(params.input) && params.input.length > 0) { |
| 16 | + const texts: string[] = []; |
| 17 | + params.input.forEach((item) => { |
| 18 | + if (typeof item === 'string') { |
| 19 | + texts.push(item); |
| 20 | + } else if (item.text) { |
| 21 | + texts.push(item.text); |
| 22 | + } |
| 23 | + }); |
| 24 | + return texts.length > 0 ? texts : undefined; |
| 25 | + } |
| 26 | + }, |
18 | 27 | },
|
19 |
| - }, |
| 28 | + { |
| 29 | + param: 'images', |
| 30 | + required: false, |
| 31 | + transform: (params: EmbedParams): string[] | undefined => { |
| 32 | + if (Array.isArray(params.input) && params.input.length > 0) { |
| 33 | + const images: string[] = []; |
| 34 | + params.input.forEach((item) => { |
| 35 | + if (typeof item === 'object' && item.image?.base64) { |
| 36 | + images.push(item.image.base64); |
| 37 | + } |
| 38 | + }); |
| 39 | + return images.length > 0 ? images : undefined; |
| 40 | + } |
| 41 | + }, |
| 42 | + }, |
| 43 | + ], |
20 | 44 | input_type: {
|
21 | 45 | param: 'input_type',
|
22 | 46 | required: true,
|
23 | 47 | },
|
24 | 48 | truncate: {
|
25 | 49 | param: 'truncate',
|
| 50 | + required: false, |
| 51 | + }, |
| 52 | + encoding_format: { |
| 53 | + param: 'embedding_types', |
| 54 | + required: false, |
| 55 | + transform: (params: any): string[] => { |
| 56 | + if (Array.isArray(params.encoding_format)) return params.encoding_format; |
| 57 | + return [params.encoding_format]; |
| 58 | + }, |
26 | 59 | },
|
27 | 60 | };
|
28 | 61 |
|
29 | 62 | export const BedrockTitanEmbedConfig: ProviderConfig = {
|
30 |
| - input: { |
31 |
| - param: 'inputText', |
32 |
| - required: true, |
| 63 | + input: [ |
| 64 | + { |
| 65 | + param: 'inputText', |
| 66 | + required: false, |
| 67 | + transform: (params: EmbedParams): string | undefined => { |
| 68 | + if ( |
| 69 | + Array.isArray(params.input) && |
| 70 | + typeof params.input[0] === 'object' && |
| 71 | + params.input[0].text |
| 72 | + ) { |
| 73 | + return params.input[0].text; |
| 74 | + } |
| 75 | + if (typeof params.input === 'string') return params.input; |
| 76 | + }, |
| 77 | + }, |
| 78 | + { |
| 79 | + param: 'inputImage', |
| 80 | + required: false, |
| 81 | + transform: (params: EmbedParams) => { |
| 82 | + // Titan models only support one image per request |
| 83 | + if ( |
| 84 | + Array.isArray(params.input) && |
| 85 | + typeof params.input[0] === 'object' && |
| 86 | + params.input[0].image?.base64 |
| 87 | + ) { |
| 88 | + return params.input[0].image.base64; |
| 89 | + } |
| 90 | + }, |
| 91 | + }, |
| 92 | + ], |
| 93 | + dimensions: [ |
| 94 | + { |
| 95 | + param: 'dimensions', |
| 96 | + required: false, |
| 97 | + transform: (params: EmbedParams): number | undefined => { |
| 98 | + if (typeof params.input === 'string') return params.dimensions; |
| 99 | + }, |
| 100 | + }, |
| 101 | + { |
| 102 | + param: 'embeddingConfig', |
| 103 | + required: false, |
| 104 | + transform: ( |
| 105 | + params: EmbedParams |
| 106 | + ): { outputEmbeddingLength: number } | undefined => { |
| 107 | + if (Array.isArray(params.input) && params.dimensions) { |
| 108 | + return { |
| 109 | + outputEmbeddingLength: params.dimensions, |
| 110 | + }; |
| 111 | + } |
| 112 | + }, |
| 113 | + }, |
| 114 | + ], |
| 115 | + encoding_format: { |
| 116 | + param: 'embeddingTypes', |
| 117 | + required: false, |
| 118 | + transform: (params: any): string[] => { |
| 119 | + if (Array.isArray(params.encoding_format)) return params.encoding_format; |
| 120 | + return [params.encoding_format]; |
| 121 | + }, |
| 122 | + }, |
| 123 | + // Titan specific parameters |
| 124 | + normalize: { |
| 125 | + param: 'normalize', |
| 126 | + required: false, |
33 | 127 | },
|
34 | 128 | };
|
35 | 129 |
|
|
0 commit comments