Skip to content

The agent engineering platform.

Add the BQ job usage tracking from LangChain (#17123)

Ashley Xuf746a73e26b0
diff --git a/libs/community/langchain_community/utils/google.py b/libs/community/langchain_community/utils/google.py
new file mode 100644
index 0000000000..4e68512296
--- /dev/null
+++ b/libs/community/langchain_community/utils/google.py
@@ -0,0 +1,25 @@
+"""Utilities to use Google provided components."""
+
+from importlib import metadata
+from typing import Any, Optional
+
+
+def get_client_info(module: Optional[str] = None) -> Any:
+ r"""Returns a custom user agent header.
+
+ Args:
+ module (Optional[str]):
+ Optional. The module for a custom user agent header.
+ Returns:
+ google.api_core.gapic_v1.client_info.ClientInfo
+ """
+ from google.api_core.gapic_v1.client_info import ClientInfo
+
+ langchain_version = metadata.version("langchain")
+ client_library_version = (
+ f"{langchain_version}-{module}" if module else langchain_version
+ )
+ return ClientInfo(
+ client_library_version=client_library_version,
+ user_agent=f"langchain/{client_library_version}",
+ )
diff --git a/libs/community/langchain_community/vectorstores/bigquery_vector_search.py b/libs/community/langchain_community/vectorstores/bigquery_vector_search.py
index 28edc6536c..3c77ffb917 100644
--- a/libs/community/langchain_community/vectorstores/bigquery_vector_search.py
+++ b/libs/community/langchain_community/vectorstores/bigquery_vector_search.py
@@ -16,6 +16,7 @@ from langchain_core.documents import Document
from langchain_core.embeddings import Embeddings
from langchain_core.vectorstores import VectorStore
+from langchain_community.utils.google import get_client_info
from langchain_community.vectorstores.utils import (
DistanceStrategy,
maximal_marginal_relevance,
@@ -30,7 +31,6 @@ DEFAULT_TOP_K = 4 # default number of documents returned from similarity search
_MIN_INDEX_ROWS = 5000 # minimal number of rows for creating an index
_INDEX_CHECK_PERIOD_SECONDS = 60 # Do not check for index more often that this.
-
_vector_table_lock = Lock() # process-wide BigQueryVectorSearch table lock
@@ -90,8 +90,12 @@ class BigQueryVectorSearch(VectorStore):
try:
from google.cloud import bigquery
+ client_info = get_client_info(module="bigquery-vector-search")
self.bq_client = bigquery.Client(
- project=project_id, location=location, credentials=credentials
+ project=project_id,
+ location=location,
+ credentials=credentials,
+ client_info=client_info,
)
except ModuleNotFoundError:
raise ImportError(