Skip to content

Commit fd38e5f

Browse files
authored
fix: update user agent (#36)
* fix: update user agent * move to common * fix import
1 parent 7f8100e commit fd38e5f

File tree

3 files changed

+38
-21
lines changed

3 files changed

+38
-21
lines changed

src/langchain_google_datastore/chat_message_history.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
from __future__ import annotations
1616

1717
import json
18-
from typing import TYPE_CHECKING, Any, Iterator, List, Optional
18+
from typing import TYPE_CHECKING, List, Optional
1919

20-
from google.cloud import datastore
2120
from langchain_core.chat_history import BaseChatMessageHistory
2221
from langchain_core.messages import BaseMessage, messages_from_dict
2322

23+
from .common import client_with_user_agent
2424
from .version import __version__
2525

2626
USER_AGENT = "langchain-google-datastore-python:chat_history/" + __version__
@@ -45,12 +45,7 @@ def __init__(
4545
and by default it will use `ChatHistory` as the kind.
4646
client: Client for interacting with the Google Cloud Firestore API.
4747
"""
48-
self.client = client or datastore.Client()
49-
client_agent = self.client._client_info.user_agent
50-
if not client_agent:
51-
self.client._client_info.user_agent = USER_AGENT
52-
elif USER_AGENT not in client_agent:
53-
self.client._client_info.user_agent = " ".join([client_agent, USER_AGENT])
48+
self.client = client_with_user_agent(client, USER_AGENT)
5449
self.session_id = session_id
5550
self.key = self.client.key(kind, session_id)
5651
self.messages: List[BaseMessage] = []
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from google.cloud import datastore # type: ignore
16+
from google.cloud.datastore_v1.services.datastore.transports.base import (
17+
DEFAULT_CLIENT_INFO, # type: ignore
18+
)
19+
20+
21+
def client_with_user_agent(
22+
client: datastore.Client | None, user_agent: str
23+
) -> datastore.Client:
24+
client_info = DEFAULT_CLIENT_INFO
25+
client_info.user_agent = user_agent
26+
if not client:
27+
client = datastore.Client(client_info=client_info)
28+
client_agent = client._client_info.user_agent
29+
if not client_agent:
30+
client._client_info.user_agent = user_agent
31+
elif user_agent not in client_agent:
32+
client._client_info.user_agent = " ".join([user_agent, client_agent])
33+
return client

src/langchain_google_datastore/document_loader.py

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
from __future__ import annotations
1616

1717
import itertools
18-
from typing import TYPE_CHECKING, Any, Iterator, List, Optional
18+
from typing import TYPE_CHECKING, Iterator, List, Optional
1919

2020
import more_itertools
21-
from google.cloud import datastore
2221
from langchain_community.document_loaders.base import BaseLoader
2322
from langchain_core.documents import Document
2423

24+
from .common import client_with_user_agent
2525
from .document_converter import (
2626
DATASTORE_TYPE,
2727
KEY,
@@ -155,14 +155,3 @@ def delete_documents(
155155
)
156156
db_batch.delete(key)
157157
db_batch.commit()
158-
159-
160-
def client_with_user_agent(client: Client | None, user_agent: str) -> Client:
161-
if not client:
162-
client = datastore.Client()
163-
client_agent = client._client_info.user_agent
164-
if not client_agent:
165-
client._client_info.user_agent = user_agent
166-
elif user_agent not in client_agent:
167-
client._client_info.user_agent = " ".join([client_agent, user_agent])
168-
return client

0 commit comments

Comments
 (0)