Skip to content

Commit

Permalink
📝 (_data_extraction.py): remove unnecessary blank line
Browse files Browse the repository at this point in the history
♻️ (_data_extraction.py): refactor code to improve readability and maintainability
📝 (safer_python_exec.py): add import statement for black module
♻️ (safer_python_exec.py): refactor code to use black module for code formatting
♻️ (data_from_text.py): refactor code to improve readability and maintainability
♻️ (entities_from_text.py): refactor code to improve readability and maintainability
♻️ (helpers.py): refactor code to improve readability and maintainability
♻️ (datagraph_from_pydantic.py): refactor code to use safer_exec instead of kindofsafe_exec
♻️ (datagraph_from_pydantic.py): refactor code to use format_code function for code formatting
  • Loading branch information
closedLoop committed Apr 8, 2024
1 parent 7412e3b commit a92d8fd
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 14 deletions.
2 changes: 0 additions & 2 deletions promptedgraphs/_data_extraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"""



def create_messages(text, name, labels, custom_system_message=None):
label_list = list(labels.keys())

Expand Down Expand Up @@ -127,4 +126,3 @@ def create_functions(is_parent_list, schema, fn_name: str = None):
},
}
]

6 changes: 6 additions & 0 deletions promptedgraphs/code_execution/safer_python_exec.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import ast
from logging import getLogger

import black

logger = getLogger(__name__)


Expand Down Expand Up @@ -54,6 +56,10 @@ def is_potentially_unsafe(node): # sourcery skip: low-code-quality
return False


def format_code(code):
return black.format_str(code, mode=black.FileMode())


def safer_exec(model_code):
logger.warning("Executing generated data model code from datamodel_code_generator")
exec_variables = {}
Expand Down
4 changes: 3 additions & 1 deletion promptedgraphs/extraction/data_from_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,9 @@ class UserIntent(BaseModel):
load_config()

msg = """How can I learn more about your product?"""
async for intent in data_from_text(text=msg, output_type=UserIntent, config=Config()):
async for intent in data_from_text(
text=msg, output_type=UserIntent, config=Config()
):
print(intent)


Expand Down
14 changes: 9 additions & 5 deletions promptedgraphs/extraction/entities_from_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ async def entities_from_text(
):
label_list = sorted(labels.keys())

LabelEnums = enum.Enum('LabelEnums', {l:i for i,l in enumerate(label_list)})
LabelEnums = enum.Enum("LabelEnums", {l: i for i, l in enumerate(label_list)})

class EntityReference(BaseModel):
"""The raw text and label for each entity occurrence"""

text_span: str = Field(
title="Text Span",
description="The exact text of referenced entity.",
Expand All @@ -47,12 +48,16 @@ class EntityReference(BaseModel):
title="Reason",
description="A short description of why that label was selected.",
)

EntityReference.__doc__ += """\n
Labels must be one of the following: {label_list}""".format(label_list=label_list + ["==NONE=="])
Labels must be one of the following: {label_list}""".format(
label_list=label_list + ["==NONE=="]
)

EntityReference.__doc__ += """\n
Label Definitions:\n""" + "\n".join([f" * {label}: {labels[label]}" for label in label_list])

Label Definitions:\n""" + "\n".join(
[f" * {label}: {labels[label]}" for label in label_list]
)

async for er in data_from_text(
text=text,
Expand All @@ -64,7 +69,6 @@ class EntityReference(BaseModel):
yield er



async def example():
load_config()

Expand Down
2 changes: 1 addition & 1 deletion promptedgraphs/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ def deep_merge(d1: dict[str, any], d2: dict[str, any]):
d1[k] += str(v)



def add_space_before_capital(text):
return re.sub(r"(?<=[a-z])(?=[A-Z])", " ", text)

Expand All @@ -35,6 +34,7 @@ def format_fieldinfo(key, v: dict):
l += f"\n\texamples: {v.get('examples')}"
return l.rstrip()


def remove_nas(data: dict[str, any], nulls: list[str] = None):
nulls = nulls or ["==NA==", "NA", "N/A", "n/a", "#N/A", "None", "none"]
for k, v in data.items():
Expand Down
12 changes: 7 additions & 5 deletions promptedgraphs/sources/datagraph_from_pydantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import tqdm
from pydantic import BaseModel, Field

from promptedgraphs.code_execution.safer_python_exec import format_code
from promptedgraphs.llms.coding import fix_code

logger = logging.getLogger(__name__)
Expand All @@ -34,6 +35,7 @@ def kindofsafe_exec(code, dependencies: dict = None):
): # Loading in code by chunks helps with dependency resolution
dependencies.update(variables)
exec(snippet, dependencies, variables)
DeprecationWarning("This is a deprecated function: use safer_exec")
return variables


Expand Down Expand Up @@ -201,10 +203,10 @@ def aggregate_python_files(fdir):
code = []
for fname in fdir.glob("*.py"):
with open(fname) as f:
c = black.format_str(f.read(), mode=black.FileMode())
c = format_code(f.read())
code.append(c)
try:
black.format_str("\n\n".join(code), mode=black.FileMode())
format_code("\n\n".join(code))
except Exception:
logger.error(f"Static code error in: {fname}")
return
Expand All @@ -215,7 +217,7 @@ def aggregate_python_files(fdir):
code = isort.code(code, config=config)

# reformat one more time
code = black.format_str(code, mode=black.FileMode())
code = format_code(code)

with open(fdir / "_all.py", "w") as f:
f.write(code)
Expand Down Expand Up @@ -248,11 +250,11 @@ async def validate_python_files(fdir):
history = []
i = 0
while i < 4:
code = black.format_str(code, mode=black.FileMode())
code = format_code(code)
kindofsafe_exec(code)
# break
try:
code = black.format_str(code, mode=black.FileMode())
code = format_code(code)
kindofsafe_exec(code)
break
except Exception as e:
Expand Down

0 comments on commit a92d8fd

Please sign in to comment.