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

Handle non-number from language model #95

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions javascript-sdk/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ export default class RebuffSdk implements Rebuff {
}
const threshold = tacticOverride?.threshold ?? tactic.defaultThreshold;
const execution = await tactic.execute(userInput, threshold);
if (isNaN(execution.score)) {
throw new RebuffError(`Tactic ${tactic.name} returned NaN`);
}
const result = {
name: tactic.name,
score: execution.score,
Expand Down
7 changes: 5 additions & 2 deletions javascript-sdk/src/tactics/OpenAI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ export default class OpenAI implements Tactic {
throw new Error("completion.data.choices[0].message is undefined");
}

// FIXME: Handle when parseFloat returns NaN.
const score = parseFloat(completion.data.choices[0].message.content || "");
let score = parseFloat(completion.data.choices[0].message.content || "");
if (isNaN(score)) {
// If the model doesn't return a number, assume an attacker is manipulating the Rebuff prompt.
score = 1;
}
return { score };
} catch (error) {
console.error("Error in callOpenAiToDetectPI:", error);
Expand Down
6 changes: 5 additions & 1 deletion python-sdk/rebuff/sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ def detect_injection(
rendered_input, self.openai_model, self.openai_apikey
)

rebuff_model_score = float(model_response.get("completion", 0))
try:
rebuff_model_score = float(model_response.get("completion", 0))
except ValueError:
# If the model doesn't return a number, assume an attacker is manipulating the Rebuff prompt.
rebuff_model_score = 1

else:
rebuff_model_score = 0
Expand Down
Loading