Spaces:
Sleeping
Sleeping
ego commited on
Commit ·
6882231
1
Parent(s): c0a14ba
update prompt
Browse files- core/graph.py +14 -14
- prompts.py +4 -3
core/graph.py
CHANGED
|
@@ -19,12 +19,12 @@ class RAGAgent:
|
|
| 19 |
self.llm = get_llm()
|
| 20 |
self.app = self.build_graph()
|
| 21 |
|
| 22 |
-
def
|
| 23 |
query = state["current_query"]
|
| 24 |
docs = self.retriever.invoke(query)
|
| 25 |
return {"documents": docs}
|
| 26 |
|
| 27 |
-
def
|
| 28 |
question = state["question"]
|
| 29 |
docs = state["documents"]
|
| 30 |
|
|
@@ -34,7 +34,7 @@ class RAGAgent:
|
|
| 34 |
response = chain.invoke({"context": context, "question": question})
|
| 35 |
return {"generation": response}
|
| 36 |
|
| 37 |
-
def
|
| 38 |
question = state["question"]
|
| 39 |
generation = state["generation"]
|
| 40 |
docs = state["documents"]
|
|
@@ -51,7 +51,7 @@ class RAGAgent:
|
|
| 51 |
normalized_score = "yes" if "yes" in score.lower() else "no"
|
| 52 |
return {"reflection_score": normalized_score}
|
| 53 |
|
| 54 |
-
def
|
| 55 |
question = state["question"]
|
| 56 |
previous_query = state["current_query"]
|
| 57 |
failed_gen = state["generation"]
|
|
@@ -78,24 +78,24 @@ class RAGAgent:
|
|
| 78 |
workflow = StateGraph(GraphState)
|
| 79 |
|
| 80 |
|
| 81 |
-
workflow.add_node("
|
| 82 |
-
workflow.add_node("
|
| 83 |
-
workflow.add_node("
|
| 84 |
-
workflow.add_node("
|
| 85 |
|
| 86 |
-
workflow.set_entry_point("
|
| 87 |
-
workflow.add_edge("
|
| 88 |
-
workflow.add_edge("
|
| 89 |
|
| 90 |
workflow.add_conditional_edges(
|
| 91 |
-
"
|
| 92 |
self.decide_to_rewrite,
|
| 93 |
{
|
| 94 |
-
"rewrite": "
|
| 95 |
"end": END
|
| 96 |
}
|
| 97 |
)
|
| 98 |
-
workflow.add_edge("
|
| 99 |
|
| 100 |
return workflow.compile()
|
| 101 |
|
|
|
|
| 19 |
self.llm = get_llm()
|
| 20 |
self.app = self.build_graph()
|
| 21 |
|
| 22 |
+
def retriever_node(self, state: GraphState):
|
| 23 |
query = state["current_query"]
|
| 24 |
docs = self.retriever.invoke(query)
|
| 25 |
return {"documents": docs}
|
| 26 |
|
| 27 |
+
def generator_node(self, state: GraphState):
|
| 28 |
question = state["question"]
|
| 29 |
docs = state["documents"]
|
| 30 |
|
|
|
|
| 34 |
response = chain.invoke({"context": context, "question": question})
|
| 35 |
return {"generation": response}
|
| 36 |
|
| 37 |
+
def reflector_node(self, state: GraphState):
|
| 38 |
question = state["question"]
|
| 39 |
generation = state["generation"]
|
| 40 |
docs = state["documents"]
|
|
|
|
| 51 |
normalized_score = "yes" if "yes" in score.lower() else "no"
|
| 52 |
return {"reflection_score": normalized_score}
|
| 53 |
|
| 54 |
+
def rewriter_node(self, state: GraphState):
|
| 55 |
question = state["question"]
|
| 56 |
previous_query = state["current_query"]
|
| 57 |
failed_gen = state["generation"]
|
|
|
|
| 78 |
workflow = StateGraph(GraphState)
|
| 79 |
|
| 80 |
|
| 81 |
+
workflow.add_node("retriever", self.retriever_node)
|
| 82 |
+
workflow.add_node("generator", self.generator_node)
|
| 83 |
+
workflow.add_node("reflector", self.reflector_node)
|
| 84 |
+
workflow.add_node("rewriter", self.rewriter_node)
|
| 85 |
|
| 86 |
+
workflow.set_entry_point("retriever")
|
| 87 |
+
workflow.add_edge("retriever", "generator")
|
| 88 |
+
workflow.add_edge("generator", "reflector")
|
| 89 |
|
| 90 |
workflow.add_conditional_edges(
|
| 91 |
+
"reflector",
|
| 92 |
self.decide_to_rewrite,
|
| 93 |
{
|
| 94 |
+
"rewrite": "rewriter",
|
| 95 |
"end": END
|
| 96 |
}
|
| 97 |
)
|
| 98 |
+
workflow.add_edge("rewriter", "retriever")
|
| 99 |
|
| 100 |
return workflow.compile()
|
| 101 |
|
prompts.py
CHANGED
|
@@ -5,14 +5,15 @@ RAG_SYSTEM = """You are a professional research and learning assistant. Answer t
|
|
| 5 |
If the context does not contain the answer, say "I cannot answer this based on the provided material."
|
| 6 |
|
| 7 |
Requirements:
|
| 8 |
-
1. Use a professional and objective tone.
|
| 9 |
2. **In-text Citations:** Use Unicode Superscript Numbers (¹, ², ³, ⁴, ⁵, ⁶, ⁷, ⁸, ⁹, ¹⁰) strictly. Place them immediately after the punctuation or relevant phrase.
|
| 10 |
- Do NOT use `[^1]` (Markdown footnotes) or `[1]` (Brackets).
|
| 11 |
- Example: ...at compile time¹.
|
| 12 |
3. **References Section:** At the very end, include a section titled "References".
|
| 13 |
-
4. **Reference Format:** List the citations
|
| 14 |
- Format: `1. Document: <filename>, Page: <page>`
|
| 15 |
-
5.
|
|
|
|
| 16 |
|
| 17 |
RAG_HUMAN = """Context:
|
| 18 |
{context}
|
|
|
|
| 5 |
If the context does not contain the answer, say "I cannot answer this based on the provided material."
|
| 6 |
|
| 7 |
Requirements:
|
| 8 |
+
1. Use a professional and objective tone.
|
| 9 |
2. **In-text Citations:** Use Unicode Superscript Numbers (¹, ², ³, ⁴, ⁵, ⁶, ⁷, ⁸, ⁹, ¹⁰) strictly. Place them immediately after the punctuation or relevant phrase.
|
| 10 |
- Do NOT use `[^1]` (Markdown footnotes) or `[1]` (Brackets).
|
| 11 |
- Example: ...at compile time¹.
|
| 12 |
3. **References Section:** At the very end, include a section titled "References".
|
| 13 |
+
4. **Reference Format:** List the citations sequentially using normal numbers.
|
| 14 |
- Format: `1. Document: <filename>, Page: <page>`
|
| 15 |
+
5. **Emphasis:** Use **bold** to highlight key points, core concepts, or critical data.
|
| 16 |
+
6. Be concise, comprehensive, and well-structured"""
|
| 17 |
|
| 18 |
RAG_HUMAN = """Context:
|
| 19 |
{context}
|