{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "d1d5ec44", "metadata": { "execution": { "iopub.execute_input": "2023-03-22T17:00:17.499299Z", "iopub.status.busy": "2023-03-22T17:00:17.498973Z", "iopub.status.idle": "2023-03-22T17:00:17.506839Z", "shell.execute_reply": "2023-03-22T17:00:17.506087Z" }, "tags": [ "hide_inp" ] }, "outputs": [], "source": [ "\n", "desc = \"\"\"\n", "### Bash Command Suggestion\n", "\n", "Chain that ask for a command-line question and then runs the bash command. [[Code](https://github.com/srush/MiniChain/blob/main/examples/bash.py)]\n", "\n", "(Adapted from LangChain [BashChain](https://langchain.readthedocs.io/en/latest/modules/chains/examples/llm_bash.html))\n", "\"\"\"" ] }, { "cell_type": "markdown", "id": "94100dff", "metadata": {}, "source": [ "$" ] }, { "cell_type": "code", "execution_count": 2, "id": "b2ae061e", "metadata": { "execution": { "iopub.execute_input": "2023-03-22T17:00:17.509606Z", "iopub.status.busy": "2023-03-22T17:00:17.509377Z", "iopub.status.idle": "2023-03-22T17:00:18.808162Z", "shell.execute_reply": "2023-03-22T17:00:18.807504Z" } }, "outputs": [], "source": [ "from minichain import show, prompt, OpenAI, Bash" ] }, { "cell_type": "code", "execution_count": 3, "id": "9c440e40", "metadata": { "execution": { "iopub.execute_input": "2023-03-22T17:00:18.810785Z", "iopub.status.busy": "2023-03-22T17:00:18.810465Z", "iopub.status.idle": "2023-03-22T17:00:18.814003Z", "shell.execute_reply": "2023-03-22T17:00:18.813511Z" }, "lines_to_next_cell": 1 }, "outputs": [], "source": [ "@prompt(OpenAI(), template_file = \"bash.pmpt.tpl\")\n", "def cli_prompt(model, query):\n", " x = model(dict(question=query))\n", " return \"\\n\".join(x.strip().split(\"\\n\")[1:-1])" ] }, { "cell_type": "code", "execution_count": 4, "id": "74115a3f", "metadata": { "execution": { "iopub.execute_input": "2023-03-22T17:00:18.816250Z", "iopub.status.busy": "2023-03-22T17:00:18.815868Z", "iopub.status.idle": "2023-03-22T17:00:18.818697Z", "shell.execute_reply": "2023-03-22T17:00:18.818263Z" }, "lines_to_next_cell": 1 }, "outputs": [], "source": [ "@prompt(Bash())\n", "def bash_run(model, x):\n", " return model(x)" ] }, { "cell_type": "code", "execution_count": 5, "id": "88210a7d", "metadata": { "execution": { "iopub.execute_input": "2023-03-22T17:00:18.820762Z", "iopub.status.busy": "2023-03-22T17:00:18.820577Z", "iopub.status.idle": "2023-03-22T17:00:18.823279Z", "shell.execute_reply": "2023-03-22T17:00:18.822838Z" } }, "outputs": [], "source": [ "def bash(query):\n", " return bash_run(cli_prompt(query))" ] }, { "cell_type": "markdown", "id": "99d969c2", "metadata": {}, "source": [ "$" ] }, { "cell_type": "code", "execution_count": 6, "id": "fe4ffa7a", "metadata": { "execution": { "iopub.execute_input": "2023-03-22T17:00:18.825360Z", "iopub.status.busy": "2023-03-22T17:00:18.825177Z", "iopub.status.idle": "2023-03-22T17:00:19.158113Z", "shell.execute_reply": "2023-03-22T17:00:19.157509Z" }, "lines_to_next_cell": 2 }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Running on local URL: http://127.0.0.1:7861\n", "\n", "To create a public link, set `share=True` in `launch()`.\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "gradio = show(bash,\n", " subprompts=[cli_prompt, bash_run],\n", " examples=['Go up one directory, and then into the minichain directory,'\n", " 'and list the files in the directory',\n", " \"Please write a bash script that prints 'Hello World' to the console.\"],\n", " out_type=\"markdown\",\n", " description=desc,\n", " code=open(\"bash.py\", \"r\").read().split(\"$\")[1].strip().strip(\"#\").strip(),\n", " )\n", "if __name__ == \"__main__\":\n", " gradio.launch()" ] } ], "metadata": { "jupytext": { "cell_metadata_filter": "tags,-all" }, "kernelspec": { "display_name": "minichain", "language": "python", "name": "minichain" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.6" } }, "nbformat": 4, "nbformat_minor": 5 }