-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Add SQLAlchemy session backend for conversation history management #1357
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
Merged
Merged
Changes from 15 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
e534fa0
Add SQLAlchemy session backend for conversation history management
habema f669093
temporary fixes to pass typecheck and old_versions CI tests
habema 78614fd
linter fixes
habema ea1177c
Merge remote-tracking branch 'origin/main' into feat/sqlalchemy-sessions
habema 48b675c
fix mypy errors
habema 94b6ce9
more mypy fixes
habema 18d38b2
even more mypy and lint fixes
habema 83dcdd8
Merge branch 'main' into feat/sqlalchemy-sessions
habema a1bf963
Update SQLAlchemySession to default create_tables to False
habema af6c9e1
add docs
habema c4edf74
add example
habema 38f270f
fix imports in example
habema 55167e0
code review: move index to table initialization
habema f6579e8
code review: explanatory comment for order/reverse behaviour
habema 3f9f31a
code review: move serialization to separate method
habema 43f7a40
code review: move deserialization to separate method
habema 43f1fd6
remove docs to move to another PR
habema 26d44ed
fix mypy
habema d8853a9
code review: make serialization/deserialization async
habema File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import asyncio | ||
|
|
||
| from agents import Agent, Runner | ||
| from agents.extensions.memory.sqlalchemy_session import SQLAlchemySession | ||
|
|
||
|
|
||
| async def main(): | ||
| # Create an agent | ||
| agent = Agent( | ||
| name="Assistant", | ||
| instructions="Reply very concisely.", | ||
| ) | ||
|
|
||
| # Create a session instance with a session ID. | ||
| # This example uses an in-memory SQLite database. | ||
| # The `create_tables=True` flag is useful for development and testing. | ||
| session = SQLAlchemySession.from_url( | ||
| "conversation_123", | ||
| url="sqlite+aiosqlite:///:memory:", | ||
| create_tables=True, | ||
| ) | ||
|
|
||
| print("=== SQLAlchemySession Example ===") | ||
| print("The agent will remember previous messages automatically.\n") | ||
|
|
||
| # First turn | ||
| print("User: What city is the Golden Gate Bridge in?") | ||
| result = await Runner.run( | ||
| agent, | ||
| "What city is the Golden Gate Bridge in?", | ||
| session=session, | ||
| ) | ||
| print(f"Assistant: {result.final_output}\n") | ||
|
|
||
| # Second turn - the agent will remember the previous conversation | ||
| print("User: What state is it in?") | ||
| result = await Runner.run( | ||
| agent, | ||
| "What state is it in?", | ||
| session=session, | ||
| ) | ||
| print(f"Assistant: {result.final_output}\n") | ||
|
|
||
| print("=== Conversation Complete ===") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| # To run this example, you need to install the sqlalchemy extras: | ||
| # pip install "agents[sqlalchemy]" | ||
| asyncio.run(main()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
|
|
||
| """Session memory backends living in the extensions namespace. | ||
|
|
||
| This package contains optional, production-grade session implementations that | ||
| introduce extra third-party dependencies (database drivers, ORMs, etc.). They | ||
| conform to the :class:`agents.memory.session.Session` protocol so they can be | ||
| used as a drop-in replacement for :class:`agents.memory.session.SQLiteSession`. | ||
| """ | ||
| from __future__ import annotations | ||
|
|
||
| from .sqlalchemy_session import SQLAlchemySession # noqa: F401 | ||
|
|
||
| __all__: list[str] = [ | ||
| "SQLAlchemySession", | ||
| ] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.