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

Andrew/json validator #679

Merged
merged 3 commits into from
Apr 14, 2023
Merged
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
48 changes: 48 additions & 0 deletions evals/elsuite/basic/json_validator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import json
from typing import Any

import numpy as np

import evals
import evals.record
from evals.api import CompletionFn


def is_valid_json(s):
try:
json.loads(s)
return True
except ValueError:
return False


class JsonValidator(evals.Eval):
def __init__(
self,
completion_fns: list[CompletionFn],
samples_jsonl: str,
*args,
max_tokens: int = 500,
**kwargs,
):
super().__init__(completion_fns, *args, **kwargs)
assert len(completion_fns) == 1, "JsonValidator only supports one completion fn"
self.max_tokens = max_tokens
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks unused?

self.samples_jsonl = samples_jsonl

def eval_sample(self, sample: Any, *_):
prompt = sample["input"]
result = self.completion_fn(
prompt=prompt,
temperature=0.0,
)
sampled = result.get_completions()[0]
return evals.record.record_metrics(
is_valid_json=is_valid_json(sampled),
)

def run(self, recorder):
samples = self.get_samples()
self.eval_all_samples(recorder, samples)
metrics = recorder.get_metrics()
return {"num_valid_json": np.mean([m["is_valid_json"] for m in metrics])}