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

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

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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109                                                                                                                                                                                                                         
import {
  generateErrorResponse,
  generateInvalidProviderResponseError,
} from '@api/utils/ai-provider';
import type {
  AIProviderFunctionConfig,
  ResponseTransformFunction,
} from '@shared/types/ai-providers/config';
import type { ErrorResponseBody } from '@shared/types/api/response/body';
import type { CreateEmbeddingsResponseBody } from '@shared/types/api/routes/embeddings-api';
import { AIProvider } from '@shared/types/constants';
 
export const voyageEmbedConfig: AIProviderFunctionConfig = {
  model: {
    param: 'model',
    required: true,
  },
  input: {
    param: 'input',
    required: true,
  },
  input_type: {
    param: 'input_type',
    required: false,
  },
  truncation: {
    param: 'truncation',
    required: false,
    default: true,
  },
  encoding_format: {
    param: 'encoding_format',
    required: false,
  },
  output_dimension: {
    param: 'output_dimension',
    required: false,
  },
  output_dtype: {
    param: 'output_dtype',
    required: false,
    default: 'float',
  },
};
 
interface VoyageEmbedResponse {
  object: 'list';
  data: Array<{ object: 'embedding'; embedding: number[]; index: number }>;
  model: string;
  usage: {
    total_tokens: number;
  };
}
 
interface VoyageErrorResponse {
  detail: string;
}
 
export const voyageErrorResponseTransform = (
  response: VoyageErrorResponse | Record<string, unknown>,
): ErrorResponseBody => {
  if ('detail' in response) {
    return generateErrorResponse(
      {
        message: response.detail as string,
        type: 'Invalid Request',
        param: undefined,
        code: undefined,
      },
      AIProvider.VOYAGE,
    );
  }
 
  return generateInvalidProviderResponseError(
    response as Record<string, unknown>,
    AIProvider.VOYAGE,
  );
};
 
export const voyageEmbedResponseTransform: ResponseTransformFunction = (
  aiProviderResponseBody,
  aiProviderResponseStatus,
) => {
  if (aiProviderResponseStatus !== 200) {
    return voyageErrorResponseTransform(
      aiProviderResponseBody as unknown as VoyageErrorResponse,
    );
  }
 
  if ('data' in aiProviderResponseBody) {
    const response = aiProviderResponseBody as unknown as VoyageEmbedResponse;
    return {
      object: 'list',
      data: response.data,
      model: response.model,
      usage: {
        prompt_tokens: response.usage.total_tokens,
        total_tokens: response.usage.total_tokens,
      },
      provider: AIProvider.VOYAGE,
    } as CreateEmbeddingsResponseBody;
  }
 
  return generateInvalidProviderResponseError(
    aiProviderResponseBody as Record<string, unknown>,
    AIProvider.VOYAGE,
  );
};