Skip to content

Commit

Permalink
Merge branch 'main' into feature/betterdocker
Browse files Browse the repository at this point in the history
  • Loading branch information
VisargD authored Dec 26, 2024
2 parents 0957e0b + f429049 commit 2296940
Show file tree
Hide file tree
Showing 61 changed files with 2,169 additions and 1,847 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@portkey-ai/gateway",
"version": "1.8.5",
"version": "1.8.6",
"description": "A fast AI gateway by Portkey",
"repository": {
"type": "git",
Expand All @@ -20,7 +20,6 @@
"README.md",
"SECURITY.md",
"LICENSE",
"docs",
"build/public"
],
"scripts": {
Expand Down
6 changes: 3 additions & 3 deletions plugins/patronus/custom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export const handler: PluginHandler = async (
let verdict = false;
let data = null;

const evaluator = 'custom';
const profile = parameters.profile;
const evaluator = 'judge';
const criteria = parameters.criteria;

if (eventType !== 'afterRequestHook') {
return {
Expand All @@ -39,7 +39,7 @@ export const handler: PluginHandler = async (
evaluator,
parameters.credentials,
evaluationBody,
profile
criteria
);

const evalResult = result.results[0];
Expand Down
4 changes: 2 additions & 2 deletions plugins/patronus/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const postPatronus = async (
evaluator: string,
credentials: any,
data: any,
profile: string | null = null
criteria: string | null = null
) => {
const options = {
headers: {
Expand All @@ -19,7 +19,7 @@ export const postPatronus = async (
{
evaluator: evaluator,
explain_strategy: 'always',
...(profile && { profile_name: profile }),
...(criteria && { criteria: criteria }),
},
],
evaluated_model_input: data.input,
Expand Down
6 changes: 3 additions & 3 deletions plugins/patronus/isConcise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export const handler: PluginHandler = async (
let verdict = false;
let data = null;

const evaluator = 'custom';
const profile = 'system:is-concise';
const evaluator = 'judge';
const criteria = 'patronus:is-concise';

if (eventType !== 'afterRequestHook') {
return {
Expand All @@ -39,7 +39,7 @@ export const handler: PluginHandler = async (
evaluator,
parameters.credentials,
evaluationBody,
profile
criteria
);

const evalResult = result.results[0];
Expand Down
6 changes: 3 additions & 3 deletions plugins/patronus/isHelpful.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export const handler: PluginHandler = async (
let verdict = false;
let data = null;

const evaluator = 'custom';
const profile = 'system:is-helpful';
const evaluator = 'judge';
const criteria = 'patronus:is-helpful';

if (eventType !== 'afterRequestHook') {
return {
Expand All @@ -39,7 +39,7 @@ export const handler: PluginHandler = async (
evaluator,
parameters.credentials,
evaluationBody,
profile
criteria
);

const evalResult = result.results[0];
Expand Down
6 changes: 3 additions & 3 deletions plugins/patronus/isPolite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export const handler: PluginHandler = async (
let verdict = false;
let data = null;

const evaluator = 'custom';
const profile = 'system:is-polite';
const evaluator = 'judge';
const criteria = 'patronus:is-polite';

if (eventType !== 'afterRequestHook') {
return {
Expand All @@ -39,7 +39,7 @@ export const handler: PluginHandler = async (
evaluator,
parameters.credentials,
evaluationBody,
profile
criteria
);

const evalResult = result.results[0];
Expand Down
6 changes: 3 additions & 3 deletions plugins/patronus/noApologies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export const handler: PluginHandler = async (
let verdict = false;
let data = null;

const evaluator = 'custom';
const profile = 'system:no-apologies';
const evaluator = 'judge';
const criteria = 'patronus:no-apologies';

if (eventType !== 'afterRequestHook') {
return {
Expand All @@ -39,7 +39,7 @@ export const handler: PluginHandler = async (
evaluator,
parameters.credentials,
evaluationBody,
profile
criteria
);

const evalResult = result.results[0];
Expand Down
6 changes: 3 additions & 3 deletions plugins/patronus/noGenderBias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export const handler: PluginHandler = async (
let verdict = false;
let data = null;

const evaluator = 'custom';
const profile = 'system:no-gender-bias';
const evaluator = 'judge';
const criteria = 'patronus:no-gender-bias';

if (eventType !== 'afterRequestHook') {
return {
Expand All @@ -39,7 +39,7 @@ export const handler: PluginHandler = async (
evaluator,
parameters.credentials,
evaluationBody,
profile
criteria
);

const evalResult = result.results[0];
Expand Down
6 changes: 3 additions & 3 deletions plugins/patronus/noRacialBias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export const handler: PluginHandler = async (
let verdict = false;
let data = null;

const evaluator = 'custom';
const profile = 'system:no-racial-bias';
const evaluator = 'judge';
const criteria = 'patronus:no-racial-bias';

if (eventType !== 'afterRequestHook') {
return {
Expand All @@ -39,7 +39,7 @@ export const handler: PluginHandler = async (
evaluator,
parameters.credentials,
evaluationBody,
profile
criteria
);

const evalResult = result.results[0];
Expand Down
10 changes: 8 additions & 2 deletions plugins/patronus/patronus.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,10 @@ describe('custom handler (is-concise)', () => {
},
};

const parameters = { credentials: testCreds, profile: 'system:is-concise' };
const parameters = {
credentials: testCreds,
criteria: 'patronus:is-concise',
};

const result = await customHandler(context, parameters, eventType);
console.log(result);
Expand All @@ -256,7 +259,10 @@ describe('custom handler (is-concise)', () => {
},
};

const parameters = { credentials: testCreds, profile: 'system:is-concise' };
const parameters = {
credentials: testCreds,
criteria: 'patronus:is-concise',
};

const result = await customHandler(context, parameters, eventType);
console.log(result);
Expand Down
2 changes: 1 addition & 1 deletion plugins/patronus/retrievalAnswerRelevance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const handler: PluginHandler = async (
let verdict = false;
let data = null;

const evaluator = 'retrieval-answer-relevance';
const evaluator = 'answer-relevance';

if (eventType !== 'afterRequestHook') {
return {
Expand Down
4 changes: 3 additions & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ export default {
format: 'es',
},
plugins: [
typescript({ exclude: ['**/*.test.ts', 'start-test.js', 'cookbook'] }),
typescript({
exclude: ['**/*.test.ts', 'start-test.js', 'cookbook', 'docs'],
}),
terser(),
json(),
copy({
Expand Down
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
12 changes: 9 additions & 3 deletions src/providers/ai21/chatComplete.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AI21 } from '../../globals';
import { Params } from '../../types/requestBody';
import { Params, SYSTEM_MESSAGE_ROLES } from '../../types/requestBody';
import {
ChatCompletionResponse,
ErrorResponse,
Expand All @@ -19,7 +19,10 @@ export const AI21ChatCompleteConfig: ProviderConfig = {
transform: (params: Params) => {
let inputMessages: any = [];

if (params.messages?.[0]?.role === 'system') {
if (
params.messages?.[0]?.role &&
SYSTEM_MESSAGE_ROLES.includes(params.messages?.[0]?.role)
) {
inputMessages = params.messages.slice(1);
} else if (params.messages) {
inputMessages = params.messages;
Expand All @@ -35,7 +38,10 @@ export const AI21ChatCompleteConfig: ProviderConfig = {
param: 'system',
required: false,
transform: (params: Params) => {
if (params.messages?.[0].role === 'system') {
if (
params.messages?.[0]?.role &&
SYSTEM_MESSAGE_ROLES.includes(params.messages?.[0]?.role)
) {
return params.messages?.[0].content;
}
},
Expand Down
7 changes: 4 additions & 3 deletions src/providers/anthropic/chatComplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
Message,
ContentType,
AnthropicPromptCache,
SYSTEM_MESSAGE_ROLES,
} from '../../types/requestBody';
import {
ChatCompletionResponse,
Expand Down Expand Up @@ -111,7 +112,7 @@ export const AnthropicChatCompleteConfig: ProviderConfig = {
// Transform the chat messages into a simple prompt
if (!!params.messages) {
params.messages.forEach((msg: Message & AnthropicPromptCache) => {
if (msg.role === 'system') return;
if (SYSTEM_MESSAGE_ROLES.includes(msg.role)) return;

if (msg.role === 'assistant') {
messages.push(transformAssistantMessage(msg));
Expand Down Expand Up @@ -188,7 +189,7 @@ export const AnthropicChatCompleteConfig: ProviderConfig = {
if (!!params.messages) {
params.messages.forEach((msg: Message & AnthropicPromptCache) => {
if (
msg.role === 'system' &&
SYSTEM_MESSAGE_ROLES.includes(msg.role) &&
msg.content &&
typeof msg.content === 'object' &&
msg.content[0].text
Expand All @@ -203,7 +204,7 @@ export const AnthropicChatCompleteConfig: ProviderConfig = {
});
});
} else if (
msg.role === 'system' &&
SYSTEM_MESSAGE_ROLES.includes(msg.role) &&
typeof msg.content === 'string'
) {
systemMessages.push({
Expand Down
7 changes: 7 additions & 0 deletions src/providers/anyscale/chatComplete.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ANYSCALE } from '../../globals';
import { Params } from '../../types/requestBody';
import {
ChatCompletionResponse,
ErrorResponse,
Expand All @@ -20,6 +21,12 @@ export const AnyscaleChatCompleteConfig: ProviderConfig = {
messages: {
param: 'messages',
default: '',
transform: (params: Params) => {
return params.messages?.map((message) => {
if (message.role === 'developer') return { ...message, role: 'system' };
return message;
});
},
},
functions: {
param: 'functions',
Expand Down
7 changes: 7 additions & 0 deletions src/providers/azure-ai-inference/chatComplete.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Params } from '../../types/requestBody';
import { OpenAIErrorResponseTransform } from '../openai/utils';
import {
ChatCompletionResponse,
Expand All @@ -14,6 +15,12 @@ export const AzureAIInferenceChatCompleteConfig: ProviderConfig = {
messages: {
param: 'messages',
default: '',
transform: (params: Params) => {
return params.messages?.map((message) => {
if (message.role === 'developer') return { ...message, role: 'system' };
return message;
});
},
},
max_tokens: {
param: 'max_tokens',
Expand Down
15 changes: 10 additions & 5 deletions src/providers/bedrock/chatComplete.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { BEDROCK, documentMimeTypes, imagesMimeTypes } from '../../globals';
import { Message, Params, ToolCall } from '../../types/requestBody';
import {
Message,
Params,
ToolCall,
SYSTEM_MESSAGE_ROLES,
} from '../../types/requestBody';
import {
ChatCompletionResponse,
ErrorResponse,
Expand Down Expand Up @@ -150,7 +155,7 @@ export const BedrockConverseChatCompleteConfig: ProviderConfig = {
transform: (params: BedrockChatCompletionsParams) => {
if (!params.messages) return [];
const transformedMessages = params.messages
.filter((msg) => msg.role !== 'system')
.filter((msg) => !SYSTEM_MESSAGE_ROLES.includes(msg.role))
.map((msg) => {
return {
role: msg.role === 'assistant' ? 'assistant' : 'user',
Expand Down Expand Up @@ -183,7 +188,7 @@ export const BedrockConverseChatCompleteConfig: ProviderConfig = {
if (!params.messages) return;
const systemMessages = params.messages.reduce(
(acc: { text: string }[], msg) => {
if (msg.role === 'system')
if (SYSTEM_MESSAGE_ROLES.includes(msg.role))
return acc.concat(...getMessageTextContentArray(msg));
return acc;
},
Expand Down Expand Up @@ -603,7 +608,7 @@ export const BedrockCohereChatCompleteConfig: ProviderConfig = {
if (!!params.messages) {
let messages: Message[] = params.messages;
messages.forEach((msg, index) => {
if (index === 0 && msg.role === 'system') {
if (index === 0 && SYSTEM_MESSAGE_ROLES.includes(msg.role)) {
prompt += `system: ${messages}\n`;
} else if (msg.role == 'user') {
prompt += `user: ${msg.content}\n`;
Expand Down Expand Up @@ -787,7 +792,7 @@ export const BedrockAI21ChatCompleteConfig: ProviderConfig = {
if (!!params.messages) {
let messages: Message[] = params.messages;
messages.forEach((msg, index) => {
if (index === 0 && msg.role === 'system') {
if (index === 0 && SYSTEM_MESSAGE_ROLES.includes(msg.role)) {
prompt += `system: ${messages}\n`;
} else if (msg.role == 'user') {
prompt += `user: ${msg.content}\n`;
Expand Down
Loading

0 comments on commit 2296940

Please sign in to comment.