import os import base64 import json import requests def get_access_token(): username = st.secrets["GIGA_USERNAME"] password = st.secrets["GIGA_SECRET"] # Получаем строку с базовой авторизацией в формате Base64 auth_str = f'{username}:{password}' auth_bytes = auth_str.encode('utf-8') auth_base64 = base64.b64encode(auth_bytes).decode('utf-8') url = os.getenv('GIGA_AUTH_URL') headers = { 'Authorization': f'Basic {auth_base64}', # вставляем базовую авторизацию 'RqUID': os.getenv('GIGA_rquid'), 'Content-Type': 'application/x-www-form-urlencoded', 'Accept': 'application/json' } data = { 'scope': os.getenv('GIGA_SCOPE') } response = requests.post(url, headers=headers, data=data, verify=False) access_token = response.json()['access_token'] print('Got access token') return access_token def get_completion_from_gigachat(prompt, max_tokens, access_token): url_completion = os.getenv('GIGA_COMPLETION_URL') data_copm = json.dumps({ "model": os.getenv('GIGA_MODEL'), "messages": [ { "role": "user", "content": prompt } ], "stream": False, "max_tokens": max_tokens, }) headers_comp = { 'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization': 'Bearer ' + access_token } response = requests.post(url_completion, headers=headers_comp, data=data_copm, verify=False) response_data = response.json() answer_from_llm = response_data['choices'][0]['message']['content'] return answer_from_llm