-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #243 from SylphAI-Inc/main
[release][v0.2.5][doc][dataclass parser] bug fix + notebook
- Loading branch information
Showing
14 changed files
with
1,492 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Create a kernel | ||
|
||
```poetry run python -m ipykernel install --user --name my-project-kernel``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
[tool.poetry] | ||
name = "adalflow" | ||
|
||
version = "0.2.4" | ||
description = "The Library to Build and Auto-optimize Any LLM Task Pipeline" | ||
version = "0.2.5" | ||
description = "The Library to Build and Auto-optimize LLM Applications" | ||
authors = ["Li Yin <[email protected]>"] | ||
readme = "README.md" | ||
repository = "https://github.com/SylphAI-Inc/AdalFlow" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
import unittest | ||
from dataclasses import dataclass, field | ||
from typing import List | ||
from adalflow.core.base_data_class import DataClass | ||
from adalflow.components.output_parsers.dataclass_parser import DataClassParser | ||
|
||
|
||
# Define a basic DataClass for testing | ||
@dataclass | ||
class BasicOutput(DataClass): | ||
explanation: str = field( | ||
metadata={"desc": "A brief explanation of the concept in one sentence."} | ||
) | ||
example: str = field(metadata={"desc": "An example of the concept in a sentence."}) | ||
__output_fields__ = ["explanation", "example"] | ||
|
||
|
||
# Define a nested DataClass for testing | ||
@dataclass | ||
class NestedOutput(DataClass): | ||
title: str | ||
description: str | ||
items: List[str] | ||
__output_fields__ = ["title", "description", "items"] | ||
|
||
|
||
class TestDataClassParser(unittest.TestCase): | ||
|
||
def setUp(self): | ||
self.basic_data_class = BasicOutput | ||
self.nested_data_class = NestedOutput | ||
self.basic_parser = DataClassParser( | ||
data_class=self.basic_data_class, return_data_class=True, format_type="json" | ||
) | ||
self.nested_parser = DataClassParser( | ||
data_class=self.nested_data_class, | ||
return_data_class=True, | ||
format_type="yaml", | ||
) | ||
|
||
def test_basic_data_class_json(self): | ||
input_instance = BasicOutput( | ||
explanation="This is a test.", example="Example sentence." | ||
) | ||
input_str = self.basic_parser.get_input_str(input_instance) | ||
self.assertIn("This is a test.", input_str) | ||
self.assertIn("Example sentence.", input_str) | ||
|
||
output_format_str = self.basic_parser.get_output_format_str() | ||
self.assertIn("explanation", output_format_str) | ||
self.assertIn("example", output_format_str) | ||
|
||
output = self.basic_parser.call( | ||
'{"explanation": "Test explanation", "example": "Test example."}' | ||
) | ||
self.assertIsInstance(output, BasicOutput) | ||
|
||
def test_basic_data_class_yaml(self): | ||
self.yaml_parser = DataClassParser( | ||
data_class=self.basic_data_class, return_data_class=True, format_type="yaml" | ||
) | ||
input_instance = BasicOutput( | ||
explanation="This is a test.", example="Example sentence." | ||
) | ||
input_str = self.yaml_parser.get_input_str(input_instance) | ||
self.assertIn("This is a test.", input_str) | ||
|
||
self.assertIn("Example sentence.", input_str) | ||
|
||
output_format_str = self.yaml_parser.get_output_format_str() | ||
self.assertIn("explanation", output_format_str) | ||
self.assertIn("example", output_format_str) | ||
|
||
output = self.yaml_parser.call( | ||
"""explanation: Test explanation | ||
example: Test example.""" | ||
) | ||
print(f"output: {output}") | ||
self.assertIsInstance(output, BasicOutput) | ||
|
||
def test_nested_data_class_json(self): | ||
input_instance = NestedOutput( | ||
title="Title", description="Description", items=["Item 1", "Item 2"] | ||
) | ||
input_str = self.nested_parser.get_input_str(input_instance) | ||
self.assertIn("Title", input_str) | ||
self.assertIn("Description", input_str) | ||
self.assertIn("Item 1", input_str) | ||
self.assertIn("Item 2", input_str) | ||
|
||
output_format_str = self.nested_parser.get_output_format_str() | ||
self.assertIn("title", output_format_str) | ||
self.assertIn("description", output_format_str) | ||
self.assertIn("items", output_format_str) | ||
|
||
output = self.nested_parser.call( | ||
"""title: Nested Title | ||
description: Nested description | ||
items: | ||
- Item 1 | ||
- Item 2""" | ||
) | ||
self.assertIsInstance(output, NestedOutput) | ||
|
||
def test_nested_data_class_yaml(self): | ||
self.nested_parser._format_type = "yaml" | ||
input_instance = NestedOutput( | ||
title="Title", description="Description", items=["Item 1", "Item 2"] | ||
) | ||
input_str = self.nested_parser.get_input_str(input_instance) | ||
self.assertIn("Title", input_str) | ||
self.assertIn("Description", input_str) | ||
self.assertIn("Item 1", input_str) | ||
self.assertIn("Item 2", input_str) | ||
|
||
output_format_str = self.nested_parser.get_output_format_str() | ||
self.assertIn("title", output_format_str) | ||
self.assertIn("description", output_format_str) | ||
self.assertIn("items", output_format_str) | ||
|
||
output = self.nested_parser.call( | ||
"""title: Nested Title | ||
description: Nested description | ||
items: | ||
- Item 1 | ||
- Item 2""" | ||
) | ||
self.assertIsInstance(output, NestedOutput) | ||
|
||
def test_invalid_data_class(self): | ||
with self.assertRaises(ValueError): | ||
DataClassParser(data_class=dict) # dict is not a dataclass | ||
|
||
def test_invalid_format_type(self): | ||
with self.assertRaises(ValueError): | ||
DataClassParser( | ||
data_class=self.basic_data_class, format_type="xml" | ||
) # Invalid format type | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.