Skip to content

Commit

Permalink
Merge pull request #3 from jld-adriano/cli
Browse files Browse the repository at this point in the history
Add cli interface
  • Loading branch information
areibman authored Apr 2, 2024
2 parents 23e3ba8 + 211ac84 commit f78f1cd
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 9 deletions.
66 changes: 66 additions & 0 deletions jaiqu/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import json
import sys

from typer import Option, Typer

from .jaiqu import translate_schema

typer_app = Typer()


@typer_app.command()
def jaiqu(
schema_file: str = Option(..., "-s", "--schema", help="Json schema file path"),
data_file: str = Option(
None,
"-d",
"--data",
help="Json data file path. if not passed will try to read from stdin",
),
quiet: bool = Option(False, "-q", "--quiet", help="Quiet mode, only print errors"),
key_hints: str = Option(
None,
"-k",
"--key-hints",
help="Extra prompt for the ai to help it complete the task",
),
max_retries: int = Option(
10,
"-r",
"--max-retries",
help="Max number of retries for the ai to complete the task",
),
):
"""
Validate and translate a json schema to jq filter
"""
with open(schema_file) as f:
output_schema = json.load(f)
if data_file is None:
if sys.stdin.isatty():
sys.exit("Error: No data piped to stdin.")
else:
if not quiet:
print("--data not provided, reading from stdin")
data_file = sys.stdin.read()
input_json = json.loads(data_file)
else:
with open(data_file) as f:
input_json = json.load(f)

query = translate_schema(
output_schema=output_schema,
input_json=input_json,
key_hints=key_hints,
max_retries=max_retries,
quiet=quiet
)
print(query)


def main():
typer_app()


if __name__ == "__main__":
main()
12 changes: 6 additions & 6 deletions jaiqu/jaiqu.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from .helpers import identify_key, create_jq_string, repair_query, dict_to_jq_filter


def validate_schema(input_json: dict, output_schema: dict, openai_api_key: str | None = None, key_hints=None) -> tuple[dict, bool]:
def validate_schema(input_json: dict, output_schema: dict, openai_api_key: str | None = None, key_hints=None, quiet=False) -> tuple[dict, bool]:
"""Validates the schema of the input JSON against the output schema.
Args:
input_json (dict): The input JSON parsed into a dictionary.
Expand All @@ -20,7 +20,7 @@ def validate_schema(input_json: dict, output_schema: dict, openai_api_key: str |

results = {}
valid = True
with tqdm(total=len(output_schema['properties']), desc="Validating schema") as pbar:
with tqdm(total=len(output_schema['properties']), desc="Validating schema", disable=quiet) as pbar:
for key, value in output_schema['properties'].items():
pbar.set_postfix_str(f"Key: {key}", refresh=True)
response_key, response_reasoning = identify_key(key, value, input_json, openai_api_key, key_hints)
Expand All @@ -44,7 +44,7 @@ def validate_schema(input_json: dict, output_schema: dict, openai_api_key: str |
return results, valid


def translate_schema(input_json, output_schema, openai_api_key: str | None = None, key_hints=None, max_retries=10) -> str:
def translate_schema(input_json, output_schema, openai_api_key: str | None = None, key_hints=None, max_retries=10, quiet=False) -> str:
"""
Translate the input JSON schema into a filtering query using jq.
Expand All @@ -64,7 +64,7 @@ def translate_schema(input_json, output_schema, openai_api_key: str | None = Non
RuntimeError: If failed to validate the jq filter after maximum retries.
"""

schema_properties, is_valid = validate_schema(input_json, output_schema, key_hints=key_hints, openai_api_key=openai_api_key)
schema_properties, is_valid = validate_schema(input_json, output_schema, key_hints=key_hints, openai_api_key=openai_api_key, quiet=quiet)
if not is_valid:
raise RuntimeError(
f"The input JSON does not contain the required data to satisfy the output schema: \n\n{json.dumps(schema_properties, indent=2)}")
Expand All @@ -73,7 +73,7 @@ def translate_schema(input_json, output_schema, openai_api_key: str | None = Non

filter_query = {}

with tqdm(total=len(filtered_schema), desc="Translating schema") as pbar, tqdm(total=max_retries, desc="Retry attempts") as pbar_retries:
with tqdm(total=len(filtered_schema), desc="Translating schema", disable=quiet) as pbar, tqdm(total=max_retries, desc="Retry attempts", disable=quiet) as pbar_retries:
for key, value in filtered_schema.items():
pbar.set_postfix_str(f"Key: {key}", refresh=True)
jq_string = create_jq_string(input_json, key, value, openai_api_key)
Expand Down Expand Up @@ -101,7 +101,7 @@ def translate_schema(input_json, output_schema, openai_api_key: str | None = Non
complete_filter = dict_to_jq_filter(filter_query)
# Validate JSON
tries = 0
with tqdm(total=max_retries, desc="Validation attempts") as pbar_validation:
with tqdm(total=max_retries, desc="Validation attempts", disable=quiet) as pbar_validation:
while True:
try:
result = jq.compile(complete_filter).input(input_json).all()[0]
Expand Down
11 changes: 9 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "jaiqu"
version = "0.0.4"
version = "0.0.5"
authors = [
{ name = "Alex Reibman", email = "[email protected]" },
{ name = "Howard Gil", email = "[email protected]" },
Expand All @@ -21,7 +21,8 @@ classifiers = [
dependencies = [
"jq==1.6.0",
"openai~=1.12.0",
"jsonschema==4.21.1"
"jsonschema==4.21.1",
"typer==0.9.0",
]

[project.optional-dependencies]
Expand All @@ -34,3 +35,9 @@ dev = [
[project.urls]
Homepage = "https://github.com/AgentOps-AI/Jaiqu"
Issues = "https://github.com/AgentOps-AI/Jaiqu/issues"

[project.entry-points.console_scripts]
jaiqu = "jaiqu.cli:main"

[tool.setuptools]
packages = { find = { where = ["."], exclude = ["samples"] } }
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
jq==1.6.0
openai>=1.12.0,<2.0.0
jsonschema==4.21.1
jsonschema==4.21.1
typer==0.9.0
11 changes: 11 additions & 0 deletions samples/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"call.id": "123",
"datetime": "2022-01-01",
"timestamp": 1640995200,
"Address": "123 Main St",
"user": {
"name": "John Doe",
"age": 30,
"contact": "[email protected]"
}
}
22 changes: 22 additions & 0 deletions samples/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"id": {
"type": ["string", "null"],
"description": "A unique identifier for the record."
},
"date": {
"type": "string",
"description": "A string describing the date."
},
"model": {
"type": "string",
"description": "A text field representing the model used."
}
},
"required": [
"id",
"date"
]
}

0 comments on commit f78f1cd

Please sign in to comment.