All files / api/src/ai-providers/sambanova index.ts

0% Statements 0/63
100% Branches 1/1
100% Functions 1/1
0% Lines 0/63

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                                                                                                                                                     
import type { AIProviderConfig } from '@shared/types/ai-providers/config';
import { FunctionName } from '@shared/types/api/request';
import type { ErrorResponseBody } from '@shared/types/api/response/body';
import type { ChatCompletionResponseBody } from '@shared/types/api/routes/chat-completions-api';
import type { ChatCompletionChoice } from '@shared/types/api/routes/chat-completions-api/response';
import type { ChatCompletionMessage } from '@shared/types/api/routes/shared/messages';
import { AIProvider } from '@shared/types/constants';
import { chatCompleteParams, responseTransformers } from '../open-ai-base';
import sambanovaAPIConfig from './api';
import { sambanovaChatCompleteStreamChunkTransform } from './chatComplete';
 
const sambanovaConfig: AIProviderConfig = {
  chat_complete: chatCompleteParams(
    [
      'functions',
      'function_call',
      'presence_penalty',
      'frequency_penalty',
      'logit_bias',
      'user',
      'seed',
      'tools',
      'tool_choice',
      'response_format',
      'logprobs',
    ],
    {
      model: 'Meta-Llama-3.1-8B-Instruct',
    },
  ),
  api: sambanovaAPIConfig,
  responseTransforms: {
    ...responseTransformers(AIProvider.SAMBANOVA, {
      chatComplete: (
        aiProviderResponseBody,
        isError,
      ): ChatCompletionResponseBody | ErrorResponseBody => {
        if (
          isError ||
          !('choices' in aiProviderResponseBody) ||
          aiProviderResponseBody === undefined
        )
          return aiProviderResponseBody as ErrorResponseBody;
 
        const successResponse =
          aiProviderResponseBody as unknown as ChatCompletionResponseBody;
 
        return {
          ...successResponse,
          provider: AIProvider.SAMBANOVA,
          choices: successResponse.choices.map(
            (choice: ChatCompletionChoice) => ({
              //P
              ...choice,
              message: {
                ...(choice.message as ChatCompletionMessage),
                role: 'assistant',
              },
            }),
          ),
          usage: {
            prompt_tokens: successResponse.usage?.prompt_tokens || 0,
            completion_tokens: successResponse.usage?.completion_tokens || 0,
            total_tokens: successResponse.usage?.total_tokens || 0,
          },
        } as ChatCompletionResponseBody;
      },
    }),
    [FunctionName.STREAM_CHAT_COMPLETE]:
      sambanovaChatCompleteStreamChunkTransform,
  },
};
 
export default sambanovaConfig;