Sabbah13 commited on
Commit
390fec3
1 Parent(s): 0bf6fb0

Create gigiachat_requests.py

Browse files
Files changed (1) hide show
  1. gigiachat_requests.py +57 -0
gigiachat_requests.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import base64
3
+ import json
4
+ import requests
5
+
6
+ def get_access_token():
7
+ username = st.secrets["GIGA_USERNAME"]
8
+ password = st.secrets["GIGA_SECRET"]
9
+
10
+ # Получаем строку с базовой авторизацией в формате Base64
11
+ auth_str = f'{username}:{password}'
12
+ auth_bytes = auth_str.encode('utf-8')
13
+ auth_base64 = base64.b64encode(auth_bytes).decode('utf-8')
14
+ url = os.getenv('GIGA_AUTH_URL')
15
+
16
+ headers = {
17
+ 'Authorization': f'Basic {auth_base64}', # вставляем базовую авторизацию
18
+ 'RqUID': os.getenv('GIGA_rquid'),
19
+ 'Content-Type': 'application/x-www-form-urlencoded',
20
+ 'Accept': 'application/json'
21
+ }
22
+
23
+ data = {
24
+ 'scope': os.getenv('GIGA_SCOPE')
25
+ }
26
+
27
+ response = requests.post(url, headers=headers, data=data, verify=False)
28
+ access_token = response.json()['access_token']
29
+ print('Got access token')
30
+ return access_token
31
+
32
+ def get_completion_from_gigachat(prompt, max_tokens, access_token):
33
+ url_completion = os.getenv('GIGA_COMPLETION_URL')
34
+
35
+ data_copm = json.dumps({
36
+ "model": os.getenv('GIGA_MODEL'),
37
+ "messages": [
38
+ {
39
+ "role": "user",
40
+ "content": prompt
41
+ }
42
+ ],
43
+ "stream": False,
44
+ "max_tokens": max_tokens,
45
+ })
46
+
47
+ headers_comp = {
48
+ 'Content-Type': 'application/json',
49
+ 'Accept': 'application/json',
50
+ 'Authorization': 'Bearer ' + access_token
51
+ }
52
+
53
+ response = requests.post(url_completion, headers=headers_comp, data=data_copm, verify=False)
54
+ response_data = response.json()
55
+ answer_from_llm = response_data['choices'][0]['message']['content']
56
+
57
+ return answer_from_llm