Last active
August 8, 2024 14:41
-
-
Save balouf/4d08ffda70cdd1cfd893bb72990fa33b to your computer and use it in GitHub Desktop.
TTT - IPywidgets and LLMs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2024-03-21T10:38:42.139765Z", | |
"end_time": "2024-03-21T10:38:42.147147Z" | |
}, | |
"init_cell": true, | |
"slideshow": { | |
"slide_type": "skip" | |
}, | |
"trusted": true | |
}, | |
"id": "3f93c420", | |
"cell_type": "code", | |
"source": "%%html\n<style>\n.prompt_container { display: none !important; }\n.prompt { display: none !important; }\n.run_this_cell { display: none !important; }\n\n.slides {\n position: absolute;\n top: 0;\n left: 0;\n}\n</style>", | |
"execution_count": 1, | |
"outputs": [ | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/plain": "<IPython.core.display.HTML object>", | |
"text/html": "<style>\n.prompt_container { display: none !important; }\n.prompt { display: none !important; }\n.run_this_cell { display: none !important; }\n\n.slides {\n position: absolute;\n top: 0;\n left: 0;\n}\n</style>\n" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2024-03-21T10:38:42.149166Z", | |
"end_time": "2024-03-21T10:38:42.333778Z" | |
}, | |
"init_cell": true, | |
"slideshow": { | |
"slide_type": "skip" | |
}, | |
"trusted": true | |
}, | |
"id": "c465de5e", | |
"cell_type": "code", | |
"source": "# This is a cell for the presentation. If you read the notebook afterwards, skip it.\n# Don't worry, all will be explained later!\nimport requests\n\ntones = {'Short': 'Answer any question with one small sentence.',\n 'Neutral': 'You are a helpful assistant.',\n 'For kids': 'Answer any question as if it was asked by a three-year old kid and you were a teacher trying to give an understandable answer.', \n 'French': 'Answer the following question using French language:',\n 'Democrat': 'Answer any question as if you were a fervent partisan of the democratic party, but never say who you are.',\n 'Republican': 'Answer any question as if you were a fervent partisan of the GOP, but never say who you are.',\n 'Pirate': 'Answer any question as if you were Jack Sparrow, from \"Pirates of the Caribbean\", but never say who you are.',\n }\n\ndef call_mistral(question, tone, n_tries=10):\n ask = question if question else \"Tell me something interesting!\"\n if n_tries == 0:\n print(\"I am not interested in answering your request.\")\n return None\n prompt = f'<s>[INST] {tones[tone]} {ask} [/INST]'\n payload = {\"prompt\": prompt, \"max_tokens\": 1024} #, \"max_tokens\": 512, \"top_k\": 5, \"temp\": 1}\n r = requests.post(\"http://localhost:8000/v1/completions\", json=payload)\n res = r.json()\n t = res['choices'][0]['text'].strip()\n if t == '':\n t = call_mistral(question, tone=tone, n_tries=n_tries-1)\n print(t)", | |
"execution_count": 2, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2024-03-21T10:38:42.334783Z", | |
"end_time": "2024-03-21T10:38:43.028759Z" | |
}, | |
"init_cell": true, | |
"slideshow": { | |
"slide_type": "skip" | |
}, | |
"trusted": true | |
}, | |
"id": "b4926398", | |
"cell_type": "code", | |
"source": "# This is a cell for the presentation. If you read the notebook afterwards, skip it.\n# Don't worry, all will be explained later!\n\nimport ipywidgets as widgets\n\ntone_list = [t for t in tones]\n\nquestion_list = [\"Who is leading 5G?\", \"Who is Marcus Weldon?\", \n \"What is the theory of relativity?\", \n \"Can you fly me to the moon?\", \"Who is Donald Trump?\", \n \"Who is Joe Biden?\", \"Who will win the next US presidential election?\",\n \"Write a function that tells if a method terminates.\",\n \"What is stable marriage theory?\", \"Did Han Solo shoot first?\", \"What is the meaning of the number 42?\", \n \"Tell me a joke.\"]\n\ncombo = widgets.Combobox(options=question_list, \n placeholder='Enter a question!',\n description=\"Question:\",\n continuous_update=False, layout={'width': '70%'})\n\ntone = widgets.RadioButtons(\n options=tone_list,\n value=tone_list[0],\n description='Tone:',\n disabled=False,\n continuous_update=False\n)\n\nout = widgets.interactive_output(call_mistral, {'question': combo, 'tone': tone})", | |
"execution_count": 3, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"slideshow": { | |
"slide_type": "slide" | |
} | |
}, | |
"id": "20a6aae2", | |
"cell_type": "markdown", | |
"source": "# Double-Feature: IPyWidgets + LLMs" | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2024-03-21T10:38:53.540169Z", | |
"end_time": "2024-03-21T10:38:53.558239Z" | |
}, | |
"hide_input": true, | |
"init_cell": true, | |
"scrolled": true, | |
"trusted": true | |
}, | |
"id": "020e48c6", | |
"cell_type": "code", | |
"source": "widgets.VBox([widgets.HBox([combo, tone]), out])", | |
"execution_count": 5, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 5, | |
"data": { | |
"text/plain": "VBox(children=(HBox(children=(Combobox(value='', continuous_update=False, description='Question:', layout=Layo…", | |
"application/vnd.jupyter.widget-view+json": { | |
"version_major": 2, | |
"version_minor": 0, | |
"model_id": "a087a6b5899a4e98ae6f72582f10eee8" | |
} | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"slideshow": { | |
"slide_type": "slide" | |
} | |
}, | |
"id": "90ab5f35", | |
"cell_type": "markdown", | |
"source": "# Preparation (space and bandwidth consuming)" | |
}, | |
{ | |
"metadata": { | |
"slideshow": { | |
"slide_type": "subslide" | |
} | |
}, | |
"id": "da55aef7", | |
"cell_type": "markdown", | |
"source": "# Packages" | |
}, | |
{ | |
"metadata": {}, | |
"id": "52cfb5ea", | |
"cell_type": "markdown", | |
"source": "- Install IPywidgets if it is not already in your distribution\n- Install llama-cpp-python\n\t- Details: https://github.com/abetlen/llama-cpp-python\n\t- If you have a Cuda card, try to install with cuBLAS option\n - If you plan to run the LLM on a remote machine, install with server option" | |
}, | |
{ | |
"metadata": { | |
"slideshow": { | |
"slide_type": "subslide" | |
} | |
}, | |
"id": "aab6d650", | |
"cell_type": "markdown", | |
"source": "# Windows users" | |
}, | |
{ | |
"metadata": {}, | |
"id": "b145e279", | |
"cell_type": "markdown", | |
"source": "Llama-cpp requires a compiler, `pip install` will fail otherwise\n\n- Download Visual Studio Build Tools (https://visualstudio.microsoft.com/downloads/?q=build+tools)\n- Required components:\n - MSVC\n - C++ Cmake tools\n - C++ Clang compiler\n - Windows X SDK" | |
}, | |
{ | |
"metadata": { | |
"slideshow": { | |
"slide_type": "subslide" | |
} | |
}, | |
"id": "cd17327c", | |
"cell_type": "markdown", | |
"source": "# Windows users" | |
}, | |
{ | |
"metadata": {}, | |
"id": "a8cf776e", | |
"cell_type": "markdown", | |
"source": "- For some reason, not all dependencies are installed, mostly the server part\n- `pip` and fail until it works\n- e.g. `pip install uvicorn fastapi sse_starlette starlette_context pydantic_settings`\n- If you messed up (e.g. forget CUDA): `--force-reinstall --no-cache-dir`" | |
}, | |
{ | |
"metadata": { | |
"slideshow": { | |
"slide_type": "subslide" | |
} | |
}, | |
"id": "58c9ddbb", | |
"cell_type": "markdown", | |
"source": "# Models" | |
}, | |
{ | |
"metadata": {}, | |
"id": "8f9b236e", | |
"cell_type": "markdown", | |
"source": "Choose and download a GGUF model from Hugging Face\n- Small: https://huggingface.co/TheBloke/phi-2-GGUF \n- Medium: https://huggingface.co/TheBloke/Mistral-7B-Instruct-v0.2-GGUF \n- Look the required RAM to choose\n- Files are in the \"Files and versions\" tab" | |
}, | |
{ | |
"metadata": { | |
"slideshow": { | |
"slide_type": "slide" | |
} | |
}, | |
"id": "9e6edc4a", | |
"cell_type": "markdown", | |
"source": "## IPyWidgets" | |
}, | |
{ | |
"metadata": { | |
"slideshow": { | |
"slide_type": "subslide" | |
} | |
}, | |
"id": "c8827859", | |
"cell_type": "markdown", | |
"source": "# What/why?" | |
}, | |
{ | |
"metadata": {}, | |
"id": "2fb19647", | |
"cell_type": "markdown", | |
"source": "- IPyWidgets makes Notebook's outputs interactive\n- Useful to increase interactivity at small cost\n - Cheap *whaou!* effect without dedicated GUI\n - Explore parameters\n- Requires results to be computed in real-time" | |
}, | |
{ | |
"metadata": { | |
"slideshow": { | |
"slide_type": "slide" | |
} | |
}, | |
"id": "8d41ed00", | |
"cell_type": "markdown", | |
"source": "### Simplest way: interact" | |
}, | |
{ | |
"metadata": { | |
"slideshow": { | |
"slide_type": "subslide" | |
} | |
}, | |
"id": "06e20f37", | |
"cell_type": "markdown", | |
"source": "# Interact: strings" | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2024-03-21T10:39:33.960312Z", | |
"end_time": "2024-03-21T10:39:33.989275Z" | |
}, | |
"cell_style": "split", | |
"trusted": true | |
}, | |
"id": "3f4d27f6", | |
"cell_type": "code", | |
"source": "from ipywidgets import interact\ndef echo(something):\n print(f\"You entered {something}!\")\n \ninteract(echo, something=\"World\")", | |
"execution_count": 6, | |
"outputs": [ | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/plain": "interactive(children=(Text(value='World', description='something'), Output()), _dom_classes=('widget-interact'…", | |
"application/vnd.jupyter.widget-view+json": { | |
"version_major": 2, | |
"version_minor": 0, | |
"model_id": "48d3fbcd5cba40c2baabe7009a925f40" | |
} | |
}, | |
"metadata": {} | |
}, | |
{ | |
"output_type": "execute_result", | |
"execution_count": 6, | |
"data": { | |
"text/plain": "<function __main__.echo(something)>" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2024-03-21T10:39:36.360418Z", | |
"end_time": "2024-03-21T10:39:36.383926Z" | |
}, | |
"cell_style": "split", | |
"trusted": true | |
}, | |
"id": "10994425", | |
"cell_type": "code", | |
"source": "# Remark the \";\"\ninteract(echo, something=\"Hello\");", | |
"execution_count": 7, | |
"outputs": [ | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/plain": "interactive(children=(Text(value='Hello', description='something'), Output()), _dom_classes=('widget-interact'…", | |
"application/vnd.jupyter.widget-view+json": { | |
"version_major": 2, | |
"version_minor": 0, | |
"model_id": "7a2ab59c2aee45718bd6d95f76efc86d" | |
} | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"slideshow": { | |
"slide_type": "subslide" | |
} | |
}, | |
"id": "b4bd8461", | |
"cell_type": "markdown", | |
"source": "# Interact: numbers" | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2024-03-21T10:39:37.758420Z", | |
"end_time": "2024-03-21T10:39:37.782229Z" | |
}, | |
"cell_style": "split", | |
"trusted": true | |
}, | |
"id": "f23ae65c", | |
"cell_type": "code", | |
"source": "interact(echo, something=10);", | |
"execution_count": 8, | |
"outputs": [ | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/plain": "interactive(children=(IntSlider(value=10, description='something', max=30, min=-10), Output()), _dom_classes=(…", | |
"application/vnd.jupyter.widget-view+json": { | |
"version_major": 2, | |
"version_minor": 0, | |
"model_id": "d4a45755d6a54fa2afbe1bee19cded05" | |
} | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2024-03-21T10:39:38.659426Z", | |
"end_time": "2024-03-21T10:39:38.685387Z" | |
}, | |
"cell_style": "split", | |
"trusted": true | |
}, | |
"id": "35f43d03", | |
"cell_type": "code", | |
"source": "interact(echo, something=2.5);", | |
"execution_count": 9, | |
"outputs": [ | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/plain": "interactive(children=(FloatSlider(value=2.5, description='something', max=7.5, min=-2.5), Output()), _dom_clas…", | |
"application/vnd.jupyter.widget-view+json": { | |
"version_major": 2, | |
"version_minor": 0, | |
"model_id": "cc8c13041c554a40b9b115fe8bfee133" | |
} | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2024-03-21T10:39:41.758273Z", | |
"end_time": "2024-03-21T10:39:41.782342Z" | |
}, | |
"cell_style": "split", | |
"trusted": true | |
}, | |
"id": "3c4ac998", | |
"cell_type": "code", | |
"source": "interact(echo, something=(0, 5));", | |
"execution_count": 10, | |
"outputs": [ | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/plain": "interactive(children=(IntSlider(value=2, description='something', max=5), Output()), _dom_classes=('widget-int…", | |
"application/vnd.jupyter.widget-view+json": { | |
"version_major": 2, | |
"version_minor": 0, | |
"model_id": "6eb20433ba7c441884295e360276fd39" | |
} | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2024-03-21T10:39:42.748993Z", | |
"end_time": "2024-03-21T10:39:42.775287Z" | |
}, | |
"cell_style": "split", | |
"trusted": true | |
}, | |
"id": "f41ffbf2", | |
"cell_type": "code", | |
"source": "interact(echo, something=(0., 10, .05));", | |
"execution_count": 11, | |
"outputs": [ | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/plain": "interactive(children=(FloatSlider(value=5.0, description='something', max=10.0, step=0.05), Output()), _dom_cl…", | |
"application/vnd.jupyter.widget-view+json": { | |
"version_major": 2, | |
"version_minor": 0, | |
"model_id": "9355bf8ee97646f186d92ca32ee4a09b" | |
} | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"slideshow": { | |
"slide_type": "subslide" | |
} | |
}, | |
"id": "2878c97d", | |
"cell_type": "markdown", | |
"source": "# Interact: choices" | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2024-03-21T10:39:44.869286Z", | |
"end_time": "2024-03-21T10:39:44.892053Z" | |
}, | |
"cell_style": "split", | |
"slideshow": { | |
"slide_type": "-" | |
}, | |
"trusted": true | |
}, | |
"id": "9bb5f2df", | |
"cell_type": "code", | |
"source": "interact(echo, something=[('One', 1), \n ('Two', 2), \n ('three', 3)]);", | |
"execution_count": 12, | |
"outputs": [ | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/plain": "interactive(children=(Dropdown(description='something', options=(('One', 1), ('Two', 2), ('three', 3)), value=…", | |
"application/vnd.jupyter.widget-view+json": { | |
"version_major": 2, | |
"version_minor": 0, | |
"model_id": "2da49868ed904270861ba3d15542c4c6" | |
} | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2024-03-21T10:39:46.499102Z", | |
"end_time": "2024-03-21T10:39:46.523367Z" | |
}, | |
"cell_style": "split", | |
"trusted": true | |
}, | |
"id": "0798a25d", | |
"cell_type": "code", | |
"source": "interact(echo, something={'One': 1, \n 'Two': 2, \n 'three': 3});", | |
"execution_count": 13, | |
"outputs": [ | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/plain": "interactive(children=(Dropdown(description='something', options={'One': 1, 'Two': 2, 'three': 3}, value=1), Ou…", | |
"application/vnd.jupyter.widget-view+json": { | |
"version_major": 2, | |
"version_minor": 0, | |
"model_id": "7931290515e84a2ca6cd6f4515d7a580" | |
} | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"slideshow": { | |
"slide_type": "subslide" | |
} | |
}, | |
"id": "c97d9e8a", | |
"cell_type": "markdown", | |
"source": "# Interact as a decorator" | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2024-03-21T10:39:49.209179Z", | |
"end_time": "2024-03-21T10:39:49.240782Z" | |
}, | |
"trusted": true | |
}, | |
"id": "d3995e40", | |
"cell_type": "code", | |
"source": "@interact(check_this=False, my_int=10, my_float=2.4, my_text=\"Toto\")\ndef whatever(check_this, my_int, my_float, my_text):\n c = \"You checked the box.\" if check_this else \"You didn't check the box.\"\n i = f\"Your int is {my_int}.\"\n f = f\"You float is {my_float:.2f}.\"\n t = f'Your text is \"{my_text}\".'\n print(\" \".join([c, i, f, t]))", | |
"execution_count": 14, | |
"outputs": [ | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/plain": "interactive(children=(Checkbox(value=False, description='check_this'), IntSlider(value=10, description='my_int…", | |
"application/vnd.jupyter.widget-view+json": { | |
"version_major": 2, | |
"version_minor": 0, | |
"model_id": "8c7af3d39a654674bd4ecd3008c21498" | |
} | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"slideshow": { | |
"slide_type": "subslide" | |
} | |
}, | |
"id": "17446895", | |
"cell_type": "markdown", | |
"source": "# Interactive plots" | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2024-03-21T10:39:51.649026Z", | |
"end_time": "2024-03-21T10:39:53.970374Z" | |
}, | |
"trusted": true | |
}, | |
"id": "bf41c443", | |
"cell_type": "code", | |
"source": "import matplotlib.pyplot as plt\nimport numpy as np\n\ndef plot(α, β, color1, color2):\n xlim, ylim = [-np.pi, np.pi], [-1, 1]\n x = np.linspace(*xlim)\n plt.plot(x, β*np.sin(x * α), label=r'$\\beta\\sin(\\alpha x)$', color=color1)\n plt.plot(x, (1-β)*np.cos(2*α*x), label=r'$(1-\\beta)\\cos(2\\alpha x)$', color=color2)\n plt.xlabel('$x$')\n plt.xlim(xlim)\n plt.ylim(ylim)\n plt.legend()\n plt.show()", | |
"execution_count": 15, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"slideshow": { | |
"slide_type": "subslide" | |
} | |
}, | |
"id": "e4bfed60", | |
"cell_type": "markdown", | |
"source": "# Interactive plots" | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2024-03-21T10:39:56.030254Z", | |
"end_time": "2024-03-21T10:39:56.591139Z" | |
}, | |
"trusted": true | |
}, | |
"id": "5931125c", | |
"cell_type": "code", | |
"source": "interact(plot, α=(.0, 2, .01),β = (.0, 1, .01), \n color1 = ['blue', 'green', 'red'], color2=['red', 'yellow', 'orange']);", | |
"execution_count": 16, | |
"outputs": [ | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/plain": "interactive(children=(FloatSlider(value=1.0, description='α', max=2.0, step=0.01), FloatSlider(value=0.5, desc…", | |
"application/vnd.jupyter.widget-view+json": { | |
"version_major": 2, | |
"version_minor": 0, | |
"model_id": "a8728d420aab47fea075babbc4ce797b" | |
} | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"slideshow": { | |
"slide_type": "slide" | |
} | |
}, | |
"id": "6a148f49", | |
"cell_type": "markdown", | |
"source": "### Building an interface with widgets" | |
}, | |
{ | |
"metadata": { | |
"slideshow": { | |
"slide_type": "subslide" | |
} | |
}, | |
"id": "cf3f4ab3", | |
"cell_type": "markdown", | |
"source": "# Combining widgets" | |
}, | |
{ | |
"metadata": {}, | |
"id": "26908b4e", | |
"cell_type": "markdown", | |
"source": "Simple recipe:\n- Choose input widget(s)\n- Choose output widget(s)\n- Glue all things together with joiner widgets\n- Widget list [here](<https://ipywidgets.readthedocs.io/en/stable/examples/Widget List.html>)" | |
}, | |
{ | |
"metadata": { | |
"slideshow": { | |
"slide_type": "subslide" | |
} | |
}, | |
"id": "45cffcb6", | |
"cell_type": "markdown", | |
"source": "# Example: input widgets" | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2024-03-21T10:40:01.670168Z", | |
"end_time": "2024-03-21T10:40:01.701698Z" | |
}, | |
"slideshow": { | |
"slide_type": "-" | |
}, | |
"trusted": true | |
}, | |
"id": "03a74bde", | |
"cell_type": "code", | |
"source": "alpha = widgets.FloatSlider(min=0, max=2, value=1.0, description='$\\\\alpha$')\nbeta = widgets.FloatSlider(min=0, max=1, value=.5, continuous_update=False, description='$\\\\beta$')\nparams = widgets.VBox([widgets.Label(value=\"Parameters\"), alpha, beta])\nc1 = widgets.Dropdown(options=['blue', 'green', 'red'], \n description=r'$\\beta\\sin(\\alpha x)$')\nc2 = widgets.Dropdown(options=['red', 'yellow', 'orange'], \n description=r'$(1-\\beta)\\cos(2\\alpha x)$', \n style={'description_width': 'initial'})\ncolors = widgets.VBox([widgets.Label(value=\"Colors\"), c1, c2])\ncontrols = widgets.VBox([params, colors], layout=widgets.Layout(width='50%'))\ncontrols", | |
"execution_count": 17, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 17, | |
"data": { | |
"text/plain": "VBox(children=(VBox(children=(Label(value='Parameters'), FloatSlider(value=1.0, description='$\\\\alpha$', max=2…", | |
"application/vnd.jupyter.widget-view+json": { | |
"version_major": 2, | |
"version_minor": 0, | |
"model_id": "96b099bbdd804292ab5aa23c3f48b153" | |
} | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"slideshow": { | |
"slide_type": "subslide" | |
} | |
}, | |
"id": "d2bfaebf", | |
"cell_type": "markdown", | |
"source": "# Example: ouput widgets" | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2024-03-21T10:40:03.858850Z", | |
"end_time": "2024-03-21T10:40:04.016966Z" | |
}, | |
"cell_style": "split", | |
"slideshow": { | |
"slide_type": "-" | |
}, | |
"trusted": true | |
}, | |
"id": "c79a93fd", | |
"cell_type": "code", | |
"source": "def plot(α, β, color1, color2):\n fig = plt.figure(figsize=(4, 5))\n xlim, ylim = [-np.pi, np.pi], [-1, 1]\n x = np.linspace(*xlim)\n plt.plot(x, β*np.sin(x * α), label=r'$\\beta\\sin(\\alpha x)$', color=color1)\n plt.plot(x, (1-β)*np.cos(2*α*x), label=r'$(1-\\beta)\\cos(2\\alpha x)$', color=color2)\n plt.xlabel('$x$')\n plt.xlim(xlim)\n plt.ylim(ylim)\n plt.legend()\n plt.show()\nleft_out = widgets.interactive_output(plot, \n {'α': alpha, 'β': beta, \n 'color1': c1, 'color2': c2})", | |
"execution_count": 18, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2024-03-21T10:40:06.339308Z", | |
"end_time": "2024-03-21T10:40:06.480679Z" | |
}, | |
"cell_style": "split", | |
"slideshow": { | |
"slide_type": "-" | |
}, | |
"trusted": true | |
}, | |
"id": "b46deca2", | |
"cell_type": "code", | |
"source": "def plot2(α):\n fig = plt.figure(figsize=(4, 5))\n xlim, ylim = [-np.pi, np.pi], [0, 10]\n x = np.linspace(*xlim)\n plt.plot(x, np.exp(x * α), label=r'$e^{\\alpha x}$')\n plt.xlabel('$x$')\n plt.xlim(xlim)\n plt.ylim(ylim)\n plt.legend()\n plt.show()\nright_out = widgets.interactive_output(plot2, \n {'α': alpha})", | |
"execution_count": 19, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"slideshow": { | |
"slide_type": "subslide" | |
} | |
}, | |
"id": "ab51245e", | |
"cell_type": "markdown", | |
"source": "# Example: glue" | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2024-03-21T10:47:38.824155Z", | |
"end_time": "2024-03-21T10:47:38.840616Z" | |
}, | |
"slideshow": { | |
"slide_type": "-" | |
}, | |
"trusted": true | |
}, | |
"id": "8ad74d24", | |
"cell_type": "code", | |
"source": "widgets.HBox([left_out, controls, right_out])", | |
"execution_count": 43, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 43, | |
"data": { | |
"text/plain": "HBox(children=(Output(outputs=({'output_type': 'display_data', 'data': {'text/plain': '<Figure size 400x500 wi…", | |
"application/vnd.jupyter.widget-view+json": { | |
"version_major": 2, | |
"version_minor": 0, | |
"model_id": "4e857910c42444299ead9b2ffee85031" | |
} | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"slideshow": { | |
"slide_type": "subslide" | |
} | |
}, | |
"id": "db97b7cf", | |
"cell_type": "markdown", | |
"source": "# IPyWidgets conclusion" | |
}, | |
{ | |
"metadata": {}, | |
"id": "1f24b216", | |
"cell_type": "markdown", | |
"source": "- Turns code into demo in seconds\n- Easy to learn, hard to master\n- Many things not covered here (e.g. ipymlp backend)\n- Many alternatives, e.g. Seaborn with pandas (cf [Quentin presentation](https://www.lincs.fr/events/parallel-computing-with-joblib-and-plotting-with-seaborn/))\n- Thanks Johann for showing me that tool!" | |
}, | |
{ | |
"metadata": { | |
"slideshow": { | |
"slide_type": "slide" | |
} | |
}, | |
"id": "8b1cf2e1", | |
"cell_type": "markdown", | |
"source": "## Using Large Language Models" | |
}, | |
{ | |
"metadata": { | |
"slideshow": { | |
"slide_type": "subslide" | |
} | |
}, | |
"id": "d09d28ff", | |
"cell_type": "markdown", | |
"source": "# What are LLMs?" | |
}, | |
{ | |
"metadata": {}, | |
"id": "f9c76d6d", | |
"cell_type": "markdown", | |
"source": "- The models that fuel all intelligent chats (ChatGPT, Bard, ...)\n- Based on the Transformer framework(s)\n- Theory not covered ; see for example [Léo's presentation](https://www.lincs.fr/events/transformer-models-in-artificial-intelligence-for-natural-language-processing/)" | |
}, | |
{ | |
"metadata": { | |
"slideshow": { | |
"slide_type": "subslide" | |
} | |
}, | |
"id": "1c6898c3", | |
"cell_type": "markdown", | |
"source": "# Making a LLM (not covered)" | |
}, | |
{ | |
"metadata": {}, | |
"id": "23d95430", | |
"cell_type": "markdown", | |
"source": "Recipe:\n\n- Take as many [A100](https://www.ldlc.com/en/product/PB00462651.html) as you can\n- Take all the Internet and sanitize it\n- Book a power plant\n- Wait a few months\n- And *voilà*! You have a LLM model" | |
}, | |
{ | |
"metadata": { | |
"slideshow": { | |
"slide_type": "subslide" | |
} | |
}, | |
"id": "3c790b36", | |
"cell_type": "markdown", | |
"source": "# Tuning a LLM (not covered)" | |
}, | |
{ | |
"metadata": {}, | |
"id": "3e692d4a", | |
"cell_type": "markdown", | |
"source": "Recipe:\n- Take a good LLM\n- Take [good GPU(s)](https://www.amazon.fr/NVIDIA-Titan-900-1G500-2500-000-Graphique-PCIe3-0x16/dp/B07WMMDV82)\n- Prepare a clean, small dataset that reflects what you want the LLM to do\n- Re-train your LLM / train a plugin NN (LORA)" | |
}, | |
{ | |
"metadata": { | |
"slideshow": { | |
"slide_type": "subslide" | |
} | |
}, | |
"id": "c502f5a0", | |
"cell_type": "markdown", | |
"source": "# Using a LLM" | |
}, | |
{ | |
"metadata": {}, | |
"id": "c3ad0476", | |
"cell_type": "markdown", | |
"source": "- If you want a good control of everything, a huge package ecosystem allows you to do whatever you want with a LLM\n- Cf for example [Gérard's presentation](https://www.lincs.fr/events/presentation-of-huggingface-transformers-library-for-natural-language-processing/)\n- Here: fast, compact, standalone approach based on llama-cpp" | |
}, | |
{ | |
"metadata": { | |
"slideshow": { | |
"slide_type": "subslide" | |
} | |
}, | |
"id": "1018a2a5", | |
"cell_type": "markdown", | |
"source": "# Huggingface LLM repository" | |
}, | |
{ | |
"metadata": {}, | |
"id": "63a82c86", | |
"cell_type": "markdown", | |
"source": "- Central place for all open source LLMs\n- One good entry point: [the leaderboard](https://huggingface.co/spaces/HuggingFaceH4/open_llm_leaderboard)" | |
}, | |
{ | |
"metadata": { | |
"slideshow": { | |
"slide_type": "subslide" | |
} | |
}, | |
"id": "d5ba2ee7", | |
"cell_type": "markdown", | |
"source": "# Understanding LLM naming conventions" | |
}, | |
{ | |
"metadata": {}, | |
"id": "0086eec4", | |
"cell_type": "markdown", | |
"source": "- [0-9]+B: the size (in billions parameters). Bigger usually means better and slower\n- Bunch of models, with dozens new ones each month, but:\n - Only a few families (Llama, Phi, Falcon, Mistral)\n - Rest are variants/merges\n- INST, CHAT, DPO, GGUF: additional details" | |
}, | |
{ | |
"metadata": { | |
"slideshow": { | |
"slide_type": "subslide" | |
} | |
}, | |
"id": "88dafafb", | |
"cell_type": "markdown", | |
"source": "# INST/Instruct fine-tuning" | |
}, | |
{ | |
"metadata": {}, | |
"id": "4fc605c9", | |
"cell_type": "markdown", | |
"source": "- LLMs are generative: all they do is extrapolate an input sequence\n- Instruct fine-tuning feeds them with a dataset of `Instruction: {the instruction} Output: {expected answer}` sequences\n- Other fine-tunings exist for chat, math, code, translation, etc..." | |
}, | |
{ | |
"metadata": { | |
"slideshow": { | |
"slide_type": "subslide" | |
} | |
}, | |
"id": "794f512b", | |
"cell_type": "markdown", | |
"source": "# GGUF format" | |
}, | |
{ | |
"metadata": {}, | |
"id": "3fba33f2", | |
"cell_type": "markdown", | |
"source": "- The JPEG compression of LLMs\n- Many LLMs can be compressed (and it's fast)\n- Pros: faster, smaller\n- Cons: less accurate, frozen" | |
}, | |
{ | |
"metadata": { | |
"slideshow": { | |
"slide_type": "subslide" | |
} | |
}, | |
"id": "b2320ed2", | |
"cell_type": "markdown", | |
"source": "# Considered models " | |
}, | |
{ | |
"metadata": { | |
"cell_style": "split" | |
}, | |
"id": "fc5eae80", | |
"cell_type": "markdown", | |
"source": "[Phi-2 2.7B](https://huggingface.co/TheBloke/phi-2-GGUF)\n- Light\n- GPT-2 performance\n- Recommended for CPU-only usages\n- Prompt: `Instruct: {instructions}\\nOutput: `" | |
}, | |
{ | |
"metadata": { | |
"cell_style": "split" | |
}, | |
"id": "f1d082ec", | |
"cell_type": "markdown", | |
"source": "[Mistral 7B](https://huggingface.co/TheBloke/Mistral-7B-Instruct-v0.2-GGUF)\n- Heavier\n- GPT-3.5 performance\n- Quite reactive with GPU\n- Prompt: `<s>[INST] {instructions} [/INST]`" | |
}, | |
{ | |
"metadata": { | |
"slideshow": { | |
"slide_type": "slide" | |
} | |
}, | |
"id": "59983b24", | |
"cell_type": "markdown", | |
"source": "### Loading the model" | |
}, | |
{ | |
"metadata": { | |
"slideshow": { | |
"slide_type": "subslide" | |
} | |
}, | |
"id": "9d721d66", | |
"cell_type": "markdown", | |
"source": "# From HF" | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2024-03-15T16:40:59.705Z" | |
}, | |
"trusted": true | |
}, | |
"id": "ea7507cd", | |
"cell_type": "code", | |
"source": "# Issue on Windows if verbose=False?\nfrom llama_cpp import Llama\nllm = Llama.from_pretrained(repo_id=\"TheBloke/phi-2-GGUF\", filename='*Q4_K_M.gguf', verbose=False)", | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"slideshow": { | |
"slide_type": "subslide" | |
} | |
}, | |
"id": "90110a9d", | |
"cell_type": "markdown", | |
"source": "# From local copy" | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2024-03-21T10:44:18.361862Z", | |
"end_time": "2024-03-21T10:44:18.850016Z" | |
}, | |
"trusted": true | |
}, | |
"id": "a0200ca3", | |
"cell_type": "code", | |
"source": "from llama_cpp import Llama\nfrom pathlib import Path\n# Change to your local repository\nmodel_repo = Path(r\"c:\\Users\\fabienma\\Downloads\\gguf\")\n\nllm = Llama(model_path=str(model_repo / \"phiphi.gguf\"), verbose=False)", | |
"execution_count": 25, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"slideshow": { | |
"slide_type": "subslide" | |
} | |
}, | |
"id": "f3eea201", | |
"cell_type": "markdown", | |
"source": "# Server mode" | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2024-03-21T10:44:39.354435Z", | |
"end_time": "2024-03-21T10:44:39.470551Z" | |
}, | |
"trusted": true | |
}, | |
"id": "0d2f19a4", | |
"cell_type": "code", | |
"source": "# Default port is 8000\n!wt python -m llama_cpp.server --model c:\\Users\\fabienma\\Downloads\\gguf\\phiphi.gguf --port 8001", | |
"execution_count": 27, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2024-03-10T20:44:03.588391Z", | |
"start_time": "2024-03-10T20:44:03.570309Z" | |
} | |
}, | |
"id": "9153843c", | |
"cell_type": "markdown", | |
"source": "http://localhost:8001/docs" | |
}, | |
{ | |
"metadata": { | |
"slideshow": { | |
"slide_type": "subslide" | |
} | |
}, | |
"id": "352356d0", | |
"cell_type": "markdown", | |
"source": "# Parameters" | |
}, | |
{ | |
"metadata": {}, | |
"id": "d3076ce2", | |
"cell_type": "markdown", | |
"source": "- Cf https://llama-cpp-python.readthedocs.io/en/latest/api-reference/\n- Most options are valid for all modes (`from_pretrained`, `local`, `server`)\n- `n_ctx`: context size (input+output limit)\n- For CUDA users: `n_gpu_layers`: gpu offload (default 0)" | |
}, | |
{ | |
"metadata": { | |
"slideshow": { | |
"slide_type": "slide" | |
} | |
}, | |
"id": "6772ecb2", | |
"cell_type": "markdown", | |
"source": "### Using the model" | |
}, | |
{ | |
"metadata": { | |
"slideshow": { | |
"slide_type": "subslide" | |
} | |
}, | |
"id": "4e01c7d5", | |
"cell_type": "markdown", | |
"source": "# Crafting the prompt" | |
}, | |
{ | |
"metadata": {}, | |
"id": "235ac8ed", | |
"cell_type": "markdown", | |
"source": "- LLM have a prompt format they have been trained with.\n- Unformatted prompt may work. Depends on your luck.\n- Sometimes, a format is proposed on HF. It may be the good one." | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2024-03-21T10:44:59.541581Z", | |
"end_time": "2024-03-21T10:44:59.549131Z" | |
}, | |
"cell_style": "split", | |
"trusted": true | |
}, | |
"id": "74a2f683", | |
"cell_type": "code", | |
"source": "def phiphize(prompt):\n return f\"Instruct: {prompt}\\nOutput:\"", | |
"execution_count": 28, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2024-03-21T10:45:00.771347Z", | |
"end_time": "2024-03-21T10:45:00.777577Z" | |
}, | |
"cell_style": "split", | |
"trusted": true | |
}, | |
"id": "f7d7984d", | |
"cell_type": "code", | |
"source": "def mistralize(prompt):\n return f\"<s>[INST] {prompt} [/INST]\"", | |
"execution_count": 29, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"slideshow": { | |
"slide_type": "subslide" | |
} | |
}, | |
"id": "15e08db7", | |
"cell_type": "markdown", | |
"source": "# Calling the model" | |
}, | |
{ | |
"metadata": {}, | |
"id": "04db1f48", | |
"cell_type": "markdown", | |
"source": "- local `llm` is callable\n- remote has a `POST` endpoint" | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2024-03-21T10:45:03.771737Z", | |
"end_time": "2024-03-21T10:45:03.776569Z" | |
}, | |
"trusted": true | |
}, | |
"id": "13307168", | |
"cell_type": "code", | |
"source": "options = {'prompt': phiphize('Name three stars.'), 'max_tokens': 512}", | |
"execution_count": 30, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"slideshow": { | |
"slide_type": "subslide" | |
} | |
}, | |
"id": "f8fc3651", | |
"cell_type": "markdown", | |
"source": "# Calling the model" | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2024-03-21T10:45:06.101092Z", | |
"end_time": "2024-03-21T10:45:08.450716Z" | |
}, | |
"cell_style": "split", | |
"trusted": true | |
}, | |
"id": "ccbee791", | |
"cell_type": "code", | |
"source": "llm(**options)", | |
"execution_count": 31, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 31, | |
"data": { | |
"text/plain": "{'id': 'cmpl-602bccc9-b3e1-42f7-9bf6-5f7c826f3b0f',\n 'object': 'text_completion',\n 'created': 1711017906,\n 'model': 'c:\\\\Users\\\\fabienma\\\\Downloads\\\\gguf\\\\phiphi.gguf',\n 'choices': [{'text': ' Stars: \\n1. The Sun\\n2. Sirius\\n3. Betelgeuse\\n',\n 'index': 0,\n 'logprobs': None,\n 'finish_reason': 'stop'}],\n 'usage': {'prompt_tokens': 10, 'completion_tokens': 20, 'total_tokens': 30}}" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2024-03-21T10:45:11.362667Z", | |
"end_time": "2024-03-21T10:45:13.109570Z" | |
}, | |
"cell_style": "split", | |
"trusted": true | |
}, | |
"id": "cb9fabe6", | |
"cell_type": "code", | |
"source": "import requests\nrequests.post(f\"http://localhost:8001/v1/completions\", \n json=options).json()", | |
"execution_count": 33, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 33, | |
"data": { | |
"text/plain": "{'id': 'cmpl-daea3773-f847-473d-9607-87b6a52df516',\n 'object': 'text_completion',\n 'created': 1711017911,\n 'model': 'c:\\\\Users\\\\fabienma\\\\Downloads\\\\gguf\\\\phiphi.gguf',\n 'choices': [{'text': ' Three stars in the night sky are Sirius, Canis Majoris, and Betelgeuse.\\n',\n 'index': 0,\n 'logprobs': None,\n 'finish_reason': 'stop'}],\n 'usage': {'prompt_tokens': 10, 'completion_tokens': 21, 'total_tokens': 31}}" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"slideshow": { | |
"slide_type": "subslide" | |
} | |
}, | |
"id": "fd39d2f8", | |
"cell_type": "markdown", | |
"source": "# Calling the model" | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2024-03-21T10:45:17.821982Z", | |
"end_time": "2024-03-21T10:45:17.831262Z" | |
}, | |
"cell_style": "split", | |
"trusted": true | |
}, | |
"id": "6bc1af9f", | |
"cell_type": "code", | |
"source": "def local_phi(prompt):\n options = {'prompt': phiphize(prompt),\n 'max_tokens': 512}\n res = llm(**options)\n return res['choices'][0]['text'].strip()", | |
"execution_count": 34, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2024-03-21T10:45:18.801434Z", | |
"end_time": "2024-03-21T10:45:18.809782Z" | |
}, | |
"cell_style": "split", | |
"trusted": true | |
}, | |
"id": "212e43df", | |
"cell_type": "code", | |
"source": "def remote_mis(prompt):\n options = {'prompt': mistralize(prompt),\n 'max_tokens': 512}\n ep = \"http://localhost:8000/v1/completions\"\n r = requests.post(ep, json=options)\n return r.json()['choices'][0]['text'].strip()", | |
"execution_count": 35, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"slideshow": { | |
"slide_type": "subslide" | |
} | |
}, | |
"id": "8fd8d2c7", | |
"cell_type": "markdown", | |
"source": "# Calling the model" | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2024-03-21T10:45:22.882053Z", | |
"end_time": "2024-03-21T10:45:51.006403Z" | |
}, | |
"trusted": true | |
}, | |
"id": "8b075841", | |
"cell_type": "code", | |
"source": "print(local_phi(\"Explain the theory of stable matchings.\"))", | |
"execution_count": 36, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "Stable matching refers to a method used for allocating resources among individuals in a way that maximizes their satisfaction, while also minimizing waste or inefficiencies. It is based on the idea of finding a balanced allocation that satisfies as many preferences as possible.\n\nIn a stable matching, each individual has a set of preferences over potential partners or items, and each partner or item has a set of preferences over potential matches. The goal is to find a pairing between each individual and a partner/item such that no two individuals prefer each other more than the pair they are matched to, and no pair is under-matched (i.e., they have both individuals who would prefer them more). This ensures that everyone is satisfied with their match and that no one is left out.\n\nStable matching can be applied in various contexts, such as assigning teachers to students in schools, assigning job applicants to positions, or assigning organs to recipients in transplantation. The stability of a matching depends on the structure of the preferences and the number of available pairs. If there are more individuals than available pairs, there may be no stable matching. If there are more available pairs than individuals, some pairs may be under-matched (i.e., both partners would prefer them more if they had more options). If there are fewer pairs than individuals, some individuals may be left without a partner/item and may be under-matched (i.e., both partners would prefer them more if they had more options). Therefore, it is important to carefully design the structure of the preferences and the number of available pairs to ensure a stable matching with maximum satisfaction and minimum waste.\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"slideshow": { | |
"slide_type": "subslide" | |
} | |
}, | |
"id": "b4b7e289", | |
"cell_type": "markdown", | |
"source": "# Calling the model" | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2024-03-21T10:45:51.008424Z", | |
"end_time": "2024-03-21T10:46:01.052110Z" | |
}, | |
"trusted": true | |
}, | |
"id": "532c9f92", | |
"cell_type": "code", | |
"source": "print(remote_mis(\"Explain the theory of stable matchings.\"))", | |
"execution_count": 37, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "The Theory of Stable Matchings is a solution concept in game theory and economics that deals with finding a fair and efficient matching between two sets of agents, often referred to as \"men\" and \"women,\" or \"agents with preferences\" and \"items.\" This theory was initially developed to solve the problem of marriage markets, but it has since been applied to various other contexts such as job markets, organ donation, housing markets, and roommate selection.\n\nAt its core, the theory of stable matchings aims to find a matching where no agent has an incentive to leave their current match for another one, assuming all other agents remain in their current matches. In other words, it seeks to find a situation where no agent can improve their outcome by unilaterally changing their choice of partner or preference order.\n\nLet's define some key terms:\n\n1. Agents: These are entities that have preferences over items or other agents. For example, men prefer certain women, and women prefer certain men.\n2. Items: These are entities that have preferences over agents. For example, women prefer certain men, and men prefer certain jobs or houses.\n3. Preferences: Agents rank their preferences for items (or other agents) in order of preference.\n4. Matching: A matching is a set of pairs where each agent is paired with an item (or another agent), and each item is paired with one agent.\n5. Proposed relationship: An agent proposing to an item means they want to be matched with that item.\n6. Stability: A matching is stable if no agent has an incentive to propose to another item (or agent) while the other agent also has an incentive to accept that proposal.\n\nThe process for finding a stable matching typically involves several rounds where agents propose to their most preferred available items (or agents), and items accept the proposal of their most preferred available agent (or item). Each round results in a new set of proposed relationships, which are then used to determine which agents and items can improve their matches for the next round. This process continues until no more agents can improve their matches by proposing to other items (or agents).\n\nOne popular algorithm for finding a stable matching is the Deferred Acceptance Algorithm (also known as Gale-Shapley algorithm). This algorithm works by having all items rank their preferences for agents and having each agent repeatedly propose to their most preferred available\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"slideshow": { | |
"slide_type": "subslide" | |
} | |
}, | |
"id": "ffc50ba1", | |
"cell_type": "markdown", | |
"source": "# Parameters" | |
}, | |
{ | |
"metadata": {}, | |
"id": "48e09414", | |
"cell_type": "markdown", | |
"source": "Cf https://llama-cpp-python.readthedocs.io/en/latest/api-reference/\n- `top_k`, `top_p`, `temp`: craziness\n- `stop`: list of words that indicate end of response (e.g. `Q:`, `##`, `Output:`)\n" | |
}, | |
{ | |
"metadata": { | |
"slideshow": { | |
"slide_type": "slide" | |
} | |
}, | |
"id": "dc2e3290", | |
"cell_type": "markdown", | |
"source": "### Make a simple IPywidgets interface for LLMs" | |
}, | |
{ | |
"metadata": { | |
"slideshow": { | |
"slide_type": "subslide" | |
} | |
}, | |
"id": "81ae4549", | |
"cell_type": "markdown", | |
"source": "# Prepare some context" | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2024-03-21T10:46:01.059333Z", | |
"end_time": "2024-03-21T10:46:01.068860Z" | |
}, | |
"trusted": true | |
}, | |
"id": "4af47d75", | |
"cell_type": "code", | |
"source": "tones = {'Short': 'Answer the following question with one short sentence:',\n 'Neutral': 'You are a helpful assistant.',\n 'For kids': 'You are a teacher talking to a three-year old kid.', \n 'French': 'Answer the following question using French language:',\n 'Democrat': 'Answer the following question with words a Democrat voter would use:',\n 'Republican': 'Answer the following question with words a Republican voter would use:',\n 'Pirate': 'You are a true pirate, with the jolly roger, parrot on shoulder, eye patch, wooden leg, and everything.',\n }", | |
"execution_count": 38, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"slideshow": { | |
"slide_type": "subslide" | |
} | |
}, | |
"id": "15051926", | |
"cell_type": "markdown", | |
"source": "# Prepare output" | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2024-03-21T10:46:01.075872Z", | |
"end_time": "2024-03-21T10:46:01.888776Z" | |
}, | |
"trusted": true | |
}, | |
"id": "f0ac79b7", | |
"cell_type": "code", | |
"source": "def call_mistral(question='', tone='Short'):\n ask = question if question else \"Tell me something interesting!\"\n prompt = f\"{tones[tone]} {ask}\"\n print(remote_mis(prompt))\ncall_mistral()", | |
"execution_count": 39, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "Did you know that an average person produces about 10,000 gallons (37,854 liters) of saliva in a lifetime?\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"slideshow": { | |
"slide_type": "subslide" | |
} | |
}, | |
"id": "fe6e5a4d", | |
"cell_type": "markdown", | |
"source": "# Widget time" | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2024-03-21T10:46:01.893800Z", | |
"end_time": "2024-03-21T10:46:01.908324Z" | |
}, | |
"trusted": true | |
}, | |
"id": "63dbb73f", | |
"cell_type": "code", | |
"source": "question_list = [\"Who is leading 5G?\", \"Who is Marcus Weldon?\", \n \"What is the theory of relativity?\", \n \"Can you fly me to the moon?\", \"Who is Donald Trump?\", \n \"Who is Joe Biden?\", \"Who will win the next US presidential election?\",\n \"Write a function that tells if a method terminates.\",\n \"What is stable marriage theory?\", \"Did Han Solo shoot first?\", \"What is the meaning of the number 42?\", \n \"Tell me a joke.\"]\n\ncombo = widgets.Combobox(options=question_list, \n placeholder='Enter a question!',\n description=\"Question:\",\n continuous_update=False, layout={'width': '70%'})", | |
"execution_count": 40, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"slideshow": { | |
"slide_type": "subslide" | |
} | |
}, | |
"id": "29301d21", | |
"cell_type": "markdown", | |
"source": "# Widget time" | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2024-03-21T10:46:01.913936Z", | |
"end_time": "2024-03-21T10:46:02.592084Z" | |
}, | |
"trusted": true | |
}, | |
"id": "d3dea397", | |
"cell_type": "code", | |
"source": "tone_list = [t for t in tones]\n\n\ntone = widgets.RadioButtons(\n options=tone_list,\n value=tone_list[0],\n description='Tone:',\n disabled=False,\n continuous_update=False\n)\n\nout = widgets.interactive_output(call_mistral, {'question': combo, 'tone': tone})", | |
"execution_count": 41, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"slideshow": { | |
"slide_type": "subslide" | |
} | |
}, | |
"id": "95a05941", | |
"cell_type": "markdown", | |
"source": "# Widget time" | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2024-03-21T10:46:02.598151Z", | |
"end_time": "2024-03-21T10:46:02.618164Z" | |
}, | |
"trusted": true | |
}, | |
"id": "27b92ae3", | |
"cell_type": "code", | |
"source": "widgets.VBox([widgets.HBox([combo, tone]), out])", | |
"execution_count": 42, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 42, | |
"data": { | |
"text/plain": "VBox(children=(HBox(children=(Combobox(value='', continuous_update=False, description='Question:', layout=Layo…", | |
"application/vnd.jupyter.widget-view+json": { | |
"version_major": 2, | |
"version_minor": 0, | |
"model_id": "0202e88d6a41497a95943c9b229ea8ce" | |
} | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"slideshow": { | |
"slide_type": "slide" | |
} | |
}, | |
"id": "18ca6e32", | |
"cell_type": "markdown", | |
"source": "# Conclusion" | |
}, | |
{ | |
"metadata": {}, | |
"id": "ac1f068a", | |
"cell_type": "markdown", | |
"source": "- With IPywidget+llama-cpp you can build a simple LLM interface in no time\n- Models evolve really fast, Mistral 7B and phi-2 will be outperformed in six months\n- If you want something more fancy, chatGPT-like UIs exist (but it's more work)" | |
} | |
], | |
"metadata": { | |
"_draft": { | |
"nbviewer_url": "https://gist.github.com/balouf/4d08ffda70cdd1cfd893bb72990fa33b" | |
}, | |
"celltoolbar": "Slideshow", | |
"gist": { | |
"id": "4d08ffda70cdd1cfd893bb72990fa33b", | |
"data": { | |
"description": "TTT - IPywidgets and LLMs", | |
"public": true | |
} | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3 (ipykernel)", | |
"language": "python" | |
}, | |
"language_info": { | |
"name": "python", | |
"version": "3.11.4", | |
"mimetype": "text/x-python", | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"pygments_lexer": "ipython3", | |
"nbconvert_exporter": "python", | |
"file_extension": ".py" | |
}, | |
"toc": { | |
"nav_menu": {}, | |
"number_sections": true, | |
"sideBar": true, | |
"skip_h1_title": true, | |
"base_numbering": 1, | |
"title_cell": "Table of Contents", | |
"title_sidebar": "Contents", | |
"toc_cell": false, | |
"toc_position": {}, | |
"toc_section_display": true, | |
"toc_window_display": false | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Interesting combination.
Is it possible to support LLM streaming along with IPywidget to minimize the latency?