-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathutils.py
65 lines (52 loc) · 1.72 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import logging
from typing import Dict, Optional, Type, TypeVar
from gigachat.context import (
authorization_cvar,
client_id_cvar,
operation_id_cvar,
request_id_cvar,
service_id_cvar,
session_id_cvar,
)
from gigachat.pydantic_v1 import BaseModel
_logger = logging.getLogger(__name__)
USER_AGENT = "GigaChat-python-lib"
def build_headers(access_token: Optional[str] = None, session_id: Optional[str] = None) -> Dict[str, str]:
headers = {}
if access_token:
headers["Authorization"] = f"Bearer {access_token}"
headers["User-Agent"] = USER_AGENT
authorization = authorization_cvar.get()
if session_id is None:
session_id = session_id_cvar.get()
request_id = request_id_cvar.get()
service_id = service_id_cvar.get()
operation_id = operation_id_cvar.get()
client_id = client_id_cvar.get()
if authorization:
headers["Authorization"] = authorization
if session_id:
headers["X-Session-ID"] = session_id
if request_id:
headers["X-Request-ID"] = request_id
if service_id:
headers["X-Service-ID"] = service_id
if operation_id:
headers["X-Operation-ID"] = operation_id
if client_id:
headers["X-Client-ID"] = client_id
return headers
T = TypeVar("T", bound=BaseModel)
def parse_chunk(line: str, model_class: Type[T]) -> Optional[T]:
try:
name, _, value = line.partition(": ")
if name == "data":
if value == "[DONE]":
return None
else:
return model_class.parse_raw(value)
except Exception as e:
_logger.error("Error parsing chunk from server: %s, raw value: %s", e, line)
raise e
else:
return None