|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Use Open Interpreter to talk to your database" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "markdown", |
| 12 | + "metadata": {}, |
| 13 | + "source": [ |
| 14 | + "> Note: Open Interpreter should ideally be limited to read-only actions on your database. If write operations are necessary, use a copy of your data to protect against unexpected changes from the AI model. " |
| 15 | + ] |
| 16 | + }, |
| 17 | + { |
| 18 | + "cell_type": "code", |
| 19 | + "execution_count": null, |
| 20 | + "metadata": { |
| 21 | + "vscode": { |
| 22 | + "languageId": "plaintext" |
| 23 | + } |
| 24 | + }, |
| 25 | + "outputs": [], |
| 26 | + "source": [ |
| 27 | + "Set up a profile with:\n", |
| 28 | + "- Database credentials\n", |
| 29 | + "- Connection string\n", |
| 30 | + "\n", |
| 31 | + "Here is an example for a PostgreSQL database:" |
| 32 | + ] |
| 33 | + }, |
| 34 | + { |
| 35 | + "cell_type": "code", |
| 36 | + "execution_count": null, |
| 37 | + "metadata": {}, |
| 38 | + "outputs": [], |
| 39 | + "source": [ |
| 40 | + "from interpreter import interpreter\n", |
| 41 | + "import os\n", |
| 42 | + "\n", |
| 43 | + "# Use environment variables for database connection or update defaults with your credentials\n", |
| 44 | + "db_user = os.environ.get(\"DB_USER\", \"user\")\n", |
| 45 | + "db_host = os.environ.get(\"DB_HOST\", \"localhost\")\n", |
| 46 | + "db_port = os.environ.get(\"DB_PORT\", \"5432\")\n", |
| 47 | + "db_name = os.environ.get(\"DB_NAME\", \"demo_database\")\n", |
| 48 | + "db_password = os.environ.get(\"DB_PASSWORD\", \"\")\n", |
| 49 | + "\n", |
| 50 | + "# Construct connection string with optional password\n", |
| 51 | + "if db_password and db_password.strip():\n", |
| 52 | + " connection_string = (\n", |
| 53 | + " f\"postgresql://{db_user}:{db_password}@{db_host}:{db_port}/{db_name}\"\n", |
| 54 | + " )\n", |
| 55 | + "else:\n", |
| 56 | + " connection_string = f\"postgresql://{db_user}@{db_host}:{db_port}/{db_name}\"" |
| 57 | + ] |
| 58 | + }, |
| 59 | + { |
| 60 | + "cell_type": "markdown", |
| 61 | + "metadata": {}, |
| 62 | + "source": [ |
| 63 | + "Configure your instance of Open Interpreter.\n", |
| 64 | + "\n", |
| 65 | + "\n", |
| 66 | + "This example uses a local model served by Ollama but you can use a hosted model:" |
| 67 | + ] |
| 68 | + }, |
| 69 | + { |
| 70 | + "cell_type": "code", |
| 71 | + "execution_count": null, |
| 72 | + "metadata": {}, |
| 73 | + "outputs": [], |
| 74 | + "source": [ |
| 75 | + "# LLM settings\n", |
| 76 | + "interpreter.llm.model = \"ollama/llama3.1\"\n", |
| 77 | + "interpreter.llm.supports_functions = False\n", |
| 78 | + "interpreter.llm.execution_instructions = False\n", |
| 79 | + "interpreter.llm.max_tokens = 1000\n", |
| 80 | + "interpreter.llm.context_window = 7000\n", |
| 81 | + "interpreter.llm.load() \n", |
| 82 | + "\n", |
| 83 | + "# Computer settings\n", |
| 84 | + "interpreter.computer.import_computer_api = False\n", |
| 85 | + "\n", |
| 86 | + "# Misc settings\n", |
| 87 | + "interpreter.auto_run = False\n", |
| 88 | + "interpreter.offline = True" |
| 89 | + ] |
| 90 | + }, |
| 91 | + { |
| 92 | + "cell_type": "markdown", |
| 93 | + "metadata": {}, |
| 94 | + "source": [ |
| 95 | + "Set the custom instructions to maximize performance." |
| 96 | + ] |
| 97 | + }, |
| 98 | + { |
| 99 | + "cell_type": "code", |
| 100 | + "execution_count": null, |
| 101 | + "metadata": {}, |
| 102 | + "outputs": [], |
| 103 | + "source": [ |
| 104 | + "# Custom Instructions\n", |
| 105 | + "interpreter.custom_instructions = f\"\"\"\n", |
| 106 | + " You are a SQL master and are the oracle of database knowledge. You are obsessed with SQL. You only want to discuss SQL. SQL is life.\n", |
| 107 | + " Recap the plan before answering the user's query.\n", |
| 108 | + " You will connect to a PostgreSQL database, with the connection string {connection_string}.\n", |
| 109 | + " Remember to only query the {db_name} database.\n", |
| 110 | + " Execute valid SQL commands to satisfy the user's query.\n", |
| 111 | + " Write all code in a full Python script. When you have to re-write code, redo the entire script.\n", |
| 112 | + " Execute the script to get the answer for the user's query.\n", |
| 113 | + " **YOU CAN EXECUTE SQL COMMANDS IN A PYTHON SCRIPT.***\n", |
| 114 | + " Get the schema of '{db_name}' before writing any other SQL commands. It is important to know the tables. This will let you know what commands are correct.\n", |
| 115 | + " Only use real column names.\n", |
| 116 | + " ***You ARE fully capable of executing SQL commands.***\n", |
| 117 | + " Be VERY clear about the answer to the user's query. They don't understand technical jargon so make it very clear and direct.\n", |
| 118 | + " You should respond in a very concise way.\n", |
| 119 | + " You can do it, I believe in you.\n", |
| 120 | + " \"\"\"" |
| 121 | + ] |
| 122 | + }, |
| 123 | + { |
| 124 | + "cell_type": "markdown", |
| 125 | + "metadata": {}, |
| 126 | + "source": [ |
| 127 | + "\n", |
| 128 | + "Save the profile in the `profiles` directory.\n", |
| 129 | + "\n", |
| 130 | + "Once you are happy with your profile, test it on a test table/database. \n", |
| 131 | + "\n", |
| 132 | + "Run the following in your terminal:\n", |
| 133 | + "\n", |
| 134 | + "`interpreter --profile <name-of-profile.py>`\n" |
| 135 | + ] |
| 136 | + }, |
| 137 | + { |
| 138 | + "cell_type": "markdown", |
| 139 | + "metadata": {}, |
| 140 | + "source": [ |
| 141 | + "Iterate on the profile until you are happy with the performance. \n", |
| 142 | + "\n", |
| 143 | + "Remember to use the right LLM for the job. Larger models tend to have better reasoning.\n", |
| 144 | + "\n", |
| 145 | + "If you want to share your profile with the community, please open a PR." |
| 146 | + ] |
| 147 | + } |
| 148 | + ], |
| 149 | + "metadata": { |
| 150 | + "language_info": { |
| 151 | + "name": "python" |
| 152 | + } |
| 153 | + }, |
| 154 | + "nbformat": 4, |
| 155 | + "nbformat_minor": 2 |
| 156 | +} |
0 commit comments