cjber commited on
Commit
43845c7
·
1 Parent(s): 986c45c
planning_ai/__init__.py ADDED
File without changes
planning_ai/common/__init__.py ADDED
File without changes
planning_ai/graph.py CHANGED
@@ -1,4 +1,4 @@
1
- from langgraph.constants import START, Send
2
  from langgraph.graph import END, StateGraph
3
 
4
  from planning_ai.nodes.hallucination_node import (
@@ -7,13 +7,9 @@ from planning_ai.nodes.hallucination_node import (
7
  map_fix_hallucinations,
8
  map_hallucinations,
9
  )
10
- from planning_ai.nodes.map_node import (
11
- collect_summaries,
12
- generate_summary,
13
- map_summaries,
14
- )
15
  from planning_ai.nodes.reduce_node import generate_final_summary
16
- from planning_ai.states import DocumentState, OverallState
17
 
18
 
19
  def create_graph():
 
1
+ from langgraph.constants import START
2
  from langgraph.graph import END, StateGraph
3
 
4
  from planning_ai.nodes.hallucination_node import (
 
7
  map_fix_hallucinations,
8
  map_hallucinations,
9
  )
10
+ from planning_ai.nodes.map_node import generate_summary, map_summaries
 
 
 
 
11
  from planning_ai.nodes.reduce_node import generate_final_summary
12
+ from planning_ai.states import OverallState
13
 
14
 
15
  def create_graph():
planning_ai/main.py CHANGED
@@ -180,7 +180,7 @@ def main():
180
  loader_cls=TextLoader,
181
  recursive=True,
182
  )
183
- docs = [doc for doc in loader.load()[:200] if doc.page_content]
184
  text_splitter = CharacterTextSplitter.from_tiktoken_encoder(
185
  chunk_size=1000, chunk_overlap=0
186
  )
@@ -193,8 +193,7 @@ def main():
193
  {
194
  "documents": [doc.page_content for doc in split_docs],
195
  "filenames": [Path(doc.metadata["source"]) for doc in split_docs],
196
- },
197
- # {"recursion_limit": 10},
198
  ):
199
  print(list(step.keys()))
200
 
@@ -207,35 +206,36 @@ def main():
207
  if __name__ == "__main__":
208
  doc_title = "Cambridge Response Summary"
209
  out = main()
210
- build_quarto_doc(doc_title, out)
211
-
212
- d = [
213
- i
214
- for i in out["generate_final_summary"]["summaries_fixed"]
215
- if i["iteration"] == 4
216
- ][0]
217
- d["document"]
218
-
219
- h = [
220
- i["summary"].summary
221
- for i in out["generate_final_summary"]["hallucinations"]
222
- if i["document"] == d["document"]
223
- ]
224
-
225
- e = [
226
- i["hallucination"].explanation
227
- for i in out["generate_final_summary"]["hallucinations"]
228
- if i["document"] == d["document"]
229
- ]
230
-
231
- test = {
232
- "document": d["document"],
233
- "final_summary": d["summary"].summary,
234
- "attempts": h,
235
- "reasoning": e,
236
- }
237
-
238
- print(f"Document:\n\n{test['document']}\n\n")
239
- print(f"Final:\n\n{test['final_summary']}\n\n")
240
- print("Attempts: \n\n*", "\n\n* ".join(test["attempts"]), "\n\n")
241
- print("Reasoning: \n\n*", "\n\n* ".join(test["reasoning"]), "\n\n")
 
 
180
  loader_cls=TextLoader,
181
  recursive=True,
182
  )
183
+ docs = [doc for doc in loader.load()[:5] if doc.page_content]
184
  text_splitter = CharacterTextSplitter.from_tiktoken_encoder(
185
  chunk_size=1000, chunk_overlap=0
186
  )
 
193
  {
194
  "documents": [doc.page_content for doc in split_docs],
195
  "filenames": [Path(doc.metadata["source"]) for doc in split_docs],
196
+ }
 
197
  ):
198
  print(list(step.keys()))
199
 
 
206
  if __name__ == "__main__":
207
  doc_title = "Cambridge Response Summary"
208
  out = main()
209
+ out["generate_final_summary"]["summaries"]
210
+ # build_quarto_doc(doc_title, out)
211
+ #
212
+ # d = [
213
+ # i
214
+ # for i in out["generate_final_summary"]["summaries_fixed"]
215
+ # if i["iteration"] == 4
216
+ # ][0]
217
+ # d["document"]
218
+ #
219
+ # h = [
220
+ # i["summary"].summary
221
+ # for i in out["generate_final_summary"]["hallucinations"]
222
+ # if i["document"] == d["document"]
223
+ # ]
224
+ #
225
+ # e = [
226
+ # i["hallucination"].explanation
227
+ # for i in out["generate_final_summary"]["hallucinations"]
228
+ # if i["document"] == d["document"]
229
+ # ]
230
+ #
231
+ # test = {
232
+ # "document": d["document"],
233
+ # "final_summary": d["summary"].summary,
234
+ # "attempts": h,
235
+ # "reasoning": e,
236
+ # }
237
+ #
238
+ # print(f"Document:\n\n{test['document']}\n\n")
239
+ # print(f"Final:\n\n{test['final_summary']}\n\n")
240
+ # print("Attempts: \n\n*", "\n\n* ".join(test["attempts"]), "\n\n")
241
+ # print("Reasoning: \n\n*", "\n\n* ".join(test["reasoning"]), "\n\n")
planning_ai/nodes/hallucination_node.py CHANGED
@@ -1,21 +1,21 @@
1
- from typing import Literal
2
-
3
  from langgraph.constants import Send
4
 
