Skip to content

Commit

Permalink
Shorter system instructions for basic code generation prompting examp…
Browse files Browse the repository at this point in the history
…le. (google-gemini#186)

* Reformat notebook

* Reformat notebook

* Update notebook with shorter prompt

* Reformatted notebook with shorter prompt
  • Loading branch information
shilpakancharla authored Jun 4, 2024
1 parent d8750c6 commit bc1b100
Showing 1 changed file with 14 additions and 25 deletions.
39 changes: 14 additions & 25 deletions examples/prompting/Basic_Code_Generation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"metadata": {
"cellView": "form",
"id": "vQoUR__bUlfj"
Expand Down Expand Up @@ -66,7 +66,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 1,
"metadata": {
"id": "Ne-3gnXqR0hI"
},
Expand All @@ -77,7 +77,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 2,
"metadata": {
"id": "EconMHePQHGw"
},
Expand All @@ -101,7 +101,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 3,
"metadata": {
"id": "v-JZzORUpVR2"
},
Expand Down Expand Up @@ -133,45 +133,34 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 4,
"metadata": {
"id": "kVF8ZQ38Vs1P"
},
"outputs": [],
"source": [
"error_handling_system_prompt =f\"\"\"\n",
"You are a coding assistant tasked with error handling. Your task is to provide:\n",
"\n",
"Explanation:\n",
"Explain why this error occurred. Provide insights into common causes of the error.\n",
"For each common cause, provide a code example of how this error arose.\n",
"\n",
"Possible Solutions:\n",
"Offer a potential solution to resolve the error.\n",
"\n",
"Example Code:\n",
"Optionally, include example code snippets demonstrating correct usage or\n",
"implementation.\n",
"Your task is to explain exactly why this error occurred and how to fix it.\n",
"\"\"\"\n",
"error_handling_model = genai.GenerativeModel(model_name='gemini-1.5-flash-latest', generation_config={\"temperature\": 0},\n",
" system_instruction=error_handling_system_prompt)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 5,
"metadata": {
"id": "CHTdAVE0pIFf"
},
"outputs": [
{
"data": {
"text/markdown": "## Explanation:\n\nThe error \"IndexError: list index out of range\" occurs when you try to access an element in a list using an index that is outside the valid range of indices for that list.\n\n**Common Causes:**\n\n1. **Incorrect Index:** You are trying to access an element at an index that doesn't exist in the list. \n * **Example:**\n ```python\n my_list = [1, 2, 3]\n print(my_list[3]) # Trying to access the 4th element (index 3), which doesn't exist.\n ```\n\n2. **Negative Index Out of Range:** You are using a negative index to access an element from the end of the list, but the index is too large (i.e., more negative than the length of the list).\n * **Example:**\n ```python\n my_list = [1, 2, 3]\n print(my_list[-4]) # Trying to access the 4th element from the end, which doesn't exist.\n ```\n\n3. **List Modification During Iteration:** You are modifying the list while iterating over it, which can lead to unexpected index errors.\n * **Example:**\n ```python\n my_list = [1, 2, 3]\n for i in range(len(my_list)):\n if my_list[i] == 2:\n my_list.pop(i) # Removing an element while iterating can cause index errors.\n print(my_list[i])\n ```\n\n## Possible Solutions:\n\n1. **Check the Index:** Ensure that the index you are using to access the element is within the valid range of the list. You can use `len(my_list)` to get the length of the list and make sure your index is less than that.\n * **Example:**\n ```python\n my_list = [1, 2, 3]\n if 3 < len(my_list):\n print(my_list[3])\n else:\n print(\"Index out of range\")\n ```\n\n2. **Use Negative Indices Carefully:** When using negative indices, remember that `-1` refers to the last element, `-2` to the second-to-last, and so on. Make sure your negative index is within the valid range.\n * **Example:**\n ```python\n my_list = [1, 2, 3]\n if -1 >= -len(my_list):\n print(my_list[-1])\n else:\n print(\"Index out of range\")\n ```\n\n3. **Avoid Modifying Lists During Iteration:** If you need to modify a list while iterating over it, consider using a copy of the list or iterating over a range of indices instead of directly modifying the list during iteration.\n * **Example:**\n ```python\n my_list = [1, 2, 3]\n for i in range(len(my_list)):\n if my_list[i] == 2:\n my_list.pop(i) # Removing an element while iterating can cause index errors.\n print(my_list[i])\n ```\n\n **Solution:**\n ```python\n my_list = [1, 2, 3]\n for i in range(len(my_list)):\n if my_list[i] == 2:\n my_list.pop(i)\n break # Stop iterating after removing the element\n print(my_list[i])\n ```\n\nBy understanding the common causes of this error and implementing the appropriate solutions, you can avoid \"IndexError: list index out of range\" and ensure your code runs smoothly. \n",
"text/markdown": "The error message \"IndexError: list index out of range\" means you're trying to access an element in a list using an index that doesn't exist.\n\n**Explanation:**\n\n* **List Indexing:** In Python, lists are zero-indexed. This means the first element has an index of 0, the second element has an index of 1, and so on.\n* **Your Code:** In your code, `my_list = [1, 2, 3]` has three elements. The valid indices for this list are 0, 1, and 2.\n* **The Error:** You're trying to access `my_list[3]`. Since the list only has three elements, there is no element at index 3. This causes the \"IndexError: list index out of range\" error.\n\n**How to Fix It:**\n\n1. **Check the Index:** Ensure the index you're using is within the valid range of the list. In this case, you should use an index between 0 and 2.\n2. **Adjust the Code:** To access the last element of the list, use `my_list[2]`.\n\n**Corrected Code:**\n\n```python\nmy_list = [1, 2, 3]\nprint(my_list[2]) # This will print 3\n```\n\n**Important Note:** Always be mindful of the size of your lists and the indices you use to avoid this common error. \n",
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"execution_count": 6,
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
Expand Down Expand Up @@ -202,7 +191,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 6,
"metadata": {
"id": "1T1QSzjVVvE_"
},
Expand All @@ -221,7 +210,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 7,
"metadata": {
"id": "8KVpzExDqRj2"
},
Expand All @@ -233,7 +222,7 @@
"<IPython.core.display.Markdown object>"
]
},
"execution_count": 8,
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -255,7 +244,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 8,
"metadata": {
"id": "lOU_abTPSmZu"
},
Expand Down Expand Up @@ -297,7 +286,7 @@
],
"metadata": {
"colab": {
"name": "Basic_Code_Generation.ipynb",
"name": "Basic_code_generation.ipynb",
"toc_visible": true
},
"kernelspec": {
Expand Down

0 comments on commit bc1b100

Please sign in to comment.