Skip to content

Commit

Permalink
ignore header description in ai response
Browse files Browse the repository at this point in the history
  • Loading branch information
koid committed Dec 20, 2023
1 parent 68c26b3 commit 16b61eb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pr_agent/algo/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,12 @@ def _fix_key_value(key: str, value: str):


def load_yaml(response_text: str) -> dict:
# remove everything before the first ```yaml
snipet_pattern = r'```(yaml)?[\s\S]*?```'
snipet = re.search(snipet_pattern, response_text)
if snipet:
response_text = snipet.group()

response_text = response_text.removeprefix('```yaml').rstrip('`')
try:
data = yaml.safe_load(response_text)
Expand Down
12 changes: 12 additions & 0 deletions tests/unittest/test_load_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ def test_load_valid_yaml(self):
expected_output = {'name': 'John Smith', 'age': 35}
assert load_yaml(yaml_str) == expected_output

def test_load_valid_yaml_with_description(self):
yaml_str = '''\
Here is the answer in YAML format:
```yaml
name: John Smith
age: 35
```
'''
expected_output = {'name': 'John Smith', 'age': 35}
assert load_yaml(yaml_str) == expected_output

def test_load_invalid_yaml1(self):
yaml_str = \
'''\
Expand Down

0 comments on commit 16b61eb

Please sign in to comment.