Skip to main content

NLP interface for Trilogy

Project description

Trilogy NLP

Natural language interface for generating SQL queries via a Trilogy data model.

Trilogy is easier for a large language model (LLM) to interact with as it requires only identifying which objects in the data model best match the question, rather than generating arbitrary SQL from scratch. The extra data encoded in the semantic model for query generation reduces common sources of LLM errors.

This makes it more testable and less prone to hallucination than generating SQL directly.

Requires setting the following environment variables

  • OPENAI_API_KEY
  • OPENAI_MODEL

Recommended to use "gpt-3.5-turbo" or higher as the model.

Examples

Basic BQ example

from trilogy_public_models import models
from trilogy import Executor, Dialects
from trilogy_nlp import build_query

# define the model we want to parse
environment = models["bigquery.stack_overflow"]

# set up preql executor
# default bigquery executor requires local default credentials configured
executor = Dialects.BIGQUERY.default_executor(environment= environment)

# build a query off text and the selected model
processed_query = build_query(
    "How many questions are asked per year?",
    environment,
)

# make sure we got reasonable outputs
for concept in processed_query.output_columns:
    print(concept.name)

# and run that to get our answer
results = executor.execute_query(processed_query)
for row in results:
    print(row)

Don't Expecct Perfection

Results are non-determistic and may not always be accurate.

# generated from prompt: What is Taylor Swift's birthday? How many questions were asked on that day in 2020?
SELECT
    question.count,
    answer.creation_date.year,
    question.creation_date.year,
    question.creation_date,
WHERE
    question.creation_date.year = 1989
ORDER BY
    question.count desc,

    question.count desc

LIMIT 100;

Setting Up Your Environment

Recommend that you work in a virtual environment with requirements from both requirements.txt and requirements-test.txt installed. The latter is necessary to run tests (surprise).

trilogy-nlp is python 3.10+

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

pytrilogy_nlp-0.0.18.tar.gz (27.4 kB view details)

Uploaded Source

Built Distribution

pytrilogy_nlp-0.0.18-py3-none-any.whl (32.2 kB view details)

Uploaded Python 3

File details

Details for the file pytrilogy_nlp-0.0.18.tar.gz.

File metadata

  • Download URL: pytrilogy_nlp-0.0.18.tar.gz
  • Upload date:
  • Size: 27.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for pytrilogy_nlp-0.0.18.tar.gz
Algorithm Hash digest
SHA256 0a571a6659f8620e155274416f5ea528bcbd37d75b683ccbe612fdb7d197a761
MD5 ab085bb1147da14da6f570e002eb7af3
BLAKE2b-256 032557c74bdeb8df60d4b87d40f03695fb887f704485f44b4c54b38b59a2ddaa

See more details on using hashes here.

File details

Details for the file pytrilogy_nlp-0.0.18-py3-none-any.whl.

File metadata

File hashes

Hashes for pytrilogy_nlp-0.0.18-py3-none-any.whl
Algorithm Hash digest
SHA256 7ce647ecee15465a9a5c5c50814acb160a8cbe43a6bd852dece5eb0a24889fe0
MD5 4582a6d9455f90b3510bf51c5325fd47
BLAKE2b-256 157f0aee72af11117ddfd32129a63036d7d636c62c67ca2a7e39091f84562b72

See more details on using hashes here.

Supported by

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