Skip to main content

SQL with LLM

Project description

sqllm - SQL with LLM functions

With this library, you can use LLM to perform queries on your data. The only LLM function you need to learn is the "AI" function.

  • AI(prompt: str) -> str: Returns text generated by GPT given the prompt.

Thanks to the cache, if the AI function is called repeatedly with the same arguments, the LLM is called only the first time.

import os
import sqllm

os.environ["OPENAI_API_KEY"] = "your-api-key"

# query over DB
conn  # any DB connection you have (passed into pd.read_sql)
sqllm.query(
    conn,
    """
    SELECT
        AI('Classify the sentiment expressed in the following text. \ntext:' || review)
    FROM
        reviews
    """
)

# query on pandas dataframe
df  # any dataframe
sqll.query_df(
    df,
    """
    SELECT
        AI('Classify the sentiment expressed in the following text. \ntext:' || review)
    FROM
        df
    """
)

In addition, your own Python functions can also be executed in SQL. This allows you to integrate various text processing with LLM into SQL.

from functools import lru_cache
from openai import OpenAI
import sqllm


# To reduce the number of LLM calls, the use of lru_cache is recommended.
@lru_cache
def sentiment(src: str) -> str:
    client = OpenAI(api_key="your-api-key")
    chat_completion = client.chat.completions.create(
        messages=[
            {
                "role": "system",
                "content": "Classify the sentiment expressed in the following text. The output should be one of 'positive', 'negative' or 'neutral'."
            },
            {
                "role": "user",
                "content": src
            }
        ],
        model="gpt-3.5-turbo",
    )
    return chat_completion.choices[0].message.content


sqllm.query(
    conn,
    """
    SELECT
        sentiment(review) as sentiment
    FROM
        reviews
    """,
    [sentiment]
)

The sqllm.functions module contains several example implementations of user-defined functions. This implementation can be used out of the box.

import sqllm
from sqllm.functions import sentiment, summarize


sqllm.query(
    conn,
    """
    SELECT
        sentiment(review) as sentiment,
        summarize(review) as review_summary
    FROM
        reviews
    """,
    [sentiment, summarize]
)

Important notes

This library is not recommended for execution on large data.

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

sqllm-0.0.1.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

sqllm-0.0.1-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file sqllm-0.0.1.tar.gz.

File metadata

  • Download URL: sqllm-0.0.1.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for sqllm-0.0.1.tar.gz
Algorithm Hash digest
SHA256 21d6838993670f7daa7e4a3ca47e85fed5afb40e5afb4db5bd79d1de381e3d37
MD5 453fc3b9339693d758f98b35bfc3cf79
BLAKE2b-256 bb0651ec5532fb31446f5c3f18935522642765ceeb2cd1c1b78b3e50cb1193d6

See more details on using hashes here.

File details

Details for the file sqllm-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: sqllm-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for sqllm-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 280818ac5f759012159e32b1e3eddc7faa51fecc0a9d687a5761f95176aaef00
MD5 93717d8ea5a04ad1c7e0ff26cb4d47fe
BLAKE2b-256 9d6c8c7efc74363922fea88dd2f63254cf2adcfebac9b80607b714f5df953f84

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