Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

community/SQLDatabase: Fetch mode cursor, query parameters, query by selectable, expose execution options, and documentation #16655

Closed

Conversation

amotl
Copy link
Contributor

@amotl amotl commented Jan 26, 2024

Details

  • Add cursor fetch mode
  • Accept SQL query parameters
  • Accept both str and SQLAlchemy selectables as query expression
  • Expose execution_options
  • Documentation page (notebook) about SQLDatabase 1
    See About SQLDatabase.

Footnotes

  1. Apparently there hasn't been any yet?

Copy link

vercel bot commented Jan 26, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
langchain ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 5, 2024 8:41pm

@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. 🤖:improvement Medium size change to existing code to handle new use-cases labels Jan 26, 2024
@amotl amotl force-pushed the sqldatabase-improvements branch 2 times, most recently from 916e376 to 194d257 Compare January 27, 2024 00:15
@amotl amotl marked this pull request as draft January 27, 2024 00:16
@amotl amotl force-pushed the sqldatabase-improvements branch 2 times, most recently from b3df1b5 to 9cb633b Compare January 27, 2024 00:22
@amotl amotl marked this pull request as ready for review January 27, 2024 00:26
@dosubot dosubot bot added size:XL This PR changes 500-999 lines, ignoring generated files. and removed size:L This PR changes 100-499 lines, ignoring generated files. labels Jan 27, 2024
@amotl amotl requested a review from cbornet January 27, 2024 20:26
@amotl amotl changed the title community/SQLDatabase: Fetch mode, query parameters, query by selectable community/SQLDatabase: Fetch mode, query parameters, query by selectable, expose execution options, and documentation Jan 27, 2024
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this needed? feels like the on_text method should really just accept strings

Copy link
Contributor Author

@amotl amotl Jan 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @baskaryan. This adjustment was needed to satisfy the mypy linter, to accompany the possibility that this can now also return a SQLAlchemy Result object here, which would otherwise fail.

Copy link
Contributor Author

@amotl amotl Feb 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just cast the output of SQLDatabase wherever it's passed to on_text instead of changing the on_text method (since it doesn't actually support non-strings)?

Hi again. I just reverted this adjustment, and added a cast at a different spot like you suggested, on behalf of 08db71b (now squashed).

Edit: Looks good so far, the current linter failures on CI are unrelated, as they also appear on a run of a different PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After rebasing this patch on top of master, it needed a few adjustments, which have been implemented now. Based on the CI outcome, the patch may receive another round of code reviews. Thank you.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi again. It looks like @eyurtsev is taking over. Thank you so much!

@amotl amotl requested a review from baskaryan January 29, 2024 23:32
@amotl amotl changed the title community/SQLDatabase: Fetch mode, query parameters, query by selectable, expose execution options, and documentation community/SQLDatabase: Fetch mode cursor, query parameters, query by selectable, expose execution options, and documentation Jan 30, 2024
…ctable

- Add `cursor` fetch mode
- Make `_execute` accept query parameters
- Accept both `str` or SQLAlchemy selectables for querying
Up until now, the `SQLDatabase` utility only has been demonstrated on
behalf of the "toolkit integration" section, where it is about
showcasing an agent designed to interact with an SQL database. [1]

Contrary to that, this patch adds a documentation page exclusively
dedicated to the SQLDatabase adapter utility, and its features.

[1] https://python.langchain.com/docs/integrations/toolkits/sql_database
_run_manager.on_text(result, color="yellow", verbose=self.verbose)
_run_manager.on_text(str(result), color="yellow", verbose=self.verbose)
Copy link
Contributor Author

@amotl amotl Feb 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just cast the output of SQLDatabase wherever it's passed to on_text instead of changing the on_text method.

@baskaryan: Your suggestion from #16655 (comment) has been implemented here. mypy is fine with that, and we do not need to touch anything in core.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems fine to me

Copy link
Contributor Author

@amotl amotl Feb 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am wondering if this unit test file, together with test_sql_database_schema.py, should actually move into community/tests/unit_tests, following the logic of recent refactorings, and given that the files under test are also in community?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably yes, we can follow up in a separate PR

