Skip to content

Commit 9c41df7

Browse files
authored
Merge branch 'main' into docs
2 parents 333a99b + 9436fde commit 9c41df7

31 files changed

+16556
-40
lines changed

.github/README.cn.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<img src="/docs/images/gateway-border.png" width=350>
33

44
<p align="right">
5-
<a href="./README.md">English</a>|<strong>中文</strong>
5+
<a href="../README.md">English</a> | <strong>中文</strong> | <a href="./README.jp.md">日本語</a>
66
</p>
77

88
# AI Gateway
@@ -213,6 +213,7 @@ const client = new OpenAI({
213213
-[Discord](https://portkey.ai/community) 上与我们实时交流
214214
-[Twitter](https://twitter.com/PortkeyAI) 上关注我们
215215
-[LinkedIn](https://www.linkedin.com/company/portkey-ai/) 上与我们建立联系
216+
- 阅读日文版文档 [日本語](./README.jp.md)
216217

217218
<!-- - 在 [YouTube](https://www.youtube.com/channel/UCZph50gLNXAh1DpmeX8sBdw) 上访问我们 --> <!-- - 加入我们的 [Dev 社区](https://dev.to/portkeyai) --> <!-- - 在 [Stack Overflow](https://stackoverflow.com/questions/tagged/portkey) 上查看标记为 #portkey 的问题 -->
218219

.github/README.jp.md

Lines changed: 297 additions & 0 deletions
Large diffs are not rendered by default.

.github/workflows/run_tests.yml

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
1-
name: Run Gateway tests
1+
name: Gateway Tests
22

33
on:
4-
pull_request:
5-
types: [opened, synchronize, reopened]
4+
issue_comment:
5+
types: [created]
66

77
jobs:
88
gateway-tests:
9+
if: ${{ github.event.issue.pull_request && contains(github.event.comment.body, 'run tests') }}
910
runs-on: ubuntu-latest
1011
environment: production
1112
steps:
1213
- name: Checkout head
1314
uses: actions/checkout@v4
1415
with:
1516
fetch-depth: 0
17+
1618
- name: Install dependencies
1719
run: npm ci
20+
1821
- name: Build
1922
run: npm run build
23+
2024
- name: Start gateway and run tests
25+
id: run-tests
26+
continue-on-error: true
2127
run: |
2228
npm run build/start-server.js &
2329
echo "Waiting for gateway to start..."
@@ -27,6 +33,43 @@ jobs:
2733
echo "Gateway is ready. Running tests..."
2834
npm run test:gateway
2935
36+
- name: Update PR Check
37+
uses: actions/github-script@v6
38+
with:
39+
github-token: ${{secrets.GITHUB_TOKEN}}
40+
script: |
41+
const { owner, repo } = context.repo;
42+
const issue_number = context.issue.number;
43+
44+
try {
45+
const { data: pull_request } = await github.rest.pulls.get({
46+
owner,
47+
repo,
48+
pull_number: issue_number,
49+
});
50+
51+
const sha = pull_request.head.sha;
52+
53+
await github.rest.checks.create({
54+
owner,
55+
repo,
56+
name: 'Gateway Tests (Comment Triggered)',
57+
head_sha: sha,
58+
status: 'completed',
59+
conclusion: '${{ steps.run-tests.outcome }}',
60+
output: {
61+
title: 'Gateway Test Results',
62+
summary: 'Gateway tests have completed.',
63+
text: 'These tests were triggered by a comment on the PR.'
64+
},
65+
});
66+
67+
console.log('Check run created successfully');
68+
} catch (error) {
69+
console.error('Error creating check run:', error);
70+
core.setFailed(`Action failed with error: ${error}`);
71+
}
72+
3073
env:
3174
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
3275
COHERE_API_KEY: ${{ secrets.COHERE_API_KEY }}

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<div align="center">
22

33
<p align="right">
4-
<strong>English</strong> | <a href="./.github/README.cn.md">中文</a>
4+
<strong>English</strong> | <a href="./.github/README.cn.md">中文</a> | <a href="./.github/README.jp.md">日本語</a>
55
</p>
66

77

@@ -289,6 +289,7 @@ Join our growing community around the world, for help, ideas, and discussions on
289289
- Chat with us on [Discord](https://portkey.ai/community)
290290
- Follow us on [Twitter](https://twitter.com/PortkeyAI)
291291
- Connect with us on [LinkedIn](https://www.linkedin.com/company/portkey-ai/)
292+
- Read the documentation in [Japanese](./.github/README.jp.md)
292293
<!-- - Visit us on [YouTube](https://www.youtube.com/channel/UCZph50gLNXAh1DpmeX8sBdw) -->
293294
<!-- - Join our [Dev community](https://dev.to/portkeyai) -->
294295
<!-- - Questions tagged #portkey on [Stack Overflow](https://stackoverflow.com/questions/tagged/portkey) -->

package-lock.json

Lines changed: 43 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@portkey-ai/gateway",
3-
"version": "1.8.0",
3+
"version": "1.8.1",
44
"description": "A fast AI gateway by Portkey",
55
"repository": {
66
"type": "git",
@@ -42,11 +42,13 @@
4242
"dependencies": {
4343
"@aws-crypto/sha256-js": "^5.2.0",
4444
"@hono/node-server": "^1.3.3",
45+
"@hono/node-ws": "^1.0.4",
4546
"@portkey-ai/mustache": "^2.1.2",
4647
"@smithy/signature-v4": "^2.1.1",
4748
"@types/mustache": "^4.2.5",
4849
"async-retry": "^1.3.3",
49-
"hono": "^3.12.0",
50+
"hono": "^4.6.10",
51+
"ws": "^8.18.0",
5052
"zod": "^3.22.4"
5153
},
5254
"devDependencies": {
@@ -57,6 +59,7 @@
5759
"@types/async-retry": "^1.4.5",
5860
"@types/jest": "^29.5.12",
5961
"@types/node": "20.8.3",
62+
"@types/ws": "^8.5.12",
6063
"husky": "^9.1.4",
6164
"jest": "^29.7.0",
6265
"prettier": "3.2.5",

plugins/default/default.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { handler as logHandler } from './log';
1010
import { handler as allUppercaseHandler } from './alluppercase';
1111
import { handler as endsWithHandler } from './endsWith';
1212
import { handler as allLowerCaseHandler } from './alllowercase';
13+
import { handler as modelWhitelistHandler } from './modelWhitelist';
1314

1415
import { z } from 'zod';
1516
import { PluginContext, PluginParameters } from '../types';
@@ -802,3 +803,36 @@ describe('allLowercase handler', () => {
802803
expect(result.verdict).toBe(false);
803804
});
804805
});
806+
807+
describe('modelWhitelist handler', () => {
808+
it('should return true verdict when the model requested is part of the whitelist', async () => {
809+
const context: PluginContext = {
810+
request: { json: { model: 'gemini-1.5-flash-001' } },
811+
};
812+
813+
const parameters: PluginParameters = {
814+
models: ['gemini-1.5-flash-001'],
815+
};
816+
const eventType = 'beforeRequestHook';
817+
818+
const result = await modelWhitelistHandler(context, parameters, eventType);
819+
820+
expect(result.error).toBe(null);
821+
expect(result.verdict).toBe(true);
822+
});
823+
it('should return false verdict when the model requested is not part of the whitelist', async () => {
824+
const context: PluginContext = {
825+
request: { json: { model: 'gemini-1.5-pro-001' } },
826+
};
827+
828+
const parameters: PluginParameters = {
829+
models: ['gemini-1.5-flash-001'],
830+
};
831+
const eventType = 'beforeRequestHook';
832+
833+
const result = await modelWhitelistHandler(context, parameters, eventType);
834+
835+
expect(result.error).toBe(null);
836+
expect(result.verdict).toBe(false);
837+
});
838+
});

plugins/default/manifest.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,37 @@
475475
}
476476
],
477477
"parameters": {}
478+
},
479+
{
480+
"name": "Model whitelisting",
481+
"id": "modelwhitelist",
482+
"type": "guardrail",
483+
"supportedHooks": ["beforeRequestHook"],
484+
"description": [
485+
{
486+
"type": "subHeading",
487+
"text": "Check if the model in the request is part of the allowed model list."
488+
}
489+
],
490+
"parameters": {
491+
"type": "object",
492+
"properties": {
493+
"models": {
494+
"type": "array",
495+
"label": "Model list",
496+
"description": [
497+
{
498+
"type": "subHeading",
499+
"text": "Enter the allowed models."
500+
}
501+
],
502+
"items": {
503+
"type": "string"
504+
}
505+
}
506+
},
507+
"required": ["models"]
508+
}
478509
}
479510
]
480511
}

plugins/default/modelWhitelist.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import {
2+
HookEventType,
3+
PluginContext,
4+
PluginHandler,
5+
PluginParameters,
6+
} from '../types';
7+
8+
export const handler: PluginHandler = async (
9+
context: PluginContext,
10+
parameters: PluginParameters,
11+
eventType: HookEventType
12+
) => {
13+
let error = null;
14+
let verdict = false;
15+
16+
try {
17+
const modelList = parameters.models;
18+
let requestModel = context.request?.json.model;
19+
verdict = modelList.includes(requestModel);
20+
} catch (e) {
21+
error = e as Error;
22+
}
23+
24+
return { error, verdict };
25+
};

plugins/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { handler as defaultcontainsCode } from './default/containsCode';
1212
import { handler as defaultalluppercase } from './default/alluppercase';
1313
import { handler as defaultalllowercase } from './default/alllowercase';
1414
import { handler as defaultendsWith } from './default/endsWith';
15+
import { handler as defaultmodelWhitelist } from './default/modelWhitelist';
1516
import { handler as portkeymoderateContent } from './portkey/moderateContent';
1617
import { handler as portkeylanguage } from './portkey/language';
1718
import { handler as portkeypii } from './portkey/pii';
@@ -48,6 +49,7 @@ export const plugins = {
4849
alluppercase: defaultalluppercase,
4950
alllowercase: defaultalllowercase,
5051
endsWith: defaultendsWith,
52+
modelWhitelist: defaultmodelWhitelist,
5153
},
5254
portkey: {
5355
moderateContent: portkeymoderateContent,

0 commit comments

Comments
 (0)