-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodes.py
More file actions
32 lines (22 loc) · 928 Bytes
/
Copy pathnodes.py
File metadata and controls
32 lines (22 loc) · 928 Bytes
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
from retriever import retriever
# Define your custom nodes
def retrieve_answer(state):
query = state["question"]
docs_with_scores = retriever.similarity_search_with_score(query, k=1, fetch_k=3)
docs = []
for doc, score in docs_with_scores:
doc.metadata["score"] = score
docs.append(doc)
return {**state, "retrieved_docs": docs}
def is_confident_enough(state):
# Assume the retriever gives score in metadata
docs = state["retrieved_docs"]
print(f"Retrived docs:\n{docs}")
doc = state["retrieved_docs"][0]
score = doc.metadata.get("score", 100.0)
return {**state,"condition": "yes" if score < 1 else "no"} # lower = more similar
def generate_answer(state):
answer = state["retrieved_docs"][0].page_content
return {**state,"answer": answer}
def ask_followup_question(state):
return {**state,"followup": "Can you please clarify your question?"}