Skip to main content

SQL Generator and Query Result Retriever

Project description

TextualOrm

Example Image

This tool generates SQL queries from natural language and retrieves query results. This orm generates sql queries from your input and the specifics of your connected database and can also run them to retrieve records.

Currently this orm will only honour retrieval queries. i.e any queries that perform delete, create or any crud actions wont be executed on your database

The core of this application is the use of Large Language Models to generate the sql queries. This orm currently supports two LLMs:

  1. SQL Generator LLM - (free and the default llm used).
  2. OpenAI (requires subscription to OpenAI)

You can find more information about the default SQL Generator by going to the link here. This has been fine-trained from the flan-t5-base model.

Requirements

  • Python
  • Postgres
  • Redis Note: Support for additional databases such as MySQL will be added in future updates.

Installation

pip install sql-generator

Usage Instructions

  1. Initialize the Generator

    from textual_orm import TextualOrm
    textual_orm = TextualOrm(connection_string="postgresql://user:password@host:port/db_name",
                llm_type=LLMType.DEFAULT, redis_host="localhost",
                redis_port=6379)
    

    To use the OpenAI implementation, add your api-key to the arguments:

    from textual_orm import TextualOrm
    textual_orm = TextualOrm(connection_string="postgresql://user:password@host:port/db_name",
                llm_type=LLMType.DEFAULT, redis_host="localhost",
                redis_port=6379, api-key="")
    
    
  2. Call the setup method: await textual_orm.setup()

  3. Generate the SQL query

    sql_query = await textual_orm.make_sql_request("List of settings", ["setting"])
    print(sql_query)
    

    This method takes three arguments:

    • question: Input question
    • tables: List of tables as reference
    • request_data: A boolean to indicate if it should query the database or return only the sql_query (default value is False)

    By default, the make_sql_request does not actually query the database. It returns back the generated sql query which you can look at and verify. To get an alternative sql query, please modify your input question.

    To query the database with the generated sql query. Call the method passing in request_data=True. This will return a response in this format:

    {
      'query': 'SELECT * FROM setting ORDER BY created_at timestamp DESC LIMIT 5;',
      'data': [<records>]
    }
    

    Where data is a list of records from the query response. data will be None if request_data=False as in the default case.

For better performance, speed and caching, redis is required.

Additional Arguments

This orm uses a default postgres max pool of 10. You can modify it if needed by passing your value to the max_pool argument.

Below is a list of other supported arguments to Orm: min_size=1 minimum size o pool max_size=10 maxiumum size of pool api_key="" api key for the given llm

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

textual_orm-0.1.1b0.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

textual_orm-0.1.1b0-py3-none-any.whl (11.9 kB view details)

Uploaded Python 3

File details

Details for the file textual_orm-0.1.1b0.tar.gz.

File metadata

  • Download URL: textual_orm-0.1.1b0.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for textual_orm-0.1.1b0.tar.gz
Algorithm Hash digest
SHA256 f07b398bd9135e5cae608462e98128bc69107976dce902f1fc35eb2e78c8d3a6
MD5 a2ccd08ce53c4543923494cb52b69887
BLAKE2b-256 0d7d0c5dbbf25c431348e418732036e3c92353e064240ca2c7caa035e6c99a71

See more details on using hashes here.

File details

Details for the file textual_orm-0.1.1b0-py3-none-any.whl.

File metadata

  • Download URL: textual_orm-0.1.1b0-py3-none-any.whl
  • Upload date:
  • Size: 11.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for textual_orm-0.1.1b0-py3-none-any.whl
Algorithm Hash digest
SHA256 49146ece1620aa3ab169babf63b8061d8729952b703179824fe3c1c8290867b6
MD5 a7f7e5ee76db721d3a6ddb3b1621d4b6
BLAKE2b-256 e59c02fcb8abaac0f0e60d13816d0965a610880ef9e7c8be3996979eb8efbda7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page