File size: 4,263 Bytes
fa7c624 2fdbea5 88bd71c 7670c72 eb6ebad fa7c624 d956ecd fa7c624 92ce7a1 fa7c624 dd490db fa7c624 41e2dda fa7c624 ecac535 35cbe60 ecac535 35cbe60 ecac535 d6a6e5c 099f21e d6a6e5c 2d4458e d6a6e5c a84885f 1686056 a84885f 1686056 35cbe60 |
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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
import os
import streamlit as st
import requests
import json
import math
def get_completion_from_openai(prompt,model, max_tokens = None):
url = os.getenv('OPENAI_COMPLETION_URL')
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer " + st.secrets["OPENAI_TOKEN"],
}
response = requests.post(url,
json={
"model": model,
"max_tokens": max_tokens,
"messages": [
{
"role": "user",
"content": prompt
}
]
},
headers=headers,
stream=False,
)
try:
return response.json()['choices'][0]['message']['content']
except:
print(response.json())
return "Произошла ошибка"
def process_transcribation_with_assistant(prompt, transcript):
baseUrl = os.getenv('OPENAI_BASE_URL')
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer " + st.secrets["OPENAI_TOKEN"],
"OpenAI-Beta": "assistants=v2",
}
number_of_runs = math.ceil(len(transcript) / (4 * 4096))
output_text = ''
thread_response = requests.post(baseUrl + '/threads', json={}, headers=headers)
thread_id = thread_response.json()['id']
message_response = requests.post(baseUrl + '/threads/' + thread_id + '/messages',
json={"role" : "user", "content": prompt + transcript},
headers=headers)
run_response = requests.post(baseUrl + '/threads/' + thread_id + '/runs',
json={
"assistant_id": st.secrets["OPENAI_ASSISTANT_ID"],
"stream": True
},
headers=headers,
stream=True)
st.write("Результат обработки:")
text_container = st.empty()
output_text = ''
event_name = ""
for line in run_response.iter_lines(decode_unicode=True):
if line:
if line.startswith("event:"):
event_name = line.split(":")[1].strip()
if event_name == 'done':
break
elif line.startswith("data:") and event_name == 'thread.message.delta':
event_data = json.loads(line.split(":", 1)[1].strip())
output_text += event_data['delta']['content'][0]['text']['value']
text_container.text(output_text)
for i in range(number_of_runs - 1):
message_response = requests.post(baseUrl + '/threads/' + thread_id + '/messages',
json={"role" : "user", "content": "Продолжай работать на текущей задачей"},
headers=headers)
run_response = requests.post(baseUrl + '/threads/' + thread_id + '/runs',
json={
"assistant_id": st.secrets["OPENAI_ASSISTANT_ID"],
"stream": True
},
headers=headers,
stream=True)
event_name = ""
response = ''
for line in run_response.iter_lines(decode_unicode=True):
if line:
if line.startswith("event:"):
event_name = line.split(":")[1].strip()
if event_name == 'done':
break
elif line.startswith("data:") and event_name == 'thread.message.delta':
event_data = json.loads(line.split(":", 1)[1].strip())
output_text += event_data['delta']['content'][0]['text']['value']
text_container.text(output_text)
return output_text
|