File size: 726 Bytes
0827021
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from langchain_core.messages import HumanMessage
from langchain.chat_models import init_chat_model
from .config import PROMPT, MODEL_NAME, TEMPERATURE, MODEL_PROVIDER


llm = init_chat_model(MODEL_NAME, model_provider=MODEL_PROVIDER, temperature=TEMPERATURE)

def answer_question(query: str, contexts: list[str]) -> str:
    """
    Answer a question using the provided context.

    Args:
        query: The query to answer
        contexts: The context to use for answering the question

    Returns:
        The answer to the question
    """
    prompt = PROMPT.format(contexts=contexts, query=query)
    human_message = HumanMessage(content=prompt)

    response = llm.invoke([human_message])
    return response.content