├── media
├── D1.png
├── D2.png
├── G1.png
└── Banner.jpg
├── Docker-FastAPI-app
├── Dockerfile
├── requirements.txt
└── app
│ ├── main.py
│ └── pipeline.py
├── requirements.txt
├── Gradio-app
├── requirements.txt
├── app.py
└── pipeline.py
├── .gitignore
├── LICENSE
├── README.md
└── Advancd_Prompt_Generator.py
/media/D1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Thunderhead-exe/Advanced-Prompt-Generator/HEAD/media/D1.png
--------------------------------------------------------------------------------
/media/D2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Thunderhead-exe/Advanced-Prompt-Generator/HEAD/media/D2.png
--------------------------------------------------------------------------------
/media/G1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Thunderhead-exe/Advanced-Prompt-Generator/HEAD/media/G1.png
--------------------------------------------------------------------------------
/media/Banner.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Thunderhead-exe/Advanced-Prompt-Generator/HEAD/media/Banner.jpg
--------------------------------------------------------------------------------
/Docker-FastAPI-app/Dockerfile:
--------------------------------------------------------------------------------
1 | FROM tiangolo/uvicorn-gunicorn-fastapi:python3.9
2 |
3 | COPY ./requirements.txt /app/requirements.txt
4 |
5 | RUN pip install --no-cache-dir --upgrade -r /app/requirements.txt
6 |
7 | COPY ./app /app/app
--------------------------------------------------------------------------------
/requirements.txt:
--------------------------------------------------------------------------------
1 | aiohttp==3.9.5
2 | aiosignal==1.3.1
3 | annotated-types==0.7.0
4 | anyio==4.4.0
5 | attrs==23.2.0
6 | certifi==2024.7.4
7 | charset-normalizer==3.3.2
8 | click==8.1.7
9 | distro==1.9.0
10 | dnspython==2.6.1
11 | email_validator==2.2.0
12 | fastapi==0.111.1
13 | fastapi-cli==0.0.4
14 | frozenlist==1.4.1
15 | h11==0.14.0
16 | httpcore==1.0.5
17 | httptools==0.6.1
18 | httpx==0.27.0
19 | idna==3.7
20 | Jinja2==3.1.4
21 | jsonpatch==1.33
22 | jsonpointer==3.0.0
23 | langchain==0.2.10
24 | langchain-core==0.2.22
25 | langchain-openai==0.1.17
26 | langchain-text-splitters==0.2.2
27 | langsmith==0.1.93
28 | markdown-it-py==3.0.0
29 | MarkupSafe==2.1.5
30 | mdurl==0.1.2
31 | multidict==6.0.5
32 | numpy==1.26.4
33 | openai==1.35.15
34 | orjson==3.10.6
35 | packaging==24.1
36 | pydantic==2.8.2
37 | pydantic_core==2.20.1
38 | Pygments==2.18.0
39 | python-dotenv==1.0.1
40 | python-multipart==0.0.9
41 | PyYAML==6.0.1
42 | regex==2024.5.15
43 | requests==2.32.3
44 | rich==13.7.1
45 | shellingham==1.5.4
46 | sniffio==1.3.1
47 | SQLAlchemy==2.0.31
48 | starlette==0.37.2
49 | tenacity==8.5.0
50 | tiktoken==0.7.0
51 | tqdm==4.66.4
52 | typer==0.12.3
53 | typing_extensions==4.12.2
54 | urllib3==2.2.2
55 | uvicorn==0.30.3
56 | uvloop==0.19.0
57 | watchfiles==0.22.0
58 | websockets==12.0
59 | yarl==1.9.4
60 |
--------------------------------------------------------------------------------
/Docker-FastAPI-app/requirements.txt:
--------------------------------------------------------------------------------
1 | aiohttp==3.9.5
2 | aiosignal==1.3.1
3 | annotated-types==0.7.0
4 | anyio==4.4.0
5 | attrs==23.2.0
6 | certifi==2024.7.4
7 | charset-normalizer==3.3.2
8 | click==8.1.7
9 | distro==1.9.0
10 | dnspython==2.6.1
11 | email_validator==2.2.0
12 | fastapi==0.111.1
13 | fastapi-cli==0.0.4
14 | frozenlist==1.4.1
15 | h11==0.14.0
16 | httpcore==1.0.5
17 | httptools==0.6.1
18 | httpx==0.27.0
19 | idna==3.7
20 | Jinja2==3.1.4
21 | jsonpatch==1.33
22 | jsonpointer==3.0.0
23 | langchain==0.2.10
24 | langchain-core==0.2.22
25 | langchain-openai==0.1.17
26 | langchain-text-splitters==0.2.2
27 | langsmith==0.1.93
28 | markdown-it-py==3.0.0
29 | MarkupSafe==2.1.5
30 | mdurl==0.1.2
31 | multidict==6.0.5
32 | numpy==1.26.4
33 | openai==1.35.15
34 | orjson==3.10.6
35 | packaging==24.1
36 | pydantic==2.8.2
37 | pydantic_core==2.20.1
38 | Pygments==2.18.0
39 | python-dotenv==1.0.1
40 | python-multipart==0.0.9
41 | PyYAML==6.0.1
42 | regex==2024.5.15
43 | requests==2.32.3
44 | rich==13.7.1
45 | shellingham==1.5.4
46 | sniffio==1.3.1
47 | SQLAlchemy==2.0.31
48 | starlette==0.37.2
49 | tenacity==8.5.0
50 | tiktoken==0.7.0
51 | tqdm==4.66.4
52 | typer==0.12.3
53 | typing_extensions==4.12.2
54 | urllib3==2.2.2
55 | uvicorn==0.30.3
56 | uvloop==0.19.0
57 | watchfiles==0.22.0
58 | websockets==12.0
59 | yarl==1.9.4
60 |
--------------------------------------------------------------------------------
/Gradio-app/requirements.txt:
--------------------------------------------------------------------------------
1 | huggingface_hub==0.22.2
2 | aiohttp==3.9.5
3 | aiosignal==1.3.1
4 | annotated-types==0.7.0
5 | anyio==4.4.0
6 | attrs==23.2.0
7 | certifi==2024.7.4
8 | charset-normalizer==3.3.2
9 | click==8.1.7
10 | distro==1.9.0
11 | dnspython==2.6.1
12 | email_validator==2.2.0
13 | fastapi==0.111.1
14 | fastapi-cli==0.0.4
15 | frozenlist==1.4.1
16 | h11==0.14.0
17 | httpcore==1.0.5
18 | httptools==0.6.1
19 | httpx==0.27.0
20 | idna==3.7
21 | Jinja2==3.1.4
22 | jsonpatch==1.33
23 | jsonpointer==3.0.0
24 | langchain==0.2.10
25 | langchain-core==0.2.22
26 | langchain-openai==0.1.17
27 | langchain-text-splitters==0.2.2
28 | langsmith==0.1.93
29 | markdown-it-py==3.0.0
30 | MarkupSafe==2.1.5
31 | mdurl==0.1.2
32 | multidict==6.0.5
33 | numpy==1.26.4
34 | openai==1.35.15
35 | orjson==3.10.6
36 | packaging==24.1
37 | pydantic==2.8.2
38 | pydantic_core==2.20.1
39 | Pygments==2.18.0
40 | python-dotenv==1.0.1
41 | python-multipart==0.0.9
42 | PyYAML==6.0.1
43 | regex==2024.5.15
44 | requests==2.32.3
45 | rich==13.7.1
46 | shellingham==1.5.4
47 | sniffio==1.3.1
48 | SQLAlchemy==2.0.31
49 | starlette==0.37.2
50 | tenacity==8.5.0
51 | tiktoken==0.7.0
52 | tqdm==4.66.4
53 | typer==0.12.3
54 | typing_extensions==4.12.2
55 | urllib3==2.2.2
56 | uvicorn==0.30.3
57 | uvloop==0.19.0
58 | watchfiles==0.22.0
59 | websockets==12.0
60 | yarl==1.9.4
61 |
--------------------------------------------------------------------------------
/Docker-FastAPI-app/app/main.py:
--------------------------------------------------------------------------------
1 | # Importing dependecies
2 | import os
3 | import time
4 | import asyncio
5 | from fastapi import FastAPI
6 | from pydantic import BaseModel
7 |
8 | from app.pipeline import PromptEnhancer
9 |
10 |
11 | # Setting up the API key for single project
12 | # - create a .env file and add to it: OPENAI_API_KEY = the_personal_api_key
13 | # - Or: go to pipeline.py and pass it there (not recommended)
14 |
15 |
16 | app = FastAPI()
17 |
18 | class InputPrompt(BaseModel):
19 | text: str
20 |
21 | @app.post("/advanced_prompt_generation")
22 | async def advancedPromptPipeline(payload: InputPrompt):
23 |
24 | input_prompt = payload.text
25 |
26 | model="gpt-4o-mini"
27 |
28 | if model == "gpt-4o":
29 | i_cost=5/10**6
30 | o_cost=15/10**6
31 | elif model == "gpt-4o-mini":
32 | i_cost=0.15/10**6
33 | o_cost=0.6/10**6
34 |
35 | enhancer = PromptEnhancer(model)
36 |
37 | start_time = time.time()
38 | advanced_prompt = await enhancer.enhance_prompt(input_prompt)
39 | elapsed_time = time.time() - start_time
40 |
41 | return {
42 | "model": model,
43 | "elapsed_time": elapsed_time,
44 | "prompt_tokens": enhancer.prompt_tokens,
45 | "completion_tokens": enhancer.completion_tokens,
46 | "approximate_cost": (enhancer.prompt_tokens*i_cost)+(enhancer.completion_tokens*o_cost),
47 | "input_prompt": input_prompt,
48 | "advanced_prompt": advanced_prompt,
49 | }
50 |
51 |
52 |
53 |
--------------------------------------------------------------------------------
/Gradio-app/app.py:
--------------------------------------------------------------------------------
1 | import gradio as gr
2 |
3 | import os
4 | import time
5 | import asyncio
6 |
7 | from pipeline import PromptEnhancer
8 |
9 |
10 | async def advancedPromptPipeline(InputPrompt, model="gpt-4o-mini", temperature=0.0):
11 |
12 | if model == "gpt-4o":
13 | i_cost=5/10**6
14 | o_cost=15/10**6
15 | elif model == "gpt-4o-mini":
16 | i_cost=0.15/10**6
17 | o_cost=0.6/10**6
18 |
19 | enhancer = PromptEnhancer(model, temperature)
20 |
21 | start_time = time.time()
22 | advanced_prompt = await enhancer.enhance_prompt(InputPrompt, perform_eval=False)
23 | elapsed_time = time.time() - start_time
24 |
25 |
26 | """return {
27 | "model": model,
28 | "elapsed_time": elapsed_time,
29 | "prompt_tokens": enhancer.prompt_tokens,
30 | "completion_tokens": enhancer.completion_tokens,
31 | "approximate_cost": (enhancer.prompt_tokens*i_cost)+(enhancer.completion_tokens*o_cost),
32 | "inout_prompt": input_prompt,
33 | "advanced_prompt": advanced_prompt["advanced_prompt"],
34 | }"""
35 |
36 | return advanced_prompt["advanced_prompt"]
37 |
38 |
39 | demo = gr.Interface(fn=advancedPromptPipeline,
40 | inputs=[
41 | gr.Textbox(lines=11, placeholder="Enter your prompt", label="Input Prompt", min_width=100),
42 | gr.Radio(["gpt-4o-mini", "gpt-4o"], value="gpt-4o-mini", label="Select Model", info="Recommended: gpt-4o-mini"),
43 | gr.Slider(minimum=0.0, maximum=1.0, value=0.0, step=0.1, label="Temperature", info="Recommended: Temperature=0.0")
44 | ],
45 | outputs=[
46 | gr.Textbox(lines=23, label="Advanced Prompt", show_copy_button=True, autoscroll=False, min_width=220),
47 | ],
48 | title="Advanced Prompt Generator",
49 | description="This tool will enhance any given input for the optimal output!",
50 | theme="Base",
51 | )
52 |
53 |
54 | if __name__ == "__main__":
55 | demo.launch()
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Byte-compiled / optimized / DLL files
2 | __pycache__/
3 | *.py[cod]
4 | *$py.class
5 |
6 | # C extensions
7 | *.so
8 |
9 | # Distribution / packaging
10 | .Python
11 | build/
12 | develop-eggs/
13 | dist/
14 | downloads/
15 | eggs/
16 | .eggs/
17 | lib/
18 | lib64/
19 | parts/
20 | sdist/
21 | var/
22 | wheels/
23 | share/python-wheels/
24 | *.egg-info/
25 | .installed.cfg
26 | *.egg
27 | MANIFEST
28 |
29 | # PyInstaller
30 | # Usually these files are written by a python script from a template
31 | # before PyInstaller builds the exe, so as to inject date/other infos into it.
32 | *.manifest
33 | *.spec
34 |
35 | # Installer logs
36 | pip-log.txt
37 | pip-delete-this-directory.txt
38 |
39 | # Unit test / coverage reports
40 | htmlcov/
41 | .tox/
42 | .nox/
43 | .coverage
44 | .coverage.*
45 | .cache
46 | nosetests.xml
47 | coverage.xml
48 | *.cover
49 | *.py,cover
50 | .hypothesis/
51 | .pytest_cache/
52 | cover/
53 |
54 | # Translations
55 | *.mo
56 | *.pot
57 |
58 | # Django stuff:
59 | *.log
60 | local_settings.py
61 | db.sqlite3
62 | db.sqlite3-journal
63 |
64 | # Flask stuff:
65 | instance/
66 | .webassets-cache
67 |
68 | # Scrapy stuff:
69 | .scrapy
70 |
71 | # Sphinx documentation
72 | docs/_build/
73 |
74 | # PyBuilder
75 | .pybuilder/
76 | target/
77 |
78 | # Jupyter Notebook
79 | .ipynb_checkpoints
80 |
81 | # IPython
82 | profile_default/
83 | ipython_config.py
84 |
85 | # pyenv
86 | # For a library or package, you might want to ignore these files since the code is
87 | # intended to run in multiple environments; otherwise, check them in:
88 | # .python-version
89 |
90 | # pipenv
91 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92 | # However, in case of collaboration, if having platform-specific dependencies or dependencies
93 | # having no cross-platform support, pipenv may install dependencies that don't work, or not
94 | # install all needed dependencies.
95 | #Pipfile.lock
96 |
97 | # poetry
98 | # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
99 | # This is especially recommended for binary packages to ensure reproducibility, and is more
100 | # commonly ignored for libraries.
101 | # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
102 | #poetry.lock
103 |
104 | # pdm
105 | # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
106 | #pdm.lock
107 | # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
108 | # in version control.
109 | # https://pdm.fming.dev/latest/usage/project/#working-with-version-control
110 | .pdm.toml
111 | .pdm-python
112 | .pdm-build/
113 |
114 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
115 | __pypackages__/
116 |
117 | # Celery stuff
118 | celerybeat-schedule
119 | celerybeat.pid
120 |
121 | # SageMath parsed files
122 | *.sage.py
123 |
124 | # Environments
125 | .env
126 | .venv
127 | env/
128 | venv/
129 | ENV/
130 | env.bak/
131 | venv.bak/
132 |
133 | # Spyder project settings
134 | .spyderproject
135 | .spyproject
136 |
137 | # Rope project settings
138 | .ropeproject
139 |
140 | # mkdocs documentation
141 | /site
142 |
143 | # mypy
144 | .mypy_cache/
145 | .dmypy.json
146 | dmypy.json
147 |
148 | # Pyre type checker
149 | .pyre/
150 |
151 | # pytype static type analyzer
152 | .pytype/
153 |
154 | # Cython debug symbols
155 | cython_debug/
156 |
157 | # PyCharm
158 | # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
159 | # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
160 | # and can be added to the global gitignore or merged into this file. For a more nuclear
161 | # option (not recommended) you can uncomment the following to ignore the entire idea folder.
162 | #.idea/
163 |
164 | .DS_Store
165 |
166 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright [yyyy] [name of copyright owner]
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Advanced Prompt Generator
2 |
3 | 
4 |
5 | ## Overview
6 | This project is an **LLM-based Advanced Prompt Generator** designed to automate the process of prompt engineering by enhancing given input prompts using large language models (LLMs). Following established prompt engineering principles, the tool can generate advanced prompts with a simple click, leveraging LLM agents for optimized prompt generation.
7 |
8 | You can demo this solution on [Hugging Face Spaces](https://huggingface.co/spaces/Adam-Ben-Khalifa/Advanced_Prompt_Generator).
9 |
10 | Also you can check this [Medium article](https://medium.com/@AdamBenKhalifa/an-ai-agent-to-replace-prompt-engineers-ed2864e23549) about this solution for more details!
11 |
12 | ## Key Features
13 | - **Automated Prompt Engineering:** AI Agent based prompt engineering processes, requiring minimal user input.
14 | - **Uses OpenAI APIs:** Support for gpt-4o or gpt-4o-mini (we recommend gpt-4o-mini).
15 | - **FastAPI & Docker Deployment:** Ensures efficient and scalable backend deployment.
16 | - **Gradio Interface:** Provides an easy-to-use interface for testing the prompt generation.
17 | - **Hugging Face Integration:** Hosted on Hugging Face Spaces for quck demo.
18 |
19 | ## Next Steps
20 | - **Expand Model Support:** Integrate additional models to offer more variety and flexibility.
21 |
22 | ---
23 |
24 | ## Repo Structure
25 |
26 | ```
27 | ├── .gitignore # Files and directories to be ignored by Git
28 | ├── LICENSE # License information for the project
29 | ├── README.md # Project documentation (this file)
30 | ├── Advancd_Prompt_Generator.py # Script to test the tool locally
31 | ├── pipeline.py # Core logic for prompt enhancement
32 | ├── requirements.txt # Python dependencies for the project
33 | ├── Docker-FastAPI-app # Version deployed with FastAPI & Docker
34 | │ ├── app
35 | │ │ ├── main.py
36 | │ │ ├── pipeline.py
37 | │ ├── Dockerfile
38 | │ ├── requirements.txt
39 | ├── Gradio-app # Version deployed with Gradio
40 | │ ├── app.py
41 | │ ├── pipeline.py
42 | │ ├── requirements.txt
43 | ```
44 |
45 | ---
46 |
47 | ## Results Examples
48 | ## ***Example 1***
49 | ```
50 | >>> Input Prompt:
51 | how to write a book?
52 | ```
53 | ```
54 | >>> Enhanced Prompt:
55 | As a knowledgeable writing coach, please provide a comprehensive guide on how to write a book.
56 |
57 | Requirements:
58 | 1. Outline the key steps involved in the book writing process, including brainstorming, outlining, drafting, and revising.
59 | 2. Offer tips for maintaining motivation and overcoming writer's block.
60 | 3. Include advice on setting a writing schedule and establishing a writing environment.
61 | 4. Suggest resources for further learning about writing techniques and publishing options.
62 |
63 | Structure your response as follows:
64 | - Introduction to the book writing journey
65 | - Step-by-step guide with actionable tips
66 | - Strategies for motivation and productivity
67 | - Recommended resources for aspiring authors
68 |
69 | Keep the response medium in length (approximately 200-300 words) to ensure thorough coverage of the topic.
70 |
71 | ##REFERENCE SUGGESTIONS##
72 | - "On Writing: A Memoir of the Craft" by Stephen King
73 | Purpose: Offers insights into the writing process and practical advice for aspiring authors
74 | Integration: Use as a guide for understanding the nuances of writing a book
75 |
76 | - "The Elements of Style" by William Strunk Jr. and E.B. White
77 | Purpose: Provides essential rules of English style and composition
78 | Integration: Reference for improving writing clarity and effectiveness
79 |
80 | - "Bird by Bird: Some Instructions on Writing and Life" by Anne Lamott
81 | Purpose: Shares personal anecdotes and practical tips for overcoming writing challenges
82 | Integration: Use for motivation and strategies to tackle the writing process
83 |
84 | ##THOUGHT PROCESS##
85 |
86 | *Subtask 1*:
87 | - **Description**: Introduce the book writing journey and its significance.
88 | - **Reasoning**: Providing an introduction sets the context for aspiring authors, helping them understand the importance and challenges of writing a book.
89 | - **Success criteria**: The introduction should clearly articulate the purpose of writing a book and inspire readers about the journey ahead.
90 |
91 | *Subtask 2*:
92 | - **Description**: Outline the key steps involved in the book writing process: brainstorming, outlining, drafting, and revising.
93 | - **Reasoning**: Breaking down the writing process into clear steps helps authors navigate their journey systematically and reduces overwhelm.
94 | - **Success criteria**: Each step should be defined with actionable tips, such as techniques for brainstorming ideas, structuring an outline, and strategies for effective drafting and revising.
95 |
96 | *Subtask 3*:
97 | - **Description**: Provide tips for maintaining motivation and overcoming writer's block.
98 | - **Reasoning**: Addressing common challenges like writer's block is crucial for sustaining progress and ensuring authors remain engaged with their writing.
99 | - **Success criteria**: Include practical strategies, such as setting small goals, taking breaks, and using prompts to reignite creativity.
100 |
101 | *Subtask 4*:
102 | - **Description**: Advise on setting a writing schedule and establishing a conducive writing environment.
103 | - **Reasoning**: A structured writing schedule and a supportive environment are essential for productivity and focus during the writing process.
104 | - **Success criteria**: Offer specific recommendations for daily writing routines and tips for creating a distraction-free workspace.
105 |
106 | *Subtask 5*:
107 | - **Description**: Suggest resources for further learning about writing techniques and publishing options.
108 | - **Reasoning**: Providing additional resources empowers authors to deepen their knowledge and explore various publishing avenues.
109 | - **Success criteria**: List reputable books, websites, and courses that cover writing skills and the publishing process, ensuring they are accessible and relevant to aspiring authors.
110 | ```
111 | ## ***Example 2***
112 | ```
113 | >>> Input Prompt:
114 | write a python script to compute and plot the fibonacci spiral
115 | ```
116 | ```
117 | >>> Enhanced Prompt:
118 | As a programming expert, please create a Python script that computes and plots the Fibonacci spiral.
119 |
120 | Requirements:
121 | - Use the Fibonacci sequence to generate the necessary points for the spiral.
122 | - Utilize libraries such as Matplotlib for plotting and NumPy for numerical calculations.
123 | - Include comments explaining each step of the code.
124 | - Ensure the script is modular, allowing for easy adjustments to the number of Fibonacci terms used in the spiral.
125 |
126 | Provide the implementation with:
127 | - Function to compute Fibonacci numbers up to a specified term.
128 | - Function to plot the Fibonacci spiral using the computed points.
129 | - Example usage demonstrating the script in action, including a plot display.
130 |
131 | The Fibonacci spiral should start with the first few Fibonacci numbers and visually represent the growth of the spiral based on these values.
132 |
133 | ##REFERENCE SUGGESTIONS##
134 | - Matplotlib Documentation
135 | Purpose: Provides details on plotting functions and customization options
136 | Integration: Reference for creating and customizing plots in the script
137 | - Fibonacci Sequence Article
138 | Purpose: Offers insights into the mathematical properties and applications of the Fibonacci sequence
139 | Integration: Use as a reference for understanding the sequence's generation and its relation to the spiral
140 | - Python Programming Guide
141 | Purpose: Serves as a comprehensive resource for Python syntax and libraries
142 | Integration: Reference for general Python programming practices and functions used in the script
143 |
144 | ##THOUGHT PROCESS##
145 |
146 | *Subtask 1*:
147 | - **Description**: Create a function to compute Fibonacci numbers up to a specified term.
148 | - **Reasoning**: This function is essential for generating the sequence of Fibonacci numbers, which will be used to determine the points for the spiral.
149 | - **Success criteria**: The function should return a list of Fibonacci numbers up to the specified term, with correct values (e.g., for n=5, it should return [0, 1, 1, 2, 3]).
150 |
151 | *Subtask 2*:
152 | - **Description**: Implement a function to plot the Fibonacci spiral using the computed points.
153 | - **Reasoning**: Plotting the spiral visually represents the growth of the Fibonacci sequence, making it easier to understand the relationship between the numbers and the spiral shape.
154 | - **Success criteria**: The function should create a plot that accurately represents the Fibonacci spiral, with appropriate axes and labels, and should display the plot correctly.
155 |
156 | *Subtask 3*:
157 | - **Description**: Include comments explaining each step of the code.
158 | - **Reasoning**: Comments enhance code readability and maintainability, allowing others (or the future self) to understand the logic and flow of the script.
159 | - **Success criteria**: The code should have clear, concise comments that explain the purpose of each function and key steps within the functions.
160 |
161 | *Subtask 4*:
162 | - **Description**: Ensure the script is modular, allowing for easy adjustments to the number of Fibonacci terms used in the spiral.
163 | - **Reasoning**: Modularity allows users to easily modify the number of terms without altering the core logic of the script, enhancing usability and flexibility.
164 | - **Success criteria**: The script should allow the user to specify the number of Fibonacci terms as an input parameter, and the plot should update accordingly.
165 |
166 | *Subtask 5*:
167 | - **Description**: Provide example usage demonstrating the script in action, including a plot display.
168 | - **Reasoning**: Example usage helps users understand how to implement and run the script, showcasing its functionality and output.
169 | - **Success criteria**: The example should include a clear demonstration of how to call the functions, specify the number of terms, and display the resulting plot, with expected output shown.
170 | ```
171 | ---
172 |
173 | ## Solution Diagrams
174 | 
175 | 
176 |
177 | ---
178 |
179 | ## Gradio Interface
180 | 
181 | ---
182 |
183 | ## Getting Started
184 |
185 | ### Installation
186 | 1. Clone the repository:
187 | ```bash
188 | git clone https://github.com/Thunderhead-exe/Advanced-Prompt-Generator.git
189 | ```
190 | 2. Install the dependencies:
191 | ```bash
192 | pip install -r requirements.txt
193 | ```
194 | 3. Run the app:
195 | ```bash
196 | python3 Advancd_Prompt_Generator.py
197 | ```
198 | ---
199 |
200 |
220 |
221 | ## License
222 | This project is licensed under the terms of the [apache-2.0 License](LICENSE).
223 |
224 | ---
225 |
226 | ## Contributing
227 | Contributions are welcome! Please feel free to submit a Pull Request or open an Issue for any bug reports, feature requests, or general feedback.
228 |
--------------------------------------------------------------------------------
/Gradio-app/pipeline.py:
--------------------------------------------------------------------------------
1 | # Importing dependecies
2 | import os
3 | import asyncio
4 | from openai import AsyncOpenAI
5 | from dotenv import load_dotenv
6 |
7 |
8 | # Setting up the API key for single project
9 | # 1/ create a .env file and add to it:
10 | # OPENAI_API_KEY = the_personal_api_key
11 | # 2/ load variables from .env file
12 | load_dotenv()
13 | # 3/ set up the client
14 | client = AsyncOpenAI(
15 | api_key=os.getenv("OPENAI_API_KEY"),
16 | )
17 |
18 |
19 | # Defining the PromptEnhancer class containing the necessary components for the Advanced Prompt Generation Pipeline
20 | class PromptEnhancer:
21 | def __init__(self, model="gpt-4o-mini", temperature=0.0, tools_dict={}):
22 | self.model = model
23 | self.prompt_tokens = 0
24 | self.completion_tokens = 0
25 | self.tools_dict = tools_dict
26 |
27 | async def call_llm(self, prompt):
28 | """Call the LLM with the given prompt"""
29 | response = await client.chat.completions.create(
30 | model=self.model,
31 | messages=[
32 | {"role": "system",
33 | "content": "You are an assistant designed to provide concise and specific information based solely on the given tasks.\
34 | Do not include any additional information, explanations, or context beyond what is explicitly requested."
35 | },
36 | {"role": "user",
37 | "content": prompt
38 | }
39 | ],
40 | temperature=0.0, # from 0 (precise and almost deterministic answer) to 2 (creative and almost random answer)
41 | )
42 | # counting the I/O tokens
43 | self.prompt_tokens += response.usage.prompt_tokens
44 | self.completion_tokens += response.usage.completion_tokens
45 |
46 | return response.choices[0].message.content
47 |
48 | async def analyze_input(self, basic_prompt):
49 | """Analyze the input prompt to determine its key information"""
50 | analysis_prompt = f"""
51 | Analyze the following {{prompt}} and generate brief answers to these key information that will be beneficial to enhance the prompt:
52 | 1. Main topic of the prompt
53 | 2. The most convenient output format for the prompt
54 | 3. Specific requirements for the prompt, if necessary
55 | 4. Suggested strategies to enhance the prompt for better output result
56 |
57 | {{prompt}}: {basic_prompt}
58 |
59 | Your output will be only the result of the information required above in text format.
60 | Do not return a general explanation of the generation process.
61 | """
62 | return await self.call_llm(analysis_prompt)
63 |
64 | async def expand_instructions(self, basic_prompt, analysis):
65 | """Expand the basic prompt with clear, detailed instructions"""
66 | expansion_prompt = f"""
67 | Based on this {{analysis}}:
68 |
69 | {analysis}
70 |
71 | Expand the following {{basic_prompt}} following these instructions:
72 | 1. Add relevant details to clarify the prompt only if necessary
73 | 2. Suggest an appropriate persona for the AI Model
74 | 3. Generate 1-2 related examples to guide the output generation
75 | 4. Suggest an optimal output length
76 | 5. Use delimiter, {{ }}, to clearly indicate the parts of the input that should be concidered as variables
77 |
78 | {{basic_prompt}}: {basic_prompt}
79 |
80 | Your output will be only the result of the information required above in text format and not a dictionary format.
81 | Make sure the generated output maintains the sructure of a prompt for an AI Model.
82 | Make sure the generated output maintains the goal and context of the {{basic_prompt}}.
83 | Do not include the instructions headers in the generated answer.
84 | Do not return a general explanation of the generation process.
85 | Do not generate an answer for the prompt.
86 | """
87 | return await self.call_llm(expansion_prompt)
88 |
89 | async def decompose_task(self, expanded_prompt):
90 | """Break down complex tasks into subtasks"""
91 | decomposition_prompt = f"""
92 | Break down the following {{prompt}} into subtasks for better output generation and follow these instructions:
93 | 1. Identify main task components and their corresponding subtasks
94 | 2. Create specific instructions for each subtask
95 | 3. Define success criteria for each subtask
96 |
97 | {{prompt}}: {expanded_prompt}
98 |
99 | Your output will be only the result of the task required above in text format.
100 | Follow the (Main-task/ Sub-task/ Instructions/ Success-criteria) format.
101 | Do not return a general explanation of the generation process.
102 | """
103 | return await self.call_llm(decomposition_prompt)
104 |
105 | async def add_reasoning(self, expanded_prompt):
106 | """Add instructions for showing reasoning, chain-of-thought, and self-review"""
107 | reasoning_prompt = f"""
108 | Based on the following {{prompt}}, suggest instructions in order to guide the AI Model to:
109 | 1. Show reasoning through using the chain-of-thought process
110 | 2. Use inner-monologue only if it is recommended to hide parts of the thought process
111 | 3. Self-review and check for missed information
112 |
113 | {{prompt}}: {expanded_prompt}
114 |
115 | Your output will be only the set of instructions in text format.
116 | Do not return a general explanation of the generation process.
117 | """
118 | return await self.call_llm(reasoning_prompt)
119 |
120 | async def create_eval_criteria(self, expanded_prompt):
121 | """Generate evaluation criteria for the prompt output"""
122 | evaluation_prompt = f"""
123 | Create evaluation criteria for assessing the quality of the output for this {{prompt}}:
124 | 1. List 1-3 specific criteria
125 | 2. Briefly explain how to measure each criterion
126 |
127 | {{prompt}}: {expanded_prompt}
128 |
129 | Your output will be only the result of the information required above in text format.
130 | Do not return a general explanation of the generation process.
131 | """
132 | return await self.call_llm(evaluation_prompt)
133 |
134 | async def suggest_references(self, expanded_prompt):
135 | """Suggest relevant references and explain how to use them"""
136 | reference_prompt = f"""
137 | For the following {{prompt}}, suggest relevant reference texts or sources that could help enhance the output of the prompt if possible,
138 | and if not, do not return anything:
139 | 1. List 0-3 potential references
140 | 2. Briefly explain how to incorporate these references to enhance the prompt
141 |
142 | {{prompt}}: {expanded_prompt}
143 |
144 | Your output will be only the result of the information required above in a dictionary called "References" containing the references titles as keys,
145 | and their corresponding explanation of incorporation as values. If no references will be suggested, return an empty dictionary.
146 | Do not return a general explanation of the generation process.
147 | """
148 | return await self.call_llm(reference_prompt)
149 |
150 | async def suggest_tools(self, expanded_prompt, tools_dict):
151 | """Suggest relevant external tools or APIs"""
152 | tool_prompt = f"""
153 | For the following {{prompt}}, suggest relevant external tools from the provided {{tools_dict}} that can enhance the prompt for better execution.
154 | If the prompt does not require tools for its output, it is highly-recommended to not return any tools:
155 | 1. List 0-3 potential tools/APIs
156 | 2. Briefly explain how to use these tools within the prompt
157 |
158 | {{prompt}}: {expanded_prompt}
159 | {{tools_dict}}: {tools_dict}
160 |
161 | Your output will be only the result of the information required above in a dictionary containing the suggested tools as keys,
162 | and their corresponding way of usage with the prompt as values. If no tools will be suggested, return an empty dictionary.
163 | Do not return a general explanation of the generation process.
164 | """
165 | return await self.call_llm(tool_prompt)
166 |
167 | async def assemble_prompt(self, components):
168 | """Assemble all components into a cohesive advanced prompt"""
169 | assembly_prompt = f"""
170 | Assemble all the following {{components}} into a cohesive, and well-structured advanced prompt and do not generate a response for the prompt.
171 | Make sure to combine the {{reasoning_process}} and {{subtasks}} sections into one section called {{reasoning_process_and_subtasks}}.
172 |
173 | {{components}}: {components}
174 |
175 | Your output will be only the result of the tasks required above,
176 | which is an advanced coherent prompt generated from the combination of the given components dictionary.
177 | Keep only the {{reasoning_process_and_subtasks}} section instead of the {{reasoning_process}} and {{subtasks}} sections in the output.
178 | Ensure that the assembled prompt maintains the delimiter structure of variables and the suggested persona.
179 | Make sure that each sub-section of the prompt is clear and has a title.
180 | The output is in plain text format and not a dictionary format.
181 | Do not return a general explanation of the generation process.
182 | Take the return-to-line symbol into consideration.
183 | Remove the "**Expanded Prompt**" header.
184 | """
185 | return await self.call_llm(assembly_prompt)
186 |
187 | async def auto_eval(self, assembled_prompt, evaluation_criteria):
188 | """Perform Auto-Evaluation and Auto-Adjustment"""
189 | auto_eval_prompt = f"""
190 | Perform any minor adjustments on the given {{prompt}} based on how likely its output will satisfy these {{evaluation_criteria}}.
191 | Only perform minor changes if it is necessary and return the updated prompt as output.
192 | If no changes are necessary, do not change the prompt and return it as output.
193 |
194 | {{prompt}}: {assembled_prompt}
195 | {{evaluation_criteria}}: {evaluation_criteria}
196 |
197 | Your output will be only the result of the tasks required above, which is an updated version of the {{prompt}}, in text format.
198 | Make sure to keep the {{evaluation_criteria}} in the output prompt.
199 | Do not return a general explanation of the generation process.
200 | Make sure there is no generated answer for the prompt.
201 | Make sure to maintain the stucture of the {{prompt}}.
202 | """
203 | return await self.call_llm(auto_eval_prompt)
204 |
205 | async def enhance_prompt(self, basic_prompt, perform_eval=False):
206 | """Main method to enhance a basic prompt to an advanced one"""
207 | analysis = await self.analyze_input(basic_prompt)
208 | expanded_prompt = await self.expand_instructions(basic_prompt, analysis)
209 |
210 | evaluation_criteria, references, subtasks, reasoning, tools = await asyncio.gather(
211 | self.create_eval_criteria(expanded_prompt),
212 | self.suggest_references(expanded_prompt),
213 | self.decompose_task(expanded_prompt),
214 | self.add_reasoning(expanded_prompt),
215 | self.suggest_tools(expanded_prompt, tools_dict={}),
216 | )
217 |
218 | components = {
219 | "expanded_prompt": expanded_prompt,
220 | "references": references,
221 | "subtasks": subtasks,
222 | "tools": tools,
223 | "reasoning_process": reasoning,
224 | "evaluation_criteria": evaluation_criteria,
225 |
226 | }
227 |
228 | assembled_prompt = await self.assemble_prompt(components)
229 |
230 | if perform_eval:
231 | eveluated_prompt = await self.auto_eval(assembled_prompt, evaluation_criteria)
232 | advanced_prompt = eveluated_prompt
233 | else:
234 | advanced_prompt = assembled_prompt
235 |
236 | return {
237 | "advanced_prompt": advanced_prompt,
238 | "assembled_prompt": assembled_prompt,
239 | "components": components,
240 | "analysis": analysis,
241 | }
242 |
243 |
--------------------------------------------------------------------------------
/Docker-FastAPI-app/app/pipeline.py:
--------------------------------------------------------------------------------
1 | # Importing dependecies
2 | import os
3 | import asyncio
4 | from openai import AsyncOpenAI
5 | from dotenv import load_dotenv
6 |
7 |
8 | # Setting up the API key for single project
9 | # 1/ create a .env file and add to it:
10 | # OPENAI_API_KEY = the_personal_api_key
11 | # 2/ load variables from .env file
12 | load_dotenv()
13 | # 3/ set up the client
14 | client = AsyncOpenAI(
15 | api_key=os.getenv("OPENAI_API_KEY"),
16 | )
17 |
18 |
19 | # Defining the PromptEnhancer class containing the necessary components for the Advanced Prompt Generation Pipeline
20 | class PromptEnhancer:
21 | def __init__(self, model="gpt-4o-mini", tools_dict={}):
22 | self.model = model
23 | self.prompt_tokens = 0
24 | self.completion_tokens = 0
25 | self.tools_dict = tools_dict
26 |
27 |
28 | async def call_llm(self, prompt):
29 | """Call the LLM with the given prompt"""
30 | response = await client.chat.completions.create(
31 | model=self.model,
32 | messages=[
33 | {"role": "system",
34 | "content":
35 | "You are a highly intelligent AI assistant. Your task is to analyze, and comprehend the provided prompt,\
36 | then provide clear, and concise response based strictly on the given instructions.\
37 | Do not include any additional explanations or context beyond the required output."
38 | },
39 | {"role": "user",
40 | "content": prompt
41 | }
42 | ],
43 | temperature=0.0, # from 0 (precise and almost deterministic answer) to 2 (creative and almost random answer)
44 | )
45 | # counting the I/O tokens
46 | self.prompt_tokens += response.usage.prompt_tokens
47 | self.completion_tokens += response.usage.completion_tokens
48 |
49 | return response.choices[0].message.content
50 |
51 |
52 | async def analyze_and_expand_input(self, input_prompt):
53 | analysis_and_expansion_prompt = f"""
54 | You are a highly intelligent assistant.
55 | Analyze the provided {{prompt}} and generate concise answers for the following key aspects:
56 |
57 | - **Main goal of the prompt:** Identify the core subject or request within the provided prompt.
58 | - **Persona:** Recommend the most relevant persona for the AI model to adopt (e.g., expert, teacher, conversational, etc.)
59 | - **Optimal output length:** Suggest an optimal output length (short, brief, medium, long) based on the task, and give an approximate number of words if it is suitable for the case.
60 | - **Most convenient output format:** Recommend the optimal format for the result (e.g., list, paragraph, code snippet, table, JSON, etc.).
61 | - **Specific requirements:** Highlight any special conditions, rules, or expectations stated or implied within the prompt.
62 | - **Suggested improvements:** Offer recommendations on how to modify or enhance the prompt for more precise or efficient output generation.
63 | - **One-shot prompting:** Create one related examples to guide the output generation.
64 |
65 | Then use them to reformulate and expand the provided {{prompt}}.
66 | Return the expanded prompt as output in text format. Refrain from explaining the generation process.
67 |
68 | Example 1:
69 | {{prompt}}: "Explain quantum entanglement to a 10-year-old."
70 |
71 | *thought_process*:
72 | - **Main goal of the prompt:** Simplify complex quantum physics concept for children.
73 | - **Persona:** Patient, friendly teacher
74 | - **Optimal output length:** Brief (100-150 words)
75 | - **Most convenient output format:** Narrative with analogy
76 | - **Specific requirements:** Age-appropriate explanation (10-year-old).
77 | - **Suggested improvements:**
78 | - Request specific analogies
79 | - Include interactive elements
80 | - Add follow-up questions
81 | - Suggest visual aids
82 | - **One-shot prompting:**
83 | Output example:
84 | "Imagine you have two special pairs of socks. When you put one sock in your room and the other sock in the kitchen,
85 | something magical happens! Whatever happens to one sock instantly affects the other sock.
86 | If you turn one sock inside out, the other sock automatically turns inside out too, no matter how far apart they are!"
87 |
88 | *output*:
89 | As a friendly science teacher, please explain quantum entanglement to a 10-year-old student using these guidelines:
90 |
91 | Start with a relatable analogy using everyday objects
92 | Use simple, clear language avoiding technical terms
93 | Include 2-3 interactive examples that demonstrate the concept
94 | Add fun facts that will spark curiosity
95 | End with simple questions to check understanding
96 | Keep the explanation brief (100-150 words)
97 |
98 | Structure your explanation as:
99 |
100 | Opening analogy
101 | Main explanation with examples
102 | Interactive "What if?" scenarios
103 | Fun facts about quantum entanglement
104 | Check-for-understanding questions
105 |
106 | Remember to maintain an enthusiastic and encouraging tone throughout the explanation.
107 |
108 | Output example:
109 | Imagine you have two special pairs of socks. When you put one sock in your room and the other sock in the kitchen,
110 | something magical happens! Whatever happens to one sock instantly affects the other sock.
111 | If you turn one sock inside out, the other sock automatically turns inside out too, no matter how far apart they are!
112 |
113 | Example 2:
114 | {{prompt}}: "Write a function to calculate the Fibonacci sequence up to n terms."
115 |
116 | *thought_process*:
117 | - **Main goal of the prompt:** Create a programming function that generates Fibonacci numbers
118 | - **Persona:** Programming expert
119 | - **Optimal output length:** Medium (150-200 words including code)
120 | - **Most convenient output format:** Code snippet with explanatory comments
121 | - **Specific requirements:** Function must accept parameter n for sequence length
122 | - **Suggested improvements:**
123 | - Specify programming language
124 | - Clarify if 0 should be included as first term
125 | - Define expected handling of negative inputs
126 | - **One-shot prompting:**
127 |
128 | *output*:
129 | As an expert programmer, please create a well-documented function to generate the Fibonacci sequence.
130 |
131 | Requirements:
132 | Accept a parameter 'n' specifying the number of terms to generate
133 | Handle edge cases (n <= 0, n == 1)
134 | Return the sequence as a list/array
135 | Include proper error handling
136 | Add comments explaining the logic
137 |
138 | Provide the implementation in Python, including:
139 | Function definition with docstring
140 | Input validation
141 | Core algorithm
142 | Example usage with outputs for n=5, n=1, and n=0
143 |
144 | For reference, the sequence should start with [0, 1, ...] where each subsequent number is the sum of the previous two numbers.
145 |
146 |
147 | Now, analyze the following prompt then return only the generated *output*:
148 | {{prompt}}: {input_prompt}
149 | """
150 |
151 | return await self.call_llm(analysis_and_expansion_prompt)
152 |
153 |
154 | async def decompose_and_add_reasoning(self, expanded_prompt):
155 | decomposition_and_reasoning_prompt = f"""
156 | You are a highly capable AI assistant tasked with improving complex task execution.
157 | Analyze the provided {{prompt}}, and use it to generate the following output:
158 |
159 | - **Subtasks decomposition:** Break down the task described in the prompt into manageable and specific subtasks that the AI model needs to address.
160 | - **Chain-of-thought reasoning:** For subtasks that involve critical thinking or complex steps, add reasoning using a step-by-step approach to improve decision-making and output quality.
161 | - **Success criteria:** Define what constitutes a successful completion for each subtask, ensuring clear guidance for expected results.
162 |
163 | Return the following structured output for each subtask:
164 |
165 | 1. **Subtask description**: Describe a specific subtask.
166 | 2. **Reasoning**: Provide reasoning or explanation for why this subtask is essential or how it should be approached.
167 | 3. **Success criteria**: Define what successful completion looks like for this subtask.
168 |
169 | Example 1:
170 | {{Prompt}}: "Explain how machine learning models are evaluated using cross-validation."
171 |
172 | ##THOUGHT PROCESS##
173 | *Subtask 1*:
174 | - **Description**: Define cross-validation and its purpose.
175 | - **Reasoning**: Clarifying the concept ensures the reader understands the basic mechanism behind model evaluation.
176 | - **Success criteria**: The explanation should include a clear definition of cross-validation and its role in assessing model performance.
177 | *Subtask 2*:
178 | - **Description**: Describe how cross-validation splits data into training and validation sets.
179 | - **Reasoning**: Explaining the split is crucial to understanding how models are validated and tested for generalization.
180 | - **Success criteria**: A proper explanation of k-fold cross-validation with an illustration of how data is split.
181 | *Subtask 3*:
182 | - **Description**: Discuss how cross-validation results are averaged to provide a final evaluation metric.
183 | - **Reasoning**: Averaging results helps mitigate the variance in performance due to different training/validation splits.
184 | - **Success criteria**: The output should clearly explain how the final model evaluation is derived from multiple iterations of cross-validation.
185 |
186 | Example 2:
187 | {{Prompt}}: "Write a function to calculate the factorial of a number."
188 |
189 | ##THOUGHT PROCESS##
190 | *Subtask 1*:
191 | - **Description**: Define what a factorial is.
192 | - **Reasoning**: Starting with a definition ensures the user understands the mathematical operation required.
193 | - **Success criteria**: Provide a concise definition with an example (e.g., 5! = 5 x 4 x 3 x 2 x 1 = 120).
194 | *Subtask 2*:
195 | - **Description**: Write the base case for the factorial function.
196 | - **Reasoning**: In recursive programming, defining a base case is essential to avoid infinite recursion.
197 | - **Success criteria**: Include a clear base case, such as `n = 1`, to ensure termination of recursion.
198 | *Subtask 3*:
199 | - **Description**: Implement the recursive step for the factorial function.
200 | - **Reasoning**: The recursive case should reflect the mathematical definition of factorial.
201 | - **Success criteria**: The function should return `n * factorial(n-1)` for positive integers.
202 |
203 | Example 3:
204 | {{Prompt}}: "Explain the process of photosynthesis in plants."
205 |
206 | ##THOUGHT PROCESS##
207 | *Subtask 1*:
208 | - **Description**: Define photosynthesis and its overall purpose in plants.
209 | - **Reasoning**: Starting with a definition provides context and sets the stage for a detailed explanation.
210 | - **Success criteria**: Clear and concise definition of photosynthesis, mentioning its role in converting sunlight into chemical energy.
211 | *Subtask 2*:
212 | - **Description**: Break down the steps involved in the photosynthesis process (e.g., light-dependent and light-independent reactions).
213 | - **Reasoning**: Understanding the individual steps helps to grasp the complexity of how plants convert light into usable energy.
214 | - **Success criteria**: Explain both the light-dependent reactions (e.g., capturing light energy) and the Calvin cycle (sugar formation).
215 | *Subtask 3*:
216 | - **Description**: Discuss the importance of photosynthesis to the ecosystem and human life.
217 | - **Reasoning**: Highlighting the broader implications reinforces the significance of this process beyond the biological aspect.
218 | - **Success criteria**: Provide examples of how photosynthesis contributes to oxygen production and energy flow in ecosystems.
219 |
220 | Example 4:
221 | {{Prompt}}: "Design a user-friendly login interface for a mobile app."
222 |
223 | ##THOUGHT PROCESS##
224 | *Subtask 1*:
225 | - **Description**: Identify key user interface elements (e.g., username field, password field, login button).
226 | - **Reasoning**: Identifying these core elements ensures the interface includes the necessary components for functionality.
227 | - **Success criteria**: The interface should include a username input, password input, and a clearly labeled login button.
228 | *Subtask 2*:
229 | - **Description**: Focus on the user experience, ensuring simplicity and intuitive navigation.
230 | - **Reasoning**: An intuitive design ensures a seamless user experience, reducing friction for users during the login process.
231 | - **Success criteria**: The layout should be minimalistic with clear labels, making the login process simple and quick.
232 | *Subtask 3*:
233 | - **Description**: Implement security features like password masking and error handling for incorrect logins.
234 | - **Reasoning**: Security measures ensure that user data is protected and help guide users when errors occur.
235 | - **Success criteria**: Passwords should be masked by default, and error messages should be informative but secure (e.g., "Incorrect username or password").
236 |
237 | Example 5:
238 | {{Prompt}}: "Outline the steps to bake a chocolate cake from scratch."
239 |
240 | ##THOUGHT PROCESS##
241 | *Subtask 1*:
242 | - **Description**: List all the ingredients required for the cake.
243 | - **Reasoning**: Starting with ingredients ensures all necessary components are prepared before beginning the process.
244 | - **Success criteria**: Provide a complete list of ingredients, including measurements (e.g., 2 cups of flour, 1 cup of sugar, etc.).
245 | *Subtask 2*:
246 | - **Description**: Describe the preparation steps, such as mixing dry and wet ingredients.
247 | - **Reasoning**: Detailing the preparation steps ensures that the user follows the correct sequence for combining ingredients.
248 | - **Success criteria**: Instructions should specify when and how to mix ingredients to achieve the right consistency.
249 | *Subtask 3*:
250 | - **Description**: Explain the baking time and temperature.
251 | - **Reasoning**: Providing accurate baking instructions is crucial for the cake to cook properly.
252 | - **Success criteria**: Specify an appropriate baking temperature (e.g., 350°F) and time (e.g., 25-30 minutes), along with how to check for doneness.
253 |
254 | Example 6:
255 | {{Prompt}}: "Create a marketing plan for a new eco-friendly product."
256 |
257 | ##THOUGHT PROCESS##
258 | *Subtask 1*:
259 | - **Description**: Identify the target audience for the eco-friendly product.
260 | - **Reasoning**: Defining the target audience is essential for tailoring the marketing message and strategy effectively.
261 | - **Success criteria**: Provide a detailed description of the ideal customer demographics and psychographics (e.g., age, values, eco-consciousness).
262 | *Subtask 2*:
263 | - **Description**: Outline the key messaging and brand positioning.
264 | - **Reasoning**: Clear messaging ensures the product’s benefits and unique selling points are communicated effectively to the target audience.
265 | - **Success criteria**: Develop a compelling message that highlights the eco-friendliness, sustainability, and benefits of the product.
266 | *Subtask 3*:
267 | - **Description**: Define the marketing channels to be used (e.g., social media, email campaigns, influencer partnerships).
268 | - **Reasoning**: Selecting the appropriate channels ensures that the marketing plan reaches the right audience in an impactful way.
269 | - **Success criteria**: Choose a mix of channels based on the target audience’s preferences and behaviors, including both digital and traditional media.
270 |
271 |
272 | Now, analyze the following expanded prompt and return the subtasks, reasoning, and success criteria.
273 | Prompt: {expanded_prompt}
274 | """
275 | return await self.call_llm(decomposition_and_reasoning_prompt)
276 |
277 |
278 |
279 | async def suggest_enhancements(self, input_prompt, tools_dict={}):
280 | enhancement_suggestion_prompt = f"""
281 | You are a highly intelligent assistant specialized in reference suggestion and tool integration.
282 | Analyze the provided {{input_prompt}} and the available {{tools_dict}} to recommend enhancements:
283 |
284 | - **Reference necessity:** Determine if additional reference materials would benefit the task execution (e.g., websites, documentations, books, articles, etc.)
285 | - **Tool applicability:** Evaluate if any available tools could enhance efficiency or accuracy
286 | - **Integration complexity:** Assess the effort required to incorporate suggested resources
287 | - **Expected impact:** Estimate the potential improvement in output quality
288 |
289 | If enhancements are warranted, provide structured recommendations in this format:
290 |
291 | ##REFERENCE SUGGESTIONS##
292 | (Only if applicable, maximum 3)
293 | - Reference name/type
294 | - Purpose: How it enhances the output
295 | - Integration: How to incorporate it
296 |
297 | ##TOOL SUGGESTIONS##
298 | (Only if applicable, maximum 3)
299 | - Tool name from tools_dict
300 | - Purpose: How it improves the task
301 | - Integration: How to implement it
302 |
303 | If no enhancements would significantly improve the output, return an empty string ""
304 |
305 | Example 1:
306 | {{input_prompt}}: "Write a Python function to detect faces in images using computer vision."
307 | {{tools_dict}}: {{}}
308 | *output*:
309 | ##REFERENCE SUGGESTIONS##
310 | - OpenCV Face Detection Documentation
311 | Purpose: Provides implementation details and best practices
312 | Integration: Reference for optimal parameter settings and cascade classifier usage
313 |
314 | Example 2:
315 | {{input_prompt}}: "Write a haiku about spring."
316 | {{tools_dict}}: {{"textblob": "Text processing library", "gpt": "Language model"}}
317 | *output*:
318 |
319 |
320 | Example 3:
321 | {{expanded_prompt}}: "Create a sentiment analysis function for customer reviews."
322 | {{tools_dict}}: {{}}
323 | *output*:
324 | ##REFERENCE SUGGESTIONS##
325 | - VADER Sentiment Analysis Paper
326 | Purpose: Provides insights into social media text sentiment analysis
327 | Integration: Reference for understanding compound sentiment scoring
328 |
329 | Example 4:
330 | {{expanded_prompt}}: "Generate a weather forecast report for New York."
331 | {{tools_dict}}: {{"requests": "HTTP library", "json": "JSON parser", "weather_api": "Weather data service"}}
332 | *output*:
333 | ##TOOL SUGGESTIONS##
334 | - weather_api
335 | Purpose: Provides real-time weather data
336 | Integration: Use API endpoints for forecast data retrieval
337 | - requests
338 | Purpose: Make HTTP requests to weather API
339 | Integration: Use requests.get() to fetch weather data
340 |
341 | Example 5:
342 | {{expanded_prompt}}: "Calculate the factorial of a number."
343 | {{tools_dict}}: {{}}
344 | *output*:
345 |
346 |
347 | Example 6:
348 | {{expanded_prompt}}: "Create an API endpoint documentation."
349 | {{tools_dict}}: {{"swagger": "API documentation tool", "markdown": "Text formatting", "json_schema": "JSON schema validator"}}
350 | *output*:
351 | ##REFERENCE SUGGESTIONS##
352 | - OpenAPI Specification
353 | Purpose: Provides standard API documentation format
354 | Integration: Use as template for documentation structure
355 | - REST API Best Practices
356 | Purpose: Ensures documentation follows industry standards
357 | Integration: Reference for endpoint description patterns
358 |
359 | ##TOOL SUGGESTIONS##
360 | - swagger
361 | Purpose: Generate interactive API documentation
362 | Integration: Use Swagger UI for visual documentation
363 | - json_schema
364 | Purpose: Validate API request/response schemas
365 | Integration: Define and validate data structures
366 |
367 | Example 7:
368 | {{expanded_prompt}}: "Create an API endpoint documentation."
369 | {{tools_dict}}: {{}}
370 | *output*:
371 | ##REFERENCE SUGGESTIONS##
372 | - OpenAPI Specification
373 | Purpose: Provides standard API documentation format
374 | Integration: Use as template for documentation structure
375 | - REST API Best Practices
376 | Purpose: Ensures documentation follows industry standards
377 | Integration: Reference for endpoint description patterns
378 |
379 |
380 | Now, analyze the following prompt and tools, then return only the generated *output*:
381 | {{input_prompt}}: {input_prompt}
382 | {{tools_dict}}: {tools_dict}
383 | """
384 | return await self.call_llm(enhancement_suggestion_prompt)
385 |
386 |
387 | async def assemble_prompt(self, components):
388 | expanded_prompt = components.get("expanded_prompt", "")
389 | decomposition_and_reasoninng = components.get("decomposition_and_reasoninng", "")
390 | suggested_enhancements = components.get("suggested_enhancements", "")
391 |
392 | output_prompt = (
393 | f"{expanded_prompt}\n\n"
394 | f"{suggested_enhancements}\n\n"
395 | f"{decomposition_and_reasoninng}"
396 | )
397 | return output_prompt
398 |
399 |
400 | async def enhance_prompt(self, input_prompt):
401 |
402 | # TODO: Add a function to update the tools_dict
403 | # TODO: Add function calling method
404 |
405 | tools_dict = {}
406 |
407 | expanded_prompt = await self.analyze_and_expand_input(input_prompt)
408 | suggested_enhancements = await self.suggest_enhancements(input_prompt, tools_dict)
409 | decomposition_and_reasoning = await self.decompose_and_add_reasoning(expanded_prompt)
410 |
411 | components = {
412 | "expanded_prompt":expanded_prompt,
413 | "decomposition_and_reasoninng": decomposition_and_reasoning,
414 | "suggested_enhancements": suggested_enhancements
415 | }
416 |
417 | output_prompt = await self.assemble_prompt(components)
418 |
419 | return output_prompt
420 |
421 |
--------------------------------------------------------------------------------
/Advancd_Prompt_Generator.py:
--------------------------------------------------------------------------------
1 | # Importing dependencies
2 | import os
3 | import time
4 | import asyncio
5 | from openai import AsyncOpenAI
6 | from dotenv import load_dotenv
7 |
8 |
9 | # Setting up the API key for single project
10 | # 1/ create a .env file and add to it:
11 | # OPENAI_API_KEY = "sk-proj-..."
12 | # 2/ load variables from .env file
13 | load_dotenv()
14 | # 3/ set up the client
15 | client = AsyncOpenAI(
16 | api_key=os.getenv("OPENAI_API_KEY"),
17 | )
18 |
19 |
20 | # Defining the PromptEnhancer class containing the necessary components for the Advanced Prompt Generation Pipeline
21 | class PromptEnhancer:
22 | def __init__(self, model="gpt-4o-mini", tools_dict={}):
23 | self.model = model
24 | self.prompt_tokens = 0
25 | self.completion_tokens = 0
26 | self.tools_dict = tools_dict
27 |
28 |
29 | async def call_llm(self, prompt):
30 | """Call the LLM with the given prompt"""
31 | response = await client.chat.completions.create(
32 | model=self.model,
33 | messages=[
34 | {"role": "system",
35 | "content":
36 | "You are a highly intelligent AI assistant. Your task is to analyze, and comprehend the provided prompt,\
37 | then provide clear, and concise response based strictly on the given instructions.\
38 | Do not include any additional explanations or context beyond the required output."
39 | },
40 | {"role": "user",
41 | "content": prompt
42 | }
43 | ],
44 | temperature=0.0, # from 0 (precise and almost deterministic answer) to 2 (creative and almost random answer)
45 | )
46 | # counting the I/O tokens
47 | self.prompt_tokens += response.usage.prompt_tokens
48 | self.completion_tokens += response.usage.completion_tokens
49 |
50 | return response.choices[0].message.content
51 |
52 |
53 | async def analyze_and_expand_input(self, input_prompt):
54 | analysis_and_expansion_prompt = f"""
55 | You are a highly intelligent assistant.
56 | Analyze the provided {{prompt}} and generate concise answers for the following key aspects:
57 |
58 | - **Main goal of the prompt:** Identify the core subject or request within the provided prompt.
59 | - **Persona:** Recommend the most relevant persona for the AI model to adopt (e.g., expert, teacher, conversational, etc.)
60 | - **Optimal output length:** Suggest an optimal output length (short, brief, medium, long) based on the task, and give an approximate number of words if it is suitable for the case.
61 | - **Most convenient output format:** Recommend the optimal format for the result (e.g., list, paragraph, code snippet, table, JSON, etc.).
62 | - **Specific requirements:** Highlight any special conditions, rules, or expectations stated or implied within the prompt.
63 | - **Suggested improvements:** Offer recommendations on how to modify or enhance the prompt for more precise or efficient output generation.
64 | - **One-shot prompting:** Create one related examples to guide the output generation.
65 |
66 | Then use them to reformulate and expand the provided {{prompt}}.
67 | Return the expanded prompt as output in text format. Refrain from explaining the generation process.
68 |
69 | Example 1:
70 | {{prompt}}: "Explain quantum entanglement to a 10-year-old."
71 |
72 | *thought_process*:
73 | - **Main goal of the prompt:** Simplify complex quantum physics concept for children.
74 | - **Persona:** Patient, friendly teacher
75 | - **Optimal output length:** Brief (100-150 words)
76 | - **Most convenient output format:** Narrative with analogy
77 | - **Specific requirements:** Age-appropriate explanation (10-year-old).
78 | - **Suggested improvements:**
79 | - Request specific analogies
80 | - Include interactive elements
81 | - Add follow-up questions
82 | - Suggest visual aids
83 | - **One-shot prompting:**
84 | Output example:
85 | "Imagine you have two special pairs of socks. When you put one sock in your room and the other sock in the kitchen,
86 | something magical happens! Whatever happens to one sock instantly affects the other sock.
87 | If you turn one sock inside out, the other sock automatically turns inside out too, no matter how far apart they are!"
88 |
89 | *output*:
90 | As a friendly science teacher, please explain quantum entanglement to a 10-year-old student using these guidelines:
91 |
92 | Start with a relatable analogy using everyday objects
93 | Use simple, clear language avoiding technical terms
94 | Include 2-3 interactive examples that demonstrate the concept
95 | Add fun facts that will spark curiosity
96 | End with simple questions to check understanding
97 | Keep the explanation brief (100-150 words)
98 |
99 | Structure your explanation as:
100 |
101 | Opening analogy
102 | Main explanation with examples
103 | Interactive "What if?" scenarios
104 | Fun facts about quantum entanglement
105 | Check-for-understanding questions
106 |
107 | Remember to maintain an enthusiastic and encouraging tone throughout the explanation.
108 |
109 | Output example:
110 | Imagine you have two special pairs of socks. When you put one sock in your room and the other sock in the kitchen,
111 | something magical happens! Whatever happens to one sock instantly affects the other sock.
112 | If you turn one sock inside out, the other sock automatically turns inside out too, no matter how far apart they are!
113 |
114 | Example 2:
115 | {{prompt}}: "Write a function to calculate the Fibonacci sequence up to n terms."
116 |
117 | *thought_process*:
118 | - **Main goal of the prompt:** Create a programming function that generates Fibonacci numbers
119 | - **Persona:** Programming expert
120 | - **Optimal output length:** Medium (150-200 words including code)
121 | - **Most convenient output format:** Code snippet with explanatory comments
122 | - **Specific requirements:** Function must accept parameter n for sequence length
123 | - **Suggested improvements:**
124 | - Specify programming language
125 | - Clarify if 0 should be included as first term
126 | - Define expected handling of negative inputs
127 | - **One-shot prompting:**
128 |
129 | *output*:
130 | As an expert programmer, please create a well-documented function to generate the Fibonacci sequence.
131 |
132 | Requirements:
133 | Accept a parameter 'n' specifying the number of terms to generate
134 | Handle edge cases (n <= 0, n == 1)
135 | Return the sequence as a list/array
136 | Include proper error handling
137 | Add comments explaining the logic
138 |
139 | Provide the implementation in Python, including:
140 | Function definition with docstring
141 | Input validation
142 | Core algorithm
143 | Example usage with outputs for n=5, n=1, and n=0
144 |
145 | For reference, the sequence should start with [0, 1, ...] where each subsequent number is the sum of the previous two numbers.
146 |
147 |
148 | Now, analyze the following prompt then return only the generated *output*:
149 | {{prompt}}: {input_prompt}
150 | """
151 |
152 | return await self.call_llm(analysis_and_expansion_prompt)
153 |
154 |
155 | async def decompose_and_add_reasoning(self, expanded_prompt):
156 | decomposition_and_reasoning_prompt = f"""
157 | You are a highly capable AI assistant tasked with improving complex task execution.
158 | Analyze the provided {{prompt}}, and use it to generate the following output:
159 |
160 | - **Subtasks decomposition:** Break down the task described in the prompt into manageable and specific subtasks that the AI model needs to address.
161 | - **Chain-of-thought reasoning:** For subtasks that involve critical thinking or complex steps, add reasoning using a step-by-step approach to improve decision-making and output quality.
162 | - **Success criteria:** Define what constitutes a successful completion for each subtask, ensuring clear guidance for expected results.
163 |
164 | Return the following structured output for each subtask:
165 |
166 | 1. **Subtask description**: Describe a specific subtask.
167 | 2. **Reasoning**: Provide reasoning or explanation for why this subtask is essential or how it should be approached.
168 | 3. **Success criteria**: Define what successful completion looks like for this subtask.
169 |
170 | Example 1:
171 | {{Prompt}}: "Explain how machine learning models are evaluated using cross-validation."
172 |
173 | ##THOUGHT PROCESS##
174 | *Subtask 1*:
175 | - **Description**: Define cross-validation and its purpose.
176 | - **Reasoning**: Clarifying the concept ensures the reader understands the basic mechanism behind model evaluation.
177 | - **Success criteria**: The explanation should include a clear definition of cross-validation and its role in assessing model performance.
178 | *Subtask 2*:
179 | - **Description**: Describe how cross-validation splits data into training and validation sets.
180 | - **Reasoning**: Explaining the split is crucial to understanding how models are validated and tested for generalization.
181 | - **Success criteria**: A proper explanation of k-fold cross-validation with an illustration of how data is split.
182 | *Subtask 3*:
183 | - **Description**: Discuss how cross-validation results are averaged to provide a final evaluation metric.
184 | - **Reasoning**: Averaging results helps mitigate the variance in performance due to different training/validation splits.
185 | - **Success criteria**: The output should clearly explain how the final model evaluation is derived from multiple iterations of cross-validation.
186 |
187 | Example 2:
188 | {{Prompt}}: "Write a function to calculate the factorial of a number."
189 |
190 | ##THOUGHT PROCESS##
191 | *Subtask 1*:
192 | - **Description**: Define what a factorial is.
193 | - **Reasoning**: Starting with a definition ensures the user understands the mathematical operation required.
194 | - **Success criteria**: Provide a concise definition with an example (e.g., 5! = 5 x 4 x 3 x 2 x 1 = 120).
195 | *Subtask 2*:
196 | - **Description**: Write the base case for the factorial function.
197 | - **Reasoning**: In recursive programming, defining a base case is essential to avoid infinite recursion.
198 | - **Success criteria**: Include a clear base case, such as `n = 1`, to ensure termination of recursion.
199 | *Subtask 3*:
200 | - **Description**: Implement the recursive step for the factorial function.
201 | - **Reasoning**: The recursive case should reflect the mathematical definition of factorial.
202 | - **Success criteria**: The function should return `n * factorial(n-1)` for positive integers.
203 |
204 | Example 3:
205 | {{Prompt}}: "Explain the process of photosynthesis in plants."
206 |
207 | ##THOUGHT PROCESS##
208 | *Subtask 1*:
209 | - **Description**: Define photosynthesis and its overall purpose in plants.
210 | - **Reasoning**: Starting with a definition provides context and sets the stage for a detailed explanation.
211 | - **Success criteria**: Clear and concise definition of photosynthesis, mentioning its role in converting sunlight into chemical energy.
212 | *Subtask 2*:
213 | - **Description**: Break down the steps involved in the photosynthesis process (e.g., light-dependent and light-independent reactions).
214 | - **Reasoning**: Understanding the individual steps helps to grasp the complexity of how plants convert light into usable energy.
215 | - **Success criteria**: Explain both the light-dependent reactions (e.g., capturing light energy) and the Calvin cycle (sugar formation).
216 | *Subtask 3*:
217 | - **Description**: Discuss the importance of photosynthesis to the ecosystem and human life.
218 | - **Reasoning**: Highlighting the broader implications reinforces the significance of this process beyond the biological aspect.
219 | - **Success criteria**: Provide examples of how photosynthesis contributes to oxygen production and energy flow in ecosystems.
220 |
221 | Example 4:
222 | {{Prompt}}: "Design a user-friendly login interface for a mobile app."
223 |
224 | ##THOUGHT PROCESS##
225 | *Subtask 1*:
226 | - **Description**: Identify key user interface elements (e.g., username field, password field, login button).
227 | - **Reasoning**: Identifying these core elements ensures the interface includes the necessary components for functionality.
228 | - **Success criteria**: The interface should include a username input, password input, and a clearly labeled login button.
229 | *Subtask 2*:
230 | - **Description**: Focus on the user experience, ensuring simplicity and intuitive navigation.
231 | - **Reasoning**: An intuitive design ensures a seamless user experience, reducing friction for users during the login process.
232 | - **Success criteria**: The layout should be minimalistic with clear labels, making the login process simple and quick.
233 | *Subtask 3*:
234 | - **Description**: Implement security features like password masking and error handling for incorrect logins.
235 | - **Reasoning**: Security measures ensure that user data is protected and help guide users when errors occur.
236 | - **Success criteria**: Passwords should be masked by default, and error messages should be informative but secure (e.g., "Incorrect username or password").
237 |
238 | Example 5:
239 | {{Prompt}}: "Outline the steps to bake a chocolate cake from scratch."
240 |
241 | ##THOUGHT PROCESS##
242 | *Subtask 1*:
243 | - **Description**: List all the ingredients required for the cake.
244 | - **Reasoning**: Starting with ingredients ensures all necessary components are prepared before beginning the process.
245 | - **Success criteria**: Provide a complete list of ingredients, including measurements (e.g., 2 cups of flour, 1 cup of sugar, etc.).
246 | *Subtask 2*:
247 | - **Description**: Describe the preparation steps, such as mixing dry and wet ingredients.
248 | - **Reasoning**: Detailing the preparation steps ensures that the user follows the correct sequence for combining ingredients.
249 | - **Success criteria**: Instructions should specify when and how to mix ingredients to achieve the right consistency.
250 | *Subtask 3*:
251 | - **Description**: Explain the baking time and temperature.
252 | - **Reasoning**: Providing accurate baking instructions is crucial for the cake to cook properly.
253 | - **Success criteria**: Specify an appropriate baking temperature (e.g., 350°F) and time (e.g., 25-30 minutes), along with how to check for doneness.
254 |
255 | Example 6:
256 | {{Prompt}}: "Create a marketing plan for a new eco-friendly product."
257 |
258 | ##THOUGHT PROCESS##
259 | *Subtask 1*:
260 | - **Description**: Identify the target audience for the eco-friendly product.
261 | - **Reasoning**: Defining the target audience is essential for tailoring the marketing message and strategy effectively.
262 | - **Success criteria**: Provide a detailed description of the ideal customer demographics and psychographics (e.g., age, values, eco-consciousness).
263 | *Subtask 2*:
264 | - **Description**: Outline the key messaging and brand positioning.
265 | - **Reasoning**: Clear messaging ensures the product’s benefits and unique selling points are communicated effectively to the target audience.
266 | - **Success criteria**: Develop a compelling message that highlights the eco-friendliness, sustainability, and benefits of the product.
267 | *Subtask 3*:
268 | - **Description**: Define the marketing channels to be used (e.g., social media, email campaigns, influencer partnerships).
269 | - **Reasoning**: Selecting the appropriate channels ensures that the marketing plan reaches the right audience in an impactful way.
270 | - **Success criteria**: Choose a mix of channels based on the target audience’s preferences and behaviors, including both digital and traditional media.
271 |
272 |
273 | Now, analyze the following expanded prompt and return the subtasks, reasoning, and success criteria.
274 | Prompt: {expanded_prompt}
275 | """
276 | return await self.call_llm(decomposition_and_reasoning_prompt)
277 |
278 |
279 |
280 | async def suggest_enhancements(self, input_prompt, tools_dict={}):
281 | enhancement_suggestion_prompt = f"""
282 | You are a highly intelligent assistant specialized in reference suggestion and tool integration.
283 | Analyze the provided {{input_prompt}} and the available {{tools_dict}} to recommend enhancements:
284 |
285 | - **Reference necessity:** Determine if additional reference materials would benefit the task execution (e.g., websites, documentations, books, articles, etc.)
286 | - **Tool applicability:** Evaluate if any available tools could enhance efficiency or accuracy
287 | - **Integration complexity:** Assess the effort required to incorporate suggested resources
288 | - **Expected impact:** Estimate the potential improvement in output quality
289 |
290 | If enhancements are warranted, provide structured recommendations in this format:
291 |
292 | ##REFERENCE SUGGESTIONS##
293 | (Only if applicable, maximum 3)
294 | - Reference name/type
295 | - Purpose: How it enhances the output
296 | - Integration: How to incorporate it
297 |
298 | ##TOOL SUGGESTIONS##
299 | (Only if applicable, maximum 3)
300 | - Tool name from tools_dict
301 | - Purpose: How it improves the task
302 | - Integration: How to implement it
303 |
304 | If no enhancements would significantly improve the output, return an empty string ""
305 |
306 | Example 1:
307 | {{input_prompt}}: "Write a Python function to detect faces in images using computer vision."
308 | {{tools_dict}}: {{}}
309 | *output*:
310 | ##REFERENCE SUGGESTIONS##
311 | - OpenCV Face Detection Documentation
312 | Purpose: Provides implementation details and best practices
313 | Integration: Reference for optimal parameter settings and cascade classifier usage
314 |
315 | Example 2:
316 | {{input_prompt}}: "Write a haiku about spring."
317 | {{tools_dict}}: {{"textblob": "Text processing library", "gpt": "Language model"}}
318 | *output*:
319 |
320 |
321 | Example 3:
322 | {{expanded_prompt}}: "Create a sentiment analysis function for customer reviews."
323 | {{tools_dict}}: {{}}
324 | *output*:
325 | ##REFERENCE SUGGESTIONS##
326 | - VADER Sentiment Analysis Paper
327 | Purpose: Provides insights into social media text sentiment analysis
328 | Integration: Reference for understanding compound sentiment scoring
329 |
330 | Example 4:
331 | {{expanded_prompt}}: "Generate a weather forecast report for New York."
332 | {{tools_dict}}: {{"requests": "HTTP library", "json": "JSON parser", "weather_api": "Weather data service"}}
333 | *output*:
334 | ##TOOL SUGGESTIONS##
335 | - weather_api
336 | Purpose: Provides real-time weather data
337 | Integration: Use API endpoints for forecast data retrieval
338 | - requests
339 | Purpose: Make HTTP requests to weather API
340 | Integration: Use requests.get() to fetch weather data
341 |
342 | Example 5:
343 | {{expanded_prompt}}: "Calculate the factorial of a number."
344 | {{tools_dict}}: {{}}
345 | *output*:
346 |
347 |
348 | Example 6:
349 | {{expanded_prompt}}: "Create an API endpoint documentation."
350 | {{tools_dict}}: {{"swagger": "API documentation tool", "markdown": "Text formatting", "json_schema": "JSON schema validator"}}
351 | *output*:
352 | ##REFERENCE SUGGESTIONS##
353 | - OpenAPI Specification
354 | Purpose: Provides standard API documentation format
355 | Integration: Use as template for documentation structure
356 | - REST API Best Practices
357 | Purpose: Ensures documentation follows industry standards
358 | Integration: Reference for endpoint description patterns
359 |
360 | ##TOOL SUGGESTIONS##
361 | - swagger
362 | Purpose: Generate interactive API documentation
363 | Integration: Use Swagger UI for visual documentation
364 | - json_schema
365 | Purpose: Validate API request/response schemas
366 | Integration: Define and validate data structures
367 |
368 | Example 7:
369 | {{expanded_prompt}}: "Create an API endpoint documentation."
370 | {{tools_dict}}: {{}}
371 | *output*:
372 | ##REFERENCE SUGGESTIONS##
373 | - OpenAPI Specification
374 | Purpose: Provides standard API documentation format
375 | Integration: Use as template for documentation structure
376 | - REST API Best Practices
377 | Purpose: Ensures documentation follows industry standards
378 | Integration: Reference for endpoint description patterns
379 |
380 |
381 | Now, analyze the following prompt and tools, then return only the generated *output*:
382 | {{input_prompt}}: {input_prompt}
383 | {{tools_dict}}: {tools_dict}
384 | """
385 | return await self.call_llm(enhancement_suggestion_prompt)
386 |
387 |
388 | async def assemble_prompt(self, components):
389 | expanded_prompt = components.get("expanded_prompt", "")
390 | decomposition_and_reasoninng = components.get("decomposition_and_reasoninng", "")
391 | suggested_enhancements = components.get("suggested_enhancements", "")
392 |
393 | output_prompt = (
394 | f"{expanded_prompt}\n\n"
395 | f"{suggested_enhancements}\n\n"
396 | f"{decomposition_and_reasoninng}"
397 | )
398 | return output_prompt
399 |
400 |
401 | async def enhance_prompt(self, input_prompt):
402 |
403 | # TODO: Add a function to update the tools_dict
404 | # TODO: Add function calling method
405 |
406 | tools_dict = {}
407 |
408 | expanded_prompt = await self.analyze_and_expand_input(input_prompt)
409 | suggested_enhancements = await self.suggest_enhancements(input_prompt, tools_dict)
410 | decomposition_and_reasoning = await self.decompose_and_add_reasoning(expanded_prompt)
411 |
412 | components = {
413 | "expanded_prompt":expanded_prompt,
414 | "decomposition_and_reasoninng": decomposition_and_reasoning,
415 | "suggested_enhancements": suggested_enhancements
416 | }
417 |
418 | output_prompt = await self.assemble_prompt(components)
419 |
420 | return output_prompt
421 |
422 |
423 |
424 |
425 | async def main():
426 | print("-"*52)
427 | print("||||||||||| ADVANCED PROMPT GENERATOR |||||||||||")
428 | print("-"*52, "\n")
429 |
430 | print("|- Choose the model -----------------------------")
431 | print("| 1) GPT-4o")
432 | print("| 2) GPT-4o-mini")
433 | print("|")
434 |
435 | which_model = input("| Select [1/2]: \n| > ")
436 |
437 | if which_model == "1":
438 | model="gpt-4o"
439 | i_cost=5/10**6
440 | o_cost=15/10**6
441 | elif which_model == "2":
442 | model="gpt-4o-mini"
443 | i_cost=0.15/10**6
444 | o_cost=0.6/10**6
445 | else:
446 | raise Exception("Please input a valide choice")
447 |
448 | print("|")
449 |
450 | input_prompt = input("|- Enter your Prompt ---------------------------- \n| > ")
451 | print("\n")
452 |
453 | # Add option to choose output format
454 | print("|- Choose output format -------------------------")
455 | print("| 1) Console")
456 | print("| 2) .txt file")
457 | print("|")
458 |
459 | output_choice = input("| Select [1/2]: \n| > ")
460 | print("|")
461 |
462 | enhancer = PromptEnhancer(model)
463 |
464 | print(f"SELECTED MODEL: GPT-{enhancer.model[4:]}\n")
465 | print("PROCESSING ... \n")
466 |
467 | start_time = time.time()
468 |
469 | advanced_prompt = await enhancer.enhance_prompt(input_prompt)
470 |
471 | elapsed_time = time.time() - start_time
472 |
473 | print("-"*52)
474 | print("\n--- RESULTS -------------------------------------")
475 | print(f"- Execution Time: {elapsed_time:.2f} seconds")
476 | print(f"- Prompt Tokens Count = {enhancer.prompt_tokens}")
477 | print(f"- Completion Tokens Count = {enhancer.completion_tokens}")
478 | print(f"- Approximate Cost = ${(enhancer.prompt_tokens*i_cost)+(enhancer.completion_tokens*o_cost)}\n")
479 | print("-"*52, "\n")
480 |
481 | if output_choice == "1":
482 | # Print output to console
483 | print(">>> BASIC PROMPT: \n")
484 | print(input_prompt, "\n")
485 | print("-"*52, "\n")
486 | print(">>> ADVANCED PROMPT: \n")
487 | print(advanced_prompt, "\n")
488 | print("-"*52, "\n")
489 | elif output_choice == "2":
490 | # Write output to a .txt file
491 | with open("output.txt", "w") as f:
492 | f.write("Basic Prompt:\n")
493 | f.write(input_prompt + "\n")
494 | f.write("-"*52 + "\n")
495 | f.write("Advanced Prompt:\n")
496 | f.write(advanced_prompt)
497 | print("Output saved to output.txt")
498 | else:
499 | raise Exception("Please input a valid choice")
500 |
501 |
502 | if __name__ == "__main__":
503 | asyncio.run(main())
504 |
--------------------------------------------------------------------------------