summaryrefslogtreecommitdiffstats
path: root/g4f/Provider/GigaChat.py
diff options
context:
space:
mode:
authorH Lohaus <hlohaus@users.noreply.github.com>2024-03-15 13:44:15 +0100
committerGitHub <noreply@github.com>2024-03-15 13:44:15 +0100
commit7df7971d202af6e88010f17e83740d65386699ca (patch)
treeda8ffc82319214511fb34e404b5b4851c1fef02d /g4f/Provider/GigaChat.py
parentError checking for Vercel (#1704) (diff)
parentGigaChat models add (diff)
downloadgpt4free-7df7971d202af6e88010f17e83740d65386699ca.tar
gpt4free-7df7971d202af6e88010f17e83740d65386699ca.tar.gz
gpt4free-7df7971d202af6e88010f17e83740d65386699ca.tar.bz2
gpt4free-7df7971d202af6e88010f17e83740d65386699ca.tar.lz
gpt4free-7df7971d202af6e88010f17e83740d65386699ca.tar.xz
gpt4free-7df7971d202af6e88010f17e83740d65386699ca.tar.zst
gpt4free-7df7971d202af6e88010f17e83740d65386699ca.zip
Diffstat (limited to '')
-rw-r--r--g4f/Provider/GigaChat.py97
1 files changed, 97 insertions, 0 deletions
diff --git a/g4f/Provider/GigaChat.py b/g4f/Provider/GigaChat.py
new file mode 100644
index 00000000..699353b1
--- /dev/null
+++ b/g4f/Provider/GigaChat.py
@@ -0,0 +1,97 @@
+from __future__ import annotations
+
+import base64
+import os
+import ssl
+import time
+import uuid
+
+import json
+from aiohttp import ClientSession, BaseConnector, TCPConnector
+from g4f.requests import raise_for_status
+
+from ..typing import AsyncResult, Messages, ImageType
+from .base_provider import AsyncGeneratorProvider, ProviderModelMixin
+from ..image import to_bytes, is_accepted_format
+from ..errors import MissingAuthError
+from .helper import get_connector
+
+access_token = ''
+token_expires_at = 0
+
+ssl_ctx = ssl.create_default_context(
+ cafile=os.path.dirname(__file__) + '/gigachat_crt/russian_trusted_root_ca_pem.crt')
+
+
+class GigaChat(AsyncGeneratorProvider, ProviderModelMixin):
+ url = "https://developers.sber.ru/gigachat"
+ working = True
+ supports_message_history = True
+ supports_system_message = True
+ supports_stream = True
+
+ needs_auth = True
+ default_model = "GigaChat:latest"
+ models = ["GigaChat:latest", "GigaChat-Plus", "GigaChat-Pro"]
+
+ @classmethod
+ async def create_async_generator(
+ cls,
+ model: str,
+ messages: Messages,
+ stream: bool = True,
+ proxy: str = None,
+ api_key: str = None,
+ scope: str = "GIGACHAT_API_PERS",
+ update_interval: float = 0,
+ **kwargs
+ ) -> AsyncResult:
+ global access_token, token_expires_at
+ model = cls.get_model(model)
+
+ if not api_key:
+ raise MissingAuthError('Missing "api_key"')
+
+ connector = TCPConnector(ssl_context=ssl_ctx)
+
+ async with ClientSession(connector=get_connector(connector, proxy)) as session:
+ if token_expires_at - int(time.time() * 1000) < 60000:
+ async with session.post(url="https://ngw.devices.sberbank.ru:9443/api/v2/oauth",
+ headers={"Authorization": f"Bearer {api_key}",
+ "RqUID": str(uuid.uuid4()),
+ "Content-Type": "application/x-www-form-urlencoded"},
+ data={"scope": scope}) as response:
+ await raise_for_status(response)
+ data = await response.json()
+ access_token = data['access_token']
+ token_expires_at = data['expires_at']
+
+ async with session.post(url="https://gigachat.devices.sberbank.ru/api/v1/chat/completions",
+ headers={"Authorization": f"Bearer {access_token}"},
+ json={
+ "model": model,
+ "messages": messages,
+ "stream": stream,
+ "update_interval": update_interval,
+ **kwargs
+ }) as response:
+ await raise_for_status(response)
+
+ async for line in response.content:
+ if not stream:
+ yield json.loads(line.decode("utf-8"))['choices'][0]['message']['content']
+ return
+
+ if line and line.startswith(b"data:"):
+ line = line[6:-1] # remove "data: " prefix and "\n" suffix
+ if line.strip() == b"[DONE]":
+ return
+ else:
+ msg = json.loads(line.decode("utf-8"))['choices'][0]
+ content = msg['delta']['content']
+
+ if content:
+ yield content
+
+ if 'finish_reason' in msg:
+ return