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

Feature improve yaml parsing in llmanswer #127

Merged
merged 4 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
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
13 changes: 12 additions & 1 deletion council/llm/llm_answer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations
import inspect
from typing import Any, Dict, Optional
from typing import Any, Dict, List, Optional

import yaml

Expand Down Expand Up @@ -127,6 +127,17 @@ def parse_yaml(self, bloc: str) -> Dict[str, Any]:
raise LLMParsingException(f"Missing {missing_keys} in response.")
return properties_dict

def parse_yaml_list(self, bloc: str) -> List[Dict[str, Any]]:
result = []
d = yaml.safe_load(bloc)
for item in d:
properties_dict = {**item}
missing_keys = [key.name for key in self._properties if key.name not in properties_dict.keys()]
if len(missing_keys) > 0:
raise LLMParsingException(f"Missing {missing_keys} in response.")
result.append(properties_dict)
return result

def parse_yaml_bloc(self, bloc: str) -> Dict[str, Any]:
code_bloc = CodeParser.find_first(language="yaml", text=bloc)
if code_bloc is not None:
Expand Down
2 changes: 1 addition & 1 deletion council/utils/code_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def find_last(language: Optional[str] = None, text: str = "") -> Optional[CodeBl

@staticmethod
def _get_pattern(language: Optional[str]):
return r"```(\w*)\n(.*?)\n```" if language is None else rf"```({language})\n(.*?)\n```"
return r"```(\w*) *\n(.*?)\n```" if language is None else rf"```({language})\n(.*?)\n```"

@staticmethod
def _build_generator(language: Optional[str], text: str = "") -> Iterable[CodeBlock]:
Expand Down
30 changes: 22 additions & 8 deletions tests/unit/llm/test_llm_answer.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,25 @@ def test_llm_parse_yaml_answer(self):
llma = LLMAnswer(Specialist)

bloc = """
```yaml
ControllerScore:
name: first
score: 10
instructions: do this
justification: because
```"""
print(llma.parse_yaml_bloc(bloc))
```yaml
name: first
score: 10
instructions: do this
justification: because
```
"""
result = llma.parse_yaml_bloc(bloc)
instance = Specialist(**result)
self.assertEqual(instance.name, "first")

def test_parse_dict(self):
instance = LLMAnswer(Specialist)
bloc = """
- name: first
score: 10
instructions:
- do
- this
justification: because
"""
print(instance.parse_yaml_list(bloc))
Loading