mirror of
https://github.com/kennethreitz/instructor.git
synced 2026-06-05 22:50:18 +00:00
Upgrade to pydanticv2 (#63)
* Upgrade to Pydantic 2 #15 * update examples to use pydantic2 --------- Co-authored-by: Mike Harris <mharris717@gmail.com>
This commit is contained in:
@@ -1,45 +1,3 @@
|
||||
"""
|
||||
This script parses a string representation of a filesystem structure into a tree-like directory structure.
|
||||
|
||||
The 'Node' class represents a node in this tree, which can be either a file or a folder. Files cannot have
|
||||
children, while folders can.
|
||||
|
||||
The 'DirectoryTree' class contains a single root folder from which all other files/folders can be reached.
|
||||
The 'parse_tree_to_filesystem' function uses OpenAI's GPT-3 model to convert a string representation of a
|
||||
directory tree into a 'DirectoryTree' object. This object can then be manipulated programmatically as needed,
|
||||
with methods such as 'print_paths' available for convenience.
|
||||
|
||||
Please note: Recursive models currently work if they are wrapped by a non-recursive one. This is why we are
|
||||
passing a 'DirectoryTree' (which contains a single 'Node') as the function call, not a 'Node' directly. This
|
||||
is due to a limitation in how Pydantic generates schemas for recursive objects, which creates
|
||||
'dict_keys(['$ref', 'definitions'])'. Instead of writing a resolver for such references, we can simply wrap the
|
||||
recursive class in a non-recursive one so the function_call class never has a cyclic reference.
|
||||
|
||||
Example usage:
|
||||
>>> root = parse_tree_to_filesystem(
|
||||
... '''
|
||||
... root
|
||||
... ├── folder1
|
||||
... │ ├── file1.txt
|
||||
... │ └── file2.txt
|
||||
... └── folder2
|
||||
... ├── file3.txt
|
||||
... └── subfolder1
|
||||
... └── file4.txt
|
||||
... '''
|
||||
... )
|
||||
>>> root.print_paths()
|
||||
# Expected output:
|
||||
# >>> root NodeType.FOLDER
|
||||
# >>> root/folder1 NodeType.FOLDER
|
||||
# >>> root/folder1/file1.txt NodeType.FILE
|
||||
# >>> root/folder1/file2.txt NodeType.FILE
|
||||
# >>> root/folder2 NodeType.FOLDER
|
||||
# >>> root/folder2/file3.txt NodeType.FILE
|
||||
# >>> root/folder2/subfolder1 NodeType.FOLDER
|
||||
# >>> root/folder2/subfolder1/file4.txt NodeType.FILE
|
||||
"""
|
||||
|
||||
import enum
|
||||
from typing import List
|
||||
|
||||
@@ -115,8 +73,8 @@ class DirectoryTree(OpenAISchema):
|
||||
self.root.print_paths()
|
||||
|
||||
|
||||
Node.update_forward_refs()
|
||||
DirectoryTree.update_forward_refs()
|
||||
Node.model_rebuild()
|
||||
DirectoryTree.model_rebuild()
|
||||
|
||||
|
||||
@retry(stop=stop_after_attempt(3))
|
||||
|
||||
Reference in New Issue
Block a user