A vanilla loss function to evaluate a response. In particular, this module is used to evaluate any given text object. :param evaluation_instruction: The evaluation instruction variable. :type evaluation_instruction: Variable :param engine: The EngineLM objec
(self,
eval_system_prompt: Union[Variable, str],
engine: Union[EngineLM, str] = None)
| 8 | |
| 9 | class TextLoss(Module): |
| 10 | def __init__(self, |
| 11 | eval_system_prompt: Union[Variable, str], |
| 12 | engine: Union[EngineLM, str] = None): |
| 13 | """ |
| 14 | A vanilla loss function to evaluate a response. |
| 15 | In particular, this module is used to evaluate any given text object. |
| 16 | |
| 17 | :param evaluation_instruction: The evaluation instruction variable. |
| 18 | :type evaluation_instruction: Variable |
| 19 | :param engine: The EngineLM object. |
| 20 | :type engine: EngineLM |
| 21 | |
| 22 | :example: |
| 23 | >>> from textgrad import get_engine, Variable |
| 24 | >>> from textgrad.loss import TextLoss |
| 25 | >>> engine = get_engine("gpt-4o") |
| 26 | >>> evaluation_instruction = Variable("Is ths a good joke?", requires_grad=False) |
| 27 | >>> response_evaluator = TextLoss(evaluation_instruction, engine) |
| 28 | >>> response = Variable("What did the fish say when it hit the wall? Dam.", requires_grad=True) |
| 29 | >>> response_evaluator(response) |
| 30 | """ |
| 31 | super().__init__() |
| 32 | if isinstance(eval_system_prompt, str): |
| 33 | eval_system_prompt = Variable(eval_system_prompt, requires_grad=False, role_description="system prompt for the evaluation") |
| 34 | self.eval_system_prompt = eval_system_prompt |
| 35 | if ((engine is None) and (SingletonBackwardEngine().get_engine() is None)): |
| 36 | raise Exception("No engine provided. Either provide an engine as the argument to this call, or use `textgrad.set_backward_engine(engine)` to set the backward engine.") |
| 37 | elif engine is None: |
| 38 | engine = SingletonBackwardEngine().get_engine() |
| 39 | if isinstance(engine, str): |
| 40 | engine = get_engine(engine) |
| 41 | self.engine = engine |
| 42 | self.llm_call = LLMCall(self.engine, self.eval_system_prompt) |
| 43 | |
| 44 | def forward(self, instance: Variable): |
| 45 | """ |
no test coverage detected