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

Added sleep between requests to prevent errors associated with
simultaneous requests.
This commit is contained in:
Dmitry Tyumentsev 2024-01-25 09:07:19 -08:00 committed by GitHub
parent e510cfaa23
commit e86e66bad7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 19 additions and 8 deletions

View File

@ -139,7 +139,8 @@ def _make_request(
) )
except ImportError as e: except ImportError as e:
raise ImportError( 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 ) from e
if not messages: if not messages:
raise ValueError("You should provide at least one message to start the chat!") 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: except ImportError as e:
raise ImportError( 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 ) from e
if not messages: if not messages:
raise ValueError("You should provide at least one message to start the chat!") 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]: def _create_retry_decorator(llm: ChatYandexGPT) -> Callable[[Any], Any]:
from grpc import RpcError from grpc import RpcError
min_seconds = 1 min_seconds = llm.sleep_interval
max_seconds = 60 max_seconds = 60
return retry( return retry(
reraise=True, reraise=True,

View File

@ -2,6 +2,7 @@
from __future__ import annotations from __future__ import annotations
import logging import logging
import time
from typing import Any, Callable, Dict, List from typing import Any, Callable, Dict, List
from langchain_core.embeddings import Embeddings from langchain_core.embeddings import Embeddings
@ -59,6 +60,8 @@ class YandexGPTEmbeddings(BaseModel, Embeddings):
"""The url of the API.""" """The url of the API."""
max_retries: int = 6 max_retries: int = 6
"""Maximum number of retries to make when generating.""" """Maximum number of retries to make when generating."""
sleep_interval: float = 0.0
"""Delay between API requests"""
@root_validator() @root_validator()
def validate_environment(cls, values: Dict) -> Dict: def validate_environment(cls, values: Dict) -> Dict:
@ -154,7 +157,8 @@ def _make_request(self: YandexGPTEmbeddings, texts: List[str]):
) )
except ImportError as e: except ImportError as e:
raise ImportError( 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 ) from e
result = [] result = []
channel_credentials = grpc.ssl_channel_credentials() 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) request = TextEmbeddingRequest(model_uri=self.model_uri, text=text)
stub = EmbeddingsServiceStub(channel) stub = EmbeddingsServiceStub(channel)
res = stub.TextEmbedding(request, metadata=self._grpc_metadata) res = stub.TextEmbedding(request, metadata=self._grpc_metadata)
result.append(res.embedding) result.append(list(res.embedding))
time.sleep(self.sleep_interval)
return result return result

View File

@ -52,6 +52,8 @@ class _BaseYandexGPT(Serializable):
"""The url of the API.""" """The url of the API."""
max_retries: int = 6 max_retries: int = 6
"""Maximum number of retries to make when generating.""" """Maximum number of retries to make when generating."""
sleep_interval: float = 1.0
"""Delay between API requests"""
@property @property
def _llm_type(self) -> str: def _llm_type(self) -> str:
@ -195,7 +197,8 @@ def _make_request(
) )
except ImportError as e: except ImportError as e:
raise ImportError( 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 ) from e
channel_credentials = grpc.ssl_channel_credentials() channel_credentials = grpc.ssl_channel_credentials()
channel = grpc.secure_channel(self.url, 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: except ImportError as e:
raise ImportError( 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 ) from e
operation_api_url = "operation.api.cloud.yandex.net:443" operation_api_url = "operation.api.cloud.yandex.net:443"
channel_credentials = grpc.ssl_channel_credentials() 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]: def _create_retry_decorator(llm: YandexGPT) -> Callable[[Any], Any]:
from grpc import RpcError from grpc import RpcError
min_seconds = 1 min_seconds = llm.sleep_interval
max_seconds = 60 max_seconds = 60
return retry( return retry(
reraise=True, reraise=True,