{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Example: generating and running code" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Warning: This notebook runs LLM-generated code without any checks. Run at your own risk." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Loading a code model:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "2023-12-05 21:22:33.078594: I tensorflow/core/util/port.cc:110] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.\n", "2023-12-05 21:22:33.148003: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.\n", "To enable the following instructions: AVX2 AVX512F AVX512_VNNI FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.\n" ] } ], "source": [ "from guidance import models, gen\n", "from guidance.library._gen import will_gen\n", "from guidance import capture, one_or_more, any_char, zero_or_more, commit_point, select\n", "import guidance\n", "import re\n", "base_path = '/home/marcotcr_google_com/work/models/'\n", "model_path = base_path + 'mistral-7b-codealpaca-lora.Q8_0.gguf'\n", "mistral = models.LlamaCpp(model_path, n_gpu_layers=-1, n_ctx=4096)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Loading the HumanEval dataset:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "from datasets import load_dataset\n", "dataset = load_dataset(\"openai_humaneval\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's write a very simple baseline" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "import re\n", "import guidance\n", "@guidance\n", "def baseline(lm, prompt):\n", " r = re.findall('def (.*?)\\(', prompt)\n", " name = r[-1]\n", " lm += f'Here is an implementation of {name}:\\n'\n", " lm += '```python\\n' + prompt + gen(max_tokens=800, stop=['```', 'if __name__', 'def test'], name='program')\n", " lm = lm.set('program', prompt + lm['program'])\n", " return lm " ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Here is an implementation of solution:\n",
"```python\n",
"\n",
"def solution(lst):\n",
" """Given a non-empty list of integers, return the sum of all of the odd elements that are in even positions.\n",
" \n",
"\n",
" Examples\n",
" solution([5, 8, 7, 1]) ==> 12\n",
" solution([3, 3, 3, 3, 3]) ==> 9\n",
" solution([30, 13, 24, 321]) ==>0\n",
" """\n",
" return sum(lst[i] for i in range(0, len(lst), 2) if lst[i] % 2 != 0)\n",
"\n",
"# test the function\n",
"print(solution([5, 8, 7, 1])) # should print 12\n",
"print(solution([3, 3, 3, 3, 3])) # should print 9\n",
"print(solution([30, 13, 24, 321])) # should print 0\n",
""
],
"text/plain": [
"Here is an implementation of triangle_area:\n",
"```python\n",
"\n",
"def triangle_area(a, b, c):\n",
" '''\n",
" Given the lengths of the three sides of a triangle. Return the area of\n",
" the triangle rounded to 2 decimal points if the three sides form a valid triangle. \n",
" Otherwise return -1\n",
" Three sides make a valid triangle when the sum of any two sides is greater \n",
" than the third side.\n",
" Example:\n",
" triangle_area(3, 4, 5) == 6.00\n",
" triangle_area(1, 2, 10) == -1\n",
" '''\n",
" if a + b > c and a + c > b and b + c > a:\n",
" s = (a + b + c) / 2\n",
" return round(s * (s - a) * (s - b) * (s - c), 2)\n",
" else:\n",
" return -1\n",
""
],
"text/plain": [
"```python\n",
"\n",
"def triangle_area(a, b, c):\n",
" '''\n",
" Given the lengths of the three sides of a triangle. Return the area of\n",
" the triangle rounded to 2 decimal points if the three sides form a valid triangle. \n",
" Otherwise return -1\n",
" Three sides make a valid triangle when the sum of any two sides is greater \n",
" than the third side.\n",
" Example:\n",
" triangle_area(3, 4, 5) == 6.00\n",
" triangle_area(1, 2, 10) == -1\n",
" '''\n",
" pass\n",
"\n",
"def test_triangle_area():\n",
" """Turns the example(s) in the docstring above into asserts"""\n",
" assert triangle_area(3, 4, 5) == 6.00\n",
" assert triangle_area(1, 2, 10) == -1\n",
" assert triangle_area(3, 4, 7) == -1\n",
" assert triangle_area(3, 3, 3) == 0.00\n",
" assert triangle_area(1, 1, 2) == -1\n",
""
],
"text/plain": [
"Here is an implementation of triangle_area:\n",
"```python\n",
"\n",
"def triangle_area(a, b, c):\n",
" '''\n",
" Given the lengths of the three sides of a triangle. Return the area of\n",
" the triangle rounded to 2 decimal points if the three sides form a valid triangle. \n",
" Otherwise return -1\n",
" Three sides make a valid triangle when the sum of any two sides is greater \n",
" than the third side.\n",
" Example:\n",
" triangle_area(3, 4, 5) == 6.00\n",
" triangle_area(1, 2, 10) == -1\n",
" '''\n",
" if a + b > c and a + c > b and b + c > a:\n",
" s = (a + b + c) / 2\n",
" return round(s * (s - a) * (s - b) * (s - c), 2)\n",
" else:\n",
" return -1\n",
"\n",
"def test_triangle_area():\n",
" assert triangle_area(3, 4, 5) == 6.00\n",
" assert triangle_area(1, 2, 10) == -1\n",
" assert triangle_area(3, 4, 7) == -1\n",
" assert triangle_area(3, 3, 3) == 0.00\n",
" assert triangle_area(1, 1, 2) == -1\n",
"```\n",
""
],
"text/plain": [
"Running the test(s) above gives:\n",
"assert triangle_area(3, 4, 5) == 6.00\n",
"Assertion failed.\n",
"Expected: 6.00\n",
"Actual: 36.0\n",
"---\n",
"assert triangle_area(1, 2, 10) == -1\n",
"Assertion passed.\n",
"---\n",
"assert triangle_area(3, 4, 7) == -1\n",
"Assertion passed.\n",
"---\n",
"assert triangle_area(3, 3, 3) == 0.00\n",
"Assertion failed.\n",
"Expected: 0.00\n",
"Actual: 15.19\n",
"---\n",
"assert triangle_area(1, 1, 2) == -1\n",
"Assertion passed.\n",
"---\n",
""
],
"text/plain": [
"...------------------------------------------------" ], "text/plain": [ "
Here is an implementation of triangle_area:\n",
"```python\n",
"\n",
"def triangle_area(a, b, c):\n",
" '''\n",
" Given the lengths of the three sides of a triangle. Return the area of\n",
" the triangle rounded to 2 decimal points if the three sides form a valid triangle. \n",
" Otherwise return -1\n",
" Three sides make a valid triangle when the sum of any two sides is greater \n",
" than the third side.\n",
" Example:\n",
" triangle_area(3, 4, 5) == 6.00\n",
" triangle_area(1, 2, 10) == -1\n",
" '''\n",
" # Check if the sides form a valid triangle\n",
" if a + b > c and a + c > b and b + c > a:\n",
" # Calculate the semi-perimeter\n",
" s = (a + b + c) / 2\n",
" # Check if the triangle is right-angled\n",
" if a**2 + b**2 == c**2:\n",
" # Use Gauss's formula for the area of a right-angled triangle\n",
" area = ((s * (s - a) * (s - b) * (s - c)) ** 0.5)\n",
" else:\n",
" # Use Heron's formula for the area of a general triangle\n",
" area = ((s * (s - a) * (s - b) * (s - c)) ** 0.5)\n",
" return round(area, 2)\n",
" else:\n",
" return -1\n",
"\n",
"def test_triangle_area():\n",
" assert triangle_area(3, 4, 5) == 6.00\n",
" assert triangle_area(1, 2, 10) == -1\n",
" assert triangle_area(3, 4, 7) == -1\n",
" assert triangle_area(1, 1, 2) == -1\n",
" assert triangle_area(12, 5, 13) == 17.71\n",
"```\n",
""
],
"text/plain": [
"