summaryrefslogtreecommitdiff
path: root/code_eval/OpenCodeEval/backend/base.py
diff options
context:
space:
mode:
authorYuren Hao <yurenh2@timan108.cs.illinois.edu>2025-09-04 22:16:22 -0500
committerYuren Hao <yurenh2@timan108.cs.illinois.edu>2025-09-04 22:16:22 -0500
commitfc6d57ffb8d5ddb5820fcc00b5491a585c259ebc (patch)
treee9841f93a353e2107225cfc721d1ce57c0e594dc /code_eval/OpenCodeEval/backend/base.py
Initial commit
Diffstat (limited to 'code_eval/OpenCodeEval/backend/base.py')
-rw-r--r--code_eval/OpenCodeEval/backend/base.py55
1 files changed, 55 insertions, 0 deletions
diff --git a/code_eval/OpenCodeEval/backend/base.py b/code_eval/OpenCodeEval/backend/base.py
new file mode 100644
index 0000000..42091b9
--- /dev/null
+++ b/code_eval/OpenCodeEval/backend/base.py
@@ -0,0 +1,55 @@
+from typing import Callable
+from abc import ABC, abstractmethod
+
+def make_chat_template(
+ prompt: str,
+ response_prefix: str = "",
+ is_chat: bool = True,
+ tokenizer: Callable = None
+ ) -> str:
+
+ if is_chat:
+ prompt = tokenizer.apply_chat_template(
+ [
+ {"role": "user", "content": prompt},
+ ],
+ tokenize = False,
+ add_generation_prompt = True
+ ) + response_prefix
+ if tokenizer.bos_token and prompt.startswith(tokenizer.bos_token):
+ prompt = prompt[len(tokenizer.bos_token):]
+ return prompt
+ else:
+ return prompt
+
+class Generator(ABC):
+
+ model_name: str = None
+
+ def __init__(self, model_name: str) -> None:
+ """
+ :param stop_words: list
+ list of stop words if the generation uses a stopping criteria during generation
+ :param requires_execution: bool
+ wheter the task requires code execution during evaluation or not
+ """
+ self.model_name = model_name
+
+ def fewshot_examples(self):
+ """Loads and returns the few-shot examples for the task if they exist."""
+ pass
+
+ @abstractmethod
+ def set_stop(self):
+ """
+ Set the stop tokens for the model
+ """
+ pass
+
+ @abstractmethod
+ def generate(self):
+ """Builds the prompt for the LM to generate from.
+ :param doc: dict[str: str]
+ sample from the test dataset
+ """
+ pass \ No newline at end of file