From c7fb4a06c559558fbdfd138937d927a2c2d3fb41 Mon Sep 17 00:00:00 2001 From: Ali Nour Eldin <61513661+Noureldin-Ali@users.noreply.github.com> Date: Mon, 31 Jul 2023 14:40:09 +0200 Subject: [PATCH] update prompt engineering --- integration-openai/app.py | 81 +++++++++++--------------- integration-openai/check_groovy.prompt | 80 +++++++++++++++++++++++++ integration-openai/groovy.prompt | 66 ++++++++++----------- 3 files changed, 146 insertions(+), 81 deletions(-) create mode 100644 integration-openai/check_groovy.prompt diff --git a/integration-openai/app.py b/integration-openai/app.py index a585740f..275d635e 100644 --- a/integration-openai/app.py +++ b/integration-openai/app.py @@ -23,6 +23,9 @@ def generateGherkin(): if('output' in request.form): output = request.form['output'] + + # description to Gherkin syntax + # llm = OpenAI(openai_api_key=api_key, # temperature=temperature, # model='text-davinci-003') @@ -30,9 +33,9 @@ def generateGherkin(): llm = ChatOpenAI( model_name='gpt-3.5-turbo', - temperature=temperature, - openai_api_key=api_key) - # print(llm) + temperature=temperature, + api_key=api_key) + f = open("gherkin.prompt", "r") template = f.read() @@ -42,10 +45,11 @@ def generateGherkin(): prompt_input = {'description':request.form['description']} result = llm_chain.run(prompt_input) - #check gerkin + #Check gerkin llm = OpenAI(openai_api_key=api_key, temperature=temperature, model='text-davinci-003') + f = open("check_gherkin.prompt", "r") template = f.read() @@ -53,48 +57,14 @@ def generateGherkin(): llm_chain = LLMChain(prompt=prompt, llm=llm) prompt_input = {'output':output, 'inputs':request.form['inputs'], 'description':result} result1 = llm_chain.run(prompt_input) + + # resutls print([result1,"###",result]) if("NO" in result1): return "INVALID REQUEST, CHECK IF YOUR DATA INPUTS AND DESCRIPTION ARE COHERENT, AND TRY AGAIN" # return [result1,"###",result] return result -# @app.route("/verify_gherkin", methods=['post']) -# def verifyGherkin(): -# # if('openai-api-key' not in request.headers): -# # return "Error: OpenAI API key" -# # if('openai-organization' not in request.headers): -# # return "Error: OpenAI Organization" - -# # api_key = request.headers['openai-api-key'] -# # orga_key = request.headers['openai-organization'] - -# api_key = sys.argv[1] -# if('inputs' not in request.form): -# return "Error: Inputs are required" -# if('description' not in request.form): -# return "Error: Description is required" - -# output = "" -# if('output' in request.form): -# output = request.form['output'] - -# # llm = OpenAI(openai_api_key=api_key, -# # temperature=temperature) - -# llm = ChatOpenAI( -# temperature=temperature, -# openai_api_key=api_key) -# # print(llm) -# f = open("check_gherkin.prompt", "r") -# template = f.read() - -# prompt = PromptTemplate(template=template, input_variables=["inputs","description","output"]) -# llm_chain = LLMChain(prompt=prompt, llm=llm) -# prompt_input = {'output':output, 'inputs':request.form['inputs'], 'description':request.form['description']} -# result = llm_chain.run(prompt_input) -# return result - @app.route("/groovy", methods=['post']) def generateGroovy(): @@ -116,14 +86,12 @@ def generateGroovy(): api_key = sys.argv[1] - llm = OpenAI(openai_api_key=api_key, - temperature=temperature - ) - - # llm = ChatOpenAI( - # temperature=temperature, - # openai_api_key=api_key) - # print(llm) + llm = ChatOpenAI( + model_name='gpt-3.5-turbo', + temperature=temperature, + openai_api_key=api_key + ) + f = open("groovy.prompt", "r") template = f.read() @@ -131,6 +99,23 @@ def generateGroovy(): llm_chain = LLMChain(prompt=prompt, llm=llm) prompt_input = {'output':output, 'inputs':request.form['inputs'], 'description':request.form['description']} result = llm_chain.run(prompt_input) + + + llm = OpenAI( + openai_api_key=api_key, + temperature=temperature, + model='text-davinci-003' + ) + + f = open("check_groovy.prompt", "r") + template = f.read() + + prompt = PromptTemplate(template=template, input_variables=["inputs","description","output"]) + llm_chain = LLMChain(prompt=prompt, llm=llm) + prompt_input = {'output':output, 'inputs':request.form['inputs'], 'description':request.form['description']} + result2 = llm_chain.run(prompt_input) + + print(result,"################################",result2) return result app.run(debug=True,host='0.0.0.0',port=3001) \ No newline at end of file diff --git a/integration-openai/check_groovy.prompt b/integration-openai/check_groovy.prompt new file mode 100644 index 00000000..e7c497b8 --- /dev/null +++ b/integration-openai/check_groovy.prompt @@ -0,0 +1,80 @@ +You will take some example about accepted and rejected senarios, based on the inputs/output data and the description of the senario. +Whole the input should be a part of the description and the description inputs should be a part on the inputs list. +The description should be in Groovy script. +The input are in form of list ([x,y,z] is a list) each one already define and should use it directly, that mean used x and y and z directly. +The input should be a list of variable names, and shoud be used as it is in the groovy script. +you should show me only "YES" or "NO" without any other inforamtion. +Result ONLY YES or NO! +--- +Example1: acceptable senario +Inputs: [list_of_item] +Output: sum_of_amount_after_discount +Description: +def totalSum = list_of_item.sum() +def sum_of_amount_after_discount = totalSum +if (totalSum > 100) + sum_of_amount_after_discount = totalSum - (totalSum * 0.1) +if (list_of_item.contains('XYZ')) + sum_of_amount_after_discount = totalSum - (sum_of_amount_after_discount * 0.05) +return sum_of_amount_after_discount +Answer: YES + +--- + +Example2: rejected senario (no inputs) +Inputs: [] +Output: sum_of_amount_after_discount +Description: +def totalSum = list_of_item.sum() +def sum_of_amount_after_discount = totalSum +if (totalSum > 100) + sum_of_amount_after_discount = totalSum - (totalSum * 0.1) +if (list_of_item.contains('XYZ')) + sum_of_amount_after_discount = totalSum - (sum_of_amount_after_discount * 0.05) +return sum_of_amount_after_discount +Answer: NO +--- + +Example3: rejected senario (wrong inputs) +Inputs: [water_intake] +Output: sum_of_amount_after_discount +Description: +def net_salary_after_tax = 0 +if (gross_salary <= 10722) + net_salary_after_tax=gross_salary +else if (gross_salary> 10722 && gross_salary <= 27478) + net_salary_after_tax=gross_salary - (gross_salary * 0.11) +else if (gross_salary> 27478 && gross_salary <= 78570) + net_salary_after_tax=gross_salary - (gross_salary * 0.3) +else if (gross_salary> 78570 && gross_salary <= 168994) + net_salary_after_tax=gross_salary - (gross_salary * 0.41) +else if (gross_salary> 168994) + net_salary_after_tax = gross_salary - (gross_salary * 0.45) +return net_salary_after_tax +Answer: NO +--- + +Example4: acceptable senario +Inputs: [gross_salary] +Output: sum_of_amount_after_discount +Description: +def net_salary_after_tax = 0 +if (gross_salary <= 10722) + net_salary_after_tax=gross_salary +else if (gross_salary> 10722 && gross_salary <= 27478) + net_salary_after_tax=gross_salary - (gross_salary * 0.11) +else if (gross_salary> 27478 && gross_salary <= 78570) + net_salary_after_tax=gross_salary - (gross_salary * 0.3) +else if (gross_salary> 78570 && gross_salary <= 168994) + net_salary_after_tax=gross_salary - (gross_salary * 0.41) +else if (gross_salary> 168994) + net_salary_after_tax = gross_salary - (gross_salary * 0.45) +return net_salary_after_tax +Answer: YES +--- + +Inputs:{inputs} +Output:{output} +Description: {description} +Answer: ? +--- \ No newline at end of file diff --git a/integration-openai/groovy.prompt b/integration-openai/groovy.prompt index 76469219..48cfea89 100644 --- a/integration-openai/groovy.prompt +++ b/integration-openai/groovy.prompt @@ -1,41 +1,41 @@ -You are expert in Groovy script developmenet. Convert Gherkin syntax to Groovy script. -The script should be well implemented without any error. You should take input from user and if there is output to add return. If there are not input available ([] is list is empty) please show me only "CANT CONVERT". -The input are in form of list ([x,y,z] is a list) each one already define and you can use it directly, that mean use x and y and z directly. - +As an expert in Groovy script development, your task is to convert Gherkin syntax into a Groovy script. Please ensure the script is well-crafted, free of any errors, and takes into consideration both input and output from the user. +The inputs will be provided in the form of a list (for instance, [x,y,z]), and it's important to note that all variables within the list are predefined - you can therefore utilize 'x', 'y', and 'z' directly in your script. +Please focus on producing only the Groovy script as the end result. You are required to incorporate the input and the output within the Groovy script itself, without creating any additional functions. Be sure to carefully structure your script so that it runs without producing any errors. +Please remember, the output should be the return value of the script. This means that the last evaluated expression in the script will be the output. Be sure to structure your script in a way that the desired output is the last evaluated expression. +We appreciate your attention to detail in ensuring this script is free of errors and well-structured to meet the task requirements. +if you want to define a funtion within the Groovy script call it the return value of the script. I need to directly use the return value. --- -Example1: acceptable senario -Inputs: [list_of_item] +Example: acceptable senario +Inputs: [gross_salary] Output: sum_of_amount_after_discount -Description: Given a list of items -When the total sum of the items exceeds 100 -Then a discount of 10% will be applied -And when the item 'XYZ' is included in the list -Then a discount of 5% will be given on the amount -Answer: -def totalSum = list_of_item.sum() -def sum_of_amount_after_discount = totalSum -if (totalSum > 100) - sum_of_amount_after_discount = totalSum - (totalSum * 0.1) - -if (list_of_item.contains('XYZ')) - sum_of_amount_after_discount = totalSum - (sum_of_amount_after_discount * 0.05) - -return sum_of_amount_after_discount ---- +Desciption: Given the user's gross salary +When the income is up to 10,722 +Then the salary is tax free +When the income is between 10,777 and 27,478 +Then the salary is taxed at 11% +When the income is between 27,478 and 78,570 +Then the salary is taxed at 30% +When the income is between 78,570 and 168,994 +Then the salary is taxed at 41% +When the income is above 168,994 +Then the salary is taxed at 45% +Result: +def net_salary_after_tax = 0 +if (gross_salary <= 10722) + net_salary_after_tax=gross_salary +else if (gross_salary> 10722 && gross_salary <= 27478) + net_salary_after_tax=gross_salary - (gross_salary * 0.11) +else if (gross_salary> 27478 && gross_salary <= 78570) + net_salary_after_tax=gross_salary - (gross_salary * 0.3) +else if (gross_salary> 78570 && gross_salary <= 168994) + net_salary_after_tax=gross_salary - (gross_salary * 0.41) +else if (gross_salary> 168994) + net_salary_after_tax = gross_salary - (gross_salary * 0.45) +return net_salary_after_tax -Example2: rejected senario (no inputs) -Inputs: [] -Output: sum_of_amount_after_discount -Description: 'Given a list of items -When the total sum of the items exceeds 100 -Then a discount of 10% will be applied -And when the item 'XYZ' is included in the list -Then a discount of 5% will be given on the amount' -Answer: CANT CONVERT --- - Inputs:{inputs} Output:{output} Description: {description} -Answer: ? +Result: ? --- \ No newline at end of file