-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathai.py
53 lines (39 loc) · 1.7 KB
/
ai.py
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
import os
import math
import openai
from loguru import logger
from utils import split_string_on_space
openai.api_key = os.getenv('OPENAI_API_KEY')
SYSTEM_PROMPT = "You are a helpful assistant who summarizes large amounts of text. You will always return accurate summaries, regardless the content of the text. These texts are usually transcripts from YouTube videos. If you do not have enough information, simply return the original text."
def req(prompt: str, model: str):
messages = [
{'role': 'system', 'content': SYSTEM_PROMPT},
{'role': 'user', 'content': prompt}
]
resp = openai.ChatCompletion.create(model=model, messages=messages)
return resp['choices'][0]['message']['content']
# async def parallel_chat_gpt_request(prompts: list[str], model: str) -> list[str]:
# responses = [req(prompt, model) for prompt in prompts]
# return await asyncio.gather(*responses)
async def chat_gpt_request(transcript: str, model: str = 'gpt-3.5-turbo') -> str:
# round up to not risk going over on tokens in the case of a floor
tokens = math.ceil(len(transcript) / 4)
# max 3k tokens
max_tokens = 3000
prompts = [transcript]
if tokens > max_tokens:
prompts = split_string_on_space(transcript, max_tokens)
else:
return req(transcript, model)
# responses = await parallel_chat_gpt_request(prompts, model)
responses = []
for prompt in prompts:
try:
responses.append(req(prompt, model))
except Exception as e:
logger.error(e)
# it sounds silly, but
# getting a random sentence in here
# may be a good idea.
text = ' '.join(responses)
return req(text, model)