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.
Metadata
Release files for sqllm 0.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| sqllm-0.0.1.tar.gz | 6.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| sqllm-0.0.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 13.8 kB
Release files / sqllm-0.0.1.tar.gz
| Download URL | sqllm-0.0.1.tar.gz |
|---|---|
| Size | 6.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
21d6838993670f7daa7e4a3ca47e85fed5afb40e5afb4db5bd79d1de381e3d37
|
|
BLAKE2b-256 checksum How to use checksums |
bb0651ec5532fb31446f5c3f18935522642765ceeb2cd1c1b78b3e50cb1193d6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.3
|
Release files / sqllm-0.0.1-py3-none-any.whl
| Download URL | sqllm-0.0.1-py3-none-any.whl |
|---|---|
| Size | 7.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
280818ac5f759012159e32b1e3eddc7faa51fecc0a9d687a5761f95176aaef00
|
|
BLAKE2b-256 checksum How to use checksums |
9d6c8c7efc74363922fea88dd2f63254cf2adcfebac9b80607b714f5df953f84
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.3
|