sqllm


Namesqllm JSON
Version 0.0.1 PyPI version JSON
download
home_pagehttps://github.com/nyanp/sqllm
SummarySQL with LLM
upload_time2023-11-25 14:31:16
maintainer
docs_urlNone
authornyanp
requires_python
licenseMIT
keywords llm sql
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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.

 
```python
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.

```python
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.


```python
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.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/nyanp/sqllm",
    "name": "sqllm",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "llm sql",
    "author": "nyanp",
    "author_email": "Noumi.Taiga@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/bb/06/51ec5532fb31446f5c3f18935522642765ceeb2cd1c1b78b3e50cb1193d6/sqllm-0.0.1.tar.gz",
    "platform": null,
    "description": "# sqllm - SQL with LLM functions\nWith this library, you can use LLM to perform queries on your data.\nThe only LLM function you need to learn is the \"AI\" function.\n\n- `AI(prompt: str) -> str`: Returns text generated by GPT given the prompt.\n\nThanks to the cache, if the AI function is called repeatedly with the same arguments, the LLM is called only the first time.\n\n \n```python\nimport os\nimport sqllm\n\nos.environ[\"OPENAI_API_KEY\"] = \"your-api-key\"\n\n# query over DB\nconn  # any DB connection you have (passed into pd.read_sql)\nsqllm.query(\n    conn,\n    \"\"\"\n    SELECT\n        AI('Classify the sentiment expressed in the following text. \\ntext:' || review)\n    FROM\n        reviews\n    \"\"\"\n)\n\n# query on pandas dataframe\ndf  # any dataframe\nsqll.query_df(\n    df,\n    \"\"\"\n    SELECT\n        AI('Classify the sentiment expressed in the following text. \\ntext:' || review)\n    FROM\n        df\n    \"\"\"\n)\n\n```\n\nIn addition, your own Python functions can also be executed in SQL. This allows you to integrate various text processing with LLM into SQL.\n\n```python\nfrom functools import lru_cache\nfrom openai import OpenAI\nimport sqllm\n\n\n# To reduce the number of LLM calls, the use of lru_cache is recommended.\n@lru_cache\ndef sentiment(src: str) -> str:\n    client = OpenAI(api_key=\"your-api-key\")\n    chat_completion = client.chat.completions.create(\n        messages=[\n            {\n                \"role\": \"system\",\n                \"content\": \"Classify the sentiment expressed in the following text. The output should be one of 'positive', 'negative' or 'neutral'.\"\n            },\n            {\n                \"role\": \"user\",\n                \"content\": src\n            }\n        ],\n        model=\"gpt-3.5-turbo\",\n    )\n    return chat_completion.choices[0].message.content\n\n\nsqllm.query(\n    conn,\n    \"\"\"\n    SELECT\n        sentiment(review) as sentiment\n    FROM\n        reviews\n    \"\"\",\n    [sentiment]\n)\n```\n\nThe `sqllm.functions` module contains several example implementations of user-defined functions. This implementation can be used out of the box.\n\n\n```python\nimport sqllm\nfrom sqllm.functions import sentiment, summarize\n\n\nsqllm.query(\n    conn,\n    \"\"\"\n    SELECT\n        sentiment(review) as sentiment,\n        summarize(review) as review_summary\n    FROM\n        reviews\n    \"\"\",\n    [sentiment, summarize]\n)\n```\n\n## Important notes\nThis library is not recommended for execution on large data.\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "SQL with LLM",
    "version": "0.0.1",
    "project_urls": {
        "Homepage": "https://github.com/nyanp/sqllm"
    },
    "split_keywords": [
        "llm",
        "sql"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "9d6c8c7efc74363922fea88dd2f63254cf2adcfebac9b80607b714f5df953f84",
                "md5": "93717d8ea5a04ad1c7e0ff26cb4d47fe",
                "sha256": "280818ac5f759012159e32b1e3eddc7faa51fecc0a9d687a5761f95176aaef00"
            },
            "downloads": -1,
            "filename": "sqllm-0.0.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "93717d8ea5a04ad1c7e0ff26cb4d47fe",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 7837,
            "upload_time": "2023-11-25T14:31:15",
            "upload_time_iso_8601": "2023-11-25T14:31:15.657065Z",
            "url": "https://files.pythonhosted.org/packages/9d/6c/8c7efc74363922fea88dd2f63254cf2adcfebac9b80607b714f5df953f84/sqllm-0.0.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "bb0651ec5532fb31446f5c3f18935522642765ceeb2cd1c1b78b3e50cb1193d6",
                "md5": "453fc3b9339693d758f98b35bfc3cf79",
                "sha256": "21d6838993670f7daa7e4a3ca47e85fed5afb40e5afb4db5bd79d1de381e3d37"
            },
            "downloads": -1,
            "filename": "sqllm-0.0.1.tar.gz",
            "has_sig": false,
            "md5_digest": "453fc3b9339693d758f98b35bfc3cf79",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 5973,
            "upload_time": "2023-11-25T14:31:16",
            "upload_time_iso_8601": "2023-11-25T14:31:16.980013Z",
            "url": "https://files.pythonhosted.org/packages/bb/06/51ec5532fb31446f5c3f18935522642765ceeb2cd1c1b78b3e50cb1193d6/sqllm-0.0.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-11-25 14:31:16",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "nyanp",
    "github_project": "sqllm",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [],
    "lcname": "sqllm"
}
        
Elapsed time: 0.20939s