langchain-google-genai 1.0.3__py3-none-any.whl → 1.0.5__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of langchain-google-genai might be problematic. Click here for more details.

@@ -1,12 +1,19 @@
1
1
  from typing import Any, Dict, List, Optional
2
2
 
3
3
  # TODO: remove ignore once the google package is published with types
4
- import google.generativeai as genai # type: ignore[import]
4
+ from google.ai.generativelanguage_v1beta.types import (
5
+ BatchEmbedContentsRequest,
6
+ EmbedContentRequest,
7
+ )
5
8
  from langchain_core.embeddings import Embeddings
6
9
  from langchain_core.pydantic_v1 import BaseModel, Field, SecretStr, root_validator
7
10
  from langchain_core.utils import get_from_dict_or_env
8
11
 
9
- from langchain_google_genai._common import GoogleGenerativeAIError
12
+ from langchain_google_genai._common import (
13
+ GoogleGenerativeAIError,
14
+ get_client_info,
15
+ )
16
+ from langchain_google_genai._genai_extension import build_generative_service
10
17
 
11
18
 
12
19
  class GoogleGenerativeAIEmbeddings(BaseModel, Embeddings):
@@ -27,6 +34,7 @@ class GoogleGenerativeAIEmbeddings(BaseModel, Embeddings):
27
34
  embeddings.embed_query("What's our Q1 revenue?")
28
35
  """
29
36
 
37
+ client: Any #: :meta private:
30
38
  model: str = Field(
31
39
  ...,
32
40
  description="The name of the embedding model to use. "
@@ -70,44 +78,43 @@ class GoogleGenerativeAIEmbeddings(BaseModel, Embeddings):
70
78
  @root_validator()
71
79
  def validate_environment(cls, values: Dict) -> Dict:
72
80
  """Validates params and passes them to google-generativeai package."""
73
- if values.get("credentials"):
74
- genai.configure(
75
- credentials=values.get("credentials"),
76
- transport=values.get("transport"),
77
- client_options=values.get("client_options"),
78
- )
79
- else:
80
- google_api_key = get_from_dict_or_env(
81
- values, "google_api_key", "GOOGLE_API_KEY"
82
- )
83
- if isinstance(google_api_key, SecretStr):
84
- google_api_key = google_api_key.get_secret_value()
85
-
86
- genai.configure(
87
- api_key=google_api_key,
88
- transport=values.get("transport"),
89
- client_options=values.get("client_options"),
90
- )
81
+ google_api_key = get_from_dict_or_env(
82
+ values, "google_api_key", "GOOGLE_API_KEY"
83
+ )
84
+ client_info = get_client_info("GoogleGenerativeAIEmbeddings")
85
+
86
+ values["client"] = build_generative_service(
87
+ credentials=values.get("credentials"),
88
+ api_key=google_api_key,
89
+ client_info=client_info,
90
+ client_options=values.get("client_options"),
91
+ )
91
92
  return values
92
93
 
93
- def _embed(
94
- self, texts: List[str], task_type: str, title: Optional[str] = None
95
- ) -> List[List[float]]:
96
- task_type = self.task_type or "retrieval_document"
97
- try:
98
- result = genai.embed_content(
99
- model=self.model,
100
- content=texts,
101
- task_type=task_type,
102
- title=title,
103
- request_options=self.request_options,
104
- )
105
- except Exception as e:
106
- raise GoogleGenerativeAIError(f"Error embedding content: {e}") from e
107
- return result["embedding"]
94
+ def _prepare_request(
95
+ self,
96
+ text: str,
97
+ task_type: Optional[str] = None,
98
+ title: Optional[str] = None,
99
+ output_dimensionality: Optional[int] = None,
100
+ ) -> EmbedContentRequest:
101
+ task_type = self.task_type or task_type or "RETRIEVAL_DOCUMENT"
102
+ # https://ai.google.dev/api/rest/v1/models/batchEmbedContents#EmbedContentRequest
103
+ request = EmbedContentRequest(
104
+ content={"parts": [{"text": text}]},
105
+ model=self.model,
106
+ task_type=task_type.upper(),
107
+ title=title,
108
+ output_dimensionality=output_dimensionality,
109
+ )
110
+ return request
108
111
 
109
112
  def embed_documents(
110
- self, texts: List[str], batch_size: int = 5
113
+ self,
114
+ texts: List[str],
115
+ task_type: Optional[str] = None,
116
+ titles: Optional[List[str]] = None,
117
+ output_dimensionality: Optional[int] = None,
111
118
  ) -> List[List[float]]:
112
119
  """Embed a list of strings. Vertex AI currently
113
120
  sets a max batch size of 5 strings.
