All files / api/src/ai-providers/siliconflow embed.ts

0% Statements 0/56
0% Branches 0/1
0% Functions 0/1
0% Lines 0/56

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74                                                                                                                                                   
import {
  generateErrorResponse,
  generateInvalidProviderResponseError,
} from '@api/utils/ai-provider';
import type {
  AIProviderFunctionConfig,
  ResponseTransformFunction,
} from '@shared/types/ai-providers/config';
import type { CreateEmbeddingsResponseBody } from '@shared/types/api/routes/embeddings-api';
import { AIProvider } from '@shared/types/constants';
 
// TODOS: this configuration does not enforce the maximum token limit for the input parameter. If you want to enforce this, you might need to add a custom validation function or a max property to the ParameterConfig interface, and then use it in the input configuration. However, this might be complex because the token count is not a simple length check, but depends on the specific tokenization method used by the model.
 
export const siliconFlowEmbedConfig: AIProviderFunctionConfig = {
  model: {
    param: 'model',
    required: true,
    default: 'BAAI/bge-large-zh-v1.5',
  },
  input: {
    param: 'input',
    required: true,
  },
  encoding_format: {
    param: 'encoding_format',
    default: 'float',
  },
  dimensions: {
    param: 'dimensions',
  },
};
 
export const siliconFlowEmbedResponseTransform: ResponseTransformFunction = (
  aiProviderResponseBody,
  aiProviderResponseStatus,
) => {
  // Handle error responses - prioritize status code over message field presence
  if (aiProviderResponseStatus !== 200) {
    if ('message' in aiProviderResponseBody) {
      return generateErrorResponse(
        {
          message: aiProviderResponseBody.message as string,
          type: (aiProviderResponseBody.type as string) || 'api_error',
          param: aiProviderResponseBody.param as string | undefined,
          code:
            (aiProviderResponseBody.code as string) ||
            aiProviderResponseStatus.toString(),
        },
        AIProvider.SILICONFLOW,
      );
    }
 
    // Handle error responses without message field
    return generateErrorResponse(
      {
        message: 'Request failed',
        type: 'api_error',
        param: undefined,
        code: aiProviderResponseStatus.toString(),
      },
      AIProvider.SILICONFLOW,
    );
  }
 
  if ('data' in aiProviderResponseBody) {
    return aiProviderResponseBody as unknown as CreateEmbeddingsResponseBody;
  }
 
  return generateInvalidProviderResponseError(
    aiProviderResponseBody as Record<string, unknown>,
    AIProvider.SILICONFLOW,
  );
};