Skip to content

Commit 2fb9f6f

Browse files
authored
Merge pull request #1223 from Portkey-AI/chore/handle-content-array-in-pk-plugins
chore: support array content values in portkey plugin checks
2 parents a17cf29 + 12daab0 commit 2fb9f6f

File tree

3 files changed

+33
-12
lines changed

3 files changed

+33
-12
lines changed

plugins/portkey/gibberish.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
PluginHandler,
55
PluginParameters,
66
} from '../types';
7-
import { getText } from '../utils';
7+
import { getCurrentContentPart } from '../utils';
88
import { PORTKEY_ENDPOINTS, fetchPortkey } from './globals';
99

1010
export const handler: PluginHandler = async (
@@ -16,9 +16,17 @@ export const handler: PluginHandler = async (
1616
let error = null;
1717
let verdict = false;
1818
let data: any = null;
19-
19+
let text = '';
2020
try {
21-
const text = getText(context, eventType);
21+
const { content, textArray } = getCurrentContentPart(context, eventType);
22+
if (!content) {
23+
return {
24+
error: { message: 'request or response json is empty' },
25+
verdict: true,
26+
data: null,
27+
};
28+
}
29+
text = textArray.filter((text) => text).join('\n');
2230
const not = parameters.not || false;
2331

2432
const response: any = await fetchPortkey(
@@ -47,7 +55,6 @@ export const handler: PluginHandler = async (
4755
};
4856
} catch (e) {
4957
error = e as Error;
50-
const text = getText(context, eventType);
5158
data = {
5259
explanation: `An error occurred while checking for gibberish: ${error.message}`,
5360
not: parameters.not || false,

plugins/portkey/language.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
PluginHandler,
55
PluginParameters,
66
} from '../types';
7-
import { getText } from '../utils';
7+
import { getCurrentContentPart } from '../utils';
88
import { PORTKEY_ENDPOINTS, fetchPortkey } from './globals';
99

1010
export const handler: PluginHandler = async (
@@ -16,9 +16,17 @@ export const handler: PluginHandler = async (
1616
let error = null;
1717
let verdict = false;
1818
let data: any = null;
19-
19+
let text = '';
2020
try {
21-
const text = getText(context, eventType);
21+
const { content, textArray } = getCurrentContentPart(context, eventType);
22+
if (!content) {
23+
return {
24+
error: { message: 'request or response json is empty' },
25+
verdict: true,
26+
data: null,
27+
};
28+
}
29+
text = textArray.filter((text) => text).join('\n');
2230
const languages = parameters.language;
2331
const not = parameters.not || false;
2432

@@ -51,7 +59,6 @@ export const handler: PluginHandler = async (
5159
};
5260
} catch (e) {
5361
error = e as Error;
54-
const text = getText(context, eventType);
5562
data = {
5663
explanation: `An error occurred while checking language: ${error.message}`,
5764
not: parameters.not || false,

plugins/portkey/moderateContent.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
PluginHandler,
55
PluginParameters,
66
} from '../types';
7-
import { getText } from '../utils';
7+
import { getCurrentContentPart } from '../utils';
88
import { PORTKEY_ENDPOINTS, fetchPortkey } from './globals';
99

1010
export const handler: PluginHandler = async (
@@ -16,9 +16,17 @@ export const handler: PluginHandler = async (
1616
let error = null;
1717
let verdict = false;
1818
let data: any = null;
19-
19+
let text = '';
2020
try {
21-
const text = getText(context, eventType);
21+
const { content, textArray } = getCurrentContentPart(context, eventType);
22+
if (!content) {
23+
return {
24+
error: { message: 'request or response json is empty' },
25+
verdict: true,
26+
data: null,
27+
};
28+
}
29+
text = textArray.filter((text) => text).join('\n');
2230
const categories = parameters.categories;
2331
const not = parameters.not || false;
2432

@@ -59,7 +67,6 @@ export const handler: PluginHandler = async (
5967
};
6068
} catch (e) {
6169
error = e as Error;
62-
const text = getText(context, eventType);
6370
data = {
6471
explanation: `An error occurred during content moderation: ${error.message}`,
6572
not: parameters.not || false,

0 commit comments

Comments
 (0)