import os
import warnings
warnings.filterwarnings('ignore')
print(os.getcwd())
from symai import *
from IPython.display import display
from examples.sql import SQL
sql = SQL()
/Users/ryang/Work/ExtensityAI/symbolicai/notebooks Configuration file: /Users/ryang/.symai/symai.config.json
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.
The following query is sent to the neural computation engine and creates a query based on the given context:
val = None
def _fun(x):
global val
val = x
Symbol._metadata.input_handler = _fun
Symbol('I have this class').translate('German')
q = sql('Select all users above the age of 30 from the Members table.'); display(q)
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:
res = q << 'and limit the query to the last 10 minutes'
display(res)
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:
res -= ' AND ...'
display(res)
And we can now even convert our query to other familiar domain specific languages, such as SQL
or ActiveRecord
:
sql_res = res.convert("ActiveRecord")
display(sql_res)
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:
answer_doc = res.query("What does this query do?")
display(answer_doc)
Ask it in natural language to modify the query:
answer = res.query("How can you limit the number of results to 30 for an SQL query?")
display(answer)
Even translate the explanation to a different language on the fly:
locale = Symbol(answer_doc).translate('German')
display(locale)
Fixing the query on the fly if something goes wrong or the user quickly wants to adapt a query:
sql.adapt(context="""Explanation: Never allow SELECT *, always use LIMIT to a max of x <= 50 entries, where x is the user specified limit.""");
res = sql('Select all users above the age of 30 from the Members table.')
display(res)
sql.clear()
res = sql('Select all users above the age of 30 from the Members table.')
display(res)