fix
This commit is contained in:
@@ -115,7 +115,7 @@ def get_default_ollama_model() -> str:
|
||||
def get_yandex_cloud_folder() -> str:
|
||||
load_env_file()
|
||||
folder = os.getenv(YANDEX_CLOUD_FOLDER_ENV_NAME, "").strip()
|
||||
if not folder:
|
||||
if not folder or folder == "":
|
||||
raise RuntimeError(
|
||||
f"Set {YANDEX_CLOUD_FOLDER_ENV_NAME} in environment or .env file."
|
||||
)
|
||||
|
||||
@@ -9,6 +9,15 @@ class YandexAIError(AIClientError):
|
||||
pass
|
||||
|
||||
|
||||
def describe_yandex_error(exc: Exception) -> str:
|
||||
response = getattr(exc, "response", None)
|
||||
status_code = getattr(response, "status_code", None)
|
||||
response_text = getattr(response, "text", None)
|
||||
if status_code and isinstance(response_text, str) and response_text.strip():
|
||||
return f"HTTP {status_code}: {response_text.strip()[:800]}"
|
||||
return str(exc)
|
||||
|
||||
|
||||
def create_async_sdk(folder_id: str) -> Any:
|
||||
try:
|
||||
from yandex_ai_studio_sdk import AsyncAIStudio
|
||||
@@ -42,6 +51,14 @@ class YandexAIClient:
|
||||
def normalize_model(self, model: str) -> str:
|
||||
normalized = model.strip()
|
||||
if normalized.startswith("gpt://"):
|
||||
model_path = normalized.removeprefix("gpt://").strip("/")
|
||||
parts = model_path.split("/")
|
||||
if (
|
||||
not normalized.startswith(f"gpt://{self.folder_id}/")
|
||||
and len(parts) == 2
|
||||
and parts[-1] in {"latest", "rc"}
|
||||
):
|
||||
return f"gpt://{self.folder_id}/{model_path}"
|
||||
return normalized
|
||||
return f"gpt://{self.folder_id}/{normalized.lstrip('/')}"
|
||||
|
||||
@@ -49,7 +66,9 @@ class YandexAIClient:
|
||||
try:
|
||||
models = await self._sdk.chat.completions.list()
|
||||
except Exception as exc:
|
||||
raise YandexAIError(f"Не удалось получить список моделей: {exc}") from exc
|
||||
raise YandexAIError(
|
||||
f"Не удалось получить список моделей: {describe_yandex_error(exc)}"
|
||||
) from exc
|
||||
|
||||
model_names = {
|
||||
str(uri)
|
||||
@@ -67,19 +86,23 @@ class YandexAIClient:
|
||||
sdk_messages = [
|
||||
{
|
||||
"role": message.get("role", "user"),
|
||||
"text": message.get("content", ""),
|
||||
"content": message.get("content", ""),
|
||||
}
|
||||
for message in messages
|
||||
]
|
||||
|
||||
try:
|
||||
completion = self._sdk.models.completions(self.normalize_model(model))
|
||||
completion = self._sdk.chat.completions(self.normalize_model(model))
|
||||
if json_mode:
|
||||
completion = completion.configure(response_format="json")
|
||||
result = await completion.run(sdk_messages, timeout=180)
|
||||
content = getattr(result[0], "text", None)
|
||||
content = getattr(result, "text", None)
|
||||
if content is None:
|
||||
content = getattr(result[0], "text", None)
|
||||
except Exception as exc:
|
||||
raise YandexAIError(f"Запрос к модели завершился ошибкой: {exc}") from exc
|
||||
raise YandexAIError(
|
||||
f"Запрос к модели завершился ошибкой: {describe_yandex_error(exc)}"
|
||||
) from exc
|
||||
|
||||
if isinstance(content, str) and content.strip():
|
||||
return content.strip()
|
||||
|
||||
Reference in New Issue
Block a user