Skip to content

Commit

Permalink
Additional cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonWeill committed Jun 5, 2023
1 parent dca3c20 commit 6ca61c0
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 55 deletions.
99 changes: 62 additions & 37 deletions examples/commands.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,31 @@
{
"cell_type": "code",
"execution_count": 3,
"id": "1f249bdc-410b-42f4-b21b-b7bde9e06387",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Usage: %ai register [OPTIONS] NAME TARGET\n",
"\n",
" Register a new alias called NAME for the model or chain named TARGET.\n",
"\n",
"Options:\n",
" --help Show this message and exit.\n"
]
}
],
"source": [
"%ai register --help"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "0f073caa-265d-40d6-b537-d025b8df9f41",
"metadata": {},
"outputs": [
Expand All @@ -117,9 +142,9 @@
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mUsageError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[3], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mget_ipython\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrun_line_magic\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mai\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mfoo\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n",
"Cell \u001b[0;32mIn[4], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mget_ipython\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrun_line_magic\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mai\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mfoo\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n",
"File \u001b[0;32m/opt/miniconda3/envs/jupyter-ai/lib/python3.10/site-packages/IPython/core/interactiveshell.py:2417\u001b[0m, in \u001b[0;36mInteractiveShell.run_line_magic\u001b[0;34m(self, magic_name, line, _stack_depth)\u001b[0m\n\u001b[1;32m 2415\u001b[0m kwargs[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mlocal_ns\u001b[39m\u001b[38;5;124m'\u001b[39m] \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mget_local_scope(stack_depth)\n\u001b[1;32m 2416\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mbuiltin_trap:\n\u001b[0;32m-> 2417\u001b[0m result \u001b[38;5;241m=\u001b[39m \u001b[43mfn\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 2419\u001b[0m \u001b[38;5;66;03m# The code below prevents the output from being displayed\u001b[39;00m\n\u001b[1;32m 2420\u001b[0m \u001b[38;5;66;03m# when using magics with decodator @output_can_be_silenced\u001b[39;00m\n\u001b[1;32m 2421\u001b[0m \u001b[38;5;66;03m# when the last Python token in the expression is a ';'.\u001b[39;00m\n\u001b[1;32m 2422\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mgetattr\u001b[39m(fn, magic\u001b[38;5;241m.\u001b[39mMAGIC_OUTPUT_CAN_BE_SILENCED, \u001b[38;5;28;01mFalse\u001b[39;00m):\n",
"File \u001b[0;32m~/git/jupyter-ai/packages/jupyter-ai-magics/jupyter_ai_magics/magics.py:415\u001b[0m, in \u001b[0;36mAiMagics.ai\u001b[0;34m(self, line, cell)\u001b[0m\n\u001b[1;32m 413\u001b[0m args \u001b[38;5;241m=\u001b[39m cell_magic_parser(raw_args, prog_name\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;132;01m%%\u001b[39;00m\u001b[38;5;124mai\u001b[39m\u001b[38;5;124m\"\u001b[39m, standalone_mode\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mFalse\u001b[39;00m)\n\u001b[1;32m 414\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 415\u001b[0m args \u001b[38;5;241m=\u001b[39m \u001b[43mline_magic_parser\u001b[49m\u001b[43m(\u001b[49m\u001b[43mraw_args\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mprog_name\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;132;43;01m%a\u001b[39;49;00m\u001b[38;5;124;43mi\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mstandalone_mode\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mFalse\u001b[39;49;00m\u001b[43m)\u001b[49m\n\u001b[1;32m 417\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m args \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m0\u001b[39m:\n\u001b[1;32m 418\u001b[0m \u001b[38;5;66;03m# this happens when `--help` is called on the root command, in which\u001b[39;00m\n\u001b[1;32m 419\u001b[0m \u001b[38;5;66;03m# case we want to exit early.\u001b[39;00m\n\u001b[1;32m 420\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m\n",
"File \u001b[0;32m~/git/jupyter-ai/packages/jupyter-ai-magics/jupyter_ai_magics/magics.py:398\u001b[0m, in \u001b[0;36mAiMagics.ai\u001b[0;34m(self, line, cell)\u001b[0m\n\u001b[1;32m 396\u001b[0m args \u001b[38;5;241m=\u001b[39m cell_magic_parser(raw_args, prog_name\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;132;01m%%\u001b[39;00m\u001b[38;5;124mai\u001b[39m\u001b[38;5;124m\"\u001b[39m, standalone_mode\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mFalse\u001b[39;00m)\n\u001b[1;32m 397\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 398\u001b[0m args \u001b[38;5;241m=\u001b[39m \u001b[43mline_magic_parser\u001b[49m\u001b[43m(\u001b[49m\u001b[43mraw_args\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mprog_name\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;132;43;01m%a\u001b[39;49;00m\u001b[38;5;124;43mi\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mstandalone_mode\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mFalse\u001b[39;49;00m\u001b[43m)\u001b[49m\n\u001b[1;32m 400\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m args \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m0\u001b[39m:\n\u001b[1;32m 401\u001b[0m \u001b[38;5;66;03m# this happens when `--help` is called on the root command, in which\u001b[39;00m\n\u001b[1;32m 402\u001b[0m \u001b[38;5;66;03m# case we want to exit early.\u001b[39;00m\n\u001b[1;32m 403\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m\n",
"File \u001b[0;32m/opt/miniconda3/envs/jupyter-ai/lib/python3.10/site-packages/click/core.py:1130\u001b[0m, in \u001b[0;36mBaseCommand.__call__\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 1128\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__call__\u001b[39m(\u001b[38;5;28mself\u001b[39m, \u001b[38;5;241m*\u001b[39margs: t\u001b[38;5;241m.\u001b[39mAny, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs: t\u001b[38;5;241m.\u001b[39mAny) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m t\u001b[38;5;241m.\u001b[39mAny:\n\u001b[1;32m 1129\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"Alias for :meth:`main`.\"\"\"\u001b[39;00m\n\u001b[0;32m-> 1130\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmain\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n",
"File \u001b[0;32m/opt/miniconda3/envs/jupyter-ai/lib/python3.10/site-packages/click/core.py:1055\u001b[0m, in \u001b[0;36mBaseCommand.main\u001b[0;34m(self, args, prog_name, complete_var, standalone_mode, windows_expand_args, **extra)\u001b[0m\n\u001b[1;32m 1053\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m 1054\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mmake_context(prog_name, args, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mextra) \u001b[38;5;28;01mas\u001b[39;00m ctx:\n\u001b[0;32m-> 1055\u001b[0m rv \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43minvoke\u001b[49m\u001b[43m(\u001b[49m\u001b[43mctx\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1056\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m standalone_mode:\n\u001b[1;32m 1057\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m rv\n",
"File \u001b[0;32m/opt/miniconda3/envs/jupyter-ai/lib/python3.10/site-packages/click/core.py:1651\u001b[0m, in \u001b[0;36mMultiCommand.invoke\u001b[0;34m(self, ctx)\u001b[0m\n\u001b[1;32m 1647\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mchain:\n\u001b[1;32m 1648\u001b[0m \u001b[38;5;66;03m# Make sure the context is entered so we do not clean up\u001b[39;00m\n\u001b[1;32m 1649\u001b[0m \u001b[38;5;66;03m# resources until the result processor has worked.\u001b[39;00m\n\u001b[1;32m 1650\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m ctx:\n\u001b[0;32m-> 1651\u001b[0m cmd_name, cmd, args \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mresolve_command\u001b[49m\u001b[43m(\u001b[49m\u001b[43mctx\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43margs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1652\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m cmd \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[1;32m 1653\u001b[0m ctx\u001b[38;5;241m.\u001b[39minvoked_subcommand \u001b[38;5;241m=\u001b[39m cmd_name\n",
Expand All @@ -135,7 +160,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 5,
"id": "bad2d8a8-6141-4247-9af7-7583426c59a6",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -233,7 +258,7 @@
"gpt4 - openai-chat:gpt-4\n"
]
},
"execution_count": 4,
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -244,7 +269,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 6,
"id": "4d84fcac-7348-4c02-9ec3-34b300ec8459",
"metadata": {},
"outputs": [
Expand All @@ -270,7 +295,7 @@
"\n"
]
},
"execution_count": 5,
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -291,7 +316,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 7,
"id": "539263a2-1c30-4338-9622-bdc500c17830",
"metadata": {},
"outputs": [
Expand All @@ -304,7 +329,7 @@
"Registered new alias `mychat`"
]
},
"execution_count": 6,
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -315,7 +340,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 8,
"id": "4a453ca5-9f33-4393-a936-d7bc6c4c8f63",
"metadata": {
"tags": []
Expand Down Expand Up @@ -417,7 +442,7 @@
"mychat - openai-chat:gpt-4\n"
]
},
"execution_count": 7,
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -428,7 +453,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 9,
"id": "ea75ac14-d83d-45b4-8c1d-40e4fbc1d263",
"metadata": {
"tags": []
Expand All @@ -439,13 +464,13 @@
"text/markdown": [
"\n",
"\n",
"This is not a specific model."
"This model is **unspecified**."
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"execution_count": 8,
"execution_count": 9,
"metadata": {
"text/markdown": {
"jupyter_ai": {
Expand All @@ -464,7 +489,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 10,
"id": "3e7fb7fb-d61c-4909-959b-14bc97af409a",
"metadata": {},
"outputs": [
Expand All @@ -477,7 +502,7 @@
"Updated target of alias `mychat`"
]
},
"execution_count": 9,
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -488,7 +513,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 11,
"id": "a89a4c2a-b37f-4a3d-86cb-67747113d3d7",
"metadata": {
"tags": []
Expand All @@ -505,7 +530,7 @@
"<IPython.core.display.Markdown object>"
]
},
"execution_count": 10,
"execution_count": 11,
"metadata": {
"text/markdown": {
"jupyter_ai": {
Expand All @@ -524,7 +549,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 12,
"id": "c7453152-6c70-4f91-bef6-4ae38d700f52",
"metadata": {
"tags": []
Expand Down Expand Up @@ -626,7 +651,7 @@
"mychat - openai:text-davinci-003\n"
]
},
"execution_count": 11,
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -637,7 +662,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 13,
"id": "971f823d-8f35-4201-a6cb-12a21a87628a",
"metadata": {
"tags": []
Expand All @@ -652,7 +677,7 @@
"Deleted alias `mychat`"
]
},
"execution_count": 12,
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -663,7 +688,7 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 14,
"id": "6fb1b54a-ee4f-41c0-b666-ea9e0a1279ee",
"metadata": {
"tags": []
Expand Down Expand Up @@ -763,7 +788,7 @@
"gpt4 - openai-chat:gpt-4\n"
]
},
"execution_count": 13,
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -784,7 +809,7 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 15,
"id": "9ef639c7-1ca8-48af-b5e5-f76cfa5779f3",
"metadata": {
"tags": []
Expand All @@ -805,7 +830,7 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 16,
"id": "29d5239f-7601-405e-b059-4e881ebf7ab1",
"metadata": {
"tags": []
Expand All @@ -818,7 +843,7 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 17,
"id": "43e7a77c-93af-4ef7-a104-f932c9f54183",
"metadata": {
"tags": []
Expand All @@ -830,7 +855,7 @@
"text": [
"\n",
"\n",
"Rainbow Footwear.\n"
"Brightly Socks\n"
]
}
],
Expand All @@ -841,7 +866,7 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 18,
"id": "9badc567-9720-4e33-ab4a-54fda5129f36",
"metadata": {
"tags": []
Expand All @@ -856,7 +881,7 @@
"Registered new alias `company`"
]
},
"execution_count": 17,
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -867,7 +892,7 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 19,
"id": "92b75d71-8844-4872-b424-b0023706abb1",
"metadata": {
"tags": []
Expand Down Expand Up @@ -969,7 +994,7 @@
"company - custom chain\n"
]
},
"execution_count": 18,
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -980,7 +1005,7 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 20,
"id": "cfef0fee-a7c6-49e4-8d90-9aa12f7b91d1",
"metadata": {},
"outputs": [
Expand All @@ -989,13 +1014,13 @@
"text/markdown": [
"\n",
"\n",
"**Kaleidosocks**"
"BrightSox Co."
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"execution_count": 19,
"execution_count": 20,
"metadata": {
"text/markdown": {
"jupyter_ai": {
Expand All @@ -1013,7 +1038,7 @@
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": 21,
"id": "06c698e7-e2cf-41b5-88de-2be4d3b60eba",
"metadata": {},
"outputs": [
Expand All @@ -1022,10 +1047,10 @@
"text/plain": [
"\n",
"\n",
"Fancy Footwear Socks."
"Bright Socks Co."
]
},
"execution_count": 20,
"execution_count": 21,
"metadata": {
"jupyter_ai": {
"custom_chain_id": "company"
Expand Down
19 changes: 1 addition & 18 deletions packages/jupyter-ai-magics/jupyter_ai_magics/magics.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,6 @@ def _repr_mimebundle_(self, include=None, exclude=None):

NA_MESSAGE = '<abbr title="Not applicable">N/A</abbr>'

REGISTER_COMMAND_USAGE = "%ai register NAME TARGET"

DELETE_COMMAND_USAGE = "%ai delete NAME"

UPDATE_COMMAND_USAGE = "%ai update NAME TARGET"

MARKDOWN_PROMPT_TEMPLATE = '{prompt}\n\nProduce output in markdown format only.'

PROVIDER_NO_MODELS = 'This provider does not define a list of models.'
Expand All @@ -100,18 +94,7 @@ def _repr_mimebundle_(self, include=None, exclude=None):
"text": '{prompt}' # No customization
}

# Value is of the format (description, sample usage)
AI_COMMANDS = {
"help": ("Display a list of supported commands", "%ai help"),
"list": ("Display a list of models that you can use (optionally, for a single provider)",
"%ai list [PROVIDER_ID]"),
"register": ("Add an alias to a model or a LangChain chain for use with %ai magic commands",
REGISTER_COMMAND_USAGE),
"delete": ("Delete a previously registered alias",
DELETE_COMMAND_USAGE),
"update": ("Update a previously registered alias",
UPDATE_COMMAND_USAGE)
}
AI_COMMANDS = {"help", "list", "register", "delete", "update" }

class FormatDict(dict):
"""Subclass of dict to be passed to str#format(). Suppresses KeyError and
Expand Down

0 comments on commit 6ca61c0

Please sign in to comment.