File size: 1,262 Bytes
bb25312
bdd8061
bb25312
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# app/graph/nodes/hybrid_agent.py

from app.core.llm_engine import llm
from langchain_core.prompts import PromptTemplate
from langchain_core.output_parsers import StrOutputParser

hybrid_prompt = PromptTemplate(
    input_variables=["context", "query", "history"],
    template=(
        "You are a document-aware assistant.\n"
        "The uploaded document has LIMITED information on this topic.\n\n"
        "INSTRUCTIONS:\n"
        "- Start your answer using what the document says (cite it briefly)\n"
        "- Then expand with your general knowledge to give a complete answer\n"
        "- Clearly separate what came from the document vs general knowledge\n"
        "- Be concise and helpful\n\n"
        "Conversation History:\n{history}\n\n"
        "Document excerpt:\n{context}\n\n"
        "Question:\n{query}\n\n"
        "Answer:"
    )
)

chain = hybrid_prompt | llm | StrOutputParser()

def hybrid_agent_node(state):
    response = chain.invoke({
        "context": state.get("context", ""),
        "query": state.get("query", ""),
        "history": state.get("history", "")
    })
    return {
        **state,
        "general_answer": response.strip()   # synthesizer picks this up for hybrid route
    }