Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: siliconflow error response #636

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/providers/deepbricks/api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ProviderAPIConfig } from '../types';

const DeepbricksAPIConfig: ProviderAPIConfig = {
getBaseURL: () => 'https://api.deepbricks.ai/v1',
getBaseURL: () => 'https://api.deepbricks.ai',
headers: ({ providerOptions }) => {
const headersObj: Record<string, string> = {
Authorization: `Bearer ${providerOptions.apiKey}`,
Expand All @@ -20,9 +20,9 @@ const DeepbricksAPIConfig: ProviderAPIConfig = {
getEndpoint: ({ fn }) => {
switch (fn) {
case 'chatComplete':
return '/chat/completions';
return '/v1/chat/completions';
case 'imageGenerate':
return '/images/generations';
return '/v1/images/generations';
default:
return '';
}
Expand Down
8 changes: 4 additions & 4 deletions src/providers/siliconflow/api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ProviderAPIConfig } from '../types';

const SiliconFlowAPIConfig: ProviderAPIConfig = {
getBaseURL: () => 'https://api.siliconflow.cn/v1',
getBaseURL: () => 'https://api.siliconflow.cn',
headers: ({ providerOptions }) => {
return {
Authorization: `Bearer ${providerOptions.apiKey}`,
Expand All @@ -11,11 +11,11 @@ const SiliconFlowAPIConfig: ProviderAPIConfig = {
const { model = 'ByteDance/SDXL-Lightning' } = gatewayRequestBody;
switch (fn) {
case 'chatComplete':
return '/chat/completions';
return '/v1/chat/completions';
case 'embed':
return '/embeddings';
return '/v1/embeddings';
case 'imageGenerate':
return `/${model}/text-to-image`;
return `/v1/${model}/text-to-image`;
default:
return '';
}
Expand Down
17 changes: 11 additions & 6 deletions src/providers/siliconflow/chatComplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,26 +62,31 @@ export const SiliconFlowChatCompleteConfig: ProviderConfig = {
};

export const SiliconFlowErrorResponseTransform: (
response: ErrorResponse,
response: { message: string; code: string },
provider: string
) => ErrorResponse = (response, provider) => {
return generateErrorResponse(
{
...response.error,
...response,
type: null,
param: null,
},
provider
);
};

export const SiliconFlowChatCompleteResponseTransform: (
response: ChatCompletionResponse | ErrorResponse,
response: ChatCompletionResponse | string,
responseStatus: number
) => ChatCompletionResponse | ErrorResponse = (response, responseStatus) => {
if (responseStatus !== 200 && 'error' in response) {
return SiliconFlowErrorResponseTransform(response, SILICONFLOW);
if (responseStatus !== 200 && typeof response === 'string') {
return SiliconFlowErrorResponseTransform(
{ message: response, code: String(responseStatus) },
SILICONFLOW
);
}

return response;
return response as ChatCompletionResponse;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would recommend changing the structure of this function to

if ('contents' in response) {
 return response
 }
 if (responseStatus !== 200 && typeof response === 'string') {
    return SiliconFlowErrorResponseTransform(
      { message: response, code: String(responseStatus) },
      SILICONFLOW
    );
  }
  else return generateInvalidProviderResponseError(response, SILICONFLOW)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment for other updated functions as well

};

/**
Expand Down
11 changes: 7 additions & 4 deletions src/providers/siliconflow/embed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@ export const SiliconFlowEmbedConfig: ProviderConfig = {
interface SiliconFlowEmbedResponse extends EmbedResponse {}

export const SiliconFlowEmbedResponseTransform: (
response: SiliconFlowEmbedResponse | ErrorResponse,
response: SiliconFlowEmbedResponse | string,
responseStatus: number
) => EmbedResponse | ErrorResponse = (response, responseStatus) => {
if (responseStatus !== 200 && 'error' in response) {
return SiliconFlowErrorResponseTransform(response, SILICONFLOW);
if (responseStatus !== 200 && typeof response === 'string') {
return SiliconFlowErrorResponseTransform(
{ message: response, code: String(responseStatus) },
SILICONFLOW
);
}

return response;
return response as SiliconFlowEmbedResponse;
};
11 changes: 7 additions & 4 deletions src/providers/siliconflow/imageGenerate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,15 @@ interface SiliconFlowImageGenerateResponse extends ImageGenerateResponse {
}

export const SiliconFlowImageGenerateResponseTransform: (
response: SiliconFlowImageGenerateResponse | ErrorResponse,
response: SiliconFlowImageGenerateResponse | string,
responseStatus: number
) => ImageGenerateResponse | ErrorResponse = (response, responseStatus) => {
if (responseStatus !== 200 && 'error' in response) {
return SiliconFlowErrorResponseTransform(response, SILICONFLOW);
if (responseStatus !== 200 && typeof response === 'string') {
return SiliconFlowErrorResponseTransform(
{ message: response, code: String(responseStatus) },
SILICONFLOW
);
}

return response;
return response as ImageGenerateResponse;
};
Loading