|
import os |
|
import base64 |
|
import json |
|
import requests |
|
import streamlit as st |
|
|
|
def get_access_token(): |
|
username = st.secrets["GIGA_USERNAME"] |
|
password = st.secrets["GIGA_SECRET"] |
|
|
|
|
|
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_number_of_tokens(prompt, access_token): |
|
url_completion = os.getenv('GIGA_TOKENS_URL') |
|
|
|
data_copm = json.dumps({ |
|
"model": os.getenv('GIGA_MODEL'), |
|
"input": [ |
|
prompt |
|
], |
|
}) |
|
|
|
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() |
|
|
|
return response_data[0]['tokens'] |
|
|
|
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 |