{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/Users/ryang/Work/ExtensityAI/symbolicai/notebooks\n", "Configuration file: /Users/ryang/.symai/symai.config.json\n" ] } ], "source": [ "import os\n", "import warnings\n", "warnings.filterwarnings('ignore')\n", "print(os.getcwd())\n", "from symai import *\n", "from IPython.display import display\n", "from examples.sql import SQL\n", "sql = SQL()" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## SQL Example" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "The `SQL` expression is defined and passes as a global context the syntax of the SQL language. The SQL expression is then used to generate queries based on the given context. We can then use the generated queries to get data from a database." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The following query is sent to the neural computation engine and creates a query based on the given context:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "val = None\n", "def _fun(x):\n", " global val\n", " val = x\n", "Symbol._metadata.input_handler = _fun" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "<class symai.symbol.Symbol at 0x176022ad0>(value=Ich habe diesen Kurs.)" ], "text/plain": [ "(value=Ich habe diesen Kurs.)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Symbol('I have this class').translate('German')" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "<class examples.sql.SQL at 0x300e42b90>(value=SQL: SELECT * \n", "FROM Members \n", "WHERE age > 30)" ], "text/plain": [ "(value=SQL: SELECT * \n", "FROM Members \n", "WHERE age > 30)" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "q = sql('Select all users above the age of 30 from the Members table.'); display(q)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can now try to further manipulate the result by asking the model to incorporate additional information, such as filtering to a specific time range:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "<class examples.sql.SQL at 0x103eebe80>(value=SQL: SELECT * \n", "FROM Membe ... - INTERVAL 10 MINUTE)" ], "text/plain": [ "(value=SQL: SELECT * \n", "FROM Membe ... - INTERVAL 10 MINUTE)" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "res = q << 'and limit the query to the last 10 minutes'\n", "display(res)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "We can also try to remove unwanted fields from the generated query. Notice how the model tries to remove not only the given statement but attributes associated with them:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "<class examples.sql.SQL at 0x103eebe80>(value=SQL: SELECT * \n", "FROM Members \n", "WHERE age > 30)" ], "text/plain": [ "(value=SQL: SELECT * \n", "FROM Members \n", "WHERE age > 30)" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "res -= ' AND ...'\n", "display(res)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "And we can now even convert our query to other familiar domain specific languages, such as `SQL` or `ActiveRecord`:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "<class examples.sql.SQL at 0x176023c40>(value=Members.where("age > ?", 30))" ], "text/plain": [ "(value=Members.where(\"age > ?\", 30))" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "sql_res = res.convert(\"ActiveRecord\")\n", "display(sql_res)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To wrap things up, we might want to go back full circle and ask the model to generate again the explanation based on the given query:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "<class examples.sql.SQL at 0x300e42e60>(value=The query selects all col ... lue greater than 30.)" ], "text/plain": [ "(value=The query selects all col ... lue greater than 30.)" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "answer_doc = res.query(\"What does this query do?\")\n", "display(answer_doc)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Ask it in natural language to modify the query:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "<class examples.sql.SQL at 0x300e43340>(value=SQL: SELECT * \n", "FROM Membe ... E age > 30 \n", "LIMIT 30)" ], "text/plain": [ "(value=SQL: SELECT * \n", "FROM Membe ... E age > 30 \n", "LIMIT 30)" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "answer = res.query(\"How can you limit the number of results to 30 for an SQL query?\")\n", "display(answer)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Even translate the explanation to a different language on the fly:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "<class symai.symbol.Symbol at 0x300e43610>(value=Die Abfrage wählt alle Sp ... t größer als 30 hat.)" ], "text/plain": [ "(value=Die Abfrage wählt alle Sp ... t größer als 30 hat.)" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "locale = Symbol(answer_doc).translate('German')\n", "display(locale)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Fixing the query on the fly if something goes wrong or the user quickly wants to adapt a query:" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "sql.adapt(context=\"\"\"Explanation: Never allow SELECT *, always use LIMIT to a max of x <= 50 entries, where x is the user specified limit.\"\"\");" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/html": [ "<class examples.sql.SQL at 0x300ef12a0>(value=SQL: SELECT *\n", "FROM Members\n", "WHERE age > 30\n", "LIMIT 50)" ], "text/plain": [ "(value=SQL: SELECT *\n", "FROM Members\n", "WHERE age > 30\n", "LIMIT 50)" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "res = sql('Select all users above the age of 30 from the Members table.')\n", "display(res)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "sql.clear()" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/html": [ "<class examples.sql.SQL at 0x302e514b0>(value=SQL: SELECT * FROM Members WHERE age > 30)" ], "text/plain": [ "(value=SQL: SELECT * FROM Members WHERE age > 30)" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "res = sql('Select all users above the age of 30 from the Members table.')\n", "display(res)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "continualrl", "language": "python", "name": "python3" }, "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.8" }, "vscode": { "interpreter": { "hash": "7f6e76e437f50039fcb1dabeb26a40fb80edddddd3448a5d408bb52a844f2e97" } } }, "nbformat": 4, "nbformat_minor": 4 }