35 lines
962 B
Python
35 lines
962 B
Python
from time import time
|
|
import openai
|
|
openai.api_key = "sk-proj-PdTVVVvYzcd6vs2qcRxpT3BlbkFJtq78XfSrzwEK2fqyOVHE"
|
|
import requests
|
|
|
|
|
|
class AIHandlerNewStream(object):
|
|
def __init__(self):
|
|
pass
|
|
|
|
def __call__(self, text):
|
|
url = f"https://ai.travely24.com/generate/?prompt={text}";
|
|
response = requests.get(
|
|
url,
|
|
stream=True,
|
|
headers={"accept": "application/json"},
|
|
)
|
|
out = ""
|
|
for delta in response.iter_content(chunk_size=1024):
|
|
if delta:
|
|
out += delta.decode("utf-8")
|
|
if len(out) > 0 and out.strip()[-1] in ['.', '!', ',', '?']:
|
|
yield out
|
|
out = ""
|
|
if len(out) > 0:
|
|
yield out
|
|
|
|
|
|
if __name__ == "__main__":
|
|
aihandler = AIHandlerNewStream()
|
|
t1 = time()
|
|
for text in aihandler("Hello, how are you, what is your name?"):
|
|
print(time() - t1)
|
|
print(text)
|