Update code-gen ntbk

This commit is contained in:
Lance Martin
2024-05-26 15:09:30 -07:00
parent 149ebd0896
commit 765e35f24a
@@ -83,11 +83,12 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 2,
"id": "949efd30-44c7-4a4c-a05f-eca4e2769a61",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"os.environ[\"LANGCHAIN_PROJECT\"] = \"Mistral-code-gen-testing\""
]
},
@@ -103,7 +104,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 3,
"id": "a188c8ca-c053-4e6d-b7af-38a3b6b371c7",
"metadata": {},
"outputs": [],
@@ -186,7 +187,7 @@
},
{
"cell_type": "code",
"execution_count": 23,
"execution_count": 4,
"id": "183d77b8-f180-4815-b39f-8ef507ec0534",
"metadata": {},
"outputs": [],
@@ -222,7 +223,7 @@
},
{
"cell_type": "code",
"execution_count": 107,
"execution_count": 10,
"id": "14bc89d1-3ca6-4847-a048-1803e0e4600e",
"metadata": {},
"outputs": [],
@@ -295,7 +296,7 @@
" exec(imports)\n",
" except Exception as e:\n",
" print(\"---CODE IMPORT CHECK: FAILED---\")\n",
" error_message = [(\"user\", f\"Your solution failed the import test. Here is the error: {e}. Reflect on this error and your prior attempt to solve the problem. Now, try to solve this problem again. Return the FULL SOLUTION. Use the code tool to structure the output with a prefix, imports, and code block:\")]\n",
" error_message = [(\"user\", f\"Your solution failed the import test. Here is the error: {e}. Reflect on this error and your prior attempt to solve the problem. (1) State what you think went wrong with the prior solution and (2) try to solve this problem again. Return the FULL SOLUTION. Use the code tool to structure the output with a prefix, imports, and code block:\")]\n",
" messages += error_message\n",
" return {\n",
" \"generation\": code_solution,\n",
@@ -306,11 +307,14 @@
"\n",
" # Check execution\n",
" try:\n",
" print(f'CODE TO TEST: {imports} \\n {code}')\n",
" exec(imports + \"\\n\" + code)\n",
" combined_code = f\"{imports}\\n{code}\"\n",
" print(f\"CODE TO TEST: {combined_code}\")\n",
" # Use a shared scope for exec\n",
" global_scope = {}\n",
" exec(combined_code, global_scope)\n",
" except Exception as e:\n",
" print(\"---CODE BLOCK CHECK: FAILED---\")\n",
" error_message = [(\"user\", f\"Your solution failed the code execution test: {e}) Reflect on this error and your prior attempt to solve the problem. Now, try to solve this problem again. Return the FULL SOLUTION. Use the code tool to structure the output with a prefix, imports, and code block:\")]\n",
" error_message = [(\"user\", f\"Your solution failed the code execution test: {e}) Reflect on this error and your prior attempt to solve the problem. (1) State what you think went wrong with the prior solution and (2) try to solve this problem again. Return the FULL SOLUTION. Use the code tool to structure the output with a prefix, imports, and code block:\")]\n",
" messages += error_message\n",
" return {\n",
" \"generation\": code_solution,\n",
@@ -348,12 +352,31 @@
" return \"end\"\n",
" else:\n",
" print(\"---DECISION: RE-TRY SOLUTION---\")\n",
" return \"generate\""
" return \"generate\"\n",
"\n",
"### Utilities\n",
"\n",
"import uuid \n",
"\n",
"def _print_event(event: dict, _printed: set, max_length=1500):\n",
" current_state = event.get(\"dialog_state\")\n",
" if current_state:\n",
" print(f\"Currently in: \", current_state[-1])\n",
" message = event.get(\"messages\")\n",
" if message:\n",
" if isinstance(message, list):\n",
" message = message[-1]\n",
" if message.id not in _printed:\n",
" msg_repr = message.pretty_repr(html=True)\n",
" if len(msg_repr) > max_length:\n",
" msg_repr = msg_repr[:max_length] + \" ... (truncated)\"\n",
" print(msg_repr)\n",
" _printed.add(message.id)"
]
},
{
"cell_type": "code",
"execution_count": 108,
"execution_count": 11,
"id": "2dff2209-44c7-4e2c-b607-ba6675f9e45f",
"metadata": {},
"outputs": [],
@@ -385,7 +408,7 @@
},
{
"cell_type": "code",
"execution_count": 109,
"execution_count": 12,
"id": "d4bb21cd-af20-4d4d-89ff-384db034b7c3",
"metadata": {},
"outputs": [
@@ -417,23 +440,6 @@
"metadata": {},
"outputs": [],
"source": [
"import uuid \n",
"\n",
"def _print_event(event: dict, _printed: set, max_length=1500):\n",
" current_state = event.get(\"dialog_state\")\n",
" if current_state:\n",
" print(f\"Currently in: \", current_state[-1])\n",
" message = event.get(\"messages\")\n",
" if message:\n",
" if isinstance(message, list):\n",
" message = message[-1]\n",
" if message.id not in _printed:\n",
" msg_repr = message.pretty_repr(html=True)\n",
" if len(msg_repr) > max_length:\n",
" msg_repr = msg_repr[:max_length] + \" ... (truncated)\"\n",
" print(msg_repr)\n",
" _printed.add(message.id)\n",
"\n",
"_printed = set()\n",
"thread_id = str(uuid.uuid4())\n",
"config = {\n",
@@ -506,7 +512,91 @@
{
"cell_type": "code",
"execution_count": null,
"id": "d5539d9d-b538-438b-941f-44ea8dd83f5d",
"id": "0a3f946b-e2f2-44d9-905b-09f36980cf9f",
"metadata": {},
"outputs": [],
"source": [
"_printed = set()\n",
"thread_id = str(uuid.uuid4())\n",
"config = {\n",
" \"configurable\": {\n",
" # Checkpoints are accessed by thread_id\n",
" \"thread_id\": thread_id,\n",
" }\n",
"}\n",
"\n",
"question = \"\"\"Write a program that prints the numbers from 1 to 100. \n",
"But for multiples of three, print \"Fizz\" instead of the number, and for the multiples of five, print \"Buzz\". \n",
"For numbers which are multiples of both three and five, print \"FizzBuzz\".\"\"\"\n",
"\n",
"events = graph.stream(\n",
" {\"messages\": [(\"user\", question)], \"iterations\": 0}, config, stream_mode=\"values\"\n",
")\n",
"for event in events:\n",
" _print_event(event, _printed)"
]
},
{
"cell_type": "markdown",
"id": "8f3ef03b-0a07-49f5-9cbf-15e2503d020e",
"metadata": {},
"source": [
"Trace: \n",
"\n",
"https://smith.langchain.com/public/f5c19708-7592-4512-9f00-9696ab34a9eb/r"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2bb883df-540b-46ab-9415-fe27db68456f",
"metadata": {},
"outputs": [],
"source": [
"import uuid\n",
"_printed = set()\n",
"thread_id = str(uuid.uuid4())\n",
"config = {\n",
" \"configurable\": {\n",
" # Checkpoints are accessed by thread_id\n",
" \"thread_id\": thread_id,\n",
" }\n",
"}\n",
"\n",
"question = '''I want to vectorize a function\n",
"\n",
" frame = np.zeros((out_h, out_w, 3), dtype=np.uint8)\n",
" for i, val1 in enumerate(rows):\n",
" for j, val2 in enumerate(cols):\n",
" for j, val3 in enumerate(ch):\n",
" # Assuming you want to store the pair as tuples in the matrix\n",
" frame[i, j, k] = image[val1, val2, val3]\n",
"\n",
" out.write(np.array(frame))\n",
"\n",
"with a simple numpy function that does something like this what is it called. Show me a test case with this working.'''\n",
"\n",
"events = graph.stream(\n",
" {\"messages\": [(\"user\", question)], \"iterations\": 0}, config, stream_mode=\"values\"\n",
")\n",
"for event in events:\n",
" _print_event(event, _printed)"
]
},
{
"cell_type": "markdown",
"id": "750a3292-1e0e-49cf-8b28-bef179afe6a2",
"metadata": {},
"source": [
"Trace w/ good example of self-correction:\n",
"\n",
"https://smith.langchain.com/public/b54778a0-d267-4f09-bc28-71761201c522/r"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ee05da1f-c272-405d-8a7b-552cfc3106e1",
"metadata": {},
"outputs": [],
"source": [
@@ -540,26 +630,18 @@
},
{
"cell_type": "markdown",
"id": "0ed15639-d5bd-4d7d-8597-3965747dcbee",
"id": "3d900cd6-2df9-467d-8e74-803527269008",
"metadata": {},
"source": [
"Trace: failure to recover from error\n",
"Trace w/ good example of failure to correct:\n",
"\n",
"https://smith.langchain.com/public/583eb8ab-709a-409e-8b95-8a3b1b105df0/r"
"https://smith.langchain.com/public/871ae736-2f77-44d4-b0da-a600d8f5377d/r"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0a3f946b-e2f2-44d9-905b-09f36980cf9f",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "d7c01c65-0e00-45df-a4d2-1191eddf938d",
"id": "814fc2a4-8e5b-4faa-8f52-3977226bd09a",
"metadata": {},
"outputs": [],
"source": []