File size: 1,674 Bytes
2fedcf6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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)