Skip to content

Commit

Permalink
Merge pull request #1241 from expectedparrot/keep_method_and_prompt_f…
Browse files Browse the repository at this point in the history
…ail_safe

Keep method and prompt fail safe
  • Loading branch information
apostolosfilippas authored Nov 11, 2024
2 parents 3b3f9b0 + 7da5f2b commit 7df8374
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
12 changes: 8 additions & 4 deletions edsl/prompts/Prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,14 @@ def render(self, primary_replacement: dict, **additional_replacements) -> str:
>>> p.render({"person": "Mr. {{last_name}}"})
Prompt(text=\"""Hello, Mr. {{ last_name }}\""")
"""
new_text = self._render(
self.text, primary_replacement, **additional_replacements
)
return self.__class__(text=new_text)
try:
new_text = self._render(
self.text, primary_replacement, **additional_replacements
)
return self.__class__(text=new_text)
except Exception as e:
print(f"Error rendering prompt: {e}")
return self

@staticmethod
def _render(
Expand Down
1 change: 1 addition & 0 deletions edsl/scenarios/Scenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ def keep(self, list_of_keys: List[str]) -> "Scenario":
>>> s.keep(["food"])
Scenario({'food': 'wood chips'})
"""

return self.select(list_of_keys)

@classmethod
Expand Down
11 changes: 11 additions & 0 deletions edsl/scenarios/ScenarioList.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,17 @@ def drop(self, *fields) -> ScenarioList:
"""
return ScenarioList([scenario.drop(fields) for scenario in self.data])

def keep(self, *fields) -> ScenarioList:
"""Keep only the specified fields in the scenarios.
Example:
>>> s = ScenarioList([Scenario({'a': 1, 'b': 1}), Scenario({'a': 1, 'b': 2})])
>>> s.keep('a')
ScenarioList([Scenario({'a': 1}), Scenario({'a': 1})])
"""
return ScenarioList([scenario.keep(fields) for scenario in self.data])

@classmethod
def from_list(
cls, name: str, values: list, func: Optional[Callable] = None
Expand Down

0 comments on commit 7df8374

Please sign in to comment.