File size: 1,776 Bytes
390fec3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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