Skip to content
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
47 changes: 47 additions & 0 deletions js/llm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { afterAll, afterEach, beforeAll, describe, expect, test } from "vitest";
import {
Battle,
buildClassificationTools,
ClosedQA,
DEFAULT_MODEL,
LLMClassifierFromTemplate,
OpenAIClassifier,
Expand Down Expand Up @@ -666,4 +667,50 @@ Issue Description: {{page_content}}
// Reset for other tests
init({ client });
});

test("llm classifier raises on refusal", async () => {
server.use(
http.post("https://api.openai.com/v1/chat/completions", () =>
HttpResponse.json({
id: "chatcmpl-test",
object: "chat.completion",
created: 1234567890,
model: "claude-haiku-5-5",
choices: [
{
index: 0,
message: {
role: "assistant",
content: null,
tool_calls: [
{
id: "call_test",
type: "function",
function: { name: "select_choice", arguments: "{}" },
},
],
},
finish_reason: "content_filter",
},
],
}),
),
);

init({
client: new OpenAI({
apiKey: "test-api-key",
baseURL: "https://api.openai.com/v1",
}),
});

await expect(
ClosedQA({
input: "Is 2+2 four?",
output: "Yes, 4.",
criteria: "The answer says 4.",
model: "claude-haiku-5-5",
}),
).rejects.toThrow("finish_reason=content_filter");
});
});
5 changes: 5 additions & 0 deletions js/llm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,11 @@ export async function OpenAIClassifier<RenderArgs, Output>(
);

if (resp.choices.length > 0) {
if (resp.choices[0].finish_reason === "content_filter") {
throw new Error(
"Model refused to respond (finish_reason=content_filter)",
);
}
return {
name,
...parseResponse(resp.choices[0].message!, choiceScores),
Expand Down
2 changes: 2 additions & 0 deletions py/autoevals/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,8 @@ def _process_response(self, resp):

def _postprocess_response(self, resp):
if len(resp["choices"]) > 0:
if resp["choices"][0].get("finish_reason") == "content_filter":
raise ValueError("Model refused to respond (finish_reason=content_filter)")
return self._process_response(resp["choices"][0]["message"])
else:
raise ValueError("Empty response from OpenAI")
Expand Down
34 changes: 34 additions & 0 deletions py/autoevals/test_llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from autoevals.llm import (
DEFAULT_MODEL,
Battle,
ClosedQA,
Factuality,
LLMClassifier,
OpenAILLMClassifier,
Expand Down Expand Up @@ -792,3 +793,36 @@ async def get_thread(self):
classifier.eval(output="x", expected="y", trace=trace)

assert trace.calls == 0


@respx.mock
def test_llm_classifier_raises_on_refusal():
respx.route(method="POST", path__regex=r".*/chat/completions$").respond(
json={
"id": "chatcmpl-test",
"object": "chat.completion",
"created": 1734029028,
"model": "claude-haiku-5-5",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": None,
"tool_calls": [
{
"id": "call_test",
"type": "function",
"function": {"name": "select_choice", "arguments": "{}"},
}
],
},
"finish_reason": "content_filter",
}
],
}
)

llm = ClosedQA(model="claude-haiku-5-5", client=OpenAI(api_key="test"))
with pytest.raises(ValueError, match="finish_reason=content_filter"):
llm.eval(input="Is 2+2 four?", output="Yes, 4.", criteria="The answer says 4.")