-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathExternalAPIs.py
44 lines (37 loc) · 1.35 KB
/
ExternalAPIs.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
import openai
import os
from youtube_transcript_api import YouTubeTranscriptApi
class ExternalAPIs():
@staticmethod
def getCaptions(url):
# parse url to get video key
videoKey = url.split("/watch?v=")[1]
# of dictionaries obtained by the get_transcript() function
try:
srt = YouTubeTranscriptApi.get_transcript(videoKey)
except Exception as e:
return "Sorry, this video's captions are not available to us."
captions = ""
for dic in srt:
captions = captions + " " + dic.get("text", "")
return captions
def getGPT(prompt):
openai.api_key = os.environ['GPT_API_KEY']
messages = [
{"role": "system", "content": "You are a helpful assistant."},
]
while True:
messages.append(
{"role": "user", "content": prompt},
)
try:
chat_completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages
)
except Exception as e:
return "Sorry, ChatGPT is having issues."
answer = chat_completion.choices[0].message.content
print(f"ChatGPT: {answer}")
messages.append({"role": "assistant", "content": answer})
return answer