Sabbah13 commited on
Commit
493907a
·
verified ·
1 Parent(s): a2fdd27

Create gigiachat_requests.py

Browse files
Files changed (1) hide show
  1. gigiachat_requests.py +118 -0
gigiachat_requests.py ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import base64
3
+ import json
4
+ import requests
5
+
6
+ def get_access_token():
7
+ username = os.environ.get("GIGA_USERNAME")
8
+ password = os.environ.get("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_number_of_tokens(prompt, access_token):
33
+ url_completion = os.getenv('GIGA_TOKENS_URL')
34
+
35
+ data_copm = json.dumps({
36
+ "model": os.getenv('GIGA_MODEL'),
37
+ "input": [
38
+ prompt
39
+ ],
40
+ })
41
+
42
+ headers_comp = {
43
+ 'Content-Type': 'application/json',
44
+ 'Accept': 'application/json',
45
+ 'Authorization': 'Bearer ' + access_token
46
+ }
47
+
48
+ response = requests.post(url_completion, headers=headers_comp, data=data_copm, verify=False)
49
+ response_data = response.json()
50
+
51
+ return response_data[0]['tokens']
52
+
53
+ def get_completion_from_gigachat(prompt, max_tokens, access_token):
54
+ url_completion = os.getenv('GIGA_COMPLETION_URL')
55
+
56
+ data_copm = json.dumps({
57
+ "model": os.getenv('GIGA_MODEL'),
58
+ "messages": [
59
+ {
60
+ "role": "user",
61
+ "content": prompt
62
+ }
63
+ ],
64
+ "stream": False,
65
+ "max_tokens": max_tokens,
66
+ })
67
+
68
+ headers_comp = {
69
+ 'Content-Type': 'application/json',
70
+ 'Accept': 'application/json',
71
+ 'Authorization': 'Bearer ' + access_token
72
+ }
73
+
74
+ response = requests.post(url_completion, headers=headers_comp, data=data_copm, verify=False)
75
+ response_data = response.json()
76
+ answer_from_llm = response_data['choices'][0]['message']['content']
77
+
78
+ return answer_from_llm
79
+
80
+ def process_transcribation_with_gigachat(prompt, transcript, max_tokens, access_token):
81
+ url_completion = os.getenv('GIGA_COMPLETION_URL')
82
+
83
+ data_copm = json.dumps({
84
+ "model": 'GigaChat-Plus',
85
+ "max_tokens": max_tokens,
86
+ "messages": [
87
+ {
88
+ "role": "user",
89
+ "content": prompt + transcript
90
+ }
91
+ ],
92
+ "stream": True,
93
+ })
94
+
95
+ headers_comp = {
96
+ 'Content-Type': 'application/json',
97
+ 'Accept': 'application/json',
98
+ 'Authorization': 'Bearer ' + access_token
99
+ }
100
+
101
+ output_text = ''
102
+ response = requests.post(url_completion, headers=headers_comp, data=data_copm, verify=False, stream=True)
103
+ for line in response.iter_lines():
104
+ if line:
105
+ decoded_line = line.decode('utf-8')
106
+ if decoded_line.startswith('data:'):
107
+ decoded_line = decoded_line[len('data:'):].strip()
108
+ try:
109
+ data = json.loads(decoded_line)
110
+ if "choices" in data:
111
+ for choice in data["choices"]:
112
+ if "delta" in choice and "content" in choice["delta"]:
113
+ output_text += choice["delta"]["content"]
114
+ except json.JSONDecodeError:
115
+ continue
116
+
117
+
118
+ return output_text