Skip to content

Commit

Permalink
Merge pull request #835 from Ajay-Satish-01/recraft-ai
Browse files Browse the repository at this point in the history
[Provider] Recraft AI
  • Loading branch information
VisargD authored Dec 24, 2024
2 parents 0041ff2 + 83f3bd7 commit 7dcd668
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export const DASHSCOPE: string = 'dashscope';
export const X_AI: string = 'x-ai';
export const SAGEMAKER: string = 'sagemaker';
export const NEBIUS: string = 'nebius';
export const RECRAFTAI: string = 'recraft-ai';

export const VALID_PROVIDERS = [
ANTHROPIC,
Expand Down Expand Up @@ -127,6 +128,7 @@ export const VALID_PROVIDERS = [
X_AI,
SAGEMAKER,
NEBIUS,
RECRAFTAI,
];

export const CONTENT_TYPES = {
Expand Down
2 changes: 2 additions & 0 deletions src/providers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import XAIConfig from './x-ai';
import QdrantConfig from './qdrant';
import SagemakerConfig from './sagemaker';
import NebiusConfig from './nebius';
import RecraftAIConfig from './recraft-ai';

const Providers: { [key: string]: ProviderConfigs } = {
openai: OpenAIConfig,
Expand Down Expand Up @@ -102,6 +103,7 @@ const Providers: { [key: string]: ProviderConfigs } = {
qdrant: QdrantConfig,
sagemaker: SagemakerConfig,
nebius: NebiusConfig,
'recraft-ai': RecraftAIConfig,
};

export default Providers;
19 changes: 19 additions & 0 deletions src/providers/recraft-ai/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { ProviderAPIConfig } from '../types';

const RecraftAIAPIConfig: ProviderAPIConfig = {
getBaseURL: () => 'https://external.api.recraft.ai/v1',
headers: ({ providerOptions }) => ({
Authorization: `Bearer ${providerOptions.apiKey}`,
'Content-Type': 'application/json',
}),
getEndpoint: ({ fn }) => {
switch (fn) {
case 'imageGenerate':
return '/images/generations';
default:
return '';
}
},
};

export default RecraftAIAPIConfig;
80 changes: 80 additions & 0 deletions src/providers/recraft-ai/imageGenerate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { RECRAFTAI } from '../../globals';
import { ErrorResponse, ImageGenerateResponse, ProviderConfig } from '../types';
import { generateErrorResponse } from '../utils';

interface RecraftAIImageObject {
b64_json?: string;
url?: string;
}

interface RecraftAIImageGenerateResponse extends ImageGenerateResponse {
data: RecraftAIImageObject[];
}

export const RecraftAIImageGenerateConfig: ProviderConfig = {
prompt: {
param: 'prompt',
required: true,
},
style: {
param: 'style',
default: 'realistic_image',
},
style_id: {
param: 'style_id',
},
n: {
param: 'n',
default: 1,
min: 1,
max: 2,
},
size: {
param: 'size',
default: '1024x1024',
},
response_format: {
param: 'response_format',
default: 'url',
},
controls: {
param: 'controls',
},
model: {
param: 'model',
},
artistic_level: {
param: 'artistic_level',
},
substyle: {
param: 'substyle',
},
};

export const RecraftAIImageGenerateResponseTransform: (
response: RecraftAIImageGenerateResponse | ErrorResponse,
responseStatus: number
) => ImageGenerateResponse | ErrorResponse = (response, responseStatus) => {
if (responseStatus !== 200 || 'error' in response) {
return RecraftAIErrorResponseTransform(
response as ErrorResponse,
RECRAFTAI
);
}
return response;
};

export const RecraftAIErrorResponseTransform: (
response: ErrorResponse,
provider: string
) => ErrorResponse = (response, provider) => {
return generateErrorResponse(
{
message: response.error?.message || 'Unknown error occurred',
type: response.error?.type || null,
param: response.error?.param || null,
code: response.error?.code || null,
},
provider
);
};
16 changes: 16 additions & 0 deletions src/providers/recraft-ai/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { ProviderConfigs } from '../types';
import RecraftAIAPIConfig from './api';
import {
RecraftAIImageGenerateConfig,
RecraftAIImageGenerateResponseTransform,
} from './imageGenerate';

const RecraftAIConfig: ProviderConfigs = {
imageGenerate: RecraftAIImageGenerateConfig,
api: RecraftAIAPIConfig,
responseTransforms: {
imageGenerate: RecraftAIImageGenerateResponseTransform,
},
};

export default RecraftAIConfig;
17 changes: 17 additions & 0 deletions src/providers/recraft-ai/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { ErrorResponse } from '../types';
import { generateErrorResponse } from '../utils';

export const RecraftAIErrorResponseTransform: (
response: ErrorResponse,
provider: string
) => ErrorResponse = (response, provider) => {
return generateErrorResponse(
{
message: response.error?.message || 'Unknown error occurred',
type: response.error?.type || null,
param: response.error?.param || null,
code: response.error?.code || null,
},
provider
);
};

0 comments on commit 7dcd668

Please sign in to comment.