# Reference: # # https://vimeo.com/990334325/56b552bc7a # https://platform.openai.com/playground/assistants # https://cookbook.openai.com/examples/assistants_api_overview_python # https://platform.openai.com/docs/api-reference/assistants/createAssistant # https://platform.openai.com/docs/assistants/tools import gradio as gr import os from assistants import ( assistant, thread, create_assistant, load_assistant, create_thread, create_message, create_run, wait_on_run, get_run_steps, recurse_execute_tool_calls, get_messages, extract_content_values, ) def chat(message, history, openai_api_key, tavily_api_key): if not message: raise gr.Error("Message is required.") if not openai_api_key: raise gr.Error("OpenAI API key is required.") os.environ["OPENAI_API_KEY"] = openai_api_key os.environ["TAVILY_API_KEY"] = tavily_api_key global assistant, thread # On first run, create assistant and update assistant_id, # see https://platform.openai.com/playground/assistants. # On subsequent runs, load assistant. if assistant == None: #assistant = create_assistant() assistant = load_assistant() # TODO: Use Gradio session to support multiple users if thread == None or len(history) == 0: thread = create_thread() create_message(thread, message) run = create_run(assistant, thread) run = wait_on_run(thread, run) run_steps = get_run_steps(thread, run) recurse_execute_tool_calls(thread, run, run_steps, 0) messages = get_messages(thread) text_values, image_values = extract_content_values(messages) download_link = "" # TODO: Handle multiple images and other file types if len(image_values) > 0: download_link = f"