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

add Lemonfoxai #628

Merged
merged 7 commits into from
Oct 8, 2024
Merged
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
2 changes: 2 additions & 0 deletions src/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export const SILICONFLOW: string = 'siliconflow';
export const CEREBRAS: string = 'cerebras';
export const INFERENCENET: string = 'inference-net';
export const SAMBANOVA: string = 'sambanova';
export const LEMONFOX_AI: string = 'lemonfox-ai';
export const UPSTAGE: string = 'upstage';

export const VALID_PROVIDERS = [
Expand Down Expand Up @@ -113,6 +114,7 @@ export const VALID_PROVIDERS = [
CEREBRAS,
INFERENCENET,
SAMBANOVA,
LEMONFOX_AI,
UPSTAGE,
];

Expand Down
2 changes: 2 additions & 0 deletions src/providers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import HuggingfaceConfig from './huggingface';
import { cerebrasProviderAPIConfig } from './cerebras';
import { InferenceNetProviderConfigs } from './inference-net';
import SambaNovaConfig from './sambanova';
import LemonfoxAIConfig from './lemonfox-ai';
import { UpstageConfig } from './upstage';

const Providers: { [key: string]: ProviderConfigs } = {
Expand Down Expand Up @@ -86,6 +87,7 @@ const Providers: { [key: string]: ProviderConfigs } = {
cerebras: cerebrasProviderAPIConfig,
'inference-net': InferenceNetProviderConfigs,
sambanova: SambaNovaConfig,
'lemonfox-ai': LemonfoxAIConfig,
upstage: UpstageConfig,
};

Expand Down
27 changes: 27 additions & 0 deletions src/providers/lemonfox-ai/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { ProviderAPIConfig } from '../types';

const LemonfoxAIAPIConfig: ProviderAPIConfig = {
getBaseURL: () => 'https://api.lemonfox.ai/v1',
headers: ({ providerOptions, fn }) => {
const headersObj: Record<string, string> = {
Authorization: `Bearer ${providerOptions.apiKey}`,
};
if (fn === 'createTranscription')
headersObj['content-type'] = 'multipart/form-data';
return headersObj;
},
getEndpoint: ({ fn }) => {
switch (fn) {
case 'chatComplete':
return '/chat/completions';
case 'imageGenerate':
return '/images/generations';
case 'createTranscription':
return '/audio/transcriptions';
default:
return '';
}
},
};

export default LemonfoxAIAPIConfig;
44 changes: 44 additions & 0 deletions src/providers/lemonfox-ai/chatComplete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { LEMONFOX_AI } from '../../globals';

interface LemonfoxAIStreamChunk {
id: string;
object: string;
created: number;
model: string;
choices: {
delta: {
role?: string | null;
content?: string;
};
index: number;
finish_reason: string | null;
}[];
}

export const LemonfoxAIChatCompleteStreamChunkTransform: (
response: string
) => string = (responseChunk) => {
let chunk = responseChunk.trim();
chunk = chunk.replace(/^data: /, '');
chunk = chunk.trim();
if (chunk === '[DONE]') {
return `data: ${chunk}\n\n`;
}
const parsedChunk: LemonfoxAIStreamChunk = JSON.parse(chunk);
return (
`data: ${JSON.stringify({
id: parsedChunk.id,
object: parsedChunk.object,
created: parsedChunk.created,
model: parsedChunk.model,
provider: LEMONFOX_AI,
choices: [
{
index: parsedChunk.choices[0].index,
delta: parsedChunk.choices[0].delta,
finish_reason: parsedChunk.choices[0].finish_reason,
},
],
})}` + '\n\n'
);
};
34 changes: 34 additions & 0 deletions src/providers/lemonfox-ai/createTranscription.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { LEMONFOX_AI } from '../../globals';

import { generateInvalidProviderResponseError } from '../utils';

import { ErrorResponse, ProviderConfig } from '../types';

export const LemonfoxAIcreateTranscriptionConfig: ProviderConfig = {
file: {
param: 'file',
required: true,
},
response_format: {
param: 'response_format',
},
prompt: {
param: 'prompt',
},
language: {
param: 'language',
},
translate: {
param: 'translate',
},
};

export const LemonfoxAICreateTranscriptionResponseTransform: (
response: Response | ErrorResponse,
responseStatus: number
) => Response | ErrorResponse = (response, responseStatus) => {
if (responseStatus !== 200 && 'error' in response) {
return generateInvalidProviderResponseError(response, LEMONFOX_AI);
}
return response;
};
58 changes: 58 additions & 0 deletions src/providers/lemonfox-ai/imageGenerate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { LEMONFOX_AI } from '../../globals';
import { ErrorResponse, ImageGenerateResponse, ProviderConfig } from '../types';

import {
generateErrorResponse,
generateInvalidProviderResponseError,
} from '../utils';

export const LemonfoxAIImageGenerateConfig: ProviderConfig = {
prompt: {
param: 'prompt',
required: true,
},
negative_prompt: {
param: 'negative_prompt',
},
n: {
param: 'n',
max: 8,
min: 1,
default: 1,
},
response_format: {
param: 'response_format',
default: 'url',
},
size: {
param: 'size',
},
};

interface LemonfoxAIImageObject {
b64_json?: string; // The base64-encoded JSON of the generated image, if response_format is b64_json.
url?: string; // The URL of the generated image, if response_format is url (default).
}

interface LemonfoxAIImageGenerateResponse extends ImageGenerateResponse {
data: LemonfoxAIImageObject[];
}

export const LemonfoxImageGenerateResponseTransform: (
response: LemonfoxAIImageGenerateResponse | ErrorResponse,
responseStatus: number
) => ImageGenerateResponse | ErrorResponse = (response, responseStatus) => {
if (responseStatus !== 200 && 'error' in response) {
return generateErrorResponse(
{
message: response['error'].message,
type: response['error'].type,
param: null,
code: null,
},
LEMONFOX_AI
);
}

return response;
};
67 changes: 67 additions & 0 deletions src/providers/lemonfox-ai/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { ProviderConfigs } from '../types';
import LemonfoxAIAPIConfig from './api';
import { chatCompleteParams, responseTransformers } from '../open-ai-base';
import { LemonfoxAIChatCompleteStreamChunkTransform } from './chatComplete';
import {
LemonfoxAIImageGenerateConfig,
LemonfoxImageGenerateResponseTransform,
} from './imageGenerate';

import {
LemonfoxAICreateTranscriptionResponseTransform,
LemonfoxAIcreateTranscriptionConfig,
} from './createTranscription';
import { LEMONFOX_AI } from '../../globals';

const LemonfoxAIConfig: ProviderConfigs = {
chatComplete: chatCompleteParams(
[
'functions',
'function_call',
'n',
'logit_bias',
'user',
'seed',
'tools',
'tool_choice',
'response_format',
'logprobs',
'stream_options',
],
{
model: 'zephyr-chat',
}
),
imageGenerate: LemonfoxAIImageGenerateConfig,
createTranscription: LemonfoxAIcreateTranscriptionConfig,
api: LemonfoxAIAPIConfig,
responseTransforms: {
...responseTransformers(LEMONFOX_AI, {
chatComplete: (response, isError) => {
if (isError || 'choices' in response === false) return response;

return {
...response,
provider: LEMONFOX_AI,
choices: response.choices.map((choice) => ({
...choice,
message: {
role: 'assistant',
...(choice.message as any),
},
})),
usage: {
prompt_tokens: response.usage?.prompt_tokens || 0,
completion_tokens: response.usage?.completion_tokens || 0,
total_tokens: response.usage?.total_tokens || 0,
},
};
},
}),
'stream-chatComplete': LemonfoxAIChatCompleteStreamChunkTransform,
imageGenerate: LemonfoxImageGenerateResponseTransform,
createTranscription: LemonfoxAICreateTranscriptionResponseTransform,
},
};

export default LemonfoxAIConfig;
Loading