Skip to content

Commit

Permalink
Merge pull request #295 from sternelee/main
Browse files Browse the repository at this point in the history
[Provider] Openrouter, Lingyi, Moonshot and Zhipu
  • Loading branch information
VisargD authored Apr 30, 2024
2 parents e4f0520 + 41fe53d commit 8dd2602
Show file tree
Hide file tree
Showing 16 changed files with 810 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ export const SEGMIND: string = 'segmind';
export const JINA: string = 'jina';
export const FIREWORKS_AI: string = 'fireworks-ai';
export const WORKERS_AI: string = 'workers-ai';
export const MOONSHOT: string = 'moonshot';
export const OPENROUTER: string = 'openrouter';
export const LINGYI: string = 'lingyi';
export const ZHIPU: string = 'zhipu';

export const VALID_PROVIDERS = [
ANTHROPIC,
Expand All @@ -68,6 +72,10 @@ export const VALID_PROVIDERS = [
JINA,
FIREWORKS_AI,
WORKERS_AI,
MOONSHOT,
OPENROUTER,
LINGYI,
ZHIPU,
];

export const CONTENT_TYPES = {
Expand Down
8 changes: 8 additions & 0 deletions src/providers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ import SegmindConfig from './segmind';
import JinaConfig from './jina';
import FireworksAIConfig from './fireworks-ai';
import WorkersAiConfig from './workers-ai';
import MoonshotConfig from './moonshot';
import OpenrouterConfig from './openrouter';
import LingYiConfig from './lingyi';
import ZhipuConfig from './zhipu';

const Providers: { [key: string]: ProviderConfigs } = {
openai: OpenAIConfig,
Expand All @@ -45,6 +49,10 @@ const Providers: { [key: string]: ProviderConfigs } = {
jina: JinaConfig,
'fireworks-ai': FireworksAIConfig,
'workers-ai': WorkersAiConfig,
moonshot: MoonshotConfig,
openrouter: OpenrouterConfig,
lingyi: LingYiConfig,
zhipu: ZhipuConfig,
};

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

const LingYiAPIConfig: ProviderAPIConfig = {
getBaseURL: () => 'https://api.lingyiwanwu.com',
headers: ({ providerOptions }) => {
return { Authorization: `Bearer ${providerOptions.apiKey}` }; // https://platform.lingyiwanwu.com/apikeys
},
getEndpoint: ({ fn }) => {
switch (fn) {
case 'chatComplete':
return '/v1/chat/completions';
default:
return '';
}
},
};

export default LingYiAPIConfig;
148 changes: 148 additions & 0 deletions src/providers/lingyi/chatComplete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
import { LINGYI } from '../../globals';
import {
ChatCompletionResponse,
ErrorResponse,
ProviderConfig,
} from '../types';
import {
generateErrorResponse,
generateInvalidProviderResponseError,
} from '../utils';

export const LingyiChatCompleteConfig: ProviderConfig = {
model: {
param: 'model',
required: true,
default: 'yi-34b-chat-0205',
},
messages: {
param: 'messages',
default: '',
},
max_tokens: {
param: 'max_tokens',
default: 100,
min: 0,
},
temperature: {
param: 'temperature',
default: 1,
min: 0,
max: 2,
},
top_p: {
param: 'top_p',
default: 1,
min: 0,
max: 1,
},
stream: {
param: 'stream',
default: false,
},
};

interface LingyiChatCompleteResponse extends ChatCompletionResponse {
id: string;
object: string;
created: number;
model: string;
usage: {
prompt_tokens: number;
completion_tokens: number;
total_tokens: number;
};
}

export interface LingyiErrorResponse {
object: string;
message: string;
type: string;
param: string | null;
code: string;
}

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

export const LingyiChatCompleteResponseTransform: (
response: LingyiChatCompleteResponse | LingyiErrorResponse,
responseStatus: number
) => ChatCompletionResponse | ErrorResponse = (response, responseStatus) => {
if ('message' in response && responseStatus !== 200) {
return generateErrorResponse(
{
message: response.message,
type: response.type,
param: response.param,
code: response.code,
},
LINGYI
);
}

if ('choices' in response) {
return {
id: response.id,
object: response.object,
created: response.created,
model: response.model,
provider: LINGYI,
choices: response.choices.map((c) => ({
index: c.index,
message: {
role: c.message.role,
content: c.message.content,
},
finish_reason: c.finish_reason,
})),
usage: {
prompt_tokens: response.usage?.prompt_tokens,
completion_tokens: response.usage?.completion_tokens,
total_tokens: response.usage?.total_tokens,
},
};
}

return generateInvalidProviderResponseError(response, LINGYI);
};

export const LingyiChatCompleteStreamChunkTransform: (
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: LingyiStreamChunk = JSON.parse(chunk);
return (
`data: ${JSON.stringify({
id: parsedChunk.id,
object: parsedChunk.object,
created: parsedChunk.created,
model: parsedChunk.model,
provider: LINGYI,
choices: [
{
index: parsedChunk.choices[0].index,
delta: parsedChunk.choices[0].delta,
finish_reason: parsedChunk.choices[0].finish_reason,
},
],
})}` + '\n\n'
);
};
18 changes: 18 additions & 0 deletions src/providers/lingyi/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { ProviderConfigs } from '../types';
import LingyiAPIConfig from './api';
import {
LingyiChatCompleteConfig,
LingyiChatCompleteResponseTransform,
LingyiChatCompleteStreamChunkTransform,
} from './chatComplete';

const LingyiConfig: ProviderConfigs = {
chatComplete: LingyiChatCompleteConfig,
api: LingyiAPIConfig,
responseTransforms: {
chatComplete: LingyiChatCompleteResponseTransform,
'stream-chatComplete': LingyiChatCompleteStreamChunkTransform,
},
};

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

const MoonshotAPIConfig: ProviderAPIConfig = {
getBaseURL: () => 'https://api.moonshot.cn',
headers: ({ providerOptions }) => {
return { Authorization: `Bearer ${providerOptions.apiKey}` }; // https://platform.moonshot.cn/console/api-keys
},
getEndpoint: ({ fn }) => {
switch (fn) {
case 'chatComplete':
return '/v1/chat/completions';
default:
return '';
}
},
};

export default MoonshotAPIConfig;
149 changes: 149 additions & 0 deletions src/providers/moonshot/chatComplete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import { MOONSHOT } from '../../globals';

import {
ChatCompletionResponse,
ErrorResponse,
ProviderConfig,
} from '../types';
import {
generateErrorResponse,
generateInvalidProviderResponseError,
} from '../utils';

export const MoonshotChatCompleteConfig: ProviderConfig = {
model: {
param: 'model',
required: true,
default: 'moonshot-v1-8k',
},
messages: {
param: 'messages',
default: '',
},
max_tokens: {
param: 'max_tokens',
default: 100,
min: 0,
},
temperature: {
param: 'temperature',
default: 1,
min: 0,
max: 2,
},
top_p: {
param: 'top_p',
default: 1,
min: 0,
max: 1,
},
stream: {
param: 'stream',
default: false,
},
};

interface MoonshotChatCompleteResponse extends ChatCompletionResponse {
id: string;
object: string;
created: number;
model: 'moonshot-v1-8k' | 'moonshot-v1-32k' | 'moonshot-v1-128k';
usage: {
prompt_tokens: number;
completion_tokens: number;
total_tokens: number;
};
}

export interface MoonshotErrorResponse {
object: string;
message: string;
type: string;
param: string | null;
code: string;
}

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

export const MoonshotChatCompleteResponseTransform: (
response: MoonshotChatCompleteResponse | MoonshotErrorResponse,
responseStatus: number
) => ChatCompletionResponse | ErrorResponse = (response, responseStatus) => {
if ('message' in response && responseStatus !== 200) {
return generateErrorResponse(
{
message: response.message,
type: response.type,
param: response.param,
code: response.code,
},
MOONSHOT
);
}

if ('choices' in response) {
return {
id: response.id,
object: response.object,
created: response.created,
model: response.model,
provider: MOONSHOT,
choices: response.choices.map((c) => ({
index: c.index,
message: {
role: c.message.role,
content: c.message.content,
},
finish_reason: c.finish_reason,
})),
usage: {
prompt_tokens: response.usage?.prompt_tokens,
completion_tokens: response.usage?.completion_tokens,
total_tokens: response.usage?.total_tokens,
},
};
}

return generateInvalidProviderResponseError(response, MOONSHOT);
};

export const MoonshotChatCompleteStreamChunkTransform: (
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: MoonshotStreamChunk = JSON.parse(chunk);
return (
`data: ${JSON.stringify({
id: parsedChunk.id,
object: parsedChunk.object,
created: parsedChunk.created,
model: parsedChunk.model,
provider: MOONSHOT,
choices: [
{
index: parsedChunk.choices[0].index,
delta: parsedChunk.choices[0].delta,
finish_reason: parsedChunk.choices[0].finish_reason,
},
],
})}` + '\n\n'
);
};
Loading

0 comments on commit 8dd2602

Please sign in to comment.