@@ -115,21 +122,58 @@ class GoogleGenerativeAIEmbeddings(BaseModel, Embeddings):
115
122
  Args:
116
123
  texts: List[str] The list of strings to embed.
117
124
  batch_size: [int] The batch size of embeddings to send to the model
125
+ task_type: task_type (https://ai.google.dev/api/rest/v1/TaskType)
126
+ titles: An optional list of titles for texts provided.
127
+ Only applicable when TaskType is RETRIEVAL_DOCUMENT.
128
+ output_dimensionality: Optional reduced dimension for the output embedding.
129
+ https://ai.google.dev/api/rest/v1/models/batchEmbedContents#EmbedContentRequest
118
130
 
119
131
  Returns:
120
132
  List of embeddings, one for each text.
121
133
  """
122
- task_type = self.task_type or "retrieval_document"
123
- return self._embed(texts, task_type=task_type)
134
+ titles = titles if titles else [None] * len(texts) # type: ignore[list-item]
135
+ requests = [
136
+ self._prepare_request(
137
+ text=text,
138
+ task_type=task_type,
139
+ title=title,
140
+ output_dimensionality=output_dimensionality,
141
+ )
142
+ for text, title in zip(texts, titles)
143
+ ]
124
144
 
125
- def embed_query(self, text: str) -> List[float]:
145
+ try:
146
+ result = self.client.batch_embed_contents(
147
+ BatchEmbedContentsRequest(requests=requests, model=self.model)
148
+ )
149
+ except Exception as e:
150
+ raise GoogleGenerativeAIError(f"Error embedding content: {e}") from e
151
+ return [e.values for e in result.embeddings]
152
+
153
+ def embed_query(
154
+ self,
155
+ text: str,
156
+ task_type: Optional[str] = None,
157
+ title: Optional[str] = None,
158
+ output_dimensionality: Optional[int] = None,
159
+ ) -> List[float]:
126
160
  """Embed a text.
127
161
 
128
162
  Args:
129
163
  text: The text to embed.
164
+ task_type: task_type (https://ai.google.dev/api/rest/v1/TaskType)
165
+ title: An optional title for the text.
166
+ Only applicable when TaskType is RETRIEVAL_DOCUMENT.
167
+ output_dimensionality: Optional reduced dimension for the output embedding.
168
+ https://ai.google.dev/api/rest/v1/models/batchEmbedContents#EmbedContentRequest
130
169
 
131
170
  Returns:
132
171
  Embedding for the text.
133
172
  """
134
- task_type = self.task_type or "retrieval_query"
135
- return self._embed([text], task_type=task_type)[0]
173
+ task_type = self.task_type or "RETRIEVAL_QUERY"
174
+ return self.embed_documents(
175
+ [text],
176
+ task_type=task_type,
177
+ titles=[title] if title else None,
178
+ output_dimensionality=output_dimensionality,
179
+ )[0]
@@ -174,7 +174,6 @@ Supported examples:
174
174
  from google.generativeai.types.safety_types import HarmBlockThreshold, HarmCategory
175
175
 
176
176
  safety_settings = {
177
- HarmCategory.HARM_CATEGORY_UNSPECIFIED: HarmBlockThreshold.BLOCK_NONE,
178
177
  HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
179
178
  HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_ONLY_HIGH,
180
179
  HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_LOW_AND_ABOVE,
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: langchain-google-genai
3
- Version: 1.0.3
3
+ Version: 1.0.5
4
4
  Summary: An integration package connecting Google's genai package and LangChain
5
5
  Home-page: https://github.com/langchain-ai/langchain-google
6
6
  License: MIT
@@ -13,7 +13,7 @@ Classifier: Programming Language :: Python :: 3.11
13
13
  Classifier: Programming Language :: Python :: 3.12
14
14
  Provides-Extra: images
15
15
  Requires-Dist: google-generativeai (>=0.5.2,<0.6.0)
16
- Requires-Dist: langchain-core (>=0.1.45,<0.2)
16
+ Requires-Dist: langchain-core (>=0.2.0,<0.3)
17
17
  Requires-Dist: pillow (>=10.1.0,<11.0.0) ; extra == "images"
18
18
  Project-URL: Repository, https://github.com/langchain-ai/langchain-google
19
19
  Project-URL: Source Code, https://github.com/langchain-ai/langchain-google/tree/main/libs/genai
@@ -0,0 +1,16 @@
1
+ langchain_google_genai/__init__.py,sha256=Oji-S2KYWrku1wyQEskY84IOfY8MfRhujjJ4d7hbsk4,2758
2
+ langchain_google_genai/_common.py,sha256=ASlwE8hEbvOm55BVF_D4rf2nl7RYsnpsi5xbM6DW3Cc,1576
3
+ langchain_google_genai/_enums.py,sha256=KLPmxS1K83K4HjBIXFaXoL_sFEOv8Hq-2B2PDMKyDgo,197
4
+ langchain_google_genai/_function_utils.py,sha256=d0ApSCjoV9Em1CteBaGznilxrw-PDXqQ4sQa5p7cJfM,8232
5
+ langchain_google_genai/_genai_extension.py,sha256=ZwNwLV22RSf9LB7FOCLsoHzLlQDF-EQmRNYM1an2uSw,20769
6
+ langchain_google_genai/_image_utils.py,sha256=-0XgCMdYkvrIktFvUpy-2GPbFgfSVKZICawB2hiJzus,4999
7
+ langchain_google_genai/chat_models.py,sha256=Az0IrCm-7HvTkJ5UWTkTwvuy9mfhYLXjAlshb6mXLcs,33799
8
+ langchain_google_genai/embeddings.py,sha256=kQW6pl1TUGKKSxiUjc7rptp0iEu_Rer1m1LLHKVaW14,6578
9
+ langchain_google_genai/genai_aqa.py,sha256=zcC5cdFYtqLK7DGPhYGvWNeHHeU-CQKA9KhewmsA5lw,4303
10
+ langchain_google_genai/google_vector_store.py,sha256=PPIk-4FmD5UUdmYA2u7VcEhGsiztvRVN59QoGLXdfoA,16139
11
+ langchain_google_genai/llms.py,sha256=S7tOy-c37DElcHtkGl8rwvvg1zOzCxb9PEyJ4E-j7qU,13431
12
+ langchain_google_genai/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
13
+ langchain_google_genai-1.0.5.dist-info/LICENSE,sha256=DppmdYJVSc1jd0aio6ptnMUn5tIHrdAhQ12SclEBfBg,1072
14
+ langchain_google_genai-1.0.5.dist-info/METADATA,sha256=4l25SZWfEPwmCo4S6PpYtcrDMKgoPp9x-tHHe5tUYzI,3817
15
+ langchain_google_genai-1.0.5.dist-info/WHEEL,sha256=FMvqSimYX_P7y0a7UY-_Mc83r5zkBZsCYPm7Lr0Bsq4,88
16
+ langchain_google_genai-1.0.5.dist-info/RECORD,,
@@ -1,15 +0,0 @@
1
- langchain_google_genai/__init__.py,sha256=NiLfU4IyEQSlSl8hbN_fTdX-hrO5tuOflxFlEK0Sy4c,2762
2
- langchain_google_genai/_common.py,sha256=1r0VrrBSTZfGprmICZ5OV-W5SK31jKRFFCNE3vJ3jmk,136
3
- langchain_google_genai/_enums.py,sha256=q8IYAqufV-_yZ98FDnsZ3x-1w4804J_e8PrTKT0sdhY,163
4
- langchain_google_genai/_function_utils.py,sha256=uDWg2Gcuv3PtdUL14sDOVTFbq8gaaGJ360bL4IbN4AI,8705
5
- langchain_google_genai/_genai_extension.py,sha256=2Uqg7vSF0vu1J4AhAyIPzadtpM5JJwZsBXvpItO2TY4,18736
6
- langchain_google_genai/chat_models.py,sha256=edkRRStq42pxY1xBzfC2yUfN-jBsM-Vr1dx9QVg-qd4,29969
7
- langchain_google_genai/embeddings.py,sha256=QZJRd5xQkGzalAGiKeorrsnVmsyaO4NGmGuzQFDoRe0,4807
8
- langchain_google_genai/genai_aqa.py,sha256=zcC5cdFYtqLK7DGPhYGvWNeHHeU-CQKA9KhewmsA5lw,4303
9
- langchain_google_genai/google_vector_store.py,sha256=PPIk-4FmD5UUdmYA2u7VcEhGsiztvRVN59QoGLXdfoA,16139
10
- langchain_google_genai/llms.py,sha256=QPjCs0AHb-2d3GMCS3sZqzG_Yr71YbaGX_Vs8QlLTKU,13518
11
- langchain_google_genai/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
12
- langchain_google_genai-1.0.3.dist-info/LICENSE,sha256=DppmdYJVSc1jd0aio6ptnMUn5tIHrdAhQ12SclEBfBg,1072
13
- langchain_google_genai-1.0.3.dist-info/METADATA,sha256=IPH2elmUC6Rt7ZG-g5eyma97A8r48jftkeq9v8XyQMc,3818
14
- langchain_google_genai-1.0.3.dist-info/WHEEL,sha256=FMvqSimYX_P7y0a7UY-_Mc83r5zkBZsCYPm7Lr0Bsq4,88
15
- langchain_google_genai-1.0.3.dist-info/RECORD,,