Skip to content

Commit

Permalink
Merge pull request #10 from airtai/8-save-generated-plan-into-a-yaml-…
Browse files Browse the repository at this point in the history
…file-and-use-it-for-application-code-creation

Save generated plan into a JSON file
  • Loading branch information
harishmohanraj authored Aug 9, 2023
2 parents b9716d0 + 6440163 commit 5603271
Show file tree
Hide file tree
Showing 7 changed files with 213 additions and 40 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,7 @@ mkdocs/site/

# nbdev_mkdocs
mkdocs/docs/
mkdocs/site/
mkdocs/site/

# fastkafka-gen
fastkafka-gen/
24 changes: 20 additions & 4 deletions fastkafka_gen/_code_generator/plan_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from typing import *
import time
import json
from pathlib import Path

from yaspin import yaspin

Expand Down Expand Up @@ -255,14 +256,27 @@ def _validate_response(response: str) -> List[str]:
return ["JSON decoding failed. Please send JSON response only."]

# %% ../../nbs/Plan_Generator.ipynb 44
def generate_plan(description: str) -> Tuple[str, str]:
def _save_plan_as_json(contents: str, output_file: str) -> None:
"""Save the contents as JSON to the specified output file.
Args:
contents: A JSON-formatted string to be saved as a JSON file.
output_file: The path to the output file where the JSON content will be saved.
"""
Path(output_file).parent.mkdir(parents=True, exist_ok=True)
with open(output_file, "w", encoding="utf-8") as f:
json.dump(json.loads(contents), f, indent=4)

# %% ../../nbs/Plan_Generator.ipynb 46
def generate_plan(description: str, output_file: str) -> str:
"""Generate a plan from user's application description
Args:
description: Validated User application description
output_path: The path to the output file where the generated plan JSON will be saved.
Returns:
The plan generated by OpenAI as a dictionary
The total token used to generate the plan
"""
with yaspin(
text="Generating plan", # (slowest step, usually takes 30 to 90 seconds)...
Expand All @@ -273,6 +287,8 @@ def generate_plan(description: str) -> Tuple[str, str]:
plan_validator = ValidateAndFixResponse(plan_generator, _validate_response)
validated_plan, total_tokens = plan_validator.fix(description)

_save_plan_as_json(validated_plan, output_file)

sp.text = ""
sp.ok(" ✔ Plan generated")
return validated_plan, total_tokens
sp.ok(f" ✔ Plan generated and saved at: {output_file}")
return total_tokens
2 changes: 2 additions & 0 deletions fastkafka_gen/_modidx.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
'fastkafka_gen/_code_generator/helper.py')},
'fastkafka_gen._code_generator.plan_generator': { 'fastkafka_gen._code_generator.plan_generator._get_error_msgs_and_expected_keys': ( 'plan_generator.html#_get_error_msgs_and_expected_keys',
'fastkafka_gen/_code_generator/plan_generator.py'),
'fastkafka_gen._code_generator.plan_generator._save_plan_as_json': ( 'plan_generator.html#_save_plan_as_json',
'fastkafka_gen/_code_generator/plan_generator.py'),
'fastkafka_gen._code_generator.plan_generator._vaidate_plan': ( 'plan_generator.html#_vaidate_plan',
'fastkafka_gen/_code_generator/plan_generator.py'),
'fastkafka_gen._code_generator.plan_generator._validate_apps': ( 'plan_generator.html#_validate_apps',
Expand Down
12 changes: 10 additions & 2 deletions fastkafka_gen/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,26 @@ def generate_fastkafka_app(
\n"""
),
output_path: str = typer.Option(
"./fastkafka-gen",
"--output_path",
"-o",
help="Path to the output directory where generated files will be saved. This path should be relative to the current working directory.",
),
) -> None:
"""Generate a new FastKafka app(s) effortlessly with advanced AI assistance"""
try:
_ensure_openai_api_key_set()
validated_description, description_token = validate_app_description(description)
# validated_plan, plan_token = generate_plan(validated_description)

output_file = f"{output_path}/plan.json"
plan_token = generate_plan(validated_description, output_file)
# code = generate_app(validated_plan, validated_description)
# test = generate_test(code)

# total_token_usage = description_token + plan_token
# typer.secho(f" ▶ Total tokens usage: {total_token_usage}", fg=typer.colors.CYAN)
typer.secho("✨ All files were successfully generated.!", fg=typer.colors.CYAN)
typer.secho("✨ All files were successfully generated!", fg=typer.colors.CYAN)

except (ValueError, KeyError) as e:
typer.secho(e, err=True, fg=typer.colors.RED)
Expand Down
12 changes: 10 additions & 2 deletions nbs/CLI.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -226,18 +226,26 @@
"\n",
"\\n\"\"\"\n",
" ),\n",
" output_path: str = typer.Option(\n",
" \"./fastkafka-gen\",\n",
" \"--output_path\",\n",
" \"-o\",\n",
" help=\"Path to the output directory where generated files will be saved. This path should be relative to the current working directory.\",\n",
" ),\n",
") -> None:\n",
" \"\"\"Generate a new FastKafka app(s) effortlessly with advanced AI assistance\"\"\"\n",
" try:\n",
" _ensure_openai_api_key_set()\n",
" validated_description, description_token = validate_app_description(description)\n",
"# validated_plan, plan_token = generate_plan(validated_description)\n",
" \n",
" output_file = f\"{output_path}/plan.json\"\n",
" plan_token = generate_plan(validated_description, output_file)\n",
"# code = generate_app(validated_plan, validated_description)\n",
"# test = generate_test(code)\n",
" \n",
"# total_token_usage = description_token + plan_token\n",
"# typer.secho(f\" ▶ Total tokens usage: {total_token_usage}\", fg=typer.colors.CYAN)\n",
" typer.secho(\"✨ All files were successfully generated.!\", fg=typer.colors.CYAN)\n",
" typer.secho(\"✨ All files were successfully generated!\", fg=typer.colors.CYAN)\n",
" \n",
" except (ValueError, KeyError) as e:\n",
" typer.secho(e, err=True, fg=typer.colors.RED)\n",
Expand Down
42 changes: 39 additions & 3 deletions nbs/Helper.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,26 @@
"execution_count": null,
"id": "7a39d512",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"some prompt\n",
"\n",
"==== RESPONSE WITH ISSUES ====\n",
"\n",
"some response\n",
"\n",
"Read the contents of ==== RESPONSE WITH ISSUES ==== section and fix the below mentioned issues:\n",
"\n",
"error 1\n",
"error 2\n",
"error 3\n",
"\n"
]
}
],
"source": [
"def fixture_generate(initial_prompt):\n",
" return \"some response\"\n",
Expand Down Expand Up @@ -504,7 +523,15 @@
"execution_count": null,
"id": "e74e159a",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Some Valid response\n"
]
}
],
"source": [
"fixture_initial_prompt = \"some valid prompt\"\n",
"expected = \"Some Valid response\"\n",
Expand All @@ -526,7 +553,16 @@
"execution_count": null,
"id": "efbaf85e",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"error 1\n",
"error 2\n"
]
}
],
"source": [
"fixture_initial_prompt = \"some invalid prompt\"\n",
"max_attempts = 2\n",
Expand Down
Loading

0 comments on commit 5603271

Please sign in to comment.