File size: 1,547 Bytes
2f18493
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from .shared_state import SharedState


from langchain_google_genai import GoogleGenerativeAI

from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_core.messages import BaseMessage, SystemMessage

from typing import Dict, List


class Assistant:
    def __init__(self):
        self._llm = GoogleGenerativeAI(model="gemini-2.0-flash")
        self._prompt_template = ChatPromptTemplate.from_messages([
            SystemMessage(
                content="""You are a helpful assistant tasked with answering questions using a set of tools.

Your final answer must strictly follow this format:
FINAL ANSWER: [ANSWER]

Only write the answer in that exact format. Do not explain anything. Do not include any other text.

If you are provided with a similar question and its final answer, and the current question is **exactly the same**, then simply return the same final answer without using any tools.

Only use tools if the current question is different from the similar one.

Examples:
- FINAL ANSWER: FunkMonk
- FINAL ANSWER: Paris
- FINAL ANSWER: 128

If you do not follow this format exactly, your response will be considered incorrect."""
            ),
            MessagesPlaceholder(variable_name="messages")
        ])
        self._llm_chain = self._prompt_template | self._llm

    def __call__(self, state: SharedState) -> Dict[str, List[BaseMessage]]:
        """
        Call the assistant with the current state.
        """
        return {"messages": [self._llm_chain.invoke(state["messages"])]}