File size: 27,693 Bytes
3b595c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "## SN20 BitAgent\n",
    "\n",
    "### Setting up ...\n",
    "- importing standard libraries + bittensor, no special sauce required\n",
    "- fetching subnet 20\n",
    "- setting up wallet and validator\n",
    "- getting top miner\n",
    "- providing protocol (QnATask)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[34m2024-04-07 17:47:36.518\u001b[0m | \u001b[1m      INFO      \u001b[0m | You are connecting to finney network with endpoint wss://entrypoint-finney.opentensor.ai:443.\n",
      "\u001b[34m2024-04-07 17:47:36.519\u001b[0m | \u001b[33m\u001b[1m    WARNING     \u001b[0m | We strongly encourage running a local subtensor node whenever possible. This increases decentralization and resilience of the network.\n",
      "\u001b[34m2024-04-07 17:47:36.519\u001b[0m | \u001b[33m\u001b[1m    WARNING     \u001b[0m | In a future release, local subtensor will become the default endpoint. To get ahead of this change, please run a local subtensor node and point to it.\n",
      "\u001b[34m2024-04-07 17:47:36.833\u001b[0m | \u001b[1m      INFO      \u001b[0m | Connected to finney network and wss://entrypoint-finney.opentensor.ai:443.\n",
      "Top Miner UID for Subnet 20:  197\n"
     ]
    }
   ],
   "source": [
    "import asyncio\n",
    "import requests\n",
    "import bittensor as bt \n",
    "from rich import print as rprint\n",
    "from typing import Optional,List\n",
    "\n",
    "# working with subnet 20 / upsilon / BitAgent\n",
    "subnet = bt.metagraph(netuid=20)\n",
    "\n",
    "# Wallet and validator setup\n",
    "WALLET_NAME = \"TODO\" # TODO\n",
    "HOTKEY_NAME = \"TODO\" # TODO\n",
    "vali_wallet =  bt.wallet(name=WALLET_NAME, hotkey=HOTKEY_NAME)\n",
    "vali_dendrite = bt.dendrite(wallet=vali_wallet)\n",
    "\n",
    "# get the TOP miner on the subnet\n",
    "top_miner_uid = int(subnet.I.argmax())\n",
    "print(\"Top Miner UID for Subnet 20: \", top_miner_uid)\n",
    "\n",
    "# the request protocol\n",
    "class QnATask(bt.Synapse):\n",
    "    urls: List[str] = []   # not used right now\n",
    "    datas: List[dict] = [] # used to pass in relevant context, could be a company knowledge base or a set of wikipedia pages\n",
    "    prompt: str = \"\"       # the query / prompt\n",
    "    response: Optional[dict] = {}\n",
    "    timeout: Optional[float] = 3.0\n",
    "    miner_uids: Optional[List[int]] = [top_miner_uid] # put our TOP miner into the network as the miner to query (if empty list, a random list of miners will be selected)\n",
    "\n",
    "    def toJSON(self):\n",
    "        return {\"prompt\": self.prompt, \n",
    "                \"urls\": self.urls, \n",
    "                \"datas\": self.datas, \n",
    "                \"response\": self.response,\n",
    "                \"miner_uids\": self.miner_uids,\n",
    "                \"dendrite_process_time\": self.dendrite.process_time,\n",
    "                \"dendrite_status_code\": self.dendrite.status_code,\n",
    "                \"axon_status_code\": self.axon.status_code,}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Two ways to query SN20 \n",
    "\n",
    "### First way is to use your registered validator to query directly to the TOP miner\n",
    "- build a task (QnATask) with a \"prompt\" and optional \"datas\"\n",
    "- query the network\n",
    "- see response answer (1)\n",
    "- see top citation (2)\n",
    "- see full response object (3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 - Response showing answer from miner: \n",
      "\t  Meow eats grapes, berries, and occasional bananas.\n",
      "2 - Response's topmost relevant citation from miner: \n",
      "\t [{'context': 'meow prefers to eat grapes and berries, but also eats the occassional banana', 'source': 'source 3'}]\n",
      "3 - Full response: \n",
      "\t [QnATask(timeout=3.0, urls=[], datas=[{'source': 'source 1', 'context': 'Some irrelevant context'}, {'source': 'source 2', 'context': 'meow is a monkey in the jungle'}, {'source': 'source 3', 'context': 'meow prefers to eat grapes and berries, but also eats the occassional banana'}, {'source': 'source 4', 'context': 'meow climbs trees for fun'}, {'source': 'source 5', 'context': 'meow is afraid of snakes, but loves bunnies'}], prompt='hey, what does the meow eat?', response={'response': ' Meow eats grapes, berries, and occasional bananas.', 'citations': [{'context': 'meow prefers to eat grapes and berries, but also eats the occassional banana', 'source': 'source 3'}], 'context': 'meow prefers to eat grapes and berries, but also eats the occassional banana'}, miner_uids=[197])]\n"
     ]
    }
   ],
   "source": [
    "task = QnATask(prompt=\"hey, what does the meow eat?\", \n",
    "               datas=[{\"source\": \"source 1\", \"context\": \"Some irrelevant context\"},\n",
    "                      {\"source\": \"source 2\", \"context\": \"meow is a monkey in the jungle\"},\n",
    "                      {\"source\": \"source 3\", \"context\": \"meow prefers to eat grapes and berries, but also eats the occassional banana\"},\n",
    "                      {\"source\": \"source 4\", \"context\": \"meow climbs trees for fun\"},\n",
    "                      {\"source\": \"source 5\", \"context\": \"meow is afraid of snakes, but loves bunnies\"}])\n",
    "\n",
    "responses = vali_dendrite.query(\n",
    "    axons=[subnet.axons[top_miner_uid]],\n",
    "    synapse=task,\n",
    "    deserialize=False,\n",
    "    timeout=task.timeout,\n",
    ")\n",
    "\n",
    "response = responses[0].response\n",
    "print(\"1 - Response showing answer from miner: \\n\\t\", response['response'])\n",
    "print(\"2 - Response's topmost relevant citation from miner: \\n\\t\", response['citations'])\n",
    "print(\"3 - Full response: \\n\\t\", responses)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Second way is to use your validator (or any wallet) to query one of the subnet validators\n",
    "- we'll use the same QnATask from above, but we'll change the prompt\n",
    "- query the network via validator axon\n",
    "- we are specifying the miner uid for our QnATask to be the TOP miner uid\n",
    "- see response answer (1)\n",
    "- see top citation (2)\n",
    "- see full response object (3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 - Response showing answer: \n",
      "\t  Meow fears snakes the most.\n",
      "2 - Response's topmost relevant citation: \n",
      "\t [{'context': 'meow is afraid of snakes, but loves bunnies', 'source': 'source 5'}]\n",
      "3 - Full response: \n",
      "\t [QnATask(timeout=3.0, urls=[], datas=[{'source': 'source 1', 'context': 'Some irrelevant context'}, {'source': 'source 2', 'context': 'meow is a monkey in the jungle'}, {'source': 'source 3', 'context': 'meow prefers to eat grapes and berries, but also eats the occassional banana'}, {'source': 'source 4', 'context': 'meow climbs trees for fun'}, {'source': 'source 5', 'context': 'meow is afraid of snakes, but loves bunnies'}], prompt='What does meow fear the most?', response={'response': ' Meow fears snakes the most.', 'citations': [{'context': 'meow is afraid of snakes, but loves bunnies', 'source': 'source 5'}], 'context': 'meow is afraid of snakes, but loves bunnies'}, miner_uids=[])]\n"
     ]
    }
   ],
   "source": [
    "task.prompt = \"What does meow fear the most?\"\n",
    "\n",
    "responses = vali_dendrite.query(\n",
    "    axons=[subnet.axons[0]],\n",
    "    synapse=task,\n",
    "    deserialize=False,\n",
    "    timeout=task.timeout,\n",
    ")\n",
    "response = responses[0].response\n",
    "print(\"1 - Response showing answer: \\n\\t\", response['response'])\n",
    "print(\"2 - Response's topmost relevant citation: \\n\\t\", response['citations'])\n",
    "print(\"3 - Full response: \\n\\t\", responses)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Demonstration of all task types and scoring / incentives ...\n",
    "## Generating tasks from the Task API\n",
    "We'll get tasks from the Task API that are far more complicated than the one above - \n",
    "- Summarization Task\n",
    "- QnA with Citations Task\n",
    "- QnA Logic Task"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "### Setting up ...\n",
    "- methods to \n",
    "  - get the top miner, \n",
    "  - fetch a task \n",
    "  - get miner response and \n",
    "  - evaluate a task\n",
    "- setup task IDs for QnA with Citations, Pet Tricks and Summarization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "qna_task = (3,1)\n",
    "pet_tricks_task = (6,6)\n",
    "summarization_task = (8,1)\n",
    "\n",
    "def get_top_miner_uid(subnet):\n",
    "    return subnet.I.argmax()\n",
    "\n",
    "def get_task(task_id, sub_task_id):\n",
    "    # keep trying in case it's being restarted \n",
    "    while True:\n",
    "        try:\n",
    "            resp = requests.post(\"https://roguetensor.com/api/task_api/get_new_task\", json={\"task_id\": task_id, \"sub_task_id\": sub_task_id}).json()\n",
    "            return resp\n",
    "        except:\n",
    "            pass\n",
    "\n",
    "def eval_task(task_id, response):\n",
    "    # keep trying in case it's being restarted \n",
    "    while True:\n",
    "        try:\n",
    "            resp = requests.post(\"https://roguetensor.com/api/task_api/evaluate_task_response\", json={\"task_id\": task_id, \"response\": response.toJSON()}).json()\n",
    "            return resp\n",
    "        except:\n",
    "            pass\n",
    "\n",
    "def get_miner_response_to_task(subnet, validator, miner_uid, task):\n",
    "    print(\"Fetching response from TOP miner: \", miner_uid)\n",
    "\n",
    "    response = None\n",
    "    while not response:\n",
    "        response = asyncio.run(validator.call(\n",
    "            # Send the query to selected miner axons in the network.\n",
    "            target_axon=subnet.axons[miner_uid],\n",
    "            # Construct a query. \n",
    "            synapse=task,\n",
    "            # All responses have the deserialize function called on them before returning.\n",
    "            # You are encouraged to define your own deserialization function.\n",
    "            deserialize=True,\n",
    "            timeout=25.0\n",
    "        ))\n",
    "        return response\n",
    "\n",
    "def evaluate_miner(subnet, validator, miner_uid, task_id, sub_task_id):\n",
    "    task_json = get_task(task_id, sub_task_id)\n",
    "    gen_task_id = task_json[\"task\"][\"task_id\"]\n",
    "    task = QnATask(prompt=task_json['task']['prompt'], datas=task_json['task']['datas'], urls=task_json['task']['urls'])\n",
    "    print(\"Got task with prompt: \", task_json['task']['prompt'][:60] + \" ...\")\n",
    "    miner_response = get_miner_response_to_task(subnet, validator, miner_uid, task)\n",
    "    print(\"Miner response:\", miner_response.response['response'][:100] + \" ...\")\n",
    "    eval = eval_task(gen_task_id, miner_response)\n",
    "    return miner_response, *eval[\"result\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Generated Summary Task Example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Got task with prompt:  Summarize this and make sure to be concise:  Did you attend  ...\n",
      "Fetching response from TOP miner:  197\n",
      "Miner response:  Pamela and Marie both did not attend the independence march. Pamela stayed at home due to concerns  ...\n",
      "Scores:  2.25 2.25\n"
     ]
    }
   ],
   "source": [
    "task_id, sub_task_id = summarization_task\n",
    "miner_response, score, max_score, results, correct_answer_optional = evaluate_miner(subnet, vali_dendrite, top_miner_uid, task_id, sub_task_id)\n",
    "print(\"Scores: \", score, max_score)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### The results the miner would see:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #000080; text-decoration-color: #000080; font-weight: bold\">Does not error</span>\n",
       "βœ… <span style=\"color: #008000; text-decoration-color: #008000\">You successfully responded to the request.</span>\n",
       "You received <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0.25</span> of <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0.25</span> reward.\n",
       "<span style=\"color: #000080; text-decoration-color: #000080; font-weight: bold\">Does not take a long time</span>\n",
       "βœ… <span style=\"color: #008000; text-decoration-color: #008000\">You responded to the request in </span><span style=\"color: #008000; text-decoration-color: #008000; font-weight: bold\">0.8873927593231201</span><span style=\"color: #008000; text-decoration-color: #008000\">.</span>\n",
       "You received <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0.5</span> of <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0.5</span> reward.\n",
       "<span style=\"color: #000080; text-decoration-color: #000080; font-weight: bold\">Return summary shorter than original</span>\n",
       "βœ… <span style=\"color: #008000; text-decoration-color: #008000\">You responded with a valid summary length.</span>\n",
       "You received <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0.5</span> of <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0.5</span> reward.\n",
       "<span style=\"color: #000080; text-decoration-color: #000080; font-weight: bold\">Return valid summary</span>\n",
       "βœ… <span style=\"color: #008000; text-decoration-color: #008000\">You responded with a valid summary.</span>\n",
       "You received <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">1.0</span> of <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">1.0</span> reward.\n",
       "</pre>\n"
      ],
      "text/plain": [
       "\u001b[1;34mDoes not error\u001b[0m\n",
       "βœ… \u001b[32mYou successfully responded to the request.\u001b[0m\n",
       "You received \u001b[1;36m0.25\u001b[0m of \u001b[1;36m0.25\u001b[0m reward.\n",
       "\u001b[1;34mDoes not take a long time\u001b[0m\n",
       "βœ… \u001b[32mYou responded to the request in \u001b[0m\u001b[1;32m0.8873927593231201\u001b[0m\u001b[32m.\u001b[0m\n",
       "You received \u001b[1;36m0.5\u001b[0m of \u001b[1;36m0.5\u001b[0m reward.\n",
       "\u001b[1;34mReturn summary shorter than original\u001b[0m\n",
       "βœ… \u001b[32mYou responded with a valid summary length.\u001b[0m\n",
       "You received \u001b[1;36m0.5\u001b[0m of \u001b[1;36m0.5\u001b[0m reward.\n",
       "\u001b[1;34mReturn valid summary\u001b[0m\n",
       "βœ… \u001b[32mYou responded with a valid summary.\u001b[0m\n",
       "You received \u001b[1;36m1.0\u001b[0m of \u001b[1;36m1.0\u001b[0m reward.\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "rprint((\"\\n\").join(results)) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Generated QnA with Citations Task Example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Got task with prompt:   \"What was the purpose of integrating Crashlytics into Fireb ...\n",
      "Fetching response from TOP miner:  197\n",
      "Miner response:  The purpose of integrating Crashlytics into Firebase was to bring the best of both platforms togeth ...\n",
      "Scores:  5.25 5.25\n"
     ]
    }
   ],
   "source": [
    "task_id, sub_task_id = qna_task\n",
    "miner_response, score, max_score, results, correct_answer_optional = evaluate_miner(subnet, vali_dendrite, top_miner_uid, task_id, sub_task_id)\n",
    "print(\"Scores: \", score, max_score)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### The results the miner would see:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #000080; text-decoration-color: #000080; font-weight: bold\">Does not error</span>\n",
       "βœ… <span style=\"color: #008000; text-decoration-color: #008000\">You successfully responded to the request.</span>\n",
       "You received <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0.25</span> of <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0.25</span> reward.\n",
       "<span style=\"color: #000080; text-decoration-color: #000080; font-weight: bold\">Does not take a long time</span>\n",
       "βœ… <span style=\"color: #008000; text-decoration-color: #008000\">You responded to the request in </span><span style=\"color: #008000; text-decoration-color: #008000; font-weight: bold\">0.8485991954803467</span><span style=\"color: #008000; text-decoration-color: #008000\">.</span>\n",
       "You received <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0.5</span> of <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0.5</span> reward.\n",
       "<span style=\"color: #000080; text-decoration-color: #000080; font-weight: bold\">Returns expected citation source(s)</span>\n",
       "βœ… <span style=\"color: #008000; text-decoration-color: #008000\">You correctly identified some or all of the correct citation sources </span><span style=\"color: #008000; text-decoration-color: #008000; font-weight: bold\">(</span><span style=\"color: #008000; text-decoration-color: #008000; font-weight: bold\">1</span><span style=\"color: #008000; text-decoration-color: #008000\">/</span><span style=\"color: #008000; text-decoration-color: #008000; font-weight: bold\">1</span><span style=\"color: #008000; text-decoration-color: #008000\"> identified</span><span style=\"color: #008000; text-decoration-color: #008000; font-weight: bold\">)</span><span style=\"color: #008000; text-decoration-color: #008000\">.</span>\n",
       "You received <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">1.5</span> of <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">1.5</span> reward.\n",
       "<span style=\"color: #000080; text-decoration-color: #000080; font-weight: bold\">Returns a relevant response</span>\n",
       "βœ… <span style=\"color: #008000; text-decoration-color: #008000\">You responded with a relevant response compared to the context.</span>\n",
       "You received <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">1.0</span> of <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">1.0</span> reward.\n",
       "<span style=\"color: #000080; text-decoration-color: #000080; font-weight: bold\">Returns a unique response</span>\n",
       "βœ… <span style=\"color: #008000; text-decoration-color: #008000\">You responded with a novel response compared to the context.</span>\n",
       "You received <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">1.0</span> of <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">1.0</span> reward.\n",
       "<span style=\"color: #000080; text-decoration-color: #000080; font-weight: bold\">Returns valid response</span>\n",
       "βœ… <span style=\"color: #008000; text-decoration-color: #008000\">You responded with a valid response.</span>\n",
       "You received <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">1.0</span> of <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">1.0</span> reward.\n",
       "</pre>\n"
      ],
      "text/plain": [
       "\u001b[1;34mDoes not error\u001b[0m\n",
       "βœ… \u001b[32mYou successfully responded to the request.\u001b[0m\n",
       "You received \u001b[1;36m0.25\u001b[0m of \u001b[1;36m0.25\u001b[0m reward.\n",
       "\u001b[1;34mDoes not take a long time\u001b[0m\n",
       "βœ… \u001b[32mYou responded to the request in \u001b[0m\u001b[1;32m0.8485991954803467\u001b[0m\u001b[32m.\u001b[0m\n",
       "You received \u001b[1;36m0.5\u001b[0m of \u001b[1;36m0.5\u001b[0m reward.\n",
       "\u001b[1;34mReturns expected citation \u001b[0m\u001b[1;34msource\u001b[0m\u001b[1;34m(\u001b[0m\u001b[1;34ms\u001b[0m\u001b[1;34m)\u001b[0m\n",
       "βœ… \u001b[32mYou correctly identified some or all of the correct citation sources \u001b[0m\u001b[1;32m(\u001b[0m\u001b[1;32m1\u001b[0m\u001b[32m/\u001b[0m\u001b[1;32m1\u001b[0m\u001b[32m identified\u001b[0m\u001b[1;32m)\u001b[0m\u001b[32m.\u001b[0m\n",
       "You received \u001b[1;36m1.5\u001b[0m of \u001b[1;36m1.5\u001b[0m reward.\n",
       "\u001b[1;34mReturns a relevant response\u001b[0m\n",
       "βœ… \u001b[32mYou responded with a relevant response compared to the context.\u001b[0m\n",
       "You received \u001b[1;36m1.0\u001b[0m of \u001b[1;36m1.0\u001b[0m reward.\n",
       "\u001b[1;34mReturns a unique response\u001b[0m\n",
       "βœ… \u001b[32mYou responded with a novel response compared to the context.\u001b[0m\n",
       "You received \u001b[1;36m1.0\u001b[0m of \u001b[1;36m1.0\u001b[0m reward.\n",
       "\u001b[1;34mReturns valid response\u001b[0m\n",
       "βœ… \u001b[32mYou responded with a valid response.\u001b[0m\n",
       "You received \u001b[1;36m1.0\u001b[0m of \u001b[1;36m1.0\u001b[0m reward.\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "rprint((\"\\n\").join(results)) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Generated Pet Tricks QnA Logic Task Example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Got task with prompt:  Given the following Trick Descriptions with numerical IDs:\n",
      "  ...\n",
      "Fetching response from TOP miner:  197\n",
      "Miner response: 4 ...\n",
      "Scores:  1.75 1.75\n"
     ]
    }
   ],
   "source": [
    "task_id, sub_task_id = pet_tricks_task\n",
    "miner_response, score, max_score, results, correct_answer_optional = evaluate_miner(subnet, vali_dendrite, top_miner_uid, task_id, sub_task_id)\n",
    "print(\"Scores: \", score, max_score)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### The results the miner would see:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #000080; text-decoration-color: #000080; font-weight: bold\">Does not error</span>\n",
       "βœ… <span style=\"color: #008000; text-decoration-color: #008000\">You successfully responded to the request.</span>\n",
       "You received <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0.25</span> of <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0.25</span> reward.\n",
       "<span style=\"color: #000080; text-decoration-color: #000080; font-weight: bold\">Does not take a long time</span>\n",
       "βœ… <span style=\"color: #008000; text-decoration-color: #008000\">You responded to the request in </span><span style=\"color: #008000; text-decoration-color: #008000; font-weight: bold\">0.3985426425933838</span><span style=\"color: #008000; text-decoration-color: #008000\">.</span>\n",
       "You received <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0.5</span> of <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0.5</span> reward.\n",
       "<span style=\"color: #000080; text-decoration-color: #000080; font-weight: bold\">Returns expected value</span>\n",
       "βœ… <span style=\"color: #008000; text-decoration-color: #008000\">You responded with a valid answer.</span>\n",
       "You received <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">1.0</span> of <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">1.0</span> reward.\n",
       "</pre>\n"
      ],
      "text/plain": [
       "\u001b[1;34mDoes not error\u001b[0m\n",
       "βœ… \u001b[32mYou successfully responded to the request.\u001b[0m\n",
       "You received \u001b[1;36m0.25\u001b[0m of \u001b[1;36m0.25\u001b[0m reward.\n",
       "\u001b[1;34mDoes not take a long time\u001b[0m\n",
       "βœ… \u001b[32mYou responded to the request in \u001b[0m\u001b[1;32m0.3985426425933838\u001b[0m\u001b[32m.\u001b[0m\n",
       "You received \u001b[1;36m0.5\u001b[0m of \u001b[1;36m0.5\u001b[0m reward.\n",
       "\u001b[1;34mReturns expected value\u001b[0m\n",
       "βœ… \u001b[32mYou responded with a valid answer.\u001b[0m\n",
       "You received \u001b[1;36m1.0\u001b[0m of \u001b[1;36m1.0\u001b[0m reward.\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "rprint((\"\\n\").join(results)) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "## That's It!\n",
    "\n",
    "You saw how to:\n",
    " 1) Query the top miner uid\n",
    " 2) Demonstrate each reward/penalty mechanism/Scoring of the top miner response\n",
    " 3) Query the subnet 2 different ways (as a validator to a miner and through a registered validator axon)"
   ]
  }
 ],
 "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.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}