5
  from planning_ai.chains.fix_chain import fix_chain
6
- from planning_ai.chains.hallucination_chain import hallucination_chain
 
 
 
7
  from planning_ai.states import DocumentState, OverallState
8
 
9
 
10
  def check_hallucination(state: DocumentState):
11
- print(state["iteration"])
12
  if state["iteration"] > 5:
13
  state["iteration"] = -99
14
  return {"summaries_fixed": [state]}
15
 
16
- response = hallucination_chain.invoke(
17
  {"document": state["document"], "summary": state["summary"]}
18
- )
19
  if response.score == 1:
20
  return {"summaries_fixed": [state]}
21
 
@@ -43,11 +43,12 @@ def fix_hallucination(state: DocumentState):
43
  "explanation": state["hallucination"],
44
  }
45
  )
46
- state["summary"] = response
47
  return {
48
  "summaries": [
49
  {
50
  "document": state["document"],
 
51
  "summary": state["summary"],
52
  "iteration": state["iteration"],
53
  }
 
 
 
1
  from langgraph.constants import Send
2
 
3
  from planning_ai.chains.fix_chain import fix_chain
4
+ from planning_ai.chains.hallucination_chain import (
5
+ HallucinationChecker,
6
+ hallucination_chain,
7
+ )
8
  from planning_ai.states import DocumentState, OverallState
9
 
10
 
11
  def check_hallucination(state: DocumentState):
 
12
  if state["iteration"] > 5:
13
  state["iteration"] = -99
14
  return {"summaries_fixed": [state]}
15
 
16
+ response: HallucinationChecker = hallucination_chain.invoke(
17
  {"document": state["document"], "summary": state["summary"]}
18
+ ) # type: ignore
19
  if response.score == 1:
20
  return {"summaries_fixed": [state]}
21
 
 
43
  "explanation": state["hallucination"],
44
  }
45
  )
46
+ state["summary"] = response # type: ignore
47
  return {
48
  "summaries": [
49
  {
50
  "document": state["document"],
51
+ "filename": state["filename"],
52
  "summary": state["summary"],
53
  "iteration": state["iteration"],
54
  }
planning_ai/nodes/map_node.py CHANGED
@@ -1,8 +1,3 @@
1
- import logging
2
-
3
- logging.basicConfig(level=logging.WARNING)
4
-
5
- from langchain_core.documents import Document
6
  from langgraph.constants import Send
7
 
8
  from planning_ai.chains.map_chain import map_chain
@@ -31,29 +26,3 @@ def map_summaries(state: OverallState):
31
  )
32
  for document, filename in zip(state["documents"], state["filenames"])
33
  ]
34
-
35
-
36
- def collect_summaries(state: OverallState):
37
- print("test")
38
- __import__("ipdb").set_trace()
39
- state.keys()
40
- len(state["documents"])
41
- len(state["summaries_fixed"])
42
- len(state["hallucinations"])
43
- state["hallucinations"]
44
- return {
45
- "summary_documents": [
46
- Document(
47
- page_content=hallucination.summary,
48
- metadata={
49
- "stance": hallucination.stance,
50
- "aims": hallucination.aims,
51
- "places": hallucination.places,
52
- "rating": hallucination.rating,
53
- "hallucination": hallucination.score,
54
- "explanation": hallucination.explanation,
55
- },
56
- )
57
- ]
58
- for hallucination in state["summaries_fixed"]
59
- }
 
 
 
 
 
 
1
  from langgraph.constants import Send
2
 
3
  from planning_ai.chains.map_chain import map_chain
 
26
  )
27
  for document, filename in zip(state["documents"], state["filenames"])
28
  ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
planning_ai/nodes/reduce_node.py CHANGED
@@ -8,7 +8,7 @@ def generate_final_summary(state: OverallState):
8
  return {
9
  "final_summary": response,
10
  "summaries_fixed": state["summaries_fixed"],
11
- "summaries": state["summary_documents"],
12
  "hallucinations": state["hallucinations"],
13
  "documents": state["documents"],
14
  }
 
8
  return {
9
  "final_summary": response,
10
  "summaries_fixed": state["summaries_fixed"],
11
+ "summaries": state["summaries"],
12
  "hallucinations": state["hallucinations"],
13
  "documents": state["documents"],
14
  }
planning_ai/preprocessing/__init__.py ADDED
File without changes
planning_ai/states.py CHANGED
@@ -10,14 +10,13 @@ from planning_ai.chains.map_chain import BriefSummary
10
 
11
  class OverallState(TypedDict):
12
  documents: list[str]
 
 
13
  summaries: Annotated[list, operator.add]
14
  summaries_fixed: Annotated[list, operator.add]
15
  hallucinations: Annotated[list, operator.add]
16
 
17
  filenames: List[Path]
18
- summary_documents: Annotated[list[Document], operator.add]
19
- final_summary: str
20
-
21
  iterations: list[int]
22
 
23
 
@@ -25,6 +24,6 @@ class DocumentState(TypedDict):
25
  document: str
26
  summary: BriefSummary
27
  hallucination: HallucinationChecker
28
- filename: Path
29
 
 
30
  iteration: int
 
10
 
11
  class OverallState(TypedDict):
12
  documents: list[str]
13
+
14
+ final_summary: str
15
  summaries: Annotated[list, operator.add]
16
  summaries_fixed: Annotated[list, operator.add]
17
  hallucinations: Annotated[list, operator.add]
18
 
19
  filenames: List[Path]
 
 
 
20
  iterations: list[int]
21
 
22
 
 
24
  document: str
25
  summary: BriefSummary
26
  hallucination: HallucinationChecker
 
27
 
28
+ filename: Path
29
  iteration: int