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

Added sleep between requests to prevent errors associated with
simultaneous requests.
pull/16387/head
Dmitry Tyumentsev 8 months ago committed by GitHub
parent e510cfaa23
commit e86e66bad7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -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,

@ -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

@ -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,

Loading…
Cancel
Save