import re import pytz from typing import Union from datetime import datetime from langchain.chat_models import ChatOpenAI from langchain.prompts import PromptTemplate from langchain.schema import AIMessage, HumanMessage, SystemMessage def get_initial_messages( questions: str, id_interview: str ) -> list[Union[SystemMessage, HumanMessage, AIMessage]]: from utils import prompts role_prompt_template = PromptTemplate( template=prompts.ROLE_PROMPTS, input_variables=["ID_INTERVIEW", "QUESTIONS_PROMPTS"], ) role_prompt = role_prompt_template.format( ID_INTERVIEW=id_interview, QUESTIONS_PROMPTS=questions, ) tz = pytz.timezone("Asia/Jakarta") role_prompt = ( (f"Waktu sekarang: {datetime.now(tz).strftime('%d/%m/%Y %H:%M:%S')}") + "\n" + role_prompt ) history_messages = [ SystemMessage(content=prompts.STARTING_PROMPTS), SystemMessage(content=role_prompt), AIMessage(content="Baik, saya mengerti."), HumanMessage(content="mulai"), ] return history_messages def get_first_message( chat_openai: ChatOpenAI, questions: str, id_interview: str ) -> str: messages = get_initial_messages(questions, id_interview) bot_message = get_bot_message(chat_openai, messages) return bot_message def get_bot_message( chat_openai: ChatOpenAI, history_messages: list[Union[SystemMessage, HumanMessage, AIMessage]], ) -> str: result_ai_message = chat_openai(history_messages) return result_ai_message.content def remove_html_tags(text: str) -> str: clean = re.compile("<.*?>") return re.sub(clean, "", text)