langchain-b12 0.1.9__py3-none-any.whl → 0.1.11__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.
langchain_b12/genai/genai.py
CHANGED
|
@@ -35,14 +35,7 @@ from langchain_core.tools import BaseTool
|
|
|
35
35
|
from langchain_core.utils.function_calling import (
|
|
36
36
|
convert_to_openai_tool,
|
|
37
37
|
)
|
|
38
|
-
from pydantic import BaseModel, ConfigDict, Field
|
|
39
|
-
from tenacity import (
|
|
40
|
-
retry,
|
|
41
|
-
retry_if_exception_type,
|
|
42
|
-
stop_after_attempt,
|
|
43
|
-
stop_never,
|
|
44
|
-
wait_exponential_jitter,
|
|
45
|
-
)
|
|
38
|
+
from pydantic import BaseModel, ConfigDict, Field, field_validator, model_validator
|
|
46
39
|
|
|
47
40
|
from langchain_b12.genai.genai_utils import (
|
|
48
41
|
convert_messages_to_contents,
|
|
@@ -84,7 +77,9 @@ class ChatGenAI(BaseChatModel):
|
|
|
84
77
|
seed: int | None = None
|
|
85
78
|
"""Random seed for the generation."""
|
|
86
79
|
max_retries: int | None = Field(default=3)
|
|
87
|
-
"""Maximum number of retries
|
|
80
|
+
"""Maximum number of retries. Prefer `http_retry_options`, but this is kept for compatibility."""
|
|
81
|
+
http_retry_options: types.HttpRetryOptions | None = Field(default=None)
|
|
82
|
+
"""HTTP retry options for API requests. If not set, max_retries will be used to create default options."""
|
|
88
83
|
safety_settings: list[types.SafetySetting] | None = None
|
|
89
84
|
"""The default safety settings to use for all generations.
|
|
90
85
|
|
|
@@ -107,6 +102,13 @@ class ChatGenAI(BaseChatModel):
|
|
|
107
102
|
arbitrary_types_allowed=True,
|
|
108
103
|
)
|
|
109
104
|
|
|
105
|
+
@model_validator(mode="after")
|
|
106
|
+
def _setup_retry_options(self) -> "ChatGenAI":
|
|
107
|
+
"""Convert max_retries to http_retry_options if not explicitly set."""
|
|
108
|
+
if self.http_retry_options is None and self.max_retries is not None:
|
|
109
|
+
self.http_retry_options = types.HttpRetryOptions(attempts=self.max_retries)
|
|
110
|
+
return self
|
|
111
|
+
|
|
110
112
|
@property
|
|
111
113
|
def _llm_type(self) -> str:
|
|
112
114
|
return "vertexai"
|
|
@@ -183,29 +185,10 @@ class ChatGenAI(BaseChatModel):
|
|
|
183
185
|
run_manager: CallbackManagerForLLMRun | None = None,
|
|
184
186
|
**kwargs: Any,
|
|
185
187
|
) -> ChatResult:
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
stop=stop_after_attempt(self.max_retries + 1)
|
|
189
|
-
if self.max_retries is not None
|
|
190
|
-
else stop_never,
|
|
191
|
-
wait=wait_exponential_jitter(initial=1, max=60),
|
|
192
|
-
retry=retry_if_exception_type(Exception),
|
|
193
|
-
before_sleep=lambda retry_state: logger.warning(
|
|
194
|
-
"ChatGenAI._generate failed (attempt %d/%s). "
|
|
195
|
-
"Retrying in %.2fs... Error: %s",
|
|
196
|
-
retry_state.attempt_number,
|
|
197
|
-
self.max_retries + 1 if self.max_retries is not None else "∞",
|
|
198
|
-
retry_state.next_action.sleep,
|
|
199
|
-
retry_state.outcome.exception(),
|
|
200
|
-
),
|
|
188
|
+
stream_iter = self._stream(
|
|
189
|
+
messages, stop=stop, run_manager=run_manager, **kwargs
|
|
201
190
|
)
|
|
202
|
-
|
|
203
|
-
stream_iter = self._stream(
|
|
204
|
-
messages, stop=stop, run_manager=run_manager, **kwargs
|
|
205
|
-
)
|
|
206
|
-
return generate_from_stream(stream_iter)
|
|
207
|
-
|
|
208
|
-
return _generate_with_retry()
|
|
191
|
+
return generate_from_stream(stream_iter)
|
|
209
192
|
|
|
210
193
|
async def _agenerate(
|
|
211
194
|
self,
|
|
@@ -214,29 +197,10 @@ class ChatGenAI(BaseChatModel):
|
|
|
214
197
|
run_manager: AsyncCallbackManagerForLLMRun | None = None,
|
|
215
198
|
**kwargs: Any,
|
|
216
199
|
) -> ChatResult:
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
stop=stop_after_attempt(self.max_retries + 1)
|
|
220
|
-
if self.max_retries is not None
|
|
221
|
-
else stop_never,
|
|
222
|
-
wait=wait_exponential_jitter(initial=1, max=60),
|
|
223
|
-
retry=retry_if_exception_type(Exception),
|
|
224
|
-
before_sleep=lambda retry_state: logger.warning(
|
|
225
|
-
"ChatGenAI._agenerate failed (attempt %d/%s). "
|
|
226
|
-
"Retrying in %.2fs... Error: %s",
|
|
227
|
-
retry_state.attempt_number,
|
|
228
|
-
self.max_retries + 1 if self.max_retries is not None else "∞",
|
|
229
|
-
retry_state.next_action.sleep,
|
|
230
|
-
retry_state.outcome.exception(),
|
|
231
|
-
),
|
|
200
|
+
stream_iter = self._astream(
|
|
201
|
+
messages, stop=stop, run_manager=run_manager, **kwargs
|
|
232
202
|
)
|
|
233
|
-
|
|
234
|
-
stream_iter = self._astream(
|
|
235
|
-
messages, stop=stop, run_manager=run_manager, **kwargs
|
|
236
|
-
)
|
|
237
|
-
return await agenerate_from_stream(stream_iter)
|
|
238
|
-
|
|
239
|
-
return await _agenerate_with_retry()
|
|
203
|
+
return await agenerate_from_stream(stream_iter)
|
|
240
204
|
|
|
241
205
|
def _stream(
|
|
242
206
|
self,
|
|
@@ -246,10 +210,16 @@ class ChatGenAI(BaseChatModel):
|
|
|
246
210
|
**kwargs: Any,
|
|
247
211
|
) -> Iterator[ChatGenerationChunk]:
|
|
248
212
|
system_message, contents = self._prepare_request(messages=messages)
|
|
213
|
+
http_options = (
|
|
214
|
+
types.HttpOptions(retry_options=self.http_retry_options)
|
|
215
|
+
if self.http_retry_options
|
|
216
|
+
else None
|
|
217
|
+
)
|
|
249
218
|
response_iter = self.client.models.generate_content_stream(
|
|
250
219
|
model=self.model_name,
|
|
251
220
|
contents=contents,
|
|
252
221
|
config=types.GenerateContentConfig(
|
|
222
|
+
http_options=http_options,
|
|
253
223
|
system_instruction=system_message,
|
|
254
224
|
temperature=self.temperature,
|
|
255
225
|
top_k=self.top_k,
|
|
@@ -282,10 +252,16 @@ class ChatGenAI(BaseChatModel):
|
|
|
282
252
|
**kwargs: Any,
|
|
283
253
|
) -> AsyncIterator[ChatGenerationChunk]:
|
|
284
254
|
system_message, contents = self._prepare_request(messages=messages)
|
|
255
|
+
http_options = (
|
|
256
|
+
types.HttpOptions(retry_options=self.http_retry_options)
|
|
257
|
+
if self.http_retry_options
|
|
258
|
+
else None
|
|
259
|
+
)
|
|
285
260
|
response_iter = self.client.aio.models.generate_content_stream(
|
|
286
261
|
model=self.model_name,
|
|
287
262
|
contents=contents,
|
|
288
263
|
config=types.GenerateContentConfig(
|
|
264
|
+
http_options=http_options,
|
|
289
265
|
system_instruction=system_message,
|
|
290
266
|
temperature=self.temperature,
|
|
291
267
|
top_k=self.top_k,
|
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: langchain-b12
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.11
|
|
4
4
|
Summary: A reusable collection of tools and implementations for Langchain
|
|
5
5
|
Author-email: Vincent Min <vincent.min@b12-consulting.com>
|
|
6
6
|
Requires-Python: >=3.11
|
|
7
7
|
Requires-Dist: langchain-core>=0.3.60
|
|
8
|
-
Requires-Dist: tenacity>=9.1.2
|
|
9
8
|
Description-Content-Type: text/markdown
|
|
10
9
|
|
|
11
10
|
# Langchain B12
|
|
@@ -2,8 +2,8 @@ langchain_b12/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
|
2
2
|
langchain_b12/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
3
3
|
langchain_b12/citations/citations.py,sha256=ZQvYayjQXIUaRosJ0qwL3Nc7kC8sBzmaIkE-BOslaVI,12261
|
|
4
4
|
langchain_b12/genai/embeddings.py,sha256=h0Z-5PltDW9q79AjSrLemsz-_QKMB-043XXDvYSRQds,3483
|
|
5
|
-
langchain_b12/genai/genai.py,sha256=
|
|
5
|
+
langchain_b12/genai/genai.py,sha256=u-QAH_4VauBj99dWuYBaxAMT3bNbqMdxM-rYgRKctLw,18074
|
|
6
6
|
langchain_b12/genai/genai_utils.py,sha256=tA6UiJURK25-11vtaX4768UV47jDCYwVKIIWydD4Egw,10736
|
|
7
|
-
langchain_b12-0.1.
|
|
8
|
-
langchain_b12-0.1.
|
|
9
|
-
langchain_b12-0.1.
|
|
7
|
+
langchain_b12-0.1.11.dist-info/METADATA,sha256=w4uaxeVl7hg1h2Zj3J9ZOukgUAXHeJcQA0rrtBnmdyg,1205
|
|
8
|
+
langchain_b12-0.1.11.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
|
|
9
|
+
langchain_b12-0.1.11.dist-info/RECORD,,
|
|
File without changes
|