GucciA commited on
Commit
2b53d52
·
1 Parent(s): 76d21cf

Upload 4 files

Browse files
Files changed (4) hide show
  1. LICENSE +21 -0
  2. README.md +50 -0
  3. app.py +70 -0
  4. requirements.txt +2 -0
LICENSE ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Ahmed Almushtaghil
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
README.md ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # AOAI-Prompt-Optimization-Tool
2
+
3
+ The Azure OpenAI Prompt Optimization Tool is an interactive Python application powered by OpenAI's GPT-3 technology and tkinter GUI framework. This tool evaluates user-provided prompts, optimizes them, and provides enhanced responses. It features dynamic input, response presentation, and user-friendly functionalities, all while leveraging the power of AI-driven language optimization.
4
+
5
+ ## Prerequisites
6
+ An existing Azure OpenAI resource and model deployment of a chat model (e.g. gpt-35-turbo, gpt-4)
7
+
8
+ ## Deploy the tool
9
+
10
+ ### Deploy from your local machine
11
+
12
+ 1. Create an environment variables file `.env` with your API Key, Endpoint, and Deployed Model name.
13
+ - `AZURE_OPENAI_API_KEY`: your Azure OpenAI resource API key.
14
+ - `AZURE_OPENAI_API_ENDPOINT`: your Azure OpenAI resource endpoint.
15
+ - `AZURE_OPENAI_DEPLOYED_MODEL`: your Azure OpenAI deployment name.
16
+
17
+ 2. Run the following command to install all dependencies.
18
+ ```
19
+ pip install -r requirements.txt
20
+ ```
21
+
22
+ 3. Run `app.py` file.
23
+
24
+ ## How it works
25
+
26
+ The interface consists of four main elements:
27
+ - The prompt input text field to write your prompt.
28
+ - The response output window to see the omptimization recommendation.
29
+ - The Submit button to send the prompt for AI analysis.
30
+ - The Clear button to erase the prompt and start over.
31
+
32
+ ![image](https://github.com/ahmedalm1/AOAI-Prompt-Optimization-Tool/assets/88718044/4423f0fc-1c7b-4568-a825-25b212af9718)
33
+
34
+ Once you enter and submit a prompt, the following response will be generated:
35
+ - Grade: a grade describing how good the prompt is on a scale from 1 to 10.
36
+ - Reason for Grade: the analysis of the prompts highlighting missing key elements and how to improve it.
37
+ - Optimized Prompt: an example of an optimized prompt that will achieve the same goal in a more efficient way.
38
+
39
+ ![image](https://github.com/ahmedalm1/AOAI-Prompt-Optimization-Tool/assets/88718044/c7aabc3c-2e40-4af7-8eb7-e420d332290a)
40
+
41
+ ## Optimization criteria
42
+ The tool analyzes the prompt to determine its optimization level based on several criteria that aim to make the prompt clear, effective, and well-structured for the intended purpose. Below are the criteria used to evaluate whether a prompt is optimized or not:
43
+ - Clarity: The prompt should be easy to understand without ambiguity. It should convey the desired task or question clearly, so that both human readers and AI models can comprehend it accurately.
44
+ - Conciseness: An optimized prompt is concise and to the point. Unnecessary or redundant words should be eliminated to ensure efficient communication of the task to the AI model.
45
+ - Relevance: The prompt should directly relate to the desired output. Irrelevant information can confuse the AI model and result in inaccurate or off-topic responses.
46
+ - Contextual Information: Including sufficient context in the prompt can help guide the AI model towards the desired response. Relevant details or examples can improve the quality of generated content.
47
+ - Specificity: A well-optimized prompt is specific about the expected format, details, or structure of the response. Clear instructions can guide the AI model in producing accurate and relevant output.
48
+ - Language Quality: The language used in the prompt should be grammatically correct, coherent, and free of jargon that might confuse the AI model.
49
+
50
+ Overall, prompt optimization involves balancing these criteria to create a prompt that maximizes the AI model's ability to produce accurate, relevant, and contextually appropriate responses while minimizing the potential for misunderstandings or errors.
app.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import openai
3
+ import tkinter as tk
4
+ from dotenv import load_dotenv
5
+
6
+ load_dotenv()
7
+
8
+ openai.api_type = "azure"
9
+ openai.api_version = "2023-06-01-preview"
10
+ openai.api_key = os.getenv("AZURE_OPENAI_API_KEY")
11
+ openai.api_base = os.getenv("AZURE_OPENAI_API_ENDPOINT")
12
+
13
+ class PromptOptimizerApp:
14
+ def __init__(self, root):
15
+ self.root = root
16
+ self.root.title("Prompt Optimization Tool")
17
+
18
+ self.prompt_label = tk.Label(root, text="Enter your prompt:")
19
+ self.prompt_label.pack(pady=10)
20
+
21
+ self.prompt_entry = tk.Entry(root, width=50)
22
+ self.prompt_entry.pack(pady=5)
23
+
24
+ self.button_frame = tk.Frame(root)
25
+ self.button_frame.pack()
26
+
27
+ self.submit_button = tk.Button(self.button_frame, text="Submit", command=self.optimize_prompt)
28
+ self.submit_button.pack(side=tk.LEFT, padx=5)
29
+
30
+ self.clear_button = tk.Button(self.button_frame, text="Clear", command=self.clear_input)
31
+ self.clear_button.pack(side=tk.LEFT, padx=5)
32
+
33
+ self.response_label = tk.Label(root, text="Response:")
34
+ self.response_label.pack(pady=10)
35
+
36
+ self.response_text = tk.Text(root, height=10, width=50, wrap="word")
37
+ self.response_text.pack(pady=5)
38
+
39
+ def optimize_prompt(self):
40
+ user_prompt = self.prompt_entry.get()
41
+
42
+ optimized_prompt = self.optimize_prompt_with_openai(user_prompt)
43
+
44
+ self.response_text.delete(1.0, tk.END)
45
+ self.response_text.insert(tk.END, optimized_prompt)
46
+
47
+ def optimize_prompt_with_openai(self, prompt):
48
+ response = openai.ChatCompletion.create(
49
+ engine=os.getenv("AZURE_OPENAI_DEPLOYED_MODEL"),
50
+ max_tokens=200,
51
+ temperature=0.9,
52
+ top_p=0.8,
53
+ messages=[
54
+ {"role": "system", "content": "Assess the provided prompt on a scale from one to ten, where one is worst, and ten is best, considering clarity, optimization, prompt engineering, effectiveness in conveying the point, and structural quality. Afterwards, rephrase it for improvement. Respond to example prompts using: \n\nGrade: \n\nReason for Grade: \n\nOptimized Prompt:"},
55
+ {"role": "user", "content": prompt}
56
+ ]
57
+ )
58
+
59
+ optimized_prompt = response['choices'][0]['message']['content']
60
+ return optimized_prompt
61
+
62
+ def clear_input(self):
63
+ self.prompt_entry.delete(0, tk.END)
64
+ self.response_text.delete(1.0, tk.END)
65
+
66
+
67
+ if __name__ == "__main__":
68
+ root = tk.Tk()
69
+ app = PromptOptimizerApp(root)
70
+ root.mainloop()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ openai==0.28.0
2
+ python-dotenv==1.0.0