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

feat: BigtableChatMessageHistory implementation #6

Merged
merged 27 commits into from
Feb 7, 2024
Merged

feat: BigtableChatMessageHistory implementation #6

merged 27 commits into from
Feb 7, 2024

Conversation

ron-gal
Copy link
Contributor

@ron-gal ron-gal commented Jan 23, 2024

Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly:

  • Make sure to open an issue as a bug/issue before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea
  • Ensure the tests and linter pass
  • Code coverage does not decrease (if any source code was changed)
  • Appropriate docs were updated (if necessary)

Fixes #<issue_number_goes_here> 🦕

@ron-gal ron-gal requested a review from a team as a code owner January 23, 2024 18:39
"metadata": {},
"outputs": [],
"source": [
"!pip install google-cloud-bigtable"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
"!pip install google-cloud-bigtable"
"%pip install langchain-google-bigtable"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

self,
instance_id: str,
table_id: str,
session_id: Optional[str] = None,
Copy link
Collaborator

Choose a reason for hiding this comment

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

session id is required

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

src/langchain_google_bigtable/chat_message_history.py Outdated Show resolved Hide resolved
@product-auto-label product-auto-label bot added the api: bigtable Issues related to the googleapis/langchain-google-bigtable-python API. label Feb 6, 2024
"cell_type": "markdown",
"metadata": {},
"source": [
"To run this notebook, you will need a Google Cloud Project, a Bigtable instance, and Google credentials."
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we provide some links to how to create or set these things?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

"cell_type": "markdown",
"metadata": {},
"source": [
"## Setting up"
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: In Cloud Docs we call this "Before you begin" -- should we be consistent here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Pre-reqs"
"## Setting up the schema\n",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
"## Setting up the schema\n",
"## Initializing schema\n",

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

"## Pre-reqs"
"## Setting up the schema\n",
"\n",
"The chat history will be written to a column called `history` in a column family called `langchain`. If this column family does not exist in your table, you will need to call init_schema:"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Does this imply the table should be already created? Or will init_schema all create the table?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The table is not created by init_schema, only the column family. I don't mind creating the table as well, though, but at some point we said we shouldn't. I have no preference :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Table created in ctor.

client: Optional[bigtable.Client] = None,
) -> None:
self.client = (
(client or bigtable.Client(admin=True))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we memoize the bigtable Client between multiple integrations?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

def clear(self) -> None:
"""Clear session memory from DB"""
row_key_prefix = self.session_id
self.client.drop_by_prefix(row_key_prefix, timeout=200)
Copy link
Collaborator

Choose a reason for hiding this comment

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

should timeout be configurable?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

dropped it altogether. It probably shouldn't be configurable, as this operation should be quick enough.

src/test/test_chat_message_history.py Show resolved Hide resolved
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Basic Usage"
"## Cleaning up\n",

Choose a reason for hiding this comment

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

Would it flow better to add this section to the bottom of "Basic Usage"? It would then be initialize ChatMessageHistory -> add messages -> show messages -> clear messages This way you don't need to initialize another class object and can just use the one you already have...

message_history.clear()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@kurtisvg kurtisvg self-assigned this Feb 7, 2024
Comment on lines 50 to 51
")\n",
"\n",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we show the initialization of the table here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Comment on lines 61 to 65
def __get_default_client(self) -> bigtable.Client:
global default_client
if default_client is None:
default_client = bigtable.Client(admin=True)
return default_client
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: I don't think this should be attached to the class -- I think we want to use it in all classes

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will be done after this PR is merged

Comment on lines 58 to 59
self.session_id = session_id
self.__init_schema()
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should expose this to the customer and ask them to do it. It can still be a class method -- but should probably handle create table as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@ron-gal ron-gal requested a review from kurtisvg February 7, 2024 18:14
"init_schema(\n",
" instance_id=\"my-instance\",\n",
" table_id=\"my-table\",\n",
" client=bigtable.Client(...),\n",
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: Probably best to recommend declaring the client ahead of time, so it's hinted at it should be reused.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

default_client: Optional[bigtable.Client] = None


def init_schema(
Copy link
Collaborator

Choose a reason for hiding this comment

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

suggest renaming function -- "create_chat_history_table" or similar

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@ron-gal ron-gal requested a review from kurtisvg February 7, 2024 19:47
@ron-gal ron-gal requested review from averikitsch and removed request for averikitsch February 7, 2024 19:51
@ron-gal ron-gal requested a review from averikitsch February 7, 2024 20:01
@ron-gal ron-gal merged commit 55e4422 into main Feb 7, 2024
8 checks passed
@ron-gal ron-gal deleted the memory branch February 7, 2024 20:01
@release-please release-please bot mentioned this pull request Feb 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: bigtable Issues related to the googleapis/langchain-google-bigtable-python API.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants