aboutsummaryrefslogtreecommitdiff
path: root/benchmark/notebooks/LLM Score Experimentation.ipynb
blob: b14577fdb702b63f50cf516f36831fb05f8438ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "15386da8",
   "metadata": {},
   "outputs": [],
   "source": [
    "import openai\n",
    "import os\n",
    "from dotenv import load_dotenv\n",
    "\n",
    "load_dotenv()\n",
    "\n",
    "OPENAI_API_KEY = os.getenv(\"OPENAI_API_KEY\")\n",
    "\n",
    "def llm_eval(evaluation: str) -> float:\n",
    "    openai.api_key = OPENAI_API_KEY\n",
    "    answer = openai.ChatCompletion.create(\n",
    "        model=\"gpt-4\",\n",
    "        messages=[\n",
    "            {\"role\": \"system\", \"content\": evaluation},\n",
    "        ],\n",
    "    )\n",
    "    return answer[\"choices\"][0][\"message\"][\"content\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "e9d1c3a6",
   "metadata": {},
   "outputs": [],
   "source": [
    "REFERENCE_PROMPT = \"\"\"Ignore previous directions. You are now an expert at evaluating how close machine generated responses are to human answers. You essentially act as a hyper advanced BLEU score.\n",
    "In order to score the machine generated response you will {scoring}. Make sure to factor in the distance to the ideal response into your thinking, deliberation, and final result regarding scoring. Return nothing but a float score.\n",
    "\n",
    "Here is the given task for you to evaluate:\n",
    "{task}\n",
    "\n",
    "Here is the ideal response you're comparing to based on the task:\n",
    "{answer}\n",
    "\n",
    "Here are some examples of how to score a machine generated response compared to the above ideal response:\n",
    "{examples}\n",
    "\n",
    "Here is the current machine generated response to the task that you need to evaluate:\n",
    "{response}\n",
    "\n",
    "Remember to always end your response with nothing but a float score.\n",
    "Float score:\n",
    "\"\"\"\n",
    "\n",
    "RUBRIC_PROMPT = \"\"\"Ignore previous directions. You are now an expert at evaluating machine generated responses to given tasks.\n",
    "In order to score the generated texts you will {scoring}. Make sure to factor in rubric into your thinking, deliberation, and final result regarding scoring. Return nothing but a float score.\n",
    "\n",
    "Here is the given task for you to evaluate:\n",
    "{task}\n",
    "\n",
    "Use the below rubric to guide your thinking about scoring:\n",
    "{answer}\n",
    "\n",
    "Here are some examples of how to score a machine generated response based on the above rubric.\n",
    "{examples}\n",
    "\n",
    "Here is the current machine generated response to the task that you need to evaluate:\n",
    "{response}\n",
    "\n",
    "Remember to always end your response with nothing but a float score.\n",
    "Float score:\n",
    "\"\"\"\n",
    "\n",
    "QUESTION_PROMPT = \"\"\"Ignore previous directions. You are now an expert at evaluating machine generated responses to given tasks.\n",
    "In order to score the generated texts you will {scoring}. Make sure to think about whether the generated response answers the question well in order to score accurately. Return nothing but a float score.\n",
    "\n",
    "Here is the given task:\n",
    "{task}\n",
    "\n",
    "Here is a question that checks if the task was completed correctly:\n",
    "{answer}\n",
    "\n",
    "Here are some examples of how to score a machine generated response based on task-response pairs.\n",
    "{examples}\n",
    "\n",
    "Here is the current machine generated response to the task that you need to evaluate:\n",
    "{response}\n",
    "\n",
    "Remember to always end your response with nothing but a float score.\n",
    "Float score:\n",
    "\"\"\"\n",
    "\n",
    "CUSTOM_PROMPT = \"\"\"{custom}\n",
    "{scoring}\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "8ff9cd33",
   "metadata": {},
   "outputs": [],
   "source": [
    "task = \"As an agent powered by a large language model, you can execute any digital action. Your task is to find the cheapest flight to San Francisco from Toronto. Create a step by step plan for yourself to execute this task and output this plan into a .txt file.\"\n",
    "\n",
    "percentage = \"assign a float score that will represent a percentage out of 100. Use decimal points to be even more accurate. 0 represents the worst possible generation, while 100 represents the ideal generation\"\n",
    "scale = \"assign an integer score from a scale of 1-10. 1 represents a really bad generation, while 10 represents an ideal generation\"\n",
    "binary = \"assign a binary score of either 0 or 1. 0 represents a failure, while 1 represents a success\"\n",
    "\n",
    "reference_answer = \"\"\"1. Search 'Toronto to San Francisco flights' on the internet\n",
    "2. Click on the first link that is a flight aggregator such as SkyScanner or Google Flights\n",
    "3. Select the dates that you want to travel\n",
    "3. Click on the 'one way' option and click search\n",
    "4. Search through all of the given options and select the cheapest flight\"\"\"\n",
    "\n",
    "rubric_answer = \"\"\"- Reasonable: The plan's steps should be arranged in a logical sequence. Each step should naturally follow from the one before it, leading coherently towards the desired outcome.\n",
    "- Atomic: Each step of the plan should be broken down into the smallest possible units of action. This level of detail allows for better tracking and control, as well as easier troubleshooting and adjustment of the plan if necessary.\n",
    "- Relevant: Each step should directly contribute to achieving the end goal of the plan. Irrelevant steps can waste resources and detract from the main objectives.\n",
    "- Timely: The plan should include a realistic timeline for each step, and not include any redundant steps.\"\"\"\n",
    "\n",
    "question_answer = \"Will this plan find the cheapest flight to San Francisco from Toronto consistently?\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "d0bfdce5",
   "metadata": {},
   "outputs": [],
   "source": [
    "FEW_SHOT_REFERENCE_EXAMPLES = \"\"\"EXAMPLE 1\n",
    "Plan: 'Define the Parameters:\n",
    "\n",
    "Identify the potential range of dates for the journey. Ensure flexibility to capitalize on the best deals.\n",
    "Research Airlines:\n",
    "\n",
    "Compile a list of airlines that operate flights between Toronto and San Francisco. This can be accomplished by checking each airline's website or using an airline directory.\n",
    "Use Comparison Tools:\n",
    "\n",
    "Use flight comparison websites (such as Skyscanner, Expedia, Google Flights, etc.) to compare flight prices across different airlines. These tools aggregate flight data and can present the cheapest options available.\n",
    "Set Price Alerts:\n",
    "\n",
    "If the trip is not immediate, set price alerts on these comparison websites. They will notify you when there is a significant drop in flight prices on the selected route.\n",
    "Check Airlines' Official Websites:\n",
    "\n",
    "After identifying potential flights via comparison tools, visit the airlines' official websites. Sometimes airlines offer deals or discounts on their own websites that are not reflected on comparison sites.\n",
    "Consider Nearby Airports:\n",
    "\n",
    "Check flight prices to and from airports in the vicinity of both Toronto and San Francisco. Sometimes less busy airports can offer cheaper flights.\n",
    "Evaluate Cost Effectiveness:\n",
    "\n",
    "Consider factors such as baggage fees, meal costs, and transportation to and from the airport when evaluating the total cost of the flight. The cheapest ticket price does not necessarily mean the least expensive journey overall.\n",
    "Book the Flight:\n",
    "\n",
    "Once the cheapest and most convenient flight has been identified, proceed to booking. Double-check the flight details before finalizing the booking.\n",
    "Monitor Flight Details:\n",
    "\n",
    "After booking, keep an eye on flight status, gate information, and any potential changes to the flight schedule.'\n",
    "Returned score: 82.7\n",
    "Internal rationale: The plan is solid, however the plan goes to extreme lengths to make things cheap, sacrificing time and simplicity. The task just asks for a vague definition of booking a flight. There are some redundant steps.\n",
    "\n",
    "EXAMPLE 2\n",
    "Plan: 'Determine the Travel Dates and Flexibility: Decide on the dates you want to travel to San Francisco and check if you have any flexibility in your travel schedule. Being flexible with your travel dates can often lead to finding cheaper flights.\n",
    "\n",
    "Use Flight Search Engines: Start by using popular flight search engines like Google Flights, Skyscanner, Kayak, or Expedia. These platforms allow you to compare prices from various airlines and find the most affordable options.\n",
    "\n",
    "Set Up Fare Alerts: If your travel dates are flexible, consider setting up fare alerts on the flight search engines. These alerts will notify you when the prices drop for the specified route.\n",
    "\n",
    "Check Nearby Airports: In both Toronto and San Francisco, there might be multiple airports. Check flights departing from nearby airports as they may offer better deals.\n",
    "\n",
    "Consider Layovers: Non-stop flights are usually more convenient but can be more expensive. Look for flights with one or more layovers as they may offer cost savings.\n",
    "\n",
    "Check Airlines' Official Websites: Once you find a potentially cheap flight on a search engine, verify the price directly on the airline's official website. Sometimes, booking directly with the airline can be cheaper due to exclusive deals and promotions.\n",
    "\n",
    "Use Incognito/Private Browsing Mode: Flight prices can sometimes increase if the website detects repeated searches for the same route. To avoid this, use the incognito or private browsing mode in your web browser.\n",
    "\n",
    "Consider Budget Airlines: Check if there are any budget airlines flying between Toronto and San Francisco. They often offer lower fares, but be mindful of additional fees for baggage and other services.\n",
    "\n",
    "Check for Deals and Promo Codes: Look for any ongoing deals or promo codes that can help you save on your flight booking. Airlines and travel websites occasionally offer special discounts.\n",
    "\n",
    "Be Flexible with Departure and Arrival Times: If possible, consider flying during off-peak hours or mid-week, as flights during these times can be less expensive.\n",
    "\n",
    "Factor in Total Costs: While searching for cheap flights, don't forget to consider other expenses like baggage fees, seat selection, and additional amenities. Some budget airlines might have hidden costs that could make the overall trip more expensive.\n",
    "\n",
    "Book Early: Flight prices tend to rise as the departure date approaches. Once you find a good deal that suits your preferences, don't wait too long to book your flight.'\n",
    "Returned score: 74.9\n",
    "Internal rationale: The individual components of this plan are better than the one previous. But this plan doesn't follow logical steps to completion, and is just more general advice.\n",
    "\n",
    "EXAMPLE 3\n",
    "Plan: 'Search online for cheap flights.\n",
    "Check different dates.\n",
    "Look at nearby airports.\n",
    "Consider layovers.\n",
    "Try budget airlines.\n",
    "Book early if you find a good deal.'\n",
    "Returned score: 42.0\n",
    "Internal rationale: This plan is too vague and does not provide enough detail to be useful.\n",
    "\n",
    "\"\"\"\n",
    "FEW_SHOT_RUBRIC_EXAMPLES = \"\"\n",
    "FEW_SHOT_QUESTION_EXAMPLES = \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "3de1d6d4",
   "metadata": {},
   "outputs": [],
   "source": [
    "response1=\"\"\"Define the Parameters:\n",
    "\n",
    "Identify the potential range of dates for the journey. Ensure flexibility to capitalize on the best deals.\n",
    "Research Airlines:\n",
    "\n",
    "Compile a list of airlines that operate flights between Toronto and San Francisco. This can be accomplished by checking each airline's website or using an airline directory.\n",
    "Use Comparison Tools:\n",
    "\n",
    "Use flight comparison websites (such as Skyscanner, Expedia, Google Flights, etc.) to compare flight prices across different airlines. These tools aggregate flight data and can present the cheapest options available.\n",
    "Set Price Alerts:\n",
    "\n",
    "If the trip is not immediate, set price alerts on these comparison websites. They will notify you when there is a significant drop in flight prices on the selected route.\n",
    "Check Airlines' Official Websites:\n",
    "\n",
    "After identifying potential flights via comparison tools, visit the airlines' official websites. Sometimes airlines offer deals or discounts on their own websites that are not reflected on comparison sites.\n",
    "Consider Nearby Airports:\n",
    "\n",
    "Check flight prices to and from airports in the vicinity of both Toronto and San Francisco. Sometimes less busy airports can offer cheaper flights.\n",
    "Evaluate Cost Effectiveness:\n",
    "\n",
    "Consider factors such as baggage fees, meal costs, and transportation to and from the airport when evaluating the total cost of the flight. The cheapest ticket price does not necessarily mean the least expensive journey overall.\n",
    "Book the Flight:\n",
    "\n",
    "Once the cheapest and most convenient flight has been identified, proceed to booking. Double-check the flight details before finalizing the booking.\n",
    "Monitor Flight Details:\n",
    "\n",
    "After booking, keep an eye on flight status, gate information, and any potential changes to the flight schedule.\"\"\"\n",
    "\n",
    "response2=\"\"\"Determine the Travel Dates and Flexibility: Decide on the dates you want to travel to San Francisco and check if you have any flexibility in your travel schedule. Being flexible with your travel dates can often lead to finding cheaper flights.\n",
    "\n",
    "Use Flight Search Engines: Start by using popular flight search engines like Google Flights, Skyscanner, Kayak, or Expedia. These platforms allow you to compare prices from various airlines and find the most affordable options.\n",
    "\n",
    "Set Up Fare Alerts: If your travel dates are flexible, consider setting up fare alerts on the flight search engines. These alerts will notify you when the prices drop for the specified route.\n",
    "\n",
    "Check Nearby Airports: In both Toronto and San Francisco, there might be multiple airports. Check flights departing from nearby airports as they may offer better deals.\n",
    "\n",
    "Consider Layovers: Non-stop flights are usually more convenient but can be more expensive. Look for flights with one or more layovers as they may offer cost savings.\n",
    "\n",
    "Check Airlines' Official Websites: Once you find a potentially cheap flight on a search engine, verify the price directly on the airline's official website. Sometimes, booking directly with the airline can be cheaper due to exclusive deals and promotions.\n",
    "\n",
    "Use Incognito/Private Browsing Mode: Flight prices can sometimes increase if the website detects repeated searches for the same route. To avoid this, use the incognito or private browsing mode in your web browser.\n",
    "\n",
    "Consider Budget Airlines: Check if there are any budget airlines flying between Toronto and San Francisco. They often offer lower fares, but be mindful of additional fees for baggage and other services.\n",
    "\n",
    "Check for Deals and Promo Codes: Look for any ongoing deals or promo codes that can help you save on your flight booking. Airlines and travel websites occasionally offer special discounts.\n",
    "\n",
    "Be Flexible with Departure and Arrival Times: If possible, consider flying during off-peak hours or mid-week, as flights during these times can be less expensive.\n",
    "\n",
    "Factor in Total Costs: While searching for cheap flights, don't forget to consider other expenses like baggage fees, seat selection, and additional amenities. Some budget airlines might have hidden costs that could make the overall trip more expensive.\n",
    "\n",
    "Book Early: Flight prices tend to rise as the departure date approaches. Once you find a good deal that suits your preferences, don't wait too long to book your flight.\"\"\"\n",
    "\n",
    "response3 = \"\"\"Search online for cheap flights.\n",
    "Check different dates.\n",
    "Look at nearby airports.\n",
    "Consider layovers.\n",
    "Try budget airlines.\n",
    "Book early if you find a good deal.\"\"\"\n",
    "\n",
    "ideal_response = \"\"\"1. Search 'Toronto to San Francisco flights' on the internet\n",
    "2. Click on the first link that is a flight aggregator such as SkyScanner or Google Flights\n",
    "3. Select the dates that you want to travel\n",
    "3. Click on the 'one way' option and click search\n",
    "4. Search through all of the given options and select the cheapest flight\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a5bf2f5c",
   "metadata": {},
   "outputs": [],
   "source": [
    "reference_evaluation = REFERENCE_PROMPT.format(task=task, scoring=percentage, answer=reference_answer, response=ideal_response, examples=FEW_SHOT_REFERENCE_EXAMPLES)\n",
    "rubric_evaluation = RUBRIC_PROMPT.format(task=task, scoring=percentage, answer=rubric_answer, response=ideal_response, examples=FEW_SHOT_REFERENCE_EXAMPLES)\n",
    "question_evaluation = QUESTION_PROMPT.format(task=task, scoring=percentage, answer=question_answer, response=ideal_response, examples=FEW_SHOT_REFERENCE_EXAMPLES)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "1cd01a4c",
   "metadata": {},
   "outputs": [],
   "source": [
    "reference_response = llm_eval(reference_evaluation)\n",
    "rubric_response = llm_eval(rubric_evaluation)\n",
    "question_response = llm_eval(question_evaluation)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "72e6ac91",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "REFERENCE: 66.5 6.0 0.0\n",
      "RUBRIC: 65.0 4.0 0.0\n",
      "QUESTION: 87.0 6.5 0.0\n"
     ]
    }
   ],
   "source": [
    "# response3 - the worst one\n",
    "print('REFERENCE:', reference_response_percentage, reference_response_scale, reference_response_binary)\n",
    "print('RUBRIC:', rubric_response_percentage, rubric_response_scale, rubric_response_binary)\n",
    "print('QUESTION:', question_response_percentage, question_response_scale, question_response_binary)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "id": "a66014ee",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "REFERENCE: 100.0 10.0 1.0\n",
      "RUBRIC: 88.0 7.5 1.0\n",
      "QUESTION: 87.0 7.5 1.0\n"
     ]
    }
   ],
   "source": [
    "# reference_response - the best one\n",
    "print('REFERENCE:', reference_response_ideal_percentage, reference_response_ideal_scale, reference_response_ideal_binary)\n",
    "print('RUBRIC:', rubric_response_ideal_percentage, rubric_response_ideal_scale, rubric_response_ideal_binary)\n",
    "print('QUESTION:', question_response_ideal_percentage, question_response_ideal_scale, question_response_ideal_binary)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}