37 lines
1.1 KiB
Python
37 lines
1.1 KiB
Python
from time import time
|
|
import openai
|
|
openai.api_key = "sk-proj-PdTVVVvYzcd6vs2qcRxpT3BlbkFJtq78XfSrzwEK2fqyOVHE"
|
|
import requests
|
|
|
|
|
|
class AIHandlerStream(object):
|
|
def __init__(self):
|
|
self.ai = openai.OpenAI(api_key="sk-proj-GaouEG2QuAcfAr1an2uBT3BlbkFJaIh0XVFXWrYQpJazlbeO")
|
|
|
|
def __call__(self, text):
|
|
out = ""
|
|
for chunk in self.ai.chat.completions.create(
|
|
model="gpt-3.5-turbo",
|
|
messages=[
|
|
{"role": "system", "content": "You are PIA. You talk with short sentences. And help people."},
|
|
{"role": "user", "content": text}
|
|
], stream=True
|
|
):
|
|
delta = chunk.choices[0].delta.content
|
|
if delta is None:
|
|
continue
|
|
out += delta
|
|
if len(out) > 0 and out[-1] in ['.', '!', ',', '?']:
|
|
yield out
|
|
out = ""
|
|
if len(out) > 0:
|
|
yield out
|
|
|
|
|
|
if __name__ == "__main__":
|
|
aihandler = AIHandlerStream()
|
|
t1 = time()
|
|
for text in aihandler("Hello, how are you, what is your name?"):
|
|
print(time() - t1)
|
|
print(text)
|