File size: 1,127 Bytes
cf2e6bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c9c0ef3
cf2e6bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from typing import List
from langchain.prompts import ChatPromptTemplate


FOLLOW_UP_TEMPLATE = """Based on the previous question and answer, generate 2-3 relevant follow-up questions that would help explore the topic further.

Previous Question: {user_input}
Previous Answer: {answer}

Generate short, concise, focused follow-up questions
You don't need a full question as it will be reformulated later as a standalone question with the context. Eg. "Details the first point"
"""

def make_follow_up_node(llm):
    prompt = ChatPromptTemplate.from_template(FOLLOW_UP_TEMPLATE)
    
    def generate_follow_up(state):
        print("---- Generate_follow_up ----")
        if not state.get("answer"):
            return state
            
        response = llm.invoke(prompt.format(
            user_input=state["user_input"],
            answer=state["answer"]
        ))
        
        # Extract questions from response
        follow_ups = [q.strip() for q in response.content.split("\n") if q.strip()]
        state["follow_up_questions"] = follow_ups
        
        return state
        
    return generate_follow_up