From c6365ca8ddb5fc5ea280f998875c075de662d722 Mon Sep 17 00:00:00 2001 From: Anthony Hu Date: Sun, 10 Sep 2023 23:39:34 +0800 Subject: [PATCH] add a flow to check repo's grammar and spelling --- .../standard/grammar-check/flow.dag.yaml | 39 +++++++++++++++++++ .../standard/grammar-check/generate_data.py | 32 +++++++++++++++ .../grammar-check/grammar_check.jinja2 | 18 +++++++++ .../grammar-check/input_passthrough.jinja2 | 1 + .../flows/standard/grammar-check/read_file.py | 19 +++++++++ 5 files changed, 109 insertions(+) create mode 100644 examples/flows/standard/grammar-check/flow.dag.yaml create mode 100644 examples/flows/standard/grammar-check/generate_data.py create mode 100644 examples/flows/standard/grammar-check/grammar_check.jinja2 create mode 100644 examples/flows/standard/grammar-check/input_passthrough.jinja2 create mode 100644 examples/flows/standard/grammar-check/read_file.py diff --git a/examples/flows/standard/grammar-check/flow.dag.yaml b/examples/flows/standard/grammar-check/flow.dag.yaml new file mode 100644 index 00000000000..c1e2b0945ed --- /dev/null +++ b/examples/flows/standard/grammar-check/flow.dag.yaml @@ -0,0 +1,39 @@ +id: template_standard_flow +name: Template Standard Flow +inputs: + path: + type: string +outputs: + output_python: + type: string + reference: ${input_passthrough.output} + output_check: + type: string + reference: ${grammer_check.output} +nodes: +- name: read_file + type: python + source: + type: code + path: read_file.py + inputs: + path: ${inputs.path} +- name: grammer_check + type: llm + source: + type: code + path: grammar_check.jinja2 + inputs: + max_tokens: 1000 + text: ${read_file.output} + deployment_name: gpt-35-turbo + model: gpt-35-turbo + connection: open_ai_connection + api: chat +- name: input_passthrough + type: prompt + source: + type: code + path: input_passthrough.jinja2 + inputs: + input: ${inputs.path} diff --git a/examples/flows/standard/grammar-check/generate_data.py b/examples/flows/standard/grammar-check/generate_data.py new file mode 100644 index 00000000000..7bd1b2bbded --- /dev/null +++ b/examples/flows/standard/grammar-check/generate_data.py @@ -0,0 +1,32 @@ +import os +import json + +def find_py_files(directory): + + sql_files = [] + + for root, _, files in os.walk(directory): + for file in files: + if file.endswith('.py'): + sql_files.append(os.path.abspath(os.path.join(root, file))) + + return sql_files + +def write_to_jsonl(files, output_file="data.jsonl"): + """ + Write list of file paths to a JSON lines file. + + Args: + - files (list): List of file paths. + - output_file (str): Name of the output JSON lines file. + """ + with open(output_file, 'w') as out: + for file_path in files: + line = json.dumps({"path": file_path}) + out.write(line + '\n') + +if __name__ == "__main__": + directory = "../../../" + sql_files = find_py_files(directory) + write_to_jsonl(sql_files) + print(f"Output written to data.jsonl") diff --git a/examples/flows/standard/grammar-check/grammar_check.jinja2 b/examples/flows/standard/grammar-check/grammar_check.jinja2 new file mode 100644 index 00000000000..3a24ae61aaf --- /dev/null +++ b/examples/flows/standard/grammar-check/grammar_check.jinja2 @@ -0,0 +1,18 @@ +system: +You are a python programmer. Please ready the python source doe. Check for any standard/error output to customer, whether the grammar and spelling are correct. If not, please correct them. +Please output your suggestion to correct the grammar and spelling. +Please output the found issues in below json format: +{ + "line": 1, + "column": 1, + "type": "spelling", + "message": "Please correct the spelling of 'python'." +} + +If there is no issue, please output: +{ + "message": "No issues found." +} + +user: +{{text}} \ No newline at end of file diff --git a/examples/flows/standard/grammar-check/input_passthrough.jinja2 b/examples/flows/standard/grammar-check/input_passthrough.jinja2 new file mode 100644 index 00000000000..2a19217667a --- /dev/null +++ b/examples/flows/standard/grammar-check/input_passthrough.jinja2 @@ -0,0 +1 @@ +{{input}} \ No newline at end of file diff --git a/examples/flows/standard/grammar-check/read_file.py b/examples/flows/standard/grammar-check/read_file.py new file mode 100644 index 00000000000..b520ce0c5c2 --- /dev/null +++ b/examples/flows/standard/grammar-check/read_file.py @@ -0,0 +1,19 @@ +from promptflow import tool +import time + +# The inputs section will change based on the arguments of the tool function, after you save the code +# Adding type to arguments and return value will help the system show the types properly +# Please update the function name/signature per need +@tool +def my_python_tool(path: str) -> str: + content = "" + try: + time.sleep(6) + with open(path, 'r', encoding='utf-16') as file: + content = file.read() + except Exception as e: + with open(path, 'r') as file: + content = file.read() + return content + +