cjber commited on
Commit
dc0ceb1
·
1 Parent(s): 237d8a3

add theme selection using agent

Browse files
planning_ai/chains/fix_chain.py CHANGED
@@ -3,11 +3,8 @@ from langchain_core.prompts import ChatPromptTemplate
3
  from planning_ai.chains.map_chain import create_dynamic_map_chain
4
  from planning_ai.common.utils import Paths
5
 
6
- with open(Paths.PROMPTS / "themes.txt", "r") as f:
7
- themes_txt = f.read()
8
-
9
  with open(Paths.PROMPTS / "fix_hallucination.txt", "r") as f:
10
- fix_template = f"{themes_txt}\n\n {f.read()}"
11
 
12
  if __name__ == "__main__":
13
  test_document = """
 
3
  from planning_ai.chains.map_chain import create_dynamic_map_chain
4
  from planning_ai.common.utils import Paths
5
 
 
 
 
6
  with open(Paths.PROMPTS / "fix_hallucination.txt", "r") as f:
7
+ fix_template = f.read()
8
 
9
  if __name__ == "__main__":
10
  test_document = """
planning_ai/chains/map_chain.py CHANGED
@@ -9,11 +9,7 @@ from planning_ai.common.utils import Paths
9
  from planning_ai.llms.llm import LLM
10
  from planning_ai.themes import THEMES_AND_POLICIES
11
 
12
- # with open(Paths.PROMPTS / "themes.txt", "r") as f:
13
- # themes_txt = f.read()
14
-
15
  with open(Paths.PROMPTS / "map.txt", "r") as f:
16
- # map_template = f"{themes_txt}\n\n {f.read()}"
17
  map_template = f.read()
18
 
19
 
 
9
  from planning_ai.llms.llm import LLM
10
  from planning_ai.themes import THEMES_AND_POLICIES
11
 
 
 
 
12
  with open(Paths.PROMPTS / "map.txt", "r") as f:
 
13
  map_template = f.read()
14
 
15
 
planning_ai/chains/themes_chain.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from enum import Enum
2
+ from typing import Optional
3
+
4
+ from langchain_core.prompts import ChatPromptTemplate
5
+ from pydantic import BaseModel
6
+
7
+ from planning_ai.common.utils import Paths
8
+ from planning_ai.llms.llm import LLM
9
+
10
+
11
+ class Themes(Enum):
12
+ climate_change = "Climate Change"
13
+ biodiversity = "Biodiversity and Green Spaces"
14
+ wellbeing = "Wellbeing and Social Inclusion"
15
+ great_places = "Great Places"
16
+ jobs = "Jobs"
17
+ homes = "Homes"
18
+ infrastructure = "Infrastructure"
19
+
20
+
21
+ class ThemeSelector(BaseModel):
22
+ themes: Optional[list[Themes]]
23
+
24
+
25
+ with open(Paths.PROMPTS / "themes.txt", "r") as f:
26
+ themes_template = f.read()
27
+
28
+ themes_prompt = ChatPromptTemplate.from_messages([("system", themes_template)])
29
+
30
+ SLLM = LLM.with_structured_output(ThemeSelector, strict=True)
31
+
32
+ themes_chain = themes_prompt | SLLM
33
+
34
+
35
+ if __name__ == "__main__":
36
+ test_document = """
37
+ The Local Plan proposes a mass development north-west of Cambridge despite marked growth
38
+ in the last twenty years or so following the previous New Settlement Study. In this period,
39
+ the major settlement of Cambourne has been created - now over the projected 3,000 homes and
40
+ Papworth Everard has grown beyond recognition. This in itself is a matter of concern.
41
+ """
42
+
43
+ result = themes_chain.invoke({"document": test_document})
44
+ __import__("pprint").pprint(dict(result))