Skip to content

Commit

Permalink
Adds %ai error magic command to explain the most recent error (#170)
Browse files Browse the repository at this point in the history
* Adds `%ai error` command, documents it

* Updates docs

* Updates to use `click` command parser

* Updates commands workbook
  • Loading branch information
JasonWeill authored Jun 5, 2023
1 parent 7963080 commit b142127
Show file tree
Hide file tree
Showing 5 changed files with 366 additions and 83 deletions.
6 changes: 6 additions & 0 deletions docs/source/users/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -542,3 +542,9 @@ produced the following Python error:
Write a new version of this code that does not produce that error.
```

As a shortcut for explaining errors, you can use the `%ai error` command, which will explain the most recent error using the model of your choice.

```
%ai error anthropic:claude-v1.2
```

218 changes: 200 additions & 18 deletions examples/commands.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -41,32 +41,95 @@
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Usage: %%ai [OPTIONS] MODEL_ID\n",
"\n",
" Invokes a language model identified by MODEL_ID, with the prompt being\n",
" contained in all lines after the first. Both local model IDs and global\n",
" model IDs (with the provider ID explicitly prefixed, followed by a colon)\n",
" are accepted.\n",
"\n",
" To view available language models, please run `%ai list`.\n",
"\n",
"Options:\n",
" -f, --format [code|html|image|json|markdown|math|md|text]\n",
" IPython display to use when rendering\n",
" output. [default=\"markdown\"]\n",
" -r, --reset Clears the conversation transcript used when\n",
" interacting with an OpenAI chat model\n",
" provider. Does nothing with other providers.\n",
" --help Show this message and exit.\n",
"------------------------------------------------------------------------------\n",
"Usage: %%ai [OPTIONS] MODEL_ID\n",
"\n",
" Invokes a language model identified by MODEL_ID, with the prompt being\n",
" contained in all lines after the first. Both local model IDs and global\n",
" model IDs (with the provider ID explicitly prefixed, followed by a colon)\n",
" are accepted.\n",
"\n",
" To view available language models, please run `%ai list`.\n",
"\n",
"Options:\n",
" -f, --format [code|html|image|json|markdown|math|md|text]\n",
" IPython display to use when rendering\n",
" output. [default=\"markdown\"]\n",
" -r, --reset Clears the conversation transcript used when\n",
" interacting with an OpenAI chat model\n",
" provider. Does nothing with other providers.\n",
" --help Show this message and exit.\n",
"------------------------------------------------------------------------------\n",
"Usage: %ai [OPTIONS] COMMAND [ARGS]...\n",
"\n",
" Invokes a subcommand.\n",
"\n",
"Options:\n",
" --help Show this message and exit.\n",
"\n",
"Commands:\n",
" error Explains the most recent error.\n",
" help Show this message and exit.\n",
" list List language models. See `%ai list --help` for options.\n",
"\n"
]
}
],
"source": [
"%ai help"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "e1f2b767-0834-4b21-b132-093730efaffb",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/markdown": [
"| Command | Description |\n",
"| ------- | ----------- |\n",
"| `help` | Display a list of supported commands|\n",
"| `list` | Display a list of models that you can use (optionally, for a single provider)|\n"
"There have been no errors since the kernel started."
],
"text/plain": [
"help - Display a list of supported commands\n",
"list - Display a list of models that you can use (optionally, for a single provider)\n"
"There have been no errors since the kernel started."
]
},
"execution_count": 2,
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%ai help"
"%ai error chatgpt"
]
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 4,
"id": "0f073caa-265d-40d6-b537-d025b8df9f41",
"metadata": {},
"outputs": [
Expand All @@ -75,19 +138,19 @@
"text/markdown": [
"Cannot determine model provider from model ID `foo`.\n",
"\n",
"To see a list of models you can use, run `%ai list`.\n",
"To see a list of models you can use, run `%ai list`\n",
"\n",
"If you were trying to run a command, run `%ai help` to see a list of commands."
],
"text/plain": [
"Cannot determine model provider from model ID 'foo'.\n",
"\n",
"To see a list of models you can use, run '%ai list'.\n",
"To see a list of models you can use, run '%ai list'\n",
"\n",
"If you were trying to run a command, run '%ai help' to see a list of commands."
]
},
"execution_count": 3,
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -98,7 +161,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 5,
"id": "bad2d8a8-6141-4247-9af7-7583426c59a6",
"metadata": {},
"outputs": [
Expand All @@ -113,6 +176,7 @@
"| `huggingface_hub` | `HUGGINGFACEHUB_API_TOKEN` | <abbr title=\"You have set this environment variable, so you can use this provider's models.\">✅</abbr> | This provider does not define a list of models. |\n",
"| `openai` | `OPENAI_API_KEY` | <abbr title=\"You have set this environment variable, so you can use this provider's models.\">✅</abbr> | `openai:text-davinci-003`, `openai:text-davinci-002`, `openai:text-curie-001`, `openai:text-babbage-001`, `openai:text-ada-001`, `openai:davinci`, `openai:curie`, `openai:babbage`, `openai:ada` |\n",
"| `openai-chat` | `OPENAI_API_KEY` | <abbr title=\"You have set this environment variable, so you can use this provider's models.\">✅</abbr> | `openai-chat:gpt-4`, `openai-chat:gpt-4-0314`, `openai-chat:gpt-4-32k`, `openai-chat:gpt-4-32k-0314`, `openai-chat:gpt-3.5-turbo`, `openai-chat:gpt-3.5-turbo-0301` |\n",
"| `openai-chat-new` | `OPENAI_API_KEY` | <abbr title=\"You have set this environment variable, so you can use this provider's models.\">✅</abbr> | `openai-chat-new:gpt-4`, `openai-chat-new:gpt-4-0314`, `openai-chat-new:gpt-4-32k`, `openai-chat-new:gpt-4-32k-0314`, `openai-chat-new:gpt-3.5-turbo`, `openai-chat-new:gpt-3.5-turbo-0301` |\n",
"| `sagemaker-endpoint` | Not applicable. | <abbr title=\"Not applicable\">N/A</abbr> | This provider does not define a list of models. |\n"
],
"text/plain": [
Expand Down Expand Up @@ -166,12 +230,21 @@
"* openai-chat:gpt-3.5-turbo\n",
"* openai-chat:gpt-3.5-turbo-0301\n",
"\n",
"openai-chat-new\n",
"Requires environment variable OPENAI_API_KEY (set)\n",
"* openai-chat-new:gpt-4\n",
"* openai-chat-new:gpt-4-0314\n",
"* openai-chat-new:gpt-4-32k\n",
"* openai-chat-new:gpt-4-32k-0314\n",
"* openai-chat-new:gpt-3.5-turbo\n",
"* openai-chat-new:gpt-3.5-turbo-0301\n",
"\n",
"sagemaker-endpoint\n",
"* This provider does not define a list of models.\n",
"\n"
]
},
"execution_count": 4,
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -182,7 +255,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 6,
"id": "4d84fcac-7348-4c02-9ec3-34b300ec8459",
"metadata": {},
"outputs": [
Expand All @@ -208,7 +281,7 @@
"\n"
]
},
"execution_count": 5,
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -219,9 +292,118 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"id": "b56ff0e3-42c2-4927-affd-be6a089dfa43",
"metadata": {},
"outputs": [
{
"ename": "SyntaxError",
"evalue": "Missing parentheses in call to 'print'. Did you mean print(...)? (1142230402.py, line 1)",
"output_type": "error",
"traceback": [
"\u001b[0;36m Cell \u001b[0;32mIn[7], line 1\u001b[0;36m\u001b[0m\n\u001b[0;31m print 'foo'\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m Missing parentheses in call to 'print'. Did you mean print(...)?\n"
]
}
],
"source": [
"print 'foo'"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "63581a74-9237-4f11-aff2-48612c97cb27",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/markdown": [
"The error message \"SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?\", occurs when attempting to use the print function without parentheses in Python 3, which is not syntactically valid. In Python 2, it was possible to use the print statement without parentheses, but in Python 3 it has become a function and its use requires parentheses. The error message suggests that the print statement should be replaced with print() to rectify the error."
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"execution_count": 8,
"metadata": {
"text/markdown": {
"jupyter_ai": {
"model_id": "gpt-3.5-turbo",
"provider_id": "openai-chat"
}
}
},
"output_type": "execute_result"
}
],
"source": [
"%ai error chatgpt"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "1afe4536-f908-4bd7-aec4-f8d1cc3bf01f",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/opt/miniconda3/envs/jupyter-ai/lib/python3.10/site-packages/langchain/llms/anthropic.py:134: UserWarning: This Anthropic LLM is deprecated. Please use `from langchain.chat_models import ChatAnthropic` instead\n",
" warnings.warn(\n"
]
},
{
"data": {
"text/markdown": [
"\n",
"The error `Cell In[7], line 1 print 'foo' ^ SyntaxError: Missing parentheses in call to 'print' . Did you mean print(...)?` \n",
"is occurring because in Python 3, the `print` statement has changed. \n",
"\n",
"In Python 2, you could simply do:\n",
"`print 'foo'`\n",
"\n",
"to print the string `foo`. \n",
"\n",
"However, in Python 3, the `print` function requires parentheses:\n",
"`print('foo')`\n",
"\n",
"So the error is telling you that you're trying to use the Python 2 `print` statement in Python 3 code. \n",
"It's suggesting that you likely meant to call the `print()` function instead, with parentheses: \n",
"`print('foo')`.\n",
"\n",
"Adding the parentheses will fix the error."
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"execution_count": 9,
"metadata": {
"text/markdown": {
"jupyter_ai": {
"model_id": "claude-v1.2",
"provider_id": "anthropic"
}
}
},
"output_type": "execute_result"
}
],
"source": [
"%ai error anthropic:claude-v1.2"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d47aa5ec-31e0-4670-b1d1-6b4cd7f75832",
"metadata": {},
"outputs": [],
"source": []
}
Expand All @@ -242,7 +424,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.16"
"version": "3.10.8"
}
},
"nbformat": 4,
Expand Down
Loading

0 comments on commit b142127

Please sign in to comment.