@eyurtsev eyurtsev self-assigned this Feb 7, 2024
@eyurtsev
Copy link
Collaborator

eyurtsev commented Feb 7, 2024

Reviewing and comandeering to adjust a few things in the API

@eyurtsev
Copy link
Collaborator

eyurtsev commented Feb 7, 2024

@amotl are you able to give me permissions to modify the head on the given branch? (it's an option you can enable when creating the PR)

I fixed a breaking change due to lack of * syntax, and updated the notebook with a security notebook. We can merge afterwards.

I left another review on the original PR to see if keyset pagination is an option (instead of relying on a cursor)

@eyurtsev eyurtsev added the waiting-on-author PR Status: Confirmation from author is required label Feb 7, 2024
@eyurtsev
Copy link
Collaborator

eyurtsev commented Feb 7, 2024

Here is a PR that contains the proposed changes: https://github.com/langchain-ai/langchain/pull/17191/files, would prefer to push here so you can 👍 👎 the differences

@amotl
Copy link
Contributor Author

amotl commented Feb 7, 2024

Hi @eyurtsev. Thanks for the review.

are you able to give me permissions to modify the head on the given branch? (it's an option you can enable when creating the PR)

Is there a way to modify it on the PR afterwards? I admit I never recognized that option, apologies. In any case, I've added you to the list of contributors, where you may be able to accept it on the invitations page. Does that help already?

Here is a PR that contains the proposed changes: https://github.com/langchain-ai/langchain/pull/17191/files, would prefer to push here so you can 👍 👎 the differences

Thanks. I've looked at 4f3b063 and 806ba2e, and I appreciate your improvements. Now, I hope we will get the permission problem sorted out?

@amotl amotl requested a review from eyurtsev February 7, 2024 21:20
@amotl
Copy link
Contributor Author

amotl commented Feb 7, 2024

I hope we will get the permission problem sorted out?

I've just digested the documentation about Allowing changes to a pull request branch created from a fork, but I don't see the Allow edits from maintainers. or Allow edits and access to secrets by maintainers. options on the PR.

Within the documentation, they refer to "forks owned by a personal account". Is that the reason it doesn't work, because this fork is on an organization account?

@amotl
Copy link
Contributor Author

amotl commented Feb 7, 2024

Is that the reason it doesn't work, because this fork is on an organization account?

Right, here we go -- it does not work:

You cannot give push permissions to a fork owned by an organization.

-- https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/about-permissions-and-visibility-of-forks#about-permissions-of-forks


What I was able to do is to add you to the list of contributors, where you may be able to accept it on the invitations page.

Does that help you in any other way?

@eyurtsev
Copy link
Collaborator

eyurtsev commented Feb 8, 2024

@amotl I'll close this PR and merge the branch I had: #17191 hope this is OK

@eyurtsev
Copy link
Collaborator

eyurtsev commented Feb 8, 2024

Merged the other PR in. I'll read up on the links you sent to try and figure out if there's any other way to do this -- from what i understood it sounds like there's a difference between forks owned by individuals vs. forks owned by orgs

@eyurtsev eyurtsev closed this Feb 8, 2024
@amotl
Copy link
Contributor Author

amotl commented Feb 8, 2024

Merged the other PR in. Hope this is OK.

Definitively, I appreciate it. Thanks!

from what i understood it sounds like there's a difference between forks owned by individuals vs. forks owned by orgs

Exactly. Apparently, repos owned by orgs do not offer that feature to permit upstream authors access to their PRs. Bummer!

eyurtsev pushed a commit that referenced this pull request Feb 13, 2024
- **Description:** Improve test cases for `SQLDatabase` adapter
component, see
[suggestion](#16655 (review)).
  - **Depends on:** GH-16655
  - **Addressed to:** @baskaryan, @cbornet, @eyurtsev

_Remark: This PR is stacked upon GH-16655, so that one will need to go
in first._

Edit: Thank you for bringing in GH-17191, @eyurtsev. This is a little
aftermath, improving/streamlining the corresponding test cases.
snsten pushed a commit to snsten/langchain that referenced this pull request Feb 15, 2024
…ai#16659)

- **Description:** Improve test cases for `SQLDatabase` adapter
component, see
[suggestion](langchain-ai#16655 (review)).
  - **Depends on:** langchain-aiGH-16655
  - **Addressed to:** @baskaryan, @cbornet, @eyurtsev

_Remark: This PR is stacked upon langchain-aiGH-16655, so that one will need to go
in first._

Edit: Thank you for bringing in langchain-aiGH-17191, @eyurtsev. This is a little
aftermath, improving/streamlining the corresponding test cases.
haydeniw pushed a commit to haydeniw/langchain that referenced this pull request Feb 27, 2024
…ai#16659)

- **Description:** Improve test cases for `SQLDatabase` adapter
component, see
[suggestion](langchain-ai#16655 (review)).
  - **Depends on:** langchain-aiGH-16655
  - **Addressed to:** @baskaryan, @cbornet, @eyurtsev

_Remark: This PR is stacked upon langchain-aiGH-16655, so that one will need to go
in first._

Edit: Thank you for bringing in langchain-aiGH-17191, @eyurtsev. This is a little
aftermath, improving/streamlining the corresponding test cases.
eyurtsev added a commit that referenced this pull request Feb 28, 2024
- **Description:** A generic document loader adapter for SQLAlchemy on
top of LangChain's `SQLDatabaseLoader`.
  - **Needed by:** crate-workbench#1
  - **Depends on:** GH-16655
  - **Addressed to:** @baskaryan, @cbornet, @eyurtsev

Hi from CrateDB again,

in the same spirit like GH-16243 and GH-16244, this patch breaks out
another commit from crate-workbench#1,
in order to reduce the size of this patch before submitting it, and to
separate concerns.

To accompany the SQLAlchemy adapter implementation, the patch includes
integration tests for both SQLite and PostgreSQL. Let me know if
corresponding utility resources should be added at different spots.

With kind regards,
Andreas.


### Software Tests

```console
docker compose --file libs/community/tests/integration_tests/document_loaders/docker-compose/postgresql.yml up
```

```console
cd libs/community
pip install psycopg2-binary
pytest -vvv tests/integration_tests -k sqldatabase
```

```
14 passed
```



![image](https://github.com/langchain-ai/langchain/assets/453543/42be233c-eb37-4c76-a830-474276e01436)

---------

Co-authored-by: Andreas Motl <andreas.motl@crate.io>
gkorland pushed a commit to FalkorDB/langchain that referenced this pull request Mar 30, 2024
…ai#18281)

- **Description:** A generic document loader adapter for SQLAlchemy on
top of LangChain's `SQLDatabaseLoader`.
  - **Needed by:** crate-workbench#1
  - **Depends on:** langchain-aiGH-16655
  - **Addressed to:** @baskaryan, @cbornet, @eyurtsev

Hi from CrateDB again,

in the same spirit like langchain-aiGH-16243 and langchain-aiGH-16244, this patch breaks out
another commit from crate-workbench#1,
in order to reduce the size of this patch before submitting it, and to
separate concerns.

To accompany the SQLAlchemy adapter implementation, the patch includes
integration tests for both SQLite and PostgreSQL. Let me know if
corresponding utility resources should be added at different spots.

With kind regards,
Andreas.


### Software Tests

```console
docker compose --file libs/community/tests/integration_tests/document_loaders/docker-compose/postgresql.yml up
```

```console
cd libs/community
pip install psycopg2-binary
pytest -vvv tests/integration_tests -k sqldatabase
```

```
14 passed
```



![image](https://github.com/langchain-ai/langchain/assets/453543/42be233c-eb37-4c76-a830-474276e01436)

---------

Co-authored-by: Andreas Motl <andreas.motl@crate.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤖:improvement Medium size change to existing code to handle new use-cases size:XL This PR changes 500-999 lines, ignoring generated files. waiting-on-author PR Status: Confirmation from author is required
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants