Spaces:
Sleeping
Sleeping
File size: 8,107 Bytes
d232caa |
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 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import json\n",
"from dotenv import load_dotenv\n",
"from openai import OpenAI\n",
"from IPython.display import Markdown"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Refresh dot env"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"load_dotenv(override=True)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"open_api_key = os.getenv(\"OPENAI_API_KEY\")\n",
"google_api_key = os.getenv(\"GOOGLE_API_KEY\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Create initial query to get challange reccomendation"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"query = 'Please come up with a challenging, nuanced question that I can ask a number of LLMs to evaluate their intelligence. '\n",
"query += 'Answer only with the question, no explanation.'\n",
"\n",
"messages = [{'role':'user', 'content':query}]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(messages)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Call openai gpt-4o-mini "
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"openai = OpenAI()\n",
"\n",
"response = openai.chat.completions.create(\n",
" messages=messages,\n",
" model='gpt-4o-mini'\n",
")\n",
"\n",
"challange = response.choices[0].message.content\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(challange)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"competitors = []\n",
"answers = []"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Create messages with the challange query"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"messages = [{'role':'user', 'content':challange}]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(messages)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!ollama pull llama3.2"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"from threading import Thread"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"def gpt_mini_processor():\n",
" modleName = 'gpt-4o-mini'\n",
" competitors.append(modleName)\n",
" response_gpt = openai.chat.completions.create(\n",
" messages=messages,\n",
" model=modleName\n",
" )\n",
" answers.append(response_gpt.choices[0].message.content)\n",
"\n",
"def gemini_processor():\n",
" gemini = OpenAI(api_key=google_api_key, base_url='https://generativelanguage.googleapis.com/v1beta/openai/')\n",
" modleName = 'gemini-2.0-flash'\n",
" competitors.append(modleName)\n",
" response_gemini = gemini.chat.completions.create(\n",
" messages=messages,\n",
" model=modleName\n",
" )\n",
" answers.append(response_gemini.choices[0].message.content)\n",
"\n",
"def llama_processor():\n",
" ollama = OpenAI(base_url='http://localhost:11434/v1', api_key='ollama')\n",
" modleName = 'llama3.2'\n",
" competitors.append(modleName)\n",
" response_llama = ollama.chat.completions.create(\n",
" messages=messages,\n",
" model=modleName\n",
" )\n",
" answers.append(response_llama.choices[0].message.content)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Paraller execution of LLM calls"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"thread1 = Thread(target=gpt_mini_processor)\n",
"thread2 = Thread(target=gemini_processor)\n",
"thread3 = Thread(target=llama_processor)\n",
"\n",
"thread1.start()\n",
"thread2.start()\n",
"thread3.start()\n",
"\n",
"thread1.join()\n",
"thread2.join()\n",
"thread3.join()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(competitors)\n",
"print(answers)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for competitor, answer in zip(competitors, answers):\n",
" print(f'Competitor:{competitor}\\n\\n{answer}')"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"together = ''\n",
"for index, answer in enumerate(answers):\n",
" together += f'# Response from competitor {index + 1}\\n\\n'\n",
" together += answer + '\\n\\n'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(together)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Prompt to judge the LLM results"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"to_judge = f'''You are judging a competition between {len(competitors)} competitors.\n",
"Each model has been given this question:\n",
"\n",
"{challange}\n",
"\n",
"Your job is to evaluate each response for clarity and strength of argument, and rank them in order of best to worst.\n",
"Respond with JSON, and only JSON, with the following format:\n",
"{{\"results\": [\"best competitor number\", \"second best competitor number\", \"third best competitor number\", ...]}}\n",
"\n",
"Here are the responses from each competitor:\n",
"\n",
"{together}\n",
"\n",
"Now respond with the JSON with the ranked order of the competitors, nothing else. Do not include markdown formatting or code blocks.\"\"\"\n",
"\n",
"'''"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"to_judge_message = [{'role':'user', 'content':to_judge}]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Execute o3-mini to analyze the LLM results"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"openai = OpenAI()\n",
"response = openai.chat.completions.create(\n",
" messages=to_judge_message,\n",
" model='o3-mini'\n",
")\n",
"result = response.choices[0].message.content\n",
"print(result)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"results_dict = json.loads(result)\n",
"ranks = results_dict[\"results\"]\n",
"for index, result in enumerate(ranks):\n",
" competitor = competitors[int(result)-1]\n",
" print(f\"Rank {index+1}: {competitor}\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"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.12.10"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|