Skip to content

The agent engineering platform.

community[patch]: YandexGPT models - add sleep_interval (#16566)

Dmitry Tyumentseve86e66bad727
diff --git a/libs/community/langchain_community/chat_models/yandex.py b/libs/community/langchain_community/chat_models/yandex.py
index 42e61a91f5..61de024089 100644
--- a/libs/community/langchain_community/chat_models/yandex.py
+++ b/libs/community/langchain_community/chat_models/yandex.py
@@ -139,7 +139,8 @@ def _make_request(
)
except ImportError as e:
raise ImportError(
- "Please install YandexCloud SDK" " with `pip install yandexcloud`."
+ "Please install YandexCloud SDK with `pip install yandexcloud` \
+ or upgrade it to recent version."
) from e
if not messages:
raise ValueError("You should provide at least one message to start the chat!")
@@ -182,7 +183,8 @@ async def _amake_request(self: ChatYandexGPT, messages: List[BaseMessage]) -> st
)
except ImportError as e:
raise ImportError(
- "Please install YandexCloud SDK" " with `pip install yandexcloud`."
+ "Please install YandexCloud SDK with `pip install yandexcloud` \
+ or upgrade it to recent version."
) from e
if not messages:
raise ValueError("You should provide at least one message to start the chat!")
@@ -219,7 +221,7 @@ async def _amake_request(self: ChatYandexGPT, messages: List[BaseMessage]) -> st
def _create_retry_decorator(llm: ChatYandexGPT) -> Callable[[Any], Any]:
from grpc import RpcError
- min_seconds = 1
+ min_seconds = llm.sleep_interval
max_seconds = 60
return retry(
reraise=True,
diff --git a/libs/community/langchain_community/embeddings/yandex.py b/libs/community/langchain_community/embeddings/yandex.py
index 4022eb1199..d3e6ed69e4 100644
--- a/libs/community/langchain_community/embeddings/yandex.py
+++ b/libs/community/langchain_community/embeddings/yandex.py
@@ -2,6 +2,7 @@
from __future__ import annotations
import logging
+import time
from typing import Any, Callable, Dict, List
from langchain_core.embeddings import Embeddings
@@ -59,6 +60,8 @@ class YandexGPTEmbeddings(BaseModel, Embeddings):
"""The url of the API."""
max_retries: int = 6
"""Maximum number of retries to make when generating."""
+ sleep_interval: float = 0.0
+ """Delay between API requests"""
@root_validator()
def validate_environment(cls, values: Dict) -> Dict:
@@ -154,7 +157,8 @@ def _make_request(self: YandexGPTEmbeddings, texts: List[str]):
)
except ImportError as e:
raise ImportError(
- "Please install YandexCloud SDK" " with `pip install yandexcloud`."
+ "Please install YandexCloud SDK with `pip install yandexcloud` \
+ or upgrade it to recent version."
) from e
result = []
channel_credentials = grpc.ssl_channel_credentials()
@@ -164,6 +168,7 @@ def _make_request(self: YandexGPTEmbeddings, texts: List[str]):
request = TextEmbeddingRequest(model_uri=self.model_uri, text=text)
stub = EmbeddingsServiceStub(channel)
res = stub.TextEmbedding(request, metadata=self._grpc_metadata)
- result.append(res.embedding)
+ result.append(list(res.embedding))
+ time.sleep(self.sleep_interval)
return result
diff --git a/libs/community/langchain_community/llms/yandex.py b/libs/community/langchain_community/llms/yandex.py
index c96570357c..f8e0537331 100644
--- a/libs/community/langchain_community/llms/yandex.py
+++ b/libs/community/langchain_community/llms/yandex.py
@@ -52,6 +52,8 @@ class _BaseYandexGPT(Serializable):
"""The url of the API."""
max_retries: int = 6
"""Maximum number of retries to make when generating."""
+ sleep_interval: float = 1.0
+ """Delay between API requests"""
@property
def _llm_type(self) -> str:
@@ -195,7 +197,8 @@ def _make_request(
)
except ImportError as e:
raise ImportError(
- "Please install YandexCloud SDK" " with `pip install yandexcloud`."
+ "Please install YandexCloud SDK with `pip install yandexcloud` \
+ or upgrade it to recent version."
) from e
channel_credentials = grpc.ssl_channel_credentials()
channel = grpc.secure_channel(self.url, channel_credentials)
@@ -235,7 +238,8 @@ async def _amake_request(self: YandexGPT, prompt: str) -> str:
)
except ImportError as e:
raise ImportError(
- "Please install YandexCloud SDK" " with `pip install yandexcloud`."
+ "Please install YandexCloud SDK with `pip install yandexcloud` \
+ or upgrade it to recent version."
) from e
operation_api_url = "operation.api.cloud.yandex.net:443"
channel_credentials = grpc.ssl_channel_credentials()
@@ -269,7 +273,7 @@ async def _amake_request(self: YandexGPT, prompt: str) -> str:
def _create_retry_decorator(llm: YandexGPT) -> Callable[[Any], Any]:
from grpc import RpcError
- min_seconds = 1
+ min_seconds = llm.sleep_interval
max_seconds = 60
return retry(
reraise=True,