Streaming Update (NEEDS OPTIMIZATIONS AND FAILSAFES!!!!!!!!!!!!!!!!!!!!!!)
This commit is contained in:
77
main.py
77
main.py
@@ -13,6 +13,12 @@ from openai import OpenAI
|
||||
# For streaming
|
||||
from typing_extensions import override
|
||||
|
||||
import threading
|
||||
|
||||
# Output buffer and thread lock
|
||||
output_buffer = []
|
||||
output_lock = threading.Lock()
|
||||
|
||||
# To get the env var
|
||||
from dotenv import load_dotenv
|
||||
import os
|
||||
@@ -21,36 +27,6 @@ load_dotenv()
|
||||
|
||||
### OpenAI Config
|
||||
|
||||
# This is copy and pasted straight up from the quickstart guide:
|
||||
class EventHandler(AssistantEventHandler):
|
||||
@override
|
||||
def on_text_created(self, text) -> None:
|
||||
print(f"\nassistant > ", end="", flush=True)
|
||||
|
||||
|
||||
@override
|
||||
def on_text_delta(self, delta, snapshot):
|
||||
print(delta.value, end="", flush=True)
|
||||
|
||||
|
||||
def on_tool_call_created(self, tool_call):
|
||||
print(f"\nassistant > {tool_call.type}\n", flush=True)
|
||||
|
||||
|
||||
def on_tool_call_delta(self, delta, snapshot):
|
||||
if delta.type == 'code_interpreter':
|
||||
if delta.code_interpreter.input:
|
||||
print(delta.code_interpreter.input, end="", flush=True)
|
||||
|
||||
if delta.code_interpreter.outputs:
|
||||
print(f"\n\noutput >", flush=True)
|
||||
|
||||
for output in delta.code_interpreter.outputs:
|
||||
if output.type == "logs":
|
||||
print(f"\n{output.logs}", flush=True)
|
||||
|
||||
|
||||
|
||||
# Setting up OpenAI Client with API Key
|
||||
api_key = os.getenv("OPENAI_API_KEY")
|
||||
client = OpenAI(
|
||||
@@ -62,23 +38,32 @@ client = OpenAI(
|
||||
# screw bardo assistant that is configured to make notes and 5Q&A based on any given YouTube Transcript
|
||||
asst_screw_bardo_id = "asst_JGFaX6uOIotqy5mIJnu3Yyp7"
|
||||
|
||||
# uhh no we need a new thread each time tf
|
||||
# make sure to call the function after the transcript is confirmed to work, it would be very stupid to call the function and make a new thread this early
|
||||
def create_and_stream(transcript):
|
||||
with client.beta.threads.create_and_run_stream(
|
||||
assistant_id=asst_screw_bardo_id,
|
||||
thread={
|
||||
"messages" : [
|
||||
{"role": "user",
|
||||
"content": transcript}
|
||||
]
|
||||
},
|
||||
event_handler=EventHandler()
|
||||
) as stream:
|
||||
stream.until_done()
|
||||
messages = stream.get_final_messages()
|
||||
return messages[0].content[0].text.value
|
||||
# This is copy and pasted straight up from the quickstart guide, just appending to an output buffer instead of directly printing:
|
||||
class EventHandler(AssistantEventHandler):
|
||||
@override
|
||||
def on_text_created(self, text) -> None:
|
||||
with output_lock:
|
||||
output_buffer.append(f"\nassistant > {text}")
|
||||
|
||||
@override
|
||||
def on_text_delta(self, delta, snapshot):
|
||||
with output_lock:
|
||||
output_buffer.append(delta.value)
|
||||
|
||||
def on_tool_call_created(self, tool_call):
|
||||
with output_lock:
|
||||
output_buffer.append(f"\nassistant > {tool_call.type}\n")
|
||||
|
||||
def create_and_stream(transcript):
|
||||
with client.beta.threads.create_and_run_stream(
|
||||
assistant_id=asst_screw_bardo_id,
|
||||
thread={
|
||||
"messages": [{"role": "user", "content": transcript}]
|
||||
},
|
||||
event_handler=EventHandler()
|
||||
) as stream:
|
||||
stream.until_done()
|
||||
|
||||
def get_video_id(url):
|
||||
youtu_be = r'(?<=youtu.be/)([A-Za-z0-9_-]{11})'
|
||||
youtube_com = r'(?<=youtube\.com\/watch\?v=)([A-Za-z0-9_-]{11})'
|
||||
|
||||
Reference in New Issue
Block a user