File size: 1,110 Bytes
778116a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from langchain.tools import tool
from langchain_core.messages import SystemMessage, HumanMessage
from langchain_openai import ChatOpenAI

from core.messages import attachmentHandler
from utils.prompt_manager import prompt_mgmt

audio_model = ChatOpenAI(model="gpt-4o-audio-preview")


@tool
def query_audio(question: str, file_reference: str) -> str:
    """
    Tool to answer questions based on the audio file identified by the provided file reference
    :param question: Question to be answered
    :param file_reference: file reference
    :return: the answer to the given question
    """
    sys_msg = SystemMessage(content=prompt_mgmt.render_template("audio_evaluation", []))
    content_bytes = attachmentHandler.fetch_file_from_reference(file_reference)

    content = [{"type": "text", "text": question}, attachmentHandler.get_representation("audio", content_bytes, "mp3", None)]

    message = [HumanMessage(content=content)]
    try:
        response = audio_model.invoke([sys_msg] + message)
        return response
    except Exception as e:
        print("Exception while invoking audio tool")