|
|
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") |
|
